この 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++;
}