分享好友 资讯首页 资讯分类 切换频道

电话薄程序java

2025-11-23 22:0000

电话薄程序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
打赏 0
评论 0
手机卡实名认证短信怎么查询余额
手机卡实名认证后,可通过短信查询余额。发送短信“余额”或“CXYE”至运营商服务号码,如移动发送到10086,联通发送到10010,电信发送到10000。稍后,系统会回复短信,展示余额信息。查询简洁明了,方便快捷。

0评论2026-04-070

java随机验证码验证方法有几种类型
Java随机验证码验证方法主要有以下几种类型:基于数字的验证码、基于字母和数字的验证码、基于图片的验证码(含干扰线条、噪点等)以及基于音频的验证码。这些方法通过生成随机验证码,结合用户输入进行验证,以提高系统安全性。

0评论2026-04-062

 
友情链接