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