网站建设资讯

NEWS

网站建设资讯

java使用集合实现通讯录功能-创新互联

本文实例为大家分享了java使用集合实现通讯录的具体代码,供大家参考,具体内容如下

10年积累的做网站、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站设计后付款的网站建设流程,更有象山免费网站建设让你可以放心的选择与我们合作。

代码有些繁琐,只适合初学者。

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


当前名称:java使用集合实现通讯录功能-创新互联
分享URL:http://cdweb.net/article/dgccpj.html