2

我正在设计一个地址簿,为了使我的 AddressBookApp 类工作(包括我的主要方法),我必须创建实例变量并将它们设为静态,以便我的类中的每个方法都能够访问我的姓名、电子邮件, 和 Phone 对象。我认为有更好的方法,但我很难知道那是什么。我应该在 main 方法中创建对象吗?实例变量是正确的方法吗?你们对我如何改进我的设计有什么想法吗?(如果您有任何其他与我的问题无关的设计建议,请告诉我)

这是我的 AddressBookApp 类的代码:

import java.util.Scanner;

public class AddressBookApp {

//Instance Variables
private static Name name;
private static Email email;
private static Phone phone;

//Constructor
public AddressBookApp() {
    name = new Name();
    email = new Email();
    phone = new Phone();
}

//Main method
public static void main(String[] args) {
    new AddressBookApp();

    System.out.println("Welcome to the Address Book Application\n");
    Scanner sc = new Scanner(System.in);

    int menuNumber;
    do {
        menu();

        menuNumber = sc.nextInt();
        System.out.println();

        if (menuNumber < 1 || menuNumber > 4){
            System.out.println("Please enter a valid menu number\n");
        } else if (menuNumber == 1) {
            printEntries();
        } else if (menuNumber == 2) {
            addEntry();
        } else if (menuNumber == 3) {
            removeEntry();
        } else {
            System.out.println("Thanks!  Goodbye.");
            sc.close();
            return;
        }

        continue;

    } while (menuNumber != 4);
    sc.close();
} 

/**
 * Prints out Main Menu
 */
public static void menu() {
    System.out.println("1 - List entries\n" + 
                       "2 - Add entry\n" +
                       "3 - Remove entry\n" +
                       "4 - Exit\n");

    System.out.print("Enter menu Number: ");
}

/**
 * Prints all entries in the Address Book
 */
public static void printEntries() {
    name.printNames();
    System.out.println();

    email.printEmails();
    System.out.println();

    phone.printPhoneNumbers();
    System.out.println();
}


/**
 * Adds an entry to the Address Book
 */
public static void addEntry() {
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter Name: ");
    name.addName(sc.nextLine());

    System.out.print("Enter Email Address: ");
    email.addEmail(sc.nextLine());

    System.out.print("Enter Phone Number: ");
    phone.addPhone(sc.nextLine());

    System.out.println("\nRecord Saved.\n");
}

/**
 * Removes and entry from the Address Book
 */
public static void removeEntry() {
    Scanner sc = new Scanner(System.in);

    System.out.print("Please Enter the record number that you would like to remove: ");

    int records = sc.nextInt();
    name.removeNames(records - 1);
    email.removeEmail(records - 1);
    phone.removePhone(records - 1);
}
}
4

2 回答 2

4

AddressBook并且AddressBookApp应该是两个不同的类。AddressBook应该是这样的:

public class AddressBook {

//Instance Variables
private Name name;
private Email email;
private Phone phone;

//Constructor
public AddressBook() {
    name = new Name();
    email = new Email();
    phone = new Phone();
}

// more Constructors

public void setName(Name name) {
    this.name = name
}

public Name getName() {
    return name;
}
// more getters and setters

main()然后,您的应用程序可以在您的方法中创建 this 的实例:

AddressBook book = new AddressBook();
book.setName(new Name("Jeff"));
//more operations on book

您可以将对象传递book给您需要它的任何方法或继续创建新实例。您还可以将其作为应用程序类中的静态引用:

private static AddressBook book = new AddressBook();

// in your app class methods
book.burnBeforeReading();
于 2013-01-04T21:19:56.907 回答
0

很简单,创建两个类:MainAddressBook.

主要只有

public static void main(String[] args) {

new AddressBook().execute();
...

AddressBook只有实例方法。

于 2013-01-04T21:23:20.897 回答