java使用集合实现通讯录功能
程序员文章站
2022-10-19 20:28:50
本文实例为大家分享了java使用集合实现通讯录的具体代码,供大家参考,具体内容如下
代码有些繁琐,只适合初学者。
项目1
java通讯录
方法(声明方法的类)...
本文实例为大家分享了java使用集合实现通讯录的具体代码,供大家参考,具体内容如下
代码有些繁琐,只适合初学者。
项目1
java通讯录
方法(声明方法的类)
代码块
代码块语法遵循标准markdown代码,例如:
package com.lanou.homwork; import java.util.collection; import java.util.hashmap; import java.util.linkedlist; import java.util.scanner; import java.util.set; class contactperson { public hashmap map = null; public hashmap getmap() { return map; } public void setmap(hashmap map) { this.map = map; } public contactperson(hashmap map) { super(); this.map = map; } public contactperson() { } // 添加联系人 public hashmap addcontact(person person) { // map集合为空 if (this.map.size() == 0) { linkedlist list = new linkedlist(); // 添加到list集合里 list.add(person); this.map.put(person.getfirstcard(), list);// 添加map集合 system.out.println(person.getfirstcard()); } else { system.out.println("1111111111111111111"); // 不为空的时候.首先判断有没有这个分组 set keyset = this.map.keyset();// 得到所有的键key // 遍历所有键 for (object object : keyset) { if (person.getfirstcard().equals(object)) { object object2 = this.map.get(person.getfirstcard()); linkedlist linkedlist = (linkedlist) object2; // 有这个分组 linkedlist.add(person); } else { // 没有这个分组 linkedlist list3 = new linkedlist(); list3.add(person); this.map.put(person.getfirstcard(), list3); system.out.println(keyset); system.out.println(map.values()); return map; } } } return map; } // 删除联系人 public void remove(person person) { // 首先找到那个分组 // 所有的值 set set = this.map.keyset(); for (object object : set) { string namestring = person.getfirstcard(); if (namestring.equals(object)) { // 找见分组 collection collection = this.map.values(); for (object object1 : collection) {// list数组 linkedlist list1 = (linkedlist) object1;// 强制转换 for (object object2 : list1) {// 遍历 person person2 = (person) object2;// if (person.getnamestring().equals( person2.getnamestring())) { if (list1.size() == 1) { this.map.remove(person2.getfirstcard());// 删除整个分组 show(); } else { list1.remove(person2); show(); } } } } } } } // 展示方法 public void show() { system.out.println("*************************************"); system.out.println("请选择功能"); string[] newarraystrings = { "1.添加联系人", "2.删除联系人", "3.修改联系人", "4.展示所有联系人" }; for (int i = 0; i < newarraystrings.length; i++) { system.out.println("******" + newarraystrings[i] + "******"); } system.out.println("*************************************"); scanner scanner = new scanner(system.in); int i = scanner.nextint(); switch (i) { case 1: { addperosn(); } break; case 2: { removeperson(); } break; case 3: { upperson(); } break; case 4: { printresult(this.map); } break; default: break; } } // 修改联系人信息 public void upperson() { person person = new person(); system.out.println("请输入要修改人的姓名:"); scanner scanner = new scanner(system.in); string nastring = scanner.next(); person.setnamestring(nastring); shanchu(person); } // 输入新的人名,年龄 public void cc(person person) { scanner scanner = new scanner(system.in); system.out.println("请输入新的名字:"); string nastring2 = scanner.next(); system.out.println("请输入新的年龄:"); int age = scanner.nextint(); system.out.println("请输入新的分组"); string group = scanner.next(); uppersonmessage(person, nastring2, group, age); } // 判断有没有这个人 public void shanchu(person person) { if (this.map.isempty()) { system.out.println("通讯录为空,修改失败"); system.out.println("是否添加联系人?(yes/no)"); scanner scanner = new scanner(system.in); string namestring = scanner.next(); if (namestring.equals("yes")) { addperosn(); } else { system.out.println("傻逼,滚你妈"); } } collection collection = this.map.values(); for (object object : collection) { linkedlist list = (linkedlist) object; for (object object2 : list) { person person2 = (person) object2; if (person.getnamestring().equals(person2.getnamestring())) { system.out.println("恭喜你,通讯录有此人"); cc(person); } else { system.out.println("输入有误"); } } } } public person addperosn() { person p = new person(); system.out.println("请输入添加人名字:"); scanner scanner = new scanner(system.in); // 姓名 string name = scanner.next(); // 年龄 system.out.println("请输入年龄:"); int age = scanner.nextint(); // 分组 system.out.println("请输入分组名称:"); string firstcardstring = scanner.next(); p.setfirstcard(firstcardstring); p.setage(age); p.setnamestring(name); system.out.println(p); addcontact(p); system.out.println("是否继续操作?y/n"); if (scanner.next().equals("y")) { show(); } else { system.out.println("***************欢迎您下次再来****************"); } return p; } public person removeperson() { person p = new person(); system.out.println("请输入删除人名字:"); scanner scanner = new scanner(system.in); // 姓名 string name = scanner.next(); // 年龄 system.out.println("请输入删除人的年龄:"); int age = scanner.nextint(); // 分组 system.out.println("请输入删除的分组名称:"); string firstcardstring = scanner.next(); p.setfirstcard(firstcardstring); p.setage(age); p.setnamestring(name); remove(p); return p; } // 修改联系人 public hashmap uppersonmessage(person person, string namestring, string firstcard, int age) { // 判断有没有此联系人 if (this.map.isempty()) { system.out.println("通讯录为空"); } else { string name = person.getfirstcard(); // 找联系人 collection collection = this.map.values();// 得到所有的值 for (object object : collection) { linkedlist list = (linkedlist) object; for (object object2 : list) { person p = (person) object2; if (person.getnamestring().equals(p.getnamestring())) { // 相等话,说明有这个人 system.out.println("有次联系人"); if (list.size() == 1) { this.map.remove(p.getfirstcard()); person person2 = new person(); person2.setage(age); person2.setfirstcard(firstcard); person2.setnamestring(namestring); linkedlist list2 = new linkedlist(); list2.add(person2); this.map.put(person2.getfirstcard(), list2); system.out.println(this.map); } else { person person2 = new person(); person2.setage(age); person2.setfirstcard(firstcard); person2.setnamestring(namestring); list.add(person2); system.out.println(this.map); } } } } } return map; } // 显示所有的联系人 public void printresult(hashmap map1) { if (map1.isempty()) { system.out.println("通讯录为空"); } else { system.out.println(map1); scanner scanner = new scanner(system.in); system.out.println("是否继续y/n"); if (scanner.next().equals("y")) { show(); } else { system.out.println("***************欢迎您下次再来****************"); } } } } class person { private string namestring; private string firstcard; private int age; public string getnamestring() { return namestring; } public void setnamestring(string namestring) { this.namestring = namestring; } public string getfirstcard() { return firstcard; } public void setfirstcard(string firstcard) { this.firstcard = firstcard; } public int getage() { return age; } public void setage(int age) { this.age = age; } public person() { super(); // todo auto-generated constructor stub } public person(string namestring, string firstcard) { this.namestring = namestring; this.firstcard = firstcard; } public person(string namestring, string firstcard, int age) { this(namestring, firstcard); this.age = age; } @override public string tostring() { return "姓名:" + this.getnamestring() + "年龄:" + this.age + "分组:" + this.firstcard; } }
脚注
项目2
测试类
代码块
代码块语法遵循标准markdown代码,例如:
package com.lanou.homwork; import java.util.hashmap; public class testperson { public static void main(string[] args) { hashmap map = new hashmap(); contactperson contactperson = new contactperson(map); contactperson.show(); } }
感谢浏览。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读