简体   繁体   中英

New List/Array without a set amount of numbers?

I'm working on a restaurant system and a management system, however it is currently the restaurant system. Basically, I am trying to create an empty list so that I can add items to it without saying that it has to have a certain amount of items in the list. I have looked around but as a beginner, I'm finding it harder to understand the answers on some questions. If somebody could show me how to incorporate it into my code, that would be great! Thanks in advance :)

    import java.util.Scanner;

class RestaurantMain {
    public static void main(String[] args)
    {
        //Variables//
        int choice;
        int customerChoice;
        boolean trueFalse;
        int restart = 0;
        String choice2;

        //EndVariables//
        Scanner in = new Scanner(System.in);

        System.out.println("Welcome to the Cooper's restaurant system!");
        System.out.println("How can I help?");
        System.out.println("");
        System.out.println("1. Customer System");
        System.out.println("2. Management System");
        System.out.println("");
        System.out.println("");
        System.out.print("Which option do you choose: ");
        choice = in.nextInt();

            if (choice == 1) {
                System.out.println("Our menu's are as follows:");
                System.out.println("");
                System.out.println("1. Drinks");
                System.out.println("2. Starters");
                System.out.println("3. Mains");
                System.out.println("4. Desserts");
                System.out.println("");
                System.out.print("What menu would you like to follow? ");
                customerChoice = in.nextInt();

                    if (customerChoice == 1) {
                        System.out.println("Drinks Menu");
                            System.out.println("Would you like to order? ");
                            in.nextLine();
                    }
                    if (customerChoice == 2) {
                        System.out.println("Starters Menu");
                    }
                    if (customerChoice == 3) {
                        System.out.println("Mains menu");
                    }
                    if (customerChoice == 4) {
                        System.out.println("Desserts Menu");
                    }



            }

Try using an ArrayList. It is like an array, but with dynamic size.

Also see: http://www.tutorialspoint.com/java/java_arraylist_class.htm

Try the ArrayList class. You can set an initial size on construction time, but it resizes on the go as it gets full. It's behaviour it's the same of an array, but it 'grows' when runs out of space.

I have no idea, why do you wanna List. I'm sure that better use arrays and static methods. The one method for handling one menu item. Why is it better? Because if you will use many many if-else you get a spaghetti code(unstructured). Java is very powerful language, just create other class for handling your menu items, use static methods and you will be happy! Good luck!

import java.util.Scanner;

class RestaurantMain {

    //created static variables for using arrays inside static methods of the class
    static String[] menuMainItems = {
            "Customer System",
            "Management System"
    };

    static String[] menuItems = {
            "Drinks Menu",
            "Starters Menu",
            "Mains menu",
            "Desserts Menu"
    };

    static String[] menuCustomer = {
            "Drinks",
            "Starters",
            "Mains",
            "Desserts"
    };


    public static void main(String[] args)
    {
        //Variables//
        int choice = 0;
        int customerChoice = 0;
        boolean trueFalse = false;
        int restart = 0;
        String choice2 = "";

        //EndVariables//
        Scanner in = new Scanner(System.in);

        System.out.println("Welcome to the Cooper's restaurant system!");
        System.out.println("How can I help?\n"); // \n - is just symbol of end line in ascii

        //print our list from array
        for(int i = 0; i < menuMainItems.length; i++)
            System.out.println( (i+1) + ". " + menuMainItems[i] );

        System.out.print("Which option do you choose: ");
        choice = in.nextInt();

        switch(choice)
        {
            case 1:
                    handleCustomerSystem(in); //it's better to use methods
                    break;  //don't forget this! It's important!

            case 2:
                    //and so on
                    break;
        }

    }

    //the handling of the first item
    private static void handleCustomerSystem(Scanner in)
    {
        int customerChoice = 0;
        System.out.println("Our menu's are as follows:\n"); // \n - is just symbol of end line in ascii

        //print our list from array
        for(int i = 0; i < menuItems.length; i++)
                System.out.println( (i+1) + ". " + menuItems[i] ); // (i+1) because we wanna to see the count from 1, not 0

        System.out.print("\nWhat menu would you like to follow? ");
        customerChoice = in.nextInt();

        //and again using switch
        switch(customerChoice)
        {
                case 1:
                        System.out.println(menuItems[customerChoice]); 
                        in.nextLine();
                        break;

                case 2:
                        //ans so on
                        break;
        }
    }
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM