Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get the total cost of sales of each item within a loop?
    primarykey
    data
    text
    <p>Within this while loop, there are 4 items that can be picked. Coffee (1), Latte (2), Cappuccino (3), and Espresso (4). The simulation picks a bunch of random numbers in to fill in each customer's order and quantity, and then it calculates the total cost for each customer. Now I have to make a sales report after this while loop and I have to calculate the total amount of sales for each item. How do I do that since the numbers are random and the loop repeats itself depending on the number of customers?</p> <pre><code>while (CustomersSimulated &lt;= MaxCustomersSimulated) { //customer number System.out.println("Customer " + CustomersSimulated); //one random item for each customer Random RandomList = new Random(); int RandomItem = RandomList.nextInt(4) + 1; if (RandomItem == 1) { System.out.println("Item purchased: Coffee"); final double CoffeePrice = 1.50; //random quantity for the item Random RandomListTwo = new Random(); int RandomQuantity = RandomListTwo.nextInt(5) + 1; System.out.println("Quantity purchased: " + RandomQuantity); //total cost for the one customer double TotalCoffeeCost = RandomQuantity * CoffeePrice; System.out.println("Total Cost: $" + NumberFormat.format(TotalCoffeeCost)); } else if (RandomItem == 2) { System.out.println("Item purchased: Latte"); final double LattePrice = 3.50; Random RandomListTwo = new Random(); int RandomQuantity = RandomListTwo.nextInt(5) + 1; System.out.println("Quantity purchased: " + RandomQuantity); double TotalLatteCost = RandomQuantity * LattePrice; System.out.println("Total Cost: $" + NumberFormat.format(TotalLatteCost)); } else if (RandomItem == 3) { System.out.println("Item purchased: Cappuccino"); final double CappuccinoPrice = 3.25; Random RandomListTwo = new Random(); int RandomQuantity = RandomListTwo.nextInt(5) + 1; System.out.println("Quantity purchased: " + RandomQuantity); double TotalCappuccinoCost = RandomQuantity * CappuccinoPrice; System.out.println("Total Cost: $" + NumberFormat.format(TotalCappuccinoCost)); } else if (RandomItem == 4) { System.out.println("Item purchased: Espresso"); final double EspressoPrice = 2.00; Random RandomListTwo = new Random(); int RandomQuantity = RandomListTwo.nextInt(5) + 1; System.out.println("Quantity purchased: " + RandomQuantity); double TotalEspressoCost = RandomQuantity * EspressoPrice; System.out.println("Total Cost: $" + NumberFormat.format(TotalEspressoCost)); } System.out.println(" "); CustomersSimulated++; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload