
这是一个简单的电话薄程序的Java实现。这个程序使用HashMap来存储联系人信息,包括姓名和电话号码。用户可以添加、查找和删除联系人。请注意,这是一个基本的实现,你可以根据需要添加更多的功能和优化。

import java.util.*;
class Contact {
String name;
String phoneNumber;
Contact(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
}
public class PhoneBook {
private Map<String, Contact> phoneBookMap = new HashMap<>();
public void addContact(String name, String phoneNumber) {
Contact contact = new Contact(name, phoneNumber);
phoneBookMap.put(name, contact);
System.out.println("Contact added successfully!");
}
public void deleteContact(String name) {
if (phoneBookMap.containsKey(name)) {
phoneBookMap.remove(name);
System.out.println("Contact deleted successfully!");
} else {
System.out.println("Contact not found!");
}
}
public void findContact(String name) {
if (phoneBookMap.containsKey(name)) {
Contact contact = phoneBookMap.get(name);
System.out.println("Name: " + contact.name);
System.out.println("Phone Number: " + contact.phoneNumber);
} else {
System.out.println("Contact not found!");
}
}
public void displayContacts() {
for (Map.Entry<String, Contact> entry : phoneBookMap.entrySet()) {
System.out.println("Name: " + entry.getValue().name);
System.out.println("Phone Number: " + entry.getValue().phoneNumber);
}
}
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1: Add Contact");
System.out.println("2: Delete Contact");
System.out.println("3: Find Contact");
System.out.println("4: Display Contacts");
System.out.println("5: Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter contact name: ");
String name = scanner.next();
System.out.print("Enter phone number: ");
String phoneNumber = scanner.next();
phoneBook.addContact(name, phoneNumber);
break;
case 2:
System.out.print("Enter contact name to delete: ");
String deleteName = scanner.next();
phoneBook.deleteContact(deleteName);
break;
case 3:
System.out.print("Enter contact name to find: ");
String findName = scanner.next();
phoneBook.findContact(findName);
break;
case 4:
phoneBook.displayContacts();
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice!");
}
}
}
} // end of class PhoneBook and main method here to start the program when you run it as a Java application with java PhoneBookProgramName in the command line interface (CLI). The program will prompt you to make choices about what you want to do with contacts in the phone book until you choose to exit the program by selecting option 5 from the menu of options displayed at each prompt in the command line interface (CLI). The program will handle adding contacts by prompting you for the contact’s name and phone number and then adding them to the phone book as a new contact entry in the HashMap data structure used to store the contacts in the phone book program as instances of the Contact class defined in this program code snippet above in this response message box here on this webpage where you can copy and paste this code into a Java development environment like Eclipse or IntelliJ IDEA or any other Java IDE that you may be using for your software development work on your computer system running on any operating system like Windows or macOS or Linux operating system on your computer device that you are using to access this webpage where




