为什么切换用例部分和运行时多态性不起作用?

时间:2019-04-27 09:05:10

标签: java polymorphism

我在(switch-case)部分遇到了问题,并且尝试在main中测试一些运行时多态性。它不起作用。在开关情况下;它总是说:

  

您的选择不可用。

最后,它不会退出程序。它连续循环。 我应该怎么做才能摆脱这个问题?

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String tempName;
        int tempQueue;
        double tempKilo, tempCalibre;
        String tempAgri;
        ArrayList<Farmer> farmers = new ArrayList<>();
        final int capacity = 2;

        for(int i=0; i<capacity; i++)            {
            System.out.println(" Please enter the farmer's name : ");
            tempName = sc.nextLine();
            System.out.println(" Please enter the queue number : ");
            tempQueue = Integer.parseInt(sc.nextLine());
            System.out.println(" Please enter the calibre of the cherry : ");
            tempCalibre = Double.parseDouble(sc.nextLine());
            System.out.println(" Please enter the kilo of the cherry : ");
            tempKilo = Double.parseDouble(sc.nextLine());
            Cherry cherry1 = new Cherry(tempName, tempQueue, tempCalibre, tempKilo);
            farmers.add(cherry1);
            cherry1.displayMenu();
            cherry1.Price();
        }
        for(int i=0; i<capacity; i++) {
            System.out.println(" Please enter the farmer's name : ");
            tempName = sc.nextLine();
            System.out.println(" Please enter the queue number : ");
            tempQueue = Integer.parseInt(sc.nextLine());
            System.out.println(" Please enter the production type of Apple : ");
            tempAgri = sc.nextLine();
            System.out.println(" Please enter the kilo of the apple : ");
            tempKilo = Double.parseDouble(sc.nextLine());
            Apple apple1 = new Apple(tempName, tempQueue, tempAgri, tempKilo);
            farmers.add(apple1);
            apple1.displayMenu();
            apple1.Price();
        }
        ArrayList<Farmer>farmers1=new ArrayList<>();
        int checkpoint;
        boolean isPFruitAvailable;
        double totalPrice;
        while(true) {
            display();
            checkpoint=Integer.parseInt(sc.nextLine());
            switch(checkpoint) {
                case 1:
                    System.out.println(" Please enter the name of the farmer that you want... ");
                    tempName=sc.nextLine();
                    isPFruitAvailable=false;
                    for(Farmer fruit : farmers1) {
                        if(fruit.getName().equals(tempName)) {
                            farmers1.add(fruit);
                            System.out.println("Your choice is successfully added.");
                            isPFruitAvailable=true;
                            break;
                        }
                    }
                    if(!isPFruitAvailable) {
                            System.out.println(" Your choice is not available");
                    }
                    break;
                case 2:
                    totalPrice=0.0;
                    System.out.println(" Please enter the name in the cart . ");
                    System.out.println(" Checking out...");
                    for(Farmer fruit1 : farmers1) {
                        fruit1.displayMenu();
                        totalPrice+=fruit1.Price();
                    }
                    System.out.println(" Price is "+totalPrice);
                    break;
                default:
                    System.out.println(" Thanks for your informations...");
                    System.out.println(" The program quits within a second... ");
                    break;
            }
        }
    }

    public static void display() {
       System.out.println(" Welcome to fruit transfer system... ");
       System.out.println(" Press 1 to add fruit into the system... ");
       System.out.println(" Press 2 to check fruit you want... ");
       System.out.println(" Press any key to quit the program.... ");
    }
}

2 个答案:

答案 0 :(得分:2)

您从未填充过farmers1列表,该列表始终为空,因此isFruitAvailable始终为false。而且,您的while循环永远不会终止,它只会不断旋转(您在switch内与“ while”无关的“ break”语句)

for(Farmer fruit : farmers1) {
    if(fruit.getName().equals(tempName)) {
        farmers1.add(fruit); //the only place where you add something, but it never gets executed
        break;
    }
}

答案 1 :(得分:0)

根据我的理解,您想将新农民与人口稀少的farmers农民列表进行比较,如果匹配,则将其添加到farmers1列表中。

因此,在switch情况下,请更改for循环,如下所示:

// Compare the farmer tempName with the list of farmers already populated
for(Farmer fruit : farmers) {
    if(fruit.getName().equals(tempName)) {
        farmers1.add(fruit);
        System.out.println("Your choice is successfully added.");
        isPFruitAvailable=true;
        break;
    }
}