电话薄程序java

   2025-11-23 00
核心提示:电话薄程序是一个基于Java的通讯录管理程序,可实现联系人信息的添加、查询、修改和删除等功能。该程序采用面向对象的设计思想,具有界面友好、操作简便、安全可靠等特点。

电话薄程序java

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

电话薄程序java

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
 
举报评论 0
 
更多>同类资讯
推荐图文
推荐资讯
点击排行
友情链接
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  版权声明  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报