用双向链表作数据结构,用java语言编写一个通讯录管理系统,本系统应完成一下几方面的功能: 1)输入信息——enter(); 2)显示信息———display( ); 3)查找以姓名作为关键字 ———search( ); 4)删除信息———delete( ); 5)存盘———save ( ); 6)装入———load( ) ; 设计要求: 1)每条信息至包含 :姓名(NAME )街道(STREET)城市(CITY)邮编(EIP)国家(STATE)几项 2)作为一个完整的系统,应具有友好的界面和较强的容错能力,上机能正常运行 3)具有可视化界面
时间: 2023-10-24 20:07:54 浏览: 71
好的,我已经记录下来了你的问题。对于你的问题,我可以给出以下回答:
实现双向链表可以使用Java的LinkedList类,该类实现了List接口,同时也是一个双向链表。通讯录管理系统的实现可以借助该类来完成。
下面是一个简单的通讯录管理系统的代码示例:
```
import java.io.*;
import java.util.*;
class Contact {
String name;
String street;
String city;
int zip;
String state;
}
class ContactList {
LinkedList<Contact> contacts = new LinkedList<Contact>();
public void enter() {
Contact contact = new Contact();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
contact.name = scanner.nextLine();
System.out.print("Enter street: ");
contact.street = scanner.nextLine();
System.out.print("Enter city: ");
contact.city = scanner.nextLine();
System.out.print("Enter zip: ");
contact.zip = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter state: ");
contact.state = scanner.nextLine();
contacts.add(contact);
}
public void display() {
for (Contact contact : contacts) {
System.out.println("Name: " + contact.name);
System.out.println("Street: " + contact.street);
System.out.println("City: " + contact.city);
System.out.println("Zip: " + contact.zip);
System.out.println("State: " + contact.state);
}
}
public void search(String name) {
boolean found = false;
for (Contact contact : contacts) {
if (contact.name.equals(name)) {
System.out.println("Name: " + contact.name);
System.out.println("Street: " + contact.street);
System.out.println("City: " + contact.city);
System.out.println("Zip: " + contact.zip);
System.out.println("State: " + contact.state);
found = true;
break;
}
}
if (!found) {
System.out.println("Contact not found.");
}
}
public void delete(String name) {
Iterator<Contact> iterator = contacts.iterator();
while (iterator.hasNext()) {
Contact contact = iterator.next();
if (contact.name.equals(name)) {
iterator.remove();
System.out.println("Contact deleted.");
return;
}
}
System.out.println("Contact not found.");
}
public void save() {
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("contacts.dat"));
outputStream.writeObject(contacts);
outputStream.close();
System.out.println("Contacts saved.");
} catch (IOException e) {
System.out.println("Error saving contacts.");
}
}
public void load() {
try {
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("contacts.dat"));
contacts = (LinkedList<Contact>)inputStream.readObject();
inputStream.close();
System.out.println("Contacts loaded.");
} catch (IOException e) {
System.out.println("Error loading contacts.");
} catch (ClassNotFoundException e) {
System.out.println("Error loading contacts.");
}
}
}
public class ContactManager {
public static void main(String[] args) {
ContactList contactList = new ContactList();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Enter contact");
System.out.println("2. Display contacts");
System.out.println("3. Search contacts");
System.out.println("4. Delete contact");
System.out.println("5. Save contacts");
System.out.println("6. Load contacts");
System.out.println("7. Exit");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
contactList.enter();
break;
case 2:
contactList.display();
break;
case 3:
System.out.print("Enter name to search: ");
String name = scanner.nextLine();
contactList.search(name);
break;
case 4:
System.out.print("Enter name to delete: ");
name = scanner.nextLine();
contactList.delete(name);
break;
case 5:
contactList.save();
break;
case 6:
contactList.load();
break;
case 7:
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
```
该代码示例使用了一个Contact类来表示每个联系人,该类中包含了姓名、街道、城市、邮编和国家等信息。ContactList类则是一个管理联系人的类,它使用LinkedList来存储联系人列表,并提供了enter()、display()、search()、delete()、save()和load()等方法来进行联系人的添加、显示、查找、删除、存储和加载等操作。最后,ContactManager类是一个简单的控制台程序,它通过一个循环来接受用户的输入,并调用ContactList类的相应方法来完成操作。
阅读全文