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 3 of 3

Thread: I need a running total. Help please!

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need a running total. Help please!

    This is my first semester taking a coding class and so far I really like it! However, I am stuck on this one problem. I want to have a running total following my if-else statements. You will see that I have equations below, but when I run the program the only output is the most recent iteration for each item. Any suggestions?


    import java.util.Random; //Creation of the Random method
    import java.util.Scanner; //Creation of the Scanner method
    import java.text.DecimalFormat; //Creation of the Decimal Formatting method.

    public class CoffeeShopSalesSimulator
    {
    public static void main(String[] args)
    {
    int item = 0, quantity = 0;
    int RerunProgram = 0;
    int hours;
    int numberofCustomersserved;
    double sales_goal;
    double TotalCost_Coffee = 0.0;
    double TotalCost_Latte = 0.0;
    double TotalCost_Cappuccino = 0.0;
    double TotalCost_Espresso = 0.0;
    double TotalnetCost_Coffee = 0;
    double TotalnetCost_Latte = 0;
    double TotalnetCost_Cappucino = 0;
    double TotalnetCost_Espresso = 0;
    double Total_Sales;
    String itemOrdered = "";
    String Total_SalesString;
    String TotalnetCost_CoffeeString;
    String TotalnetCost_LatteString;
    String TotalnetCost_CappucinoString;
    String TotalnetCost_EspressoString;
    String sales_goalString;

    Random randomNums = new Random(); //Creation of the random object
    Scanner keyboard = new Scanner (System.in); //Creation of the scanner object
    DecimalFormat formatter = new DecimalFormat ("$#0.00"); //Creation of the DecimalFormat object

    System.out.println("Welcome to the Coffee Shop Simulator!\n");

    do
    {
    System.out.println("Please enter the number of hours: ");
    hours = keyboard.nextInt();
    numberofCustomersserved = (hours * 4);
    System.out.println("\nPlease enter the sales goal without a dollar sign (e.g. 250.00): ");
    sales_goal = keyboard.nextDouble();

    final double coffee_price = 1.50;
    final double latte_price = 3.50;
    final double cappuccino_price = 3.25;
    final double espresso_price = 2.00;

    for(int n = 1; n <= numberofCustomersserved; n++)
    {
    item = randomNums.nextInt(4) + 1;
    quantity = randomNums.nextInt(10) +1;

    if (item == 1)
    {
    itemOrdered = "Coffee";
    TotalCost_Coffee = (coffee_price * quantity);
    System.out.println("Customer " + n);
    System.out.println("Item Ordered: " + itemOrdered);
    System.out.println("Quantity purchased: " + quantity);
    System.out.println("Total Cost: " + formatter.format(TotalCost_Coffee) + "\n");
    }
    else if (item == 2)
    {
    itemOrdered = "Latte";
    TotalCost_Latte = (latte_price * quantity);
    System.out.println("Customer " + n);
    System.out.println("Item Ordered: " + itemOrdered);
    System.out.println("Quantity purchased: " + quantity);
    System.out.println("Total Cost: " + formatter.format(TotalCost_Latte) + "\n");
    }
    else if (item == 3)
    {
    itemOrdered = "Cappuccino";
    TotalCost_Cappuccino = (cappuccino_price * quantity);
    System.out.println("Customer " + n);
    System.out.println("Item Ordered: " + itemOrdered);
    System.out.println("Quantity purchased: " + quantity);
    System.out.println("Total Cost: " + formatter.format(TotalCost_Cappuccino) + "\n");
    }
    else if (item == 4)
    {
    itemOrdered = "Espresso";
    TotalCost_Espresso = (espresso_price * quantity);
    System.out.println("Customer " + n);
    System.out.println("Item Ordered: " + itemOrdered);
    System.out.println("Quantity purchased: " + quantity);
    System.out.println("Total Cost: " + formatter.format(TotalCost_Espresso) + "\n");
    }
    }
    TotalnetCost_Coffee = TotalCost_Coffee;
    TotalnetCost_Latte = TotalCost_Latte;
    TotalnetCost_Cappucino = TotalCost_Cappuccino;
    TotalnetCost_Espresso = TotalCost_Espresso;
    Total_Sales = (TotalnetCost_Coffee + TotalnetCost_Latte + TotalnetCost_Cappucino + TotalnetCost_Espresso);
    TotalnetCost_CoffeeString = formatter.format(TotalnetCost_Coffee);
    TotalnetCost_LatteString = formatter.format(TotalnetCost_Latte);
    TotalnetCost_CappucinoString = formatter.format(TotalnetCost_Cappucino);
    TotalnetCost_EspressoString = formatter.format(TotalnetCost_Espresso);
    Total_SalesString = formatter.format(Total_Sales);
    sales_goalString = formatter.format(sales_goal);

    System.out.println("---Simulation End---");
    System.out.printf("%12s%12s%20s%20s%15s\n", "Coffee Sales", "Latte Sales", "Cappuccino Sales", "Espresso Sales", "Total Sales");
    System.out.printf("%12s%12s%20s%20s%15s\n", "------------", "-----------", "----------------", "--------------", "-----------");
    System.out.printf("%12s%12s%20s%20s%15s\n\n", TotalnetCost_CoffeeString, TotalnetCost_LatteString, TotalnetCost_CappucinoString, TotalnetCost_EspressoString, Total_SalesString);

    if (Total_Sales < sales_goal)
    System.out.println("The sales goal of " + sales_goalString + " was not achieved!\n");
    else
    System.out.println("The sales goal of " + sales_goalString + " was achieved!\n");

    System.out.println("Enter 1 to run another simulation or 0 to exit: ");
    RerunProgram = keyboard.nextInt();
    }while(RerunProgram == 1); //I need a while loop where if the user inputs 0, it ends the program and displays a message.

    }
    }


  2. #2
    Junior Member
    Join Date
    Oct 2014
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: I need a running total. Help please!

    Well, consider what information you need to store: I suspect the answer is the total cost after every customer. Then where would you put this variable and when would it need to be changed.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I need a running total. Help please!

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

Similar Threads

  1. Replies: 21
    Last Post: June 12th, 2013, 11:33 AM
  2. total noob needs help
    By rshull1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2013, 05:53 PM
  3. Total Newbie!
    By Luly_Marty in forum Member Introductions
    Replies: 2
    Last Post: November 26th, 2012, 07:40 PM
  4. Total NOOB question
    By coolidge in forum Java Theory & Questions
    Replies: 3
    Last Post: September 2nd, 2011, 04:09 PM
  5. I trying to calculate total price.
    By Muhanguzi in forum JDBC & Databases
    Replies: 6
    Last Post: June 15th, 2011, 03:35 PM