嗨即時通訊讀取用戶在這種形式的輸入無法將字符串轉換爲字符串拆分類中間的兩倍?
「字符串/雙/ int/int/int」或「字符串/雙/ 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";
}
}
繼承人邊喝邊Cylinder類
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";
}
}
我知道它的很多,但我感謝所有幫助,你們可能能夠提供。
使用方法'equalsIgnoreCase()',而不是兩個'等於()'。除非您正確地檢查,否則我認爲您不會這樣做,它會提高可讀性並提高性能(只需一點點)。 – initramfs