我如何填写主要班级的空白,饮料/饮料盒/饮料Incyclinder/等



我都设置了除主人以外的所有类,这就是我需要做的:

case 'A':   //Add Drink
           System.out.print("Please enter a drink information to add:n");
           inputInfo = scan.nextLine().trim();
/***********************************************************************************
***  ADD your code here to create an object of one of child classes of Drink class
***  and add it to the drinkList
***********************************************************************************/
           break;
         case 'C':   //Compute Total Prices
/***********************************************************************************
***  ADD your code here to compute the total price for all drinks in the list.
***********************************************************************************/
           for(int i = 0 ; i < drinkList.length(); i++) {
               drinkList[i]
           }
           System.out.print("total prices computedn");
           break;
         case 'D':   //Search for Drink
           System.out.print("Please enter a drinkID to search:n");
           inputInfo = scan.nextLine().trim();
/***********************************************************************************
***  ADD your code here to search a given drinkID. If found, set "found" true,
***  and set "found" false otherwise.
***********************************************************************************/
            if (found == true)
             System.out.print("drink foundn");
            else
             System.out.print("drink not foundn");
           break;

这是我的Drinkparser的代码:

Drink myDrink = null;
    if(word[0].equals("Cylinder"))
    {
        int one = Integer.parseInt(token4);
        double two = Double.parseDouble(token3);
        int three = Integer.parseInt(token5);
        myDrink = new DrinkInCylinder(token2, two, one, three);
    }
    else
    {
        double one = Double.parseDouble(match3);
        int two = Integer.parseInt(match4);
        int three = Integer.parseInt(match5);
        int four = Integer.parseInt(match6);
        myDrink = new DrinkInBox(match2, one, two, three, four);
    }
    return myDrink;
}

这是Drinkinbox和Drinkincylinder的方法:

public void computeTotalPrice()
{
    volume = (int)(Math.PI*(radius * radius * height));
    totalPrice = volume * unitPrice;
}

我需要做什么才能填写这些空白?我认为计算总价格应该是:

for(int i = 0 ; i < drinkList.length(); i++) {
               drinkList[i] //I don't know how to call the computeTotalPrice method for any particular index of drinkList, I can't say .drinkInCylinder or drinkInBox because there isn't a way to tell which object type it is

您可以做这个

for(int i = 0 ; i < drinkList.length(); i++) {
    if(drinkList[i] instanceof DrinkInBox)
        (DrinkInBox)drinkList[i].computeTotalPrice();
    else if(drinkList[i] instanceof DrinkInCylinder)
        (DrinkInCylinder)drinkList[i].computeTotalPrice();
}

但是,这有点冗余,很明显您可以拥有一个超级班级,例如Drink。您应该在该类上定义computeTotalPriceDrinkInBoxDrinkInCylinder将同时继承Drink

顺便说一句,按照Java命名惯例,课程应从大写字母开始。

相关内容

  • 没有找到相关文章

最新更新