# How Do I Obtain a Grand Total From My Loop

• February 4th, 2013, 04:45 PM
sternfox
How Do I Obtain a Grand Total From My Loop
I have the following loop below. The user enters in a customer name and the amount of gold and silver he/she will be exchanging. If the user decides to enter another customer and then terminate the program, a grand total summary will be generated that shows the total amount of gold, silver, fees, money received etc. from the customers just entered. For example customer 1 enters 2 oz gold and 3 oz silver, customer 2 enters 2 oz gold and 3 oz silver; the total result is 4 oz gold and 6 oz silver. I know I am doing something wrong because my total results are taking the data that I entered from the last customer only and not all the customers that were entered. If someone could please guide me in the right direction, it will truly be appreciated.

Code :

```import java.util.Scanner;   class customerId {   private int customerId; // holds unique eight-digit number public static final int CUSTOMERID = 0;   { this.customerId = (int) (Math.random() * 100000000); }   public int GetId() { return customerId; } }   class goldCon {   private double goldPrice; private static final double GOLDCON = 0;   { this.goldPrice = (double) (400.50); }   public double getGold() { return goldPrice; } }   class silCon {   private double silverPrice; public static final double SILCON = 0;   { this.silverPrice = (double) (6.25); }   public double getSilver() { return silverPrice; } }   public class CashforMetals {   public static void main(String[] args) {   Scanner in = new Scanner(System.in);   String firstName= "", lastName = ""; double goldWeight = 0; @SuppressWarnings("unused") double platinumWeight; double silverWeight = 0; boolean done = false; String choice = "";   customerId customer = new customerId(); goldCon gold = new goldCon(); silCon silver = new silCon();   System.out.println("***\t\t\t\t\t\t\t***"); System.out.println("***\tWelcome to Cash for Metals Calculator!!!\t***"); System.out.println("***\t\t\t\t\t\t\t***");   do {   System.out.println("\nEnter the customer name > "); firstName = in.next(); lastName = in.next();   System.out.println("Enter the weight of gold > "); goldWeight = in.nextDouble();   System.out.println("Enter the weight of silver > "); silverWeight = in.nextDouble();   System.out.println("\n***\t\tCustomer Summary\t\t***");   System.out.printf("Customer ID: " + customer.GetId());   System.out.println("\nCustomer Name: " + firstName + lastName);   System.out.printf("Gold Total: " + "\$%.2f", goldWeight * gold.getGold());   System.out.printf("\nSilver Total: " + "\$%.2f", silverWeight * silver.getSilver()); double fee = .10 * (goldWeight * gold.getGold() + silverWeight * silver.getSilver());   System.out.printf("\nHandling Fee: " + "\$%.2f", fee);   double offer = (goldWeight * gold.getGold() + silverWeight * silver.getSilver() - fee);   System.out.printf("\nTotal Offer: " + "\$%.2f", offer);   System.out.println("\n\nWould you like to enter another customer? > "); choice = in.next();   if (choice.equalsIgnoreCase("yes")) { done = false; }   if (choice.equalsIgnoreCase("no")){ done = true; } }while (done != true);   System.out.println("\n***Grand Total Summary***"); double goldTotal = 0; goldTotal += goldWeight * gold.getGold(); System.out.printf("\nGold Totals: " + goldWeight + " ounces, " + "\$%.2f", goldTotal);   double silverTotal = 0; silverTotal += silverWeight * silver.getSilver(); System.out.printf("\nSilver Totals: " + silverWeight + " ounces, " + "\$%.2f", silverTotal);   double totalFee = .10 * (goldTotal + silverTotal); System.out.printf("\nHandling Fee Total: " + "\$%.2f", +totalFee);   double totalGrand = (goldTotal + silverTotal - totalFee); System.out.printf("\nGrand Total: " + "\$%.2f", + totalGrand);   System.out.println(); System.out.println(); } }```
• February 4th, 2013, 05:20 PM
Norm
Re: How Do I Obtain a Grand Total From My Loop
Quote:

my total results are taking the data that I entered from the last customer
What variables does the program accumulate the totals in for the gold and silver?
Each customer's amounts need to be added to variables that will hold the total.