您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本文实例为大家分享了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(); } }
感谢浏览。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。