0

I am learning Java through Tim Buchalka's Java Udemy course. In essence I have to create a program that can take orders for a burger restaurant - choose a burger, meat type, bread roll and then finally number of toppings. My current idea is to create one parent class called Burger with three sub-classes: Veggie, Healthy and Deluxe. Veggie takes 4 toppings, Healthy takes 6 and Deluxe takes no toppings (2 are automatically given.)

My current plan is for the base class initialization to show you what the three burgers offer so that the user can then choose which burger they want. That means I want to create the actual burger sub-class (i.e. veggie, healthy or deluxe) after the base Burger is initialized.

Since this question happens after we are taught polymorphism I was wondering if there was a way to pass a String as the class type itself. Example: Burger burger = new (string) So if the method passes veggie burger, the code is executed as: Burger burger = new VeggieBurger().

That's not the only issue. The constructor for the first two types accept toppings but deluxe doesn't. How would one fix that issue?

My code:

public class Burger {
    private String burgerType;
    private String meatChoice;
    private String breadRollType;
    private int toppingChoice;

    Scanner scanner = new Scanner(System.in);

    public Burger() {
    } //empty constructor

    public Burger(String burgerType) {
        System.out.println("Welcome to Bob's Burgers. Happy to serve you vegan meat burger since 2011!\nWe offer 3 types of burgers: 1) Veggie Burger\n2) Healthy Burger\n" +
                "3) Deluxe Hamburger\nWould you like to see details of each?");
        String answer = scanner.nextLine().toLowerCase(Locale.ROOT);
        if (answer.equals("yes")) {
            System.out.println("Which of the three burger's information would you like to see? Type number to get information.");
            int info = scanner.nextInt();
            scanner.nextLine();
            burgerInfo(info);
        }
        System.out.println("If you've decided on which burger, please enter the number of the same.");
        int burgerSelectionInvocation = scanner.nextInt();
        scanner.nextLine();
        switch (burgerSelectionInvocation) {
            case 1:
                burgerSelection("Veggie Burger");
                break;
            case 2:
                burgerSelection("Healthy Burger");
                break;
            case 3:
                burgerSelection("Deluxe Hamburger");
                break;
            default:
                System.out.println("Invalid input entered. The veggie burger will be selected for you by default.");
        }
    }

    public void burgerSelection (String burgerChoice)
    {
        System.out.println("I see you've selected our " + burgerChoice + ". Please let us know what kind of vegan meat do you want on top of the burger?");
        meatChoice = scanner.nextLine();

        System.out.println("What type of bread roll do you want?");
        breadRollType = scanner.nextLine();

        System.out.println("And how many toppings would you like on your Veggie Burger?");
        if (scanner.hasNextInt()) {
            toppingChoice = scanner.nextInt();
            scanner.nextLine();
        } else {
            System.out.println("Invalid number of toppings chosen. You shall have no toppings.");
            toppingChoice = 0;
        }

    }

    public void burgerInfo(int a)
    {
        String answer = "yes";
        while (answer.equals("yes"))
        {
            switch (a)
            {
                case 1:
                    System.out.println("Our Veggie Burgers cost a base price of Rs. 100 and offer up to 4 toppings.");
                case 2:
                    System.out.println("Our Healthy Burgers cost a base price of Rs. 150 and offer up to 6 toppings.");
                case 3:
                    System.out.println("Our Deluxe Hamburgers cost Rs. 150 and always come with chips and soda. You cannot add any toppings.");
            }
            System.out.println("Would you like to check the information for our other burgers?");
            Scanner scanner = new Scanner(System.in);
            answer = scanner.nextLine().toLowerCase(Locale.ROOT);
        }
    }
}




1 Answers1

0

You can try using generics:

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

This is mainly used to insert Entities in databases due that you only need to save them, not instanciate them so I think you shouldn't use it here.

This is an example:

// Instance of String type
    Test<String> sObj
        = new Test<String>("GeeksForGeeks");
    System.out.println(sObj.getObject());

Anyway you can give it a chance and read about it here:

https://www.geeksforgeeks.org/generics-in-java/#:~:text=Generics%20means%20parameterized%20types.,work%20with%20different%20data%20types.

HttpDefi
  • 9
  • 3