通信录系统

package com.softeem.phone;

public class People {

	private String pno;
	private String pname;
	private String tel;
	private String QQ;
	
	public People() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((QQ == null) ? 0 : QQ.hashCode());
		result = prime * result + ((pname == null) ? 0 : pname.hashCode());
		result = prime * result + ((pno == null) ? 0 : pno.hashCode());
		result = prime * result + ((tel == null) ? 0 : tel.hashCode());
		return result;
	}
	@Override
	public String toString() {
		return "People [pno=" + pno + ", pname=" + pname + ", tel=" + tel + ", QQ=" + QQ + "]";
	}
	public People(String pno, String pname, String tel, String qQ) {
		super();
		this.pno = pno;
		this.pname = pname;
		this.tel = tel;
		QQ = qQ;
	}
	public String getPno() {
		return pno;
	}
	public void setPno(String pno) {
		this.pno = pno;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getQQ() {
		return QQ;
	}
	public void setQQ(String qQ) {
		QQ = qQ;
	}
   @Override
    public boolean equals(Object obj) {
	   if (this == obj)
		return true;
	   if (obj == null)
		return false;
	   if (getClass() != obj.getClass())
		return false;
	   People other = (People) obj;
	   if (QQ == null) {
		if (other.QQ != null)
			return false;
	     } else if (!QQ.equals(other.QQ))
		return false;
	     if (pname == null) {
		if (other.pname != null)
			return false;
	    } else if (!pname.equals(other.pname))
	   	return false;
	    if (pno == null) {
		if (other.pno != null)
			return false;
	     } else if (!pno.equals(other.pno))
		    return false;
	    if (tel == null) {
		if (other.tel != null)
			return false;
	     } else if (!tel.equals(other.tel))
		     return false;
	     return true;
}
}

package com.softeem.phone;

import java.util.ArrayList;

public interface BasePeople<T> {

	public boolean add(T obj);
	public Object findOne(T obj);
	public ArrayList findAll(T obj);
	public boolean del(T obj);
	
}

package com.softeem.phone;

import java.util.ArrayList;
import java.util.Objects;

public class PeopleManager implements BasePeople<People>{

	static ArrayList<People> p = new ArrayList<>();
	//添加联系人
	@Override
	public boolean add(People obj) {
		People pp = obj;
		return p.add(pp);
	}
	
    //查询根据姓名
	@Override
	public Object findOne(People obj) {
		People pp = obj;
		for (int i = 0; i < p.size(); i++) {
			if (p.get(i).getPname().equals(pp.getPname())) {
				System.out.println(p.get(i).toString());
			}
		}
		return false;
	}
    //显示联系人列表
	@Override
	public ArrayList findAll(People obj) {
		ArrayList list = new ArrayList();
		for (People pp : p) {
				list.add(pp);
		}
		return list;		
	}
    //根据编号删除联系人
	@Override
	public boolean del(People obj) {
		People pp = obj;
		int index = -1;
	    for (int i = 0; i < p.size(); i++) {
			if(p.get(i).getPno().equals(pp.getPno())){
				index = i;
			}
		}
	    p.remove(index);
		return false ;
	}



}

package com.softeem.phone;

import java.util.Scanner;

public class Main {
	
	static BasePeople bp = new PeopleManager();
	public void enter(){
		menu1();
		}
		private void menu1() {
			p("**************通讯录系统************");
			p("*************【1】联系人添加***************");
			p("*************【2】联系人查询***************");
			p("*************【3】显示联系人列表***************");
			p("*************【4】删除联系人***************");
			p("*************【0】退出系统***************");
			p("*************************************");
			Scanner sc = new Scanner(System.in);
			int i = sc.nextInt();
			switch(i){
			case 1:
				Add();
				break;
			case 2:
				find();
				break;
			case 3:
				show();
				break;
			case 4:
				delet();
				break;
			case 0:
				p("谢谢使用,再见!!!");
				System.exit(0);
				default:
					System.err.println("指令错误,请重新输入");
					menu1();
					break;
			}
		}

		private void show() {
			System.out.println(bp.findAll(new People()));
			menu1();
		}
		private void delet() {
			p("请输入要删除联系人的编号");
			Scanner sc = new Scanner(System.in);
			String pno = sc.next();
			People p = new People(pno, pno, pno, pno);
			bp.del(p);
			p("删除成功");
			menu1();
		}
		private void find() {
			p("请输入要查询的姓名");
			Scanner sc = new Scanner(System.in);
			String pname = sc.next();
			People p = new People(pname, pname, pname, pname);
			bp.findOne(p);
			menu1();
		}
		
		private void Add() {
			p("请输入联系人信息(例如:编号/姓名/电话/QQ)");
			Scanner sc = new Scanner(System.in);
			String[] info = sc.nextLine().split("/");
			People p = new People(info[0],info[1],info[2],info[3]);
			if(bp.add(p)){
				p("添加成功");
			}else{
				p("添加失败");
			}
			menu1();
		}
		public void p(Object msg){
			System.out.println(msg);
		}
		
		public static void main(String[] args) {
			new Main().enter();
			}
	

}

猜你喜欢

转载自blog.csdn.net/qq_42696837/article/details/81271198