您正在为学校制作一个程序,以此形式阅读用户的输入
“String / double / int / int / int”或“String / double / int / int”,具体取决于饮料的形状。
到目前为止,我已经为Parser类提供了代码。
public class DrinkParser {
public static Drink parseStringToDrink(String lineToParse){
String regex = "[/]";
String [] split = lineToParse.split(regex);
if ("Box".equals(split[0]) || "box".equals(split[0])){
DrinkInBox dIB = new DrinkInBox(split[1], split[2], split[3], split[4], split[5]);
}
if("Cylinder".equals(split[0]) || "cylinder".equals(split[0])){
DrinkInCylinder dIC = new DrinkInCylinder(split[1], split[2], split[3], split[4]);
}
}
}
并且继承了DrinkInBox和DrinkInCylinder类的代码......我觉得它的相关性。
public class DrinkInBox extends Drink {
private int height;
private int width;
private int depth;
public DrinkInBox(String drinkId, double unitPrice, int height, int width, int depth){
super(drinkId, unitPrice);
this.height = height;
this.width = width;
this.depth = depth;
}
public void computeTotalPrice(){
volume = height * width * depth;
totalPrice = volume * unitPrice;
}
public String toString(){
return "\nThe Drink in a Box\nThe Height:\t\t" +height+ "\nThe Width:\t\t" +width+ "\nThe Depth:\t\t" +depth+ "\nThe DrinkId:\t\t" +drinkId+ "\n The Volume:\t\t" +volume+ "\nThe Unit Price:\t\t" +unitPrice+ "\n The Total Price:\t" +totalPrice+ "\n\n";
}
}
继承人在DrinkIn Cylinder Class
public class DrinkInCylinder extends Drink {
private int radius;
private int height;
public DrinkInCylinder(String drinkId, double unitPrice, int radius, int height){
super(drinkId, unitPrice);
this.radius = radius;
this.height = height;
}
public void computeTotalPrice(){
volume = (int) (Math.PI * (radius*radius) * height);
totalPrice = volume * unitPrice;
}
public String toString(){
return "\nThe Drink in a Cylinder\nThe Radius:\t\t" +radius+ "\nThe Height:\t\t" +height+ "\nThe DrinkId:\t\t" +drinkId+ "\n The Volume:\t\t" +volume+ "\nThe Unit Price:\t\t" +unitPrice+ "\n The Total Price:\t" +totalPrice+ "\n\n";
}
}
我知道很多,但我感谢你们能提供的任何帮助。
答案 0 :(得分:2)
DrinkInBox dIB = new DrinkInBox(split[1], split[2], split[3], split[4], split[5]);
split
是String[]
,因此split[n]
,0 <= n <= split.length-1
将返回String
。现在让我们来看看你的DrinkInBox
构造函数:
public DrinkInBox(String drinkId, double unitPrice, int height, int width, int depth)
您传递5 String
作为参数,但您没有DrinkInBox
构造函数,其中包含5个String
参数。您必须相应地解析每个拆分,使其与DrinkInBox
构造函数的参数类型匹配。
DrinkInBox dIB = new DrinkInBox(split[1], Double.parseDouble(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]), Integer.parseInt(split[5]));
您的代码中还会出现另一种情况。你需要做同样的事情。
另外,只是一个改进的提示,你应该熟悉继承和多态。 DrinkInBox
和DrinkInCylinder
都有一个共同的超类。你可以改写为:
Drink drink = split[0].equalsIgnoreCase("box") ? new new DrinkInBox(split[1], Double.parseDouble(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]), Integer.parseInt(split[5])) : split[0].equalsIgnoreCase("cylinder") ? new DrinkInCylinder(split[1], Double.parseDouble(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]), Integer.parseInt(split[5])) : null;
如果它不是方框或圆柱,drink
将是null
。您可以通过执行DrinkInBox
检查来查看是DrinkInCylinder
还是instanceof
:
if(drink instanceof DrinkInBox)
if(drink instanceof DrinkInCylinder)