需要使用java类创建一个披萨店。我已经创建了一个类和驱动程序。一切都很好,但我真的不明白如何显示用户订购的比萨饼的总价和所有其他细节。
//Pizza class
import javax.swing.JOptionPane;
public class Pizza {
private String crust;
private boolean extraCheese;
private boolean drink;
private double price;
private String [] toppings = new String[5];
public Pizza() {//constructor
this.crust = "Regular Crust";
this.extraCheese = false;
this.drink = false;
this.price = 6.99;
}
public void setCrust(String crust) {//setter
this.crust = crust;
}
public void setExtraCheese(boolean extraCheese) {//setter
this.extraCheese = extraCheese;
}
public void setDrink(boolean drink) {//setter
this.drink = drink;
}
public void setPrice(double price) {//setter
this.price = price;
}
public String getCrust() {//getter
return this.crust;
}
public boolean getExtraCheese() {//getter
return this.extraCheese;
}
public boolean getDrink () {//getter
return this.drink;
}
public void selectCrust() {
crust = JOptionPane.showInputDialog(null, "What type of crust do you want? \nOriginal \nThin \nCheese Stuffed \nDeep Dish \nPlease, type a full name of crust.");
}
public void addExtraCheese(String decision) {
if(decision.equalsIgnoreCase("Yes")) {
extraCheese = true;
price = price+0.30;
price = total1;
}
}
public void addDrink (String decision) {
if(decision.equalsIgnoreCase("Yes")) {
drink = true;
price = price+1.39;
}
}
public void addToppings() {
String [] toppingsList = {"Pepperoni", "Onions", "Mushrooms", "Pepper", "Sausage"};
String Answ;
String Total = "";
double price = 0;
int index = 0;
do {
String temp = "";
toppings[index] = JOptionPane.showInputDialog(null, "Please, select toppings from the list: \nEnter the number \n1) "+ toppingsList[0] +"\n2) "+ toppingsList[1] +"\n3) "+ toppingsList[2] +"\n4) "+toppingsList[3]+"\n5) "+toppingsList[4]);
if (toppings[index].equals("1")) {
temp = temp + toppingsList[0] + ", ";
price = price+0.20;
}
if (toppings[index].equals("2")) {
temp = temp + toppingsList[1] + ", ";
price = price+0.20;
}
if (toppings[index].equals("3")) {
temp = temp + toppingsList[2] + ", ";
price = price+0.20;
}
if (toppings[index].equals("4")) {
temp = temp + toppingsList[3] + ", ";
price = price+0.20;
}
if (toppings[index].equals("5")) {
temp = temp + toppingsList[4] + ", ";
price = price+0.20;
}
index++;
Total = Total+temp;
Answ = JOptionPane.showInputDialog(null, "Would you like to add one more topping? \nYes/No");//asks if the user wants to select one more service
} while (Answ.equalsIgnoreCase("Yes") );
//JOptionPane.showMessageDialog(null, String.format("Chosen Toppings are: "+Total+" - $%.2f",price));
public double getPrice() {//getter
return this.price ;
}
}
}
驱动程序
import javax.swing.JOptionPane;
public class Driver {
public static void main (String[] args) {
Pizza NotRegular = new Pizza();
double price;
double TotalPrice;
JOptionPane.showMessageDialog(null, "Welcome to our Pizza Parlor!");
NotRegular.selectCrust();
String decision = JOptionPane.showInputDialog(null, "Would you like to add extra cheese?");
NotRegular.addExtraCheese(decision);
String decision1 = JOptionPane.showInputDialog(null, "Would you like to add drink?");
NotRegular.addDrink(decision1);
NotRegular.addToppings();
}
}
答案 0 :(得分:0)
public class Driver {
public static void main (String[] args) {
... // Your previous code
JOptionPane.showMessageDialog(null, "Price: " + NotRegular.getPrice() + " Crust: " + NotRegular.getCrust());
}
}
这将打印一个显示价格和cruts的选项窗格。使用类似的方式来获取任何其他东西。基本上,您想要调用/调用getter方法(返回属性并在其前面有注释//getters
的方法)。
此外,您可以详细了解getters
和setters
here。