2016-03-06 27 views
1

我必须在java中创建一个地址簿。我陷入了一个区域。当我添加第二个地址时,它使第一个地址为空。我有些地区评论说我还没有这么忽视这些领域。我不知道为什么第一个地址变为空。AddressBook in Java

import java.util.Scanner; 
import java.io.IOException; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.FileNotFoundException; 

class Program2 { 

    static Scanner s = new Scanner(System.in); 

    public static void main(String[] args) { 

     System.out.println("\nWelcome. Address book is loaded."); 
     loop: while(true) { 
      displayOptions(); 
      int choice = s.nextInt(); 
      switch(choice) { 
       case 1: 
        System.out.println("**Choice 1**"); 
        AddressBook.display(); 
        break; 

       case 2: 
        System.out.println("**Choice 2**"); 
        System.out.print("First Name: "); 
        String firstName = s.next(); 
        System.out.print("Last Name: "); 
        String lastName = s.next(); 
        System.out.print("Phone Number: "); 
        String phone = s.next(); 
        Contact test = new Contact(firstName, lastName, phone); 
        AddressBook.add(test); 
        break; 

       case 3: 
        System.out.println("**Choice 3**"); 
        break;   

       case 4: 
        System.out.println("**Choice 4**"); 
        break; 

       case 5: 
        break loop; 
      } 
     } 
     System.out.println("\nAddress book is saved to file.\n"); 
     System.out.println("Good bye.\n"); 
     System.exit(0); 

    } 

    private static void displayOptions() { 
     System.out.println("\nWhat would you like to do?"); 
     System.out.println(" 1) Display all contacts\n" + 
        " 2) Add a contact\n" + 
        " 3) Remove a contact\n" + 
        " 4) Search a contact\n" + 
        " 5) Exit"); 
     System.out.print("Your choice: "); 
    } 

} 

class Contact { 

    private String firstName; 
    private String lastName; 
    private String phone; 

    public Contact(String firstName, String lastName, String phone) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.phone = phone; 
    } 

    public String getFirstName() {return firstName;} 
    public String getLastName() {return lastName;} 
    public String getPhone() {return phone;} 

    public void setFirstName(String firstName) {this.firstName = firstName;} 
    public void setLastName(String lastName) {this.lastName = lastName;} 
    public void setPhone(String phone) {this.phone = phone;} 

    /*public boolean equals(Object o) { 
     if (o instanceof Contact) { 
      Contact contacts = (Contact) o; 
      return (firstName.equals(contacts.getFirstName()) && 
       lastName.equals(contacts.getLastName())); 
     } 
     return false; 
    }*/ 

    public String toString() { 
     return firstName + " " + lastName + "\t\t" + phone; 
    } 

} 

class AddressBook { 

    public final static int CAPACITY = 100; 
    static private Contact[] contacts; 
    static private int count = 0; 
    static private String addressFile = "address.txt"; 
    static File file = new File(addressFile); 

    public AddressBook(String addressFile) { 
     this.addressFile = addressFile; 
    } 

    public static boolean add(Contact c) { 
     contacts = new Contact[CAPACITY]; 
     if (count < CAPACITY) { 
      contacts[count++] = c; 
     } 
     return false; 
    } 

    //public boolean remove(fullname) { } 

    //public Contact search(fullname) { } 

    public static void display() { 
     System.out.println("Name\t\t\tPhone Number"); 
     System.out.println("-------------------------------------"); 
     for (int i=0; i<count; i++) { 
      System.out.println(contacts[i]); 
     } 
     System.out.println("-------------------------------------"); 
    } 

    /*public boolean load() { 

     try { 
     Scanner sF = new Scanner(file); 
     while (s.hasNext()) { 
      String line = s.nextLine(); 
      System.out.println(line); 
     } 
     s.close(); 

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

    /*public boolean save() { 
     try { 
      FileWriter writer = new FileWriter(addressFile); 
      writer.write(); 
      writer.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    }*/ 

    //public boolean contains(String firstName, String lastName) { 
    // return this.contacts.contains(firstName, lastName); 
    //} 

} 

回答

0

add方法覆盖contacts阵列,所以每次调用时间,以前Contact S中的引用都将丢失:

public static boolean add(Contact c) { 
    contacts = new Contact[CAPACITY]; // remove this line 
    if (count < CAPACITY) { 
     contacts[count++] = c; 
    } 
    return false; 
} 

取而代之的是去除线,只有初始化contacts阵列曾经:

static private Contact[] contacts = new Contact[CAPACITY]; 
+0

哇这么简单,我错过了。谢谢! – DarkX9109