提问人:Lavendran Muralidaran 提问时间:11/14/2023 最后编辑:enzoLavendran Muralidaran 更新时间:11/14/2023 访问量:76
我如何获得总成本和要添加的食物
How do i get the total cost to appear and the food to be added
问:
public class Food1 {
private String itemName;
private double price;
public Food1(String itemName, double price) {
this.itemName = itemName;
this.price = price;
}
public String getitemName() {
return this.itemName;
}
public double getPrice() {
return this.price;
}
public String toString() {
return " " + this.itemName + " " + "RM " + this.price;
}
}
这是我搞砸的地方,我只是无法获得显示成本和添加更多订单的代码。
import java.util.ArrayList;
import java.util.Scanner;
public class Order {
ArrayList<Food1> Orders;
double totalcost = 0;
Scanner in = new Scanner(System.in);
public Order() {
Orders = new ArrayList<>();
}
public String printReciept() {
return "Here's your receipt!!\n" + toString() + "The total amount needed to be paid: \n" + "RM " + totalcost;
}
public double gettotal() {
return totalcost;
}
public void addOrder(String itemName, double price) {
Food1 f1 = new Food1(itemName, price);
Orders.add(f1);
totalcost += f1.getPrice();
}
// this part is where all the mess is, it runs well, except it doesnt add the food i select.
public String printMenu() {
System.out.println("Please check out our amazing meals for today!!");
int selectedElement;
do {
for (int i = 0; i < Orders.size(); i++) {
System.out.println((i + 1) + ". " + Orders.get(i));
}
System.out.println("Please type out the Food Code!! <3");
selectedElement = in .nextInt();
if (selectedElement > 0 && selectedElement <= Orders.size()) {
System.out.println("The items you have currently selected are: \n" + Orders.get(selectedElement - 1));
System.out.println("Would you like to add more? If yes, press 1. If no, press 2.");
int j = in .nextInt();
if (j == 1) {
continue;
} else if (j == 2) {
break;
}
} else {
System.out.println("Oh no!! It seems that you have entered the wrong code!! :( ");
System.out.println("Please refresh to start over :)");
}
} while (true);
return printReciept();
}
public String toString() {
return "Welcome to Happy U Cafe!!" + "\n";
}
}
这部分现在还可以,所以是的
public class Ordering {
public static void main(String\[\] args) {
Order f1 = new Order();
f1.addOrder("Chilli Chicken Burger", 16.75);
f1.addOrder("Lamb Taco", 17.45);
f1.addOrder("Spaghetti Bolognese", 13.05);
f1.addOrder("Fried Rice with Butter Chicken", 14.99);
f1.addOrder("Chilli Pan Mee", 12.08);
f1.addOrder("Wan Tan Mee Noodles with Dumpling Soup", 10.76);
f1.addOrder("Club Sandwich", 12.09);
f1.addOrder("Nasi Lemak Ayam Rendang", 14.00);
f1.addOrder("Fried Chicken Teriyaki Onigiri", 4.50);
System.out.println(f1);
f1.printMenu();
f1.printReciept();
f1.gettotal();
}
}
我试过这个,但是呃,每当我添加一个新项目时,它都会重置,我无法获得显示的总成本,但我必须使用最简单的编码形式(尝试使用我从外面学到的编码,但我被指控使用聊天 gpt)。
这是结果
Welcome to Happy U Cafe!!
Please check out our amazing meals for today!!
1. Chilli Chicken Burger RM 16.75
2. Lamb Taco RM 17.45
3. Spaghetti Bolognese RM 13.05
4. Fried Rice with Butter Chicken RM 14.99
5. Chilli Pan Mee RM 12.08
6. Wan Tan Mee Noodles with Dumpling Soup RM 10.76
7. Club Sandwich RM 12.09
8. Nasi Lemak Ayam Rendang RM 14.0
9. Fried Chicken Teriyaki Onigiri RM 4.5
Please type out the Food Code!! <3
8
The items you have currently selected are:
Nasi Lemak Ayam Rendang RM 14.0
Would you like to add more? If yes, press 1. If no, press 2.
1
1. Chilli Chicken Burger RM 16.75
2. Lamb Taco RM 17.45
3. Spaghetti Bolognese RM 13.05
4. Fried Rice with Butter Chicken RM 14.99
5. Chilli Pan Mee RM 12.08
6. Wan Tan Mee Noodles with Dumpling Soup RM 10.76
7. Club Sandwich RM 12.09
8. Nasi Lemak Ayam Rendang RM 14.0
9. Fried Chicken Teriyaki Onigiri RM 4.5
Please type out the Food Code!! <3
5
The items you have currently selected are:
Chilli Pan Mee RM 12.08
Would you like to add more? If yes, press 1. If no, press 2.
2
答: 暂无答案
评论
System.out.println
f1.gettotal()
System.out.println("The items you have currently selected are: \n" + Orders.get(selectedElement - 1));
Order
Ordering