我目前正在尝试创建一个应用程序,我试图重建一个果汁店,人们可以在那里订购的东西。剩余果汁的数量是3,每购买一个果汁的数量减少1,并形成某种原因,确切的减少不起作用。
希望你能帮我...
我创建了两个类:
1.程序,其中我给出了3个命令:
public class Program {
public static void main(String[] args) {
JuiceStore Juice1 = new JuiceStore(14);
JuiceStore Juice2 = new JuiceStore(7);
JuiceStore Juice3 = new JuiceStore(17);
try {
Juice1.buyJuice();
}
catch(NoJuiceException e) {
System.out.println();
System.out.println(e.getMessage());
}
catch(TooColdException e) {
System.out.println(e.getMessage());
}
catch(TooWarmException e) {
System.out.println("The juice is too warm.");
}
try {
Juice2.buyJuice();
}
catch(NoJuiceException e) {
System.out.println();
System.out.println(e.getMessage());
}
catch(TooColdException e) {
System.out.println(e.getMessage());
}
catch(TooWarmException e) {
System.out.println(e.getMessage());
}
try {
Juice3.buyJuice();
}
catch(NoJuiceException e) {
System.out.println();
System.out.println(e.getMessage());
}
catch(TooColdException e) {
System.out.println(e.getMessage());
}
catch(TooWarmException e) {
//e.getMessage();
System.out.println(e.getMessage());
}
}
}
2.JuiceStore,我在其中声明了购买方式:
public class JuiceStore {
private int temperature;
private int leftJuices = 3;
JuiceStore(int temperature) {
this.temperature = temperature;
}
public void buyJuice() throws NoJuiceException, TooColdException, TooWarmException {
if(this.leftJuices < 1) throw new NoJuiceException("Unfortunately, there is no juice left. Come back tomorrow.");
this.leftJuices = leftJuices-1;
System.out.println();
System.out.println("You have bought a juice, there are " + this.leftJuices + " left.");
if (this.temperature < 9) throw new TooColdException("The juice is too cold.");
if (this.temperature > 15)throw new TooWarmException("The juice is too warm.");
System.out.println("Drink successful.");
}
}
1条答案
按热度按时间2skhul331#
我们创建了3个独立的
JuiceStore
示例,每个示例都有自己的leftJuices
计数器,当我们处理完所有示例后,它们的leftJuices变量都将为2。如果你想在示例之间共享一个变量,可以设置为
static
,或者只设置一个JuiceStore总计。