Bruce Bruce - 2 months ago 5x
Java Question

If each 'transaction' is an object, how do you store the total cost of all transactions made?

Foreword: sorry for all the code, I know most of it is redundant for this question.

This class takes a description (size), a cost, and a quantity as arguments and returns the number of times the object was created (transaction_number), the total quantity specified for all the objects created (total_quantity), and (is supposed to) return the total cost of all the object created.

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);

And this is just a tester class.

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

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

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

The problem is that I can't figure out how to store the grand_total. I tried doing it the same way I stored the total_quantity but I can see why that isn't working.

How can I keep track of the grand total of all the transactions (objects) so I can then print it out on the console?

I assume there's another way of expressing this in the constructor but I'm not sure how and I couldn't find any examples of this.


The simplest solution is to use a static field on your SalesTransaction class which holds the grand_total. A static variable is shared by all instances of a class.

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

However, this has some disadvantages in the long run. It would mean you can't have transactions as members of different grand totals. This is why it's called the singleton anti-pattern.

A much better way to solve the problem is to make an additional class such as TransactionGroup, which contains SalesTransaction objects in a List, and sums together the costs when needed.

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