我正在制作一个程序,允许用户从咖啡馆订购一些食物,然后打印食物的收据。我想在收据上打印订购的产品,只打印订购的产品,以及数量和价格*数量。我发现使用数组是我能以列表格式排序的唯一方法。问题是它显示的项目未按$ 0排序为空值。我希望它只显示订购的商品。此外,我无法弄清楚如何显示订购商品旁边的每件商品的数量。
产品类
import java.util.Scanner; //program uses class Scanner
public void displayMenu() {
System.out.printf("%-12s%12s%12s%n", "======", getBelliFreschiName(), "======");
System.out.printf("%-12s%12s%14s%n", "1 ", getStarbucksName(), " $" + getStarbucksPrice());
System.out.printf("%-8s%12s%22s%n", "2 ", getTazoName(), " $" + getTazoPrice());
System.out.printf("%-12s%12s%16s%n", "3 ", getEinsteinName(), " $" + getEinsteinPrice());
System.out.printf("%-12s%12s%15s%n", "4 ", getKrusteazName(), " $" + getKrusteazPrice());
System.out.printf("%-12s%12s%12s%n", "======", "==================", "======");
}
public void orderProduct() {
do {
System.out.println("Please Enter A Product Number Between 1-4 or Press 0 to Exit");
product = input.nextInt();
if (product == 0) {
break;
}
System.out.println("Please Enter Quantity");
quantity = input.nextInt();
switch (product) {
case 1:
coffeePrice = 2.11;
subTotal += (2.11 * quantity);
selectedValue[1] = 2.11 * quantity;
nameProduct[1] = "Starbucks Coffee";
break;
case 2:
teaPrice = 2.51;
subTotal += (2.51 * quantity);
selectedValue[2] = 2.51 * quantity;
nameProduct[2] = "Tazo Tea";
break;
case 3:
bagelPrice = 3.14;
subTotal += (3.14 * quantity);
selectedValue[3] = 3.14 * quantity;
nameProduct[3] = "Einstein Bagel";
break;
case 4:
muffinPrice = 3.54;
subTotal += (3.54 * quantity);
selectedValue[4] = 3.54 * quantity;
nameProduct[4] = "Krusteaz Muffin";
break;
default:
System.out.println("Please enter a valid product No.");
}
}
while (product != 0);
for (int i = 1; i < 5; i++) {
System.out.println(nameProduct[i] + " " + " $" + selectedValue[i]);
}
System.out.printf("%s%.2f", "\nSubtotal: $", subTotal);
System.out.printf("%5s%.2f", "\nTax: $", (subTotal * tax));
System.out.printf("%4s%.2f", "\nTotal: $", (subTotal + (subTotal * tax)));
}
}