使用Map集合写电话本程序

电话本管理系统 

  1.主界面


大体就是要实现对电话本的增删改查:

因为不清楚要存储的数据的数量,所以可以使用ArrayList不定长数组去实现,也可以使用集合类去做,此题我用Map集合来做。

废话不多说,直接上代码:

PhoneInfo类定义了一些属性,变量及构造方法

public class PhoneInfo {
	private String name;
	private String sex;
	private String age;
	private String tel;
	private String qq;
	private String address;

	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 getAge() {
		return age;
	}

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

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	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;
	}

	public PhoneInfo(String name, String sex, String age, String tel, String qq, String address) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.tel = tel;
		this.qq = qq;
		this.address = address;
	}

	@Override
	public String toString() {
		return "PhoneInfo [name=" + name + ", sex=" + sex + ", age=" + age + ", tel=" + tel + ", qq=" + qq
				+ ", address=" + address + "]";
	}

}


TelBook类封装了对电话本的操作:增删改查的方法。


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class TelBook {
	static Scanner sc = new Scanner(System.in);
	static TelBook tb = new TelBook();
	Map map = new HashMap<>();	
	public static void main(String[] args) {
		System.out.println("————————————————————电话本管理系统——————————————————");
		System.out.println("1.添加       2.删除       3.修改       4.查询所有       5.根据姓名查询      0.退出");
		System.out.println("————————————————————电话本管理系统——————————————————");
		int option;
		do {
		System.out.println("请选择业务");
		option = sc.nextInt();
		switch (option) {
		case 1:
			
			tb.add();
			break;
		case 2:
			tb.delete();
			break;
		case 3:
			tb.modify();
			break;
		case 4:
			tb.find();
			break;
		case 5:
			tb.findName();
			break;
		case 0:
			System.out.println("电话本系统结束");
			break;
		default:

		}}
		while(option!=0);
	}
	public void add() {
		System.out.println("———————————添加电话本————————————");
		shuru();
		Set set = map.entrySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			Map.Entry mapEntry = (Map.Entry) it.next();
			System.out.print(mapEntry.getKey() + "   " + mapEntry.getValue());
		}
		System.out.println();
		System.out.println("添加成功");

	}
	public void delete() {
		System.out.println("————————————删除电话本——————————————");
		System.out.println("请输入要删除的姓名");
	    String deleteName = sc.next();
	        if(map.containsValue(deleteName)) {
	        	map.remove(deleteName);
	        	System.out.println("删除成功");
	        }else 
	        	System.out.println("用户不存在!");
		
	}
	public void modify() {
		System.out.println("————————————修改电话本——————————————");
		System.out.println("请输入要修改的姓名");
        String modifyName = sc.next();
        if(map.containsValue(modifyName)) {
        map.remove(modifyName);
            shuru(); 
            Set set = map.entrySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			Map.Entry mapEntry = (Map.Entry) it.next();
			System.out.print(mapEntry.getKey() + "   " + mapEntry.getValue());
		}
		System.out.println();
        }
        else {
        	System.out.println("用户不存在");
        }   
		
	}
	public void find() {
		System.out.println("——————————————查询所有——————————————");
		/* Set set = map.entrySet();
			Iterator it = set.iterator();
			while (it.hasNext()) {
				Map.Entry mapEntry = (Map.Entry) it.next();
				System.out.print(mapEntry.getKey() + "   " + mapEntry.getValue());
			}*/
		Set<String> set = map.keySet();
		for(String s:set) {
			System.out.println(map.get(s).toString());
		}
	}
	public void findName() {
		System.out.println("————————————根据姓名查询——————————————");
		System.out.println("请输入要查询的姓名");
        String findName = sc.next();
        if(map.containsValue(findName)) {
        	  Set set = map.entrySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			Map.Entry mapEntry = (Map.Entry) it.next();
			System.out.print(mapEntry.getKey() + "   " + mapEntry.getValue());
		}
		System.out.println();
        }
        else {
        	System.out.println("用户不存在");
        }
      
        
	}
	public void shuru() {
		PhoneInfo p = new PhoneInfo("", "", "", "", "", "");
		System.out.print("姓名:");
		String name = sc.next();
		p.setName(name);
		map.put("姓名:", name);	
		System.out.println();
		System.out.print("性别:");
		String sex = sc.next();
		p.setSex(sex);
		map.put("性别:", sex);
		System.out.println();
		System.out.print("年龄:");
		String age = sc.next();
		p.setAge(age);
		map.put("年龄:", age);
		System.out.println();	
		System.out.print("电话:");
		String tel = sc.next();
		p.setTel(tel);
		map.put("电话:", tel);
		System.out.println();	
		System.out.print("QQ:");
		String qq = sc.next();
		p.setQq(qq);
		map.put("QQ:", qq);
		System.out.println();	
		System.out.print("地址:");
		String address = sc.next();
		p.setAddress(address);
		map.put("地址:", address);
		System.out.println();		
	}
	

}


部分代码截图:




运行截图:



写在最后:从Map中获取元素一定要注意是Key值还是Value值,修改时要先remove.

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

普罗旺斯_浅笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值