-5

我需要一点帮助。我正在尝试缩短我的 main() 并将一些代码移动到方法中并将其放在我的另一个类 Contact.java 中。就像读取文件这样的某些事情,我想要一个方法,它只用于读取文件并将其放入 Contact.java 中。写入文件也是如此,我想要一个可以在 main() 中回调的单独方法。我了解如何制作该方法,但是如何将该方法从 Contact.java 调用回 ContactList.java?有什么建议么?

import java.io.*;
import java.util.*;

public class ContactList {

public static void main(String args[]) throws IOException {
    Contact contact;
    contact = new Contact();
    int action = 0;

    ArrayList<Contact> contacts = new ArrayList<Contact>();
    while (action != 6) {

        System.out.println("\nWelcome to Contact List DB. "
                + "What would you like to do? \n");

        System.out.println("1. Enter a new person" + "\n"
                + "2. Print the contact list" + "\n"
                + "3. Retrieve a person's information by last name" + "\n"
                + "4. Retrieve a person's information by email address" + "\n"
                + "5. Retrieve all people who live in a given zip code" + "\n" 
                + "6. Exit");
        Scanner reader = new Scanner(System.in);
        reader.useDelimiter("\n");
        action = reader.nextInt();

        if (action <= 0 || action > 6) {
            System.out.println("Invalid selection. ");

        }

        switch (action) {

        case 1: {

            System.out.println("\nEnter Contact Last Name:");
            String lastname = reader.next();
            if (lastname == null) {
                System.out.println("\nInvalid entry. ");
                break;
            } 

            else {
                contact.setLastName(lastname.toLowerCase());
            }
            System.out.println("Enter Contact First Name: ");
            String firstname = reader.next();
            contact.setFirstName(firstname.toLowerCase());
            System.out.println("Enter Contact Street Address: ");
            String address = reader.next();
            contact.setHouseAddress(address.toLowerCase());
            System.out.println("Enter Contact City: ");
            String city = reader.next();
            contact.setCity(city.toLowerCase());
            System.out.println("Enter Contact Zip Code: ");
            String zip = reader.next();
            contact.setZip(zip.toLowerCase());
            System.out.println("Enter Contact Email: ");
            String email = reader.next();
            contact.setEmail(email.toLowerCase());
            System.out.println("Enter Contact Phone Number: ");
            String phone = reader.next();
            contact.setPhone(phone.toLowerCase());
            System.out.println("Enter Contact Notes: ");
            String notes = reader.next();
            contact.setNotes(notes.toLowerCase());

            contacts.add(contact);

            try {

                Contact c = contact;

                File file = new File("contactlist.csv");

                // If file doesn't exists, then create it.
                if (!file.exists()) {
                    file.createNewFile();
                }

                try (PrintWriter output = new PrintWriter(new FileWriter(
                        "contactlist.csv", true))) {
                    output.printf("%s\r\n", c);
                } catch (Exception e) {
                }

                System.out.println("Your contact has been saved.");
            }

            catch (IOException e) {
                e.printStackTrace();        
    }
        }

        break;


        case 2: {

            int counter = 0;
            String line = null;

            // Location of file to read
            File file = new File("contactlist.csv");

            // Sort contacts and print to console
            try {

                Scanner scanner = new Scanner(file);

                // Before printing, add each line to a sorted set. by Seth
                // Copeland
                Set<String> lines = new TreeSet<>();
                while (scanner.hasNextLine()) {
                    line = scanner.nextLine();
                    lines.add(line);
                    counter++;

                }

                // Print sorted contacts to console.
                for (String fileLine : lines) {
                    String outlook = fileLine.substring(0, 1).toUpperCase()
                            + fileLine.substring(1);
                    System.out.println(outlook);

                }


                scanner.close();

            } catch (FileNotFoundException e) {

            }
            System.out.println("\n" + counter + " contacts in records.");

        }

        break;


        case 3:

            try {
                System.out.println("\nEnter the last"
                        + "name to search for: ");
                String searchterm = reader.next();

                // Open the file as a buffered reader
                BufferedReader bf = new BufferedReader(new FileReader(
                        "contactlist.csv"));

                // Start a line count and declare a string to hold our
                // current line.
                int linecount = 0;
                String line;

                // Let the user know what we are searching for
                System.out.println("Searching for " + searchterm
                        + " in file...");
                // Loop through each line, putting the line into our line
                // variable.
                boolean noMatches = true;
                while ((line = bf.readLine()) != null) {
                    // Increment the count and find the index of the word.
                    linecount++;
                    int indexfound = line.indexOf(searchterm.toLowerCase());

                    // If greater than -1, means we found a match.
                    if (indexfound > -1) {
                        System.out.println("\nContact was FOUND\n"
                                + "\nContact " + linecount + ": " + line);
                        noMatches = false;
                    }

                }

                // Close the file after done searching
                bf.close();
                if (noMatches) {
                    System.out.println("\nNO MATCH FOUND.\n");
                }
            }

            catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }

            break;


        case 4:

            try {
                System.out.println("\nEnter the email "
                        + "address to search for: ");
                String searchterm = reader.next();

                // Open the file as a buffered reader
                BufferedReader bf = new BufferedReader(new FileReader(
                        "contactlist.csv"));

                // Start a line count and declare a string to hold our
                // current line.
                int linecount = 0;
                String line;

                // Let the user know what we are searching for
                System.out.println("\nSearching for " + searchterm
                        + " in file...");

                // Loop through each line, put the line into our line
                // variable.
                boolean noMatches = true;
                while ((line = bf.readLine()) != null) {

                    // Increment the count and find the index of the word
                    linecount++;
                    int indexfound = line.indexOf(searchterm.toLowerCase());

                    // If greater than -1, means we found a match
                    if (indexfound > -1) {
                        System.out.println("\nContact was FOUND\n"
                                + "\nContact " + linecount + ": " + line);
                        noMatches = false;
                    }

                }
                // Close the file after done searching
                bf.close();
                if (noMatches) {
                    System.out.println("\nNO MATCH FOUND.\n");
                }

            }

            catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }

            break;


        case 5:

            try {
                System.out.println("\nEnter the Zipcode to search for: ");
                String searchterm = reader.next();

                // Open the file as a buffered reader
                BufferedReader bf = new BufferedReader(new FileReader(
                        "contactlist.csv"));

                // Start a line count and declare a string to hold our
                // current line.
                int linecount = 0;
                String line;

                // Let the user know what we are searching for
                System.out.println("\nSearching for " + searchterm
                        + " in file...");

                // Loop through each line, stashing the line into our line
                // variable.
                boolean noMatches = true;
                while ((line = bf.readLine()) != null) {

                    // Increment the count and find the index of the word.
                    linecount++;
                    int indexfound = line.indexOf(searchterm.toLowerCase());

                    // If greater than -1, means we found a match.
                    if (indexfound > -1) {
                        System.out.println("\nContact was FOUND\n"
                                + "\nContact " + linecount + ": " + line);
                        noMatches = false;
                    }
                }
                // Close the file after done searching
                bf.close();
                if (noMatches) {
                    System.out.println("\nNO MATCH FOUND.\n");
                }
            }

            catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }

            break;
        }
    }
}
}
4

3 回答 3

1

首先,尝试将代码拆分为方法:

    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.

要将这个东西分成类,首先要确定你拥有的对象。对象通常用名词表示。例如,您有一个联系人列表文件。您可以创建一个具有, ,等ContactListFile方法的类。AddContactSearchContactGetContacts

然后,您可以使用如下代码:

contactListFile = new ContactListFile("contactlist.csv");
contact = readContact();
contactListFile.AddContacts(contact);
于 2013-03-12T15:11:07.367 回答
0

这听起来像是代码审查 Stack Exchange 站点的候选者,但我的 $.02:将处理每个操作的代码分解为它们自己的方法,并调用这些方法。一旦你得到它,你可以将循环移动到一个名为 runActions() 的方法,然后只需从 main() 调用 runActions()。

于 2013-03-12T15:08:29.007 回答
0

在 App 类的这个示例中,我调用了 Test 类的 meth1 方法:

class Test{
 public void meth1(){
  System.out.println("meth1 from Test Class");
 }//end of meth1
}//end of class Test
public class App{
 public static void main(String arg[]){
  Test t = new Test();
  t.meth1();
 }//end of main
}//end of class App
于 2013-03-12T15:15:00.483 回答