2013-01-04 2 views
2

주소록을 디자인하고 있는데 내 AddressBookApp 클래스가 작동하도록하기 위해 (내 주 방법 포함) 인스턴스 변수를 만들어 각각에 대해 정적으로 만들어야했습니다. 메서드를 호출하여 내 이름, 전자 메일 및 전화 개체에 액세스 할 수 있습니다. 나는 더 나은 방법이 있다고 가정하지만 그것이 무엇인지 알기 위해 고심하고있다. 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); 
} 
} 
+8

디자인에 대한 일반적인 의견을 찾고 계시다면 http://codereview.stackexchange.com에 문의하십시오. –

+3

"인스턴스 변수를 만들고 정적으로 만들어야했습니다."- 음, 뭐라구? 인스턴스 변수는 정의에 따라 정적이 아닙니다. 특정 인스턴스와 연결되어 있습니다. –

+3

또한, 용어의 문제로, '정적'은 '인스턴스'변수의 반대입니다. – SJuan76

답변

4

AddressBookAddressBookApp 두 개의 다른 클래스해야합니다. 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() 방법이의 인스턴스를 만들 수 있습니다 앱 :

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

당신은 당신이 그것을 필요로하는 어떤 방법으로 개체 book 주위에 전달할 수 있습니다 또는 새 인스턴스를 계속 작성하십시오. 또한 앱 클래스의 정적 참조로 할 수 있습니다 : MainAddressBook :

private static AddressBook book = new AddressBook(); 

// in your app class methods 
book.burnBeforeReading(); 
+0

고마워요. 그게 내가 알아야 할 전부 야. – Keven

0

간단한 두 개의 클래스를 만들 수 있습니다.

홈페이지는

public static void main(String[] args) { 

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

AddressBook는 인스턴스 메소드를 가지고있다.

+0

감사! 너도 옳았지 만 Sotirios가 너를 때렸으니 수표를받을 까봐 걱정된다. – Keven