Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How Do I Obtain a Grand Total From My Loop

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.

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


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: How Do I Obtain a Grand Total From My Loop

    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.

  3. The Following User Says Thank You to Norm For This Useful Post:

    sternfox (February 4th, 2013)

Similar Threads

  1. Obtain string from certain index in a string array
    By MartialLaw in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 16th, 2013, 08:55 PM
  2. how to obtain the combination of ip in String??
    By sxlend in forum Java Networking
    Replies: 2
    Last Post: July 15th, 2012, 02:03 PM
  3. Error using a structure to obtain information of an specific array
    By andresfelquintero in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2011, 07:51 PM
  4. Replies: 1
    Last Post: February 4th, 2010, 04:23 PM