[Solved] JAVA: Split main into methods and put in another class? [closed]


First, try to split your code in methods:

    while (action != 6) {

        printIntro()
        action = getAction();

        switch (action) {

        case 1: {
            contact = readContact()
            contacts.add(contact);

            try {
                writeContact(contact);
                System.out.println("Your contact has been saved.");
            }
            catch (IOException e) {
                e.printStackTrace();        
            }
        }

        break;
        etc.

For splitting this thing into classes, first determine the objects you have. Objects are often indicated by nouns. For example, you have a contact list file. You can create a class ContactListFile that has methods like AddContact, SearchContact, GetContacts, etc.

You can then use some code like this:

contactListFile = new ContactListFile("contactlist.csv");
contact = readContact();
contactListFile.AddContacts(contact);

1

solved JAVA: Split main into methods and put in another class? [closed]