gpt4 book ai didi

java - 如果每个 'transaction' 都是一个对象,那么如何存储所有交易的总成本?

转载 作者:行者123 更新时间:2023-12-02 03:26:49 24 4
gpt4 key购买 nike

前言:对所有代码感到抱歉,我知道其中大部分对于这个问题来说都是多余的。

此类采用描述(大小)、成本和数量作为参数,并返回创建对象的次数 (transaction_number)、为所有创建的对象指定的总数量 (total_quantity),以及(是应该)返回创建的所有对象的总成本。

public class SalesTransaction
{
private static int counter = 1;
public final int transaction_number;
private static int order_quantity = 0;
public final int total_quantity;
private static double temp_grand_total = 0;
public final double grand_total;
private String size;
private double cost;
private int quantity;

public SalesTransaction(String size, double cost, int quantity)
{
this.size = size;
this.cost = cost;
this.quantity = quantity;

this.transaction_number = counter++;
order_quantity += quantity;
this.total_quantity = order_quantity;
temp_grand_total += totalCostAfterTax(cost*quantity); // this is wrong!
this.grand_total = temp_grand_total;
}

public static double discountAmount(int quantity, double cost)
{
double discount_amount = 0;
if (quantity > 20)
{
discount_amount = cost * 0.10;
}
return discount_amount;
}

public static double totalCostBeforeTax(SalesTransaction temp)
{
double total_cost;
int quantity = temp.quantity;
double cost = temp.cost;
total_cost = quantity * cost;
double discount_amount = discountAmount(quantity, total_cost);
total_cost = total_cost - discount_amount;
return total_cost;
}

public static double totalCostAfterTax(double total_cost)
{
total_cost = total_cost * 1.15;
return total_cost;
}

public static void printStats(SalesTransaction temp)
{
System.out.println("Transaction Number: " + temp.transaction_number);
System.out.println("Size: " + temp.size);
System.out.println("Cost Per Table: "+ temp.cost);
System.out.println("Number of Tables: " + temp.quantity);
System.out.println("Total Tables So Far: " + temp.total_quantity);
double total_cost_before_tax = totalCostBeforeTax(temp);
double discount_amount = discountAmount(temp.quantity, total_cost_before_tax);
System.out.println("Discount: " + discount_amount);
double total_cost_after_tax = totalCostAfterTax(total_cost_before_tax);
temp.temp_grand_total = total_cost_after_tax;
System.out.println("Cost for this transaction: " + total_cost_after_tax);
System.out.println("Total cost: "+ temp.grand_total);
System.out.println();
}
}

这只是一个测试类。

public class SalesTester
{
public static void main(String[] args)
{
SalesTransaction one = new SalesTransaction("Small", 10.0, 10);
one.printStats(one);

SalesTransaction two = new SalesTransaction("Medium", 20.0, 30);
two.printStats(two);

SalesTransaction three = new SalesTransaction("Large", 30.0, 40);
three.printStats(three);
}
}

问题是我不知道如何存储 grand_total。我尝试以与存储total_quantity相同的方式进行操作,但我明白为什么这不起作用。

如何跟踪所有事务(对象)的总计,以便可以在控制台上打印出来?

我假设在构造函数中还有另一种表达方式,但我不确定如何表达,而且我找不到任何示例。

最佳答案

最简单的解决方案是在 SalesTransaction 类中使用一个 static 字段来保存 grand_total静态变量由类的所有实例共享。

private static double grandTotal = 0;
public SalesTransaction(double cost) {
grandTotal += cost;
}

但是,从长远来看,这有一些缺点。这意味着您不能作为不同总计的成员进行交易。这就是为什么它被称为 singleton anti-pattern .

解决该问题的更好方法是创建一个附加类,例如 TransactionGroup,其中包含 List 中的 SalesTransaction 对象,并在需要时将成本加在一起。

public class TransactionGroup {
private List<SalesTransaction> transactions = new ArrayList<>();
public void addTransaction(SalesTransaction st) {
transactions.add(st);
}
public double getGrandTotal() {
double sum = 0;
for (SalesTransaction st : transactions) {
sum += st.getCost();
}
return sum;
}
}

关于java - 如果每个 'transaction' 都是一个对象,那么如何存储所有交易的总成本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38730181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com