Java简单通讯录

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_24023751/article/details/81477649
package com.csdn.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AddressList {
    static Scanner scanner = new Scanner(System.in);
    static List<Entity> enlist = new ArrayList<Entity>();
    public static void main(String[] args) {
        int input;
        AddressList addre = new AddressList();
        System.out.println("===============欢迎使用**通讯录===============");
        do{     
            System.out.println("1、插入联系人");
            System.out.println("2、删除练习人");
            System.out.println("3、修改联系人");
            System.out.println("4、查询练习人");
            System.out.println("5、查询全部联系人");
            System.out.println("0、退出系统");
            System.out.println("请选择:");
            input = scanner.nextInt();
            switch(input){
                case 1:
                    Entity e = new Entity();
                    e.id = enlist.size();
                    System.out.println("请输入联系人姓名:");
                    e.name = scanner.next();
                    System.out.println("请输入联系方式:");
                    e.number = scanner.nextInt();
                    addre.add(e);
                    System.out.println("插入成功!");
                    break;
                case 2:
                    System.out.println("请输入要删除联系人编号:");
                    int num = scanner.nextInt();
                    if(addre.delete(num))
                        System.out.println("删除成功!");
                    else
                        System.out.println("删除失败,请确认信息是否正确!");
                    break;
                case 3:
                    System.out.println("请输入要修改人编号:");
                    int unum = scanner.nextInt();
                    addre.update(unum);
                    System.out.println("修改完成!");
                    break;
                case 4:
                    System.out.println("请输入要查询人姓名:");
                    String name = scanner.next();
                    Entity ent = addre.select(name);
                    if(ent!=null){
                        System.out.println(name+"的联系方式为:"+ent.number);
                    }else{
                        System.out.println("查无此人!");
                    }
                    break;
                case 5:
                    for(Entity entit:enlist){
                        System.out.println(entit.name+"的联系方式为:"+entit.number);
                    }
                    break;
            }
        }while(input!=0);
        System.out.println("谢谢使用!");
    }
    
    /*
     * 添加联系人
     */
    public boolean add(Entity e){
        enlist.add(e);
        return true;
    }
    /*
     * 删除联系人
     */
    public boolean delete(int num){
        if(num>enlist.size())
            return false;
        else
            enlist.remove(num);
        return true;
    }
    /*
     * 修改联系人
     */
    public void update(int num){
        if(num>enlist.size())
            System.out.println("查无此人!");
        else{
            Entity e = new Entity();
            e.id = num;
            System.out.println("请输入联系人姓名:");
            e.name = scanner.next();
            System.out.println("请输入联系方式:");
            e.number = scanner.nextInt();
            enlist.set(num, e);
        }
    }
    /*
     * 查询指定联系人电话
     */
    public Entity select(String name){
        Entity en = null;
        for(Entity e : enlist){
            if(e.name.equals(name))
                en = e;
        }
        return en;
    }
}

类:

package com.csdn.demo;

public class Entity{
    public int id;//编号
    public String name;//姓名
    public int number;//联系电话
	public Entity() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Entity(int id, String name, int number) {
		super();
		this.id = id;
		this.name = name;
		this.number = number;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	@Override
	public String toString() {
		return "Entity [id=" + id + ", name=" + name + ", number=" + number + "]";
	}
    
    
}

 

展开阅读全文

没有更多推荐了,返回首页