我们对Java有关IceCream商店的实际测试。我们需要在没有库存的情况下处理异常。以下应用程序的代码工作正常。例外情况得到妥善管理。
public class IceCreamApp2 {
public static void main(String[] args) {
Prijslijst priceList2 = new Prijslijst(2, 5, 8);
Stock stock = new Stock(1, 8, 2, 1);
IceCreamCar iceCar = new IceCreamCar(priceList2, stock);
try {
Eatable[] eatCar = {
iceCar.orderCone(new Cone.Flavor[]{Cone.Flavor.CHOCOLATE, Cone.Flavor.BANANA, Cone.Flavor.VANILLA}),
iceCar.orderCone(new Cone.Flavor[]{Cone.Flavor.VANILLA, Cone.Flavor.VANILLA}),
iceCar.orderMagnum(Magnum.MagnumType.ROMANTICSTRAWBERRIES),
iceCar.orderMagnum(Magnum.MagnumType.ALPINENUTS),
iceCar.orderIceRocket()
};
for (int i = 0; i < eatCar.length; i++) {
eatCar[i].eat();
}
System.out.println(iceCar.getProfit());
} catch (NoMoreIceCreamException noMoreIce) {
System.out.println("No More Ice To sell... Beat it!!");
System.out.println("Message: " + noMoreIce.getMessage());
System.out.println("Cause: " + noMoreIce.getCause());
}
System.out.println(iceCar.getProfit());
}
}
然而,这种逻辑是有缺陷的,因为它会在出现异常时停止计算其余的顺序。因此,Magnums和Icerockets尽管有库存,但不计入利润。 为此,我们知道我们需要遍历“Eatable”表。但它不起作用并退出代码1,但有例外:
public class IceCreamApp2 {
public static void main(String[] args) {
Prijslijst priceList2 = new Prijslijst(2, 5, 8);
Stock stock = new Stock(1, 8, 2, 1);
IceCreamCar iceCar = new IceCreamCar(priceList2, stock);
Eatable[] eatCar = {
iceCar.orderCone(new Cone.Flavor[]{Cone.Flavor.CHOCOLATE, Cone.Flavor.BANANA, Cone.Flavor.VANILLA}),
iceCar.orderCone(new Cone.Flavor[]{Cone.Flavor.VANILLA, Cone.Flavor.VANILLA}),
iceCar.orderMagnum(Magnum.MagnumType.ROMANTICSTRAWBERRIES),
iceCar.orderMagnum(Magnum.MagnumType.ALPINENUTS),
iceCar.orderIceRocket()
};
for (int i = 0; i < eatCar.length; i++) {
try {
eatCar[i].eat();
} catch (NoMoreIceCreamException noMoreIce) {
System.out.println("No More Ice To sell... Beat it!!");
System.out.println("Message: " + noMoreIce.getMessage());
System.out.println("Cause: " + noMoreIce.getCause());
}
}
System.out.println(iceCar.getProfit());
}
}
有什么可能是错的?
答案 0 :(得分:0)
这是堆栈跟踪:
Preparing your Balls on a cone
Exception in thread "main" Seller.NoMoreIceCreamException: No more Balls or Cones
at Seller.IceCreamCar.prepareCone(IceCreamCar.java:39)
at Seller.IceCreamCar.orderCone(IceCreamCar.java:30)
at App.IceCreamApp2.main(IceCreamApp2.java:18)
Process finished with exit code 1
@Andrew S条件已在“IceCreamCar”类中处理。简而言之:
public class IceCreamCar implements IceCreamSeller {
// Instance Variables
Prijslijst priceList;
Stock stock;
private double profit;
// Constructor
public IceCreamCar() {
}
public IceCreamCar(Prijslijst priceList, Stock stock) {
this.priceList = priceList;
this.stock = stock;
}
// Methods
// Order Cone
@Override
public Cone orderCone(Cone.Flavor[] balls) {
this.prepareCone(balls);
return new Cone(balls);
}
// // Prepare Cone
private Cone prepareCone(Cone.Flavor[] balls) {
int countCones = 1;
if (stock.getCones() < 0 || stock.getBalls() < 0) {
throw new NoMoreIceCreamException("No more Balls or Cones");
} else {
for (int i = 0; i < balls.length; i++) {
stock.setBalls(stock.getBalls() - balls.length);
profit += priceList.getBallPrice();
}
System.out.println("Preparing your Balls on a cone");
countCones++;
stock.setCones(stock.getCones() - countCones);
}
return new Cone(balls);
}
感谢你们两位的回答:)
答案 1 :(得分:0)
好的,问题解决了。实际上解决方案是在每个orderCone,orderMagnum等中放置try和catch ... 这是例外处理得当。