【实战】java之电话本管理系统HashMap实现

Map 提供了一个更通用的元素存储方法。
Map 集合类用于存储元素对(称作“键”和“值”),其中每个键映射到一个值。
Map map = new HashMap();

本项目使用Persons类作为value,Persons类中name为key。

实体类Persons

import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Persons {
	private String name;
	private String sex;
	private String phonenum;
	private String age;
	private String qq;
	private String address;
	
	Scanner sc = new Scanner(System.in);
	
	public Persons addPerson() {
		Persons person = new Persons();
		System.out.println("输入姓名:");
		String name = sc.next();
		System.out.println("输入性别:");
		String sex = sc.next();
		System.out.println("输入电话号码:");
		String phonenum = sc.next();
		System.out.println("输入年龄:");
		String age = sc.next();
		System.out.println("输入qq:");
		String qq = sc.next();
		System.out.println("输入地址:");
		String address = sc.next();

		person.setName(name);
		person.setAge(age);
		person.setPhonenum(phonenum);
		person.setQq(qq);
		person.setSex(sex);
		person.setAddress(address);
		System.out.println(person);
		System.out.println("添加成功");
		return person;
	}
	
	public String selectByName(Map<String, String> map){
		Persons person = new Persons();
		System.out.println("请输入姓名:");
		String name = sc.next();
		for(int j = 0;j<map.size();j++){
			if(map.containsKey(name)){
				System.out.println(map.get(name).toString());
				return name;
			}else {
				person = null;
			}
		
		}
		return null;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getPhonenum() {
		return phonenum;
	}

	public void setPhonenum(String phonenum) {
		this.phonenum = phonenum;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "姓名=" + name + ", 性别=" + sex + ", 电话号码=" + phonenum + ", 年龄=" + age + ", qq=" + qq
				+ ", 地址=" + address ;
	}
}

实现类PhoneBooks

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import MapPhoneBooks.Persons;

public class PhoneBooks {
	/*
	 * map.remove
	 * map.containsKey
	 * map.get(key);/map.put(key,values);/
	 */
	
	public static void main(String[] args) {
		Map<String,String> map = new HashMap<String,String>();
		String name = null;
		Scanner sc = new Scanner(System.in);
		while(true){
			System.out.println("----------------------电话簿系统------------"
					+ "----------");
			System.out.println("1.添加	2.删除	3.修改	4.查询所有  5.根据姓名查询	0.退出");
			System.out.println("----------------------电话簿系统------------"
					+ "----------");
			System.out.println("请选择业务:");
			Persons p1 = new Persons();
			switch (sc.nextInt()){
			case 1:
				System.out.println("----------------------添加电话簿------------"
						+ "----------");
				p1 = p1.addPerson();
				map.put(p1.getName().toString(), p1.toString());
				break;
			case 2:
				System.out.println("----------------------删除电话簿------------"
						+ "----------");
				name = p1.selectByName(map);
				if(name!=null){
					
					System.out.println("确定么?1(是)0(否)");
					int answer = sc.nextInt();
					if(answer ==1){
						map.remove(name);
						System.out.println("删除成功");
						}
				}else{
					System.out.println("此人不存在");
				}
				break;
			case 3:
				System.out.println("----------------------修改电话簿------------"
						+ "----------");
				name = p1.selectByName(map);
				if(name!=null){
					
					System.out.println("确定么?1(是)0(否)");
					int answer = sc.nextInt();
					if(answer ==1){
						map.remove(name);
						p1 = p1.addPerson();
				        map.put(p1.getName().toString(), p1.toString());
						System.out.println("修改成功");
						}
				}else{
					System.out.println("此人不存在");
				}
				break;
			case 4:
				System.out.println("----------------------查询所有------------"
						+ "----------");
				for(Map.Entry<String, String> entry:map.entrySet()){
					System.out.println(entry.getValue());
				}
				break;
			case 5:
				System.out.println("----------------------根据姓名查询------------"
						+ "----------");
				name = p1.selectByName(map);
				if(name==null){
					System.out.println("此人不存在");
				}
				break;
			case 0:return;
		}
		}
	}
}

你可能感兴趣的:(【实战】,Java,类)