首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java中的AddressBook

Java中的AddressBook
EN

Stack Overflow用户
提问于 2016-03-06 08:28:02
回答 1查看 1K关注 0票数 1

我必须用java创建一个地址簿。我被困在一个地方。当我添加第二个地址时,它使第一个地址为空。我有评论说,我还没有做到,所以忽略那些领域。我不明白为什么第一个地址变成零。

代码语言:javascript
复制
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);
    //}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-06 08:30:08

add方法覆盖contacts数组,因此每次调用它时,都会丢失对前一个Contact的引用:

代码语言:javascript
复制
public static boolean add(Contact c) {
    contacts = new Contact[CAPACITY]; // remove this line
    if (count < CAPACITY) {
        contacts[count++] = c;
    }
    return false;
}

只初始化一次contacts数组,而不是删除的行:

代码语言:javascript
复制
static private Contact[] contacts = new Contact[CAPACITY];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35824697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档