真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java使用集合實(shí)現(xiàn)通訊錄功能-創(chuàng)新互聯(lián)

本文實(shí)例為大家分享了java使用集合實(shí)現(xiàn)通訊錄的具體代碼,供大家參考,具體內(nèi)容如下

10年積累的做網(wǎng)站、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有象山免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

代碼有些繁瑣,只適合初學(xué)者。

項(xiàng)目1

java通訊錄

方法(聲明方法的類)

代碼塊

代碼塊語(yǔ)法遵循標(biāo)準(zhǔn)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() {

  }

  // 添加聯(lián)系人
  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");
      // 不為空的時(shí)候.首先判斷有沒(méi)有這個(gè)分組
      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;
          // 有這個(gè)分組
          linkedList.add(person);
        } else {
          // 沒(méi)有這個(gè)分組
          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;
  }

  // 刪除聯(lián)系人
  public void remove(Person person) {
    // 首先找到那個(gè)分組
    // 所有的值
    Set set = this.map.keySet();
    for (Object object : set) {
      String nameString = person.getFirstCard();
      if (nameString.equals(object)) {
        // 找見(jiàn)分組
        Collection collection = this.map.values();
        for (Object object1 : collection) {// list數(shù)組
          LinkedList list1 = (LinkedList) object1;// 強(qiáng)制轉(zhuǎn)換
          for (Object object2 : list1) {// 遍歷
            Person person2 = (Person) object2;//
            if (person.getNameString().equals(
                person2.getNameString())) {
              if (list1.size() == 1) {
                this.map.remove(person2.getFirstCard());// 刪除整個(gè)分組
                show();
              } else {
                list1.remove(person2);
                show();
              }

            }
          }
        }
      }
    }
  }

  // 展示方法
  public void show() {
    System.out.println("*************************************");
    System.out.println("請(qǐng)選擇功能");
    String[] newArrayStrings = { "1.添加聯(lián)系人", "2.刪除聯(lián)系人", "3.修改聯(lián)系人",
        "4.展示所有聯(lián)系人" };
    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;
    }

  }

  // 修改聯(lián)系人信息
  public void upPerson() {
    Person person = new Person();
    System.out.println("請(qǐng)輸入要修改人的姓名:");
    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("請(qǐng)輸入新的名字:");
    String naString2 = scanner.next();
    System.out.println("請(qǐng)輸入新的年齡:");
    int age = scanner.nextInt();
    System.out.println("請(qǐng)輸入新的分組");
    String group = scanner.next();
    upPersonMessage(person, naString2, group, age);
  }

  // 判斷有沒(méi)有這個(gè)人
  public void shanChu(Person person) {
    if (this.map.isEmpty()) {
      System.out.println("通訊錄為空,修改失敗");
      System.out.println("是否添加聯(lián)系人?(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("請(qǐng)輸入添加人名字:");
    Scanner scanner = new Scanner(System.in);
    // 姓名
    String name = scanner.next();
    // 年齡
    System.out.println("請(qǐng)輸入年齡:");
    int age = scanner.nextInt();
    // 分組
    System.out.println("請(qǐng)輸入分組名稱:");
    String firstCardString = scanner.next();
    p.setFirstCard(firstCardString);
    p.setAge(age);
    p.setNameString(name);
    System.out.println(p);
    addContact(p);
    System.out.println("是否繼續(xù)操作?y/n");
    if (scanner.next().equals("y")) {
      show();
    } else {
      System.out.println("***************歡迎您下次再來(lái)****************");

    }
    return p;
  }

  public Person removePerson() {
    Person p = new Person();
    System.out.println("請(qǐng)輸入刪除人名字:");
    Scanner scanner = new Scanner(System.in);
    // 姓名
    String name = scanner.next();
    // 年齡
    System.out.println("請(qǐng)輸入刪除人的年齡:");
    int age = scanner.nextInt();
    // 分組
    System.out.println("請(qǐng)輸入刪除的分組名稱:");
    String firstCardString = scanner.next();
    p.setFirstCard(firstCardString);
    p.setAge(age);
    p.setNameString(name);
    remove(p);
    return p;
  }

  // 修改聯(lián)系人
  public HashMap upPersonMessage(Person person, String nameString,
      String firstCard, int age) {
    // 判斷有沒(méi)有此聯(lián)系人
    if (this.map.isEmpty()) {
      System.out.println("通訊錄為空");
    } else {
      String name = person.getFirstCard();
      // 找聯(lián)系人
      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())) {
            // 相等話,說(shuō)明有這個(gè)人
            System.out.println("有次聯(lián)系人");
            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;
  }

  // 顯示所有的聯(lián)系人
  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("是否繼續(xù)y/n");
      if (scanner.next().equals("y")) {
        show();
      } else {
        System.out.println("***************歡迎您下次再來(lái)****************");

      }
    }
  }
}

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使用集合實(shí)現(xiàn)通訊錄功能-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)源:http://weahome.cn/article/dgccpj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部