samedi 27 avril 2019

Most optimal way to incorporate menu system into program?

I'm creating a small program that will be used to manipulate a phone directory. I obviously will need a menu system of some sort and am wondering what is the most optimal way to incorporate one in my program.

I've seen several different ways however haven't enough experience to judge which is better in the long run.

  • Should I create a Menu class and have its methods in there?

  • Should I have a constructor in my ArrayDirectory class (contains all methods for manipulating directory) that outputs the menu options and asks for input every time a Directory object is made? My current constructor for the class just loads phonebook records from a text file so they can be used.

  • Should I just add the menu methods in the ArrayDirectory class and call them in main method when needed?

Right now I have it set up like so:

public class ArrayDirectory implements Directory {

    // Initialise variables for program
    private final static int MAX_ENTRIES = 20;
    private static Scanner input = new Scanner(System.in);
    DirectoryFile file = new DirectoryFile("C:\\Users\\John\\Documents\\Phonebook.txt");
    static ArrayList<Entry> phonebook = new ArrayList<>(MAX_ENTRIES);

    public ArrayDirectory() throws IOException {
        loadEntries(file.getFile());
        System.out.println("Staff Phonebook Directory Program");
        System.out.println("1) Add Entry");
        System.out.println("2) Delete Entry");
        System.out.println("3) Search Entry");
        System.out.println("4) Edit Entry");
        System.out.println("5) Display All Entries");
        System.out.println("6) Exit Program");

        int choice = input.nextInt();
        switch (choice){
        case 1:

            break;
        case 2:

            break;
        case 3:

            break;
        case 4:

            break;
        case 5:

            break;          
        case 6:

            break;
        default:

            break;
        }
    }

...(Other operation methods)...

So that when I create an ArrayDirectory class in my eventual main class, everything will be loaded straight away. Or is this not as efficient as I am thinking?

Also, is it more efficient to use if-else statements here instead of switch/case statements?

Aucun commentaire:

Enregistrer un commentaire