Java中的地址簿程序

时间:2011-12-03 18:32:30

标签: java

我只是在做一个小程序。这是一本地址簿,有四个选项:

  1. 插入新联系人
  2. 按姓氏搜索联系人
  3. 按名称删除联系人
  4. 显示所有联系人
  5. 退出计划
  6. 只是想知道如何获得插入接触部件以及如何存储它。我已经硬编码了一个联系人来测试它。

    这是我开始的代码

    package addressbook;
    import java.util.Scanner;
    
    public class addressbooks
    {
    
    
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
    
            //create a table to hold information
    
            String[][] addressbooks = new String[100][8];
    
            addressbooks[0][0]="Mobile Number";
            addressbooks[0][1]="First Name";
            addressbooks[0][2]="Last Name";
            addressbooks[0][3]="Address";
            addressbooks[0][4]="City";
            addressbooks[0][5]="County";
            addressbooks[0][7]="Telephone Number";
    
            //pre-populate address book for testing purposes and records
    
            addressbooks[1][0]="1";
            addressbooks[1][1]="David";
            addressbooks[1][2]="Needham";
            addressbooks[1][3]="Sraheens, Achill";
            addressbooks[1][4]="Galway";
            addressbooks[1][5]="Mayo";
            addressbooks[1][6]="086-1581077";
            addressbooks[1][7]="098-45368";
    
            addressbooks[2][0]="2";
            addressbooks[2][1]="Mc";
            addressbooks[2][2]="lovin";
            addressbooks[2][3]="Hawaii";
            addressbooks[2][4]="Hawaii";
            addressbooks[2][5]="Hawaii";
            addressbooks[2][6]="12345";
            addressbooks[2][7]="412-555-1234";
    
            //menu options
            System.out.print("Welcome to my Address book!");
            System.out.print("\n");
            System.out.print("\n1 - Insert a New Contact \n2 - Search Contact by Last Name \n3 - Delete Contact \n4 - Show All Contacts \n5 - Exit " );
            System.out.print("\n");
            System.out.print("\nChoose your option: ");
    
            int option = input.nextInt();
    
            if (option ==1)
            {
                System.out.print("\nPlease enter your First Name : ");
            }
            if (option ==2)
            {
            }
    
            if (option ==3)
            {
            }
    
            if (option ==4)
            {
            System.out.println(addressbooks[1][0]+
            "\t"+addressbooks[1][2]+ ", "+addressbooks[1][1]+
            "\n\t"+addressbooks[1][3]+
            "\n\t"+addressbooks[1][4]+ ", "+addressbooks[1][5]+ " "+addressbooks[1][6]+
            "\n\t"+addressbooks[1][7]);
            }
    
            if (option ==5)
            {
            }
    
        }
    }
    

3 个答案:

答案 0 :(得分:3)

我是这样开始的:

package model;

public class Person {
    private String firstName;
    private String lastName;
    // What else means something to your problem?  Birthday?
    // Constructors, getters (make them immutable), equals/hashCode
}

public class Address {
    private String street;
    private String city;
    private String county;
    private String postalCode;
    // Constructors, getters (make them immutable), equals/hashCode
}

public class AddressBook {
    private Map<Person, Address> contacts = new ConcurrentHashMap<Person, Address>();

    public void addContact(Person p, Address a) {
        this.contacts.put(p, a);
    }

    public void removeContact(Person p) {
        this.contacts.remove(p);
    }

    public Collection<Person> findAllContacts() {
        return new Collections.unmodifiableCollection(this.contacts.keySet());
    }

    public boolean hasContact(Person p) {
        return this.contacts.contains(p);
    }
    // etc.
}

我建议将基于文本的IO的所有内容与问题的基础分开。如果你做对了,下一步就是编写一个Web UI。如果你正确地执行它,大多数代码都是可重用的。

考虑分层应用:

view->services->persistence

模型类可以在所有三个层中使用。

推荐JDBC的答案并不错。如果将服务和持久性类编写为接口,则可以轻松地为使用JDBC的数据库版本替换内存中的联系人映射:

package persistence;

public interface ContactDao {
    Collection<Contact> find();
    Contact find(Long id);
    Collection<Contact> find(String lastName);
    Collection<Contact> find(Address address);
    Long save(Contact c);
    void update(Contact c);
    void delete(Contact c);
}

有很多方法可以做。我已经改变了主意:我已经介绍了一个Contact类:

package model; 

public class Contact {
    private Person;
    private Address;
}

答案 1 :(得分:1)

对象的使用将极大地帮助您:

class ADdressBook  
{  
   List<Contact> contacts;  

   function addContact(Contact contact)  
   {
         contacts.add(contact);  
   }    
}

您将需要使用作为List / Collection API的一部分公开的方法来使您更容易。实施Contact课程是一项练习。

答案 2 :(得分:0)

我实际创建了一个类似的程序,我建议简单地构建一个数据库,这也允许你在程序的使用之间保存数据。使用JBDC非常简单,这是我使用的网站:http://www.zentus.com/sqlitejdbc/