在这个 while 循环中,可以选择 4 个项目。咖啡 (1)、拿铁 (2)、卡布奇诺 (3) 和浓缩咖啡 (4)。模拟选择一堆随机数填写每个客户的订单和数量,然后计算每个客户的总成本。现在我必须在这个while循环之后制作一个销售报告,我必须计算每个项目的总销售额。我该怎么做,因为数字是随机的,并且循环会根据客户数量自行重复?
while (CustomersSimulated <= 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++;
}