-
1 package kai1; 2 3 public class Message { 4 private String name; 5 private String sex; 6 private int age; 7 private int telephone; 8 private int qq; 9 private String adress; 10 11 public Message() { 12 super(); 13 } 14 public Message(String name, String sex, int age, int telephone, int qq, String adress) { 15 super(); 16 this.name = name; 17 this.sex = sex; 18 this.age = age; 19 this.telephone = telephone; 20 this.qq = qq; 21 this.adress = adress; 22 } 23 24 public String getName() { 25 return name; 26 } 27 public void setName(String name) { 28 this.name = name; 29 } 30 public String getSex() { 31 return sex; 32 } 33 public void setSex(String sex) { 34 this.sex = sex; 35 } 36 public int getAge() { 37 return age; 38 } 39 public void setAge(int age) { 40 this.age = age; 41 } 42 public int getTelephone() { 43 return telephone; 44 } 45 public void setTelephone(int telephone) { 46 this.telephone = telephone; 47 } 48 public int getQq() { 49 return qq; 50 } 51 public void setQq(int qq) { 52 this.qq = qq; 53 } 54 public String getAdress() { 55 return adress; 56 } 57 public void setAdress(String adress) { 58 this.adress = adress; 59 } 60 61 @Override 62 public String toString() { 63 return "姓名:" + name + ", 性别:" + sex + ", 年齡:" + age + ", 電話:" + telephone + ", QQ:" + qq 64 + ", 位址:" + adress; 65 } 66 67 }
1 package kai1; 2 3 import java.util.Scanner; 4 5 public class Method { 6 Scanner s = new Scanner(System.in); 7 8 public void add(Message[] m) { 9 for (int i = 0; i < m.length; i++) { 10 if (m[i] == null) { 11 m[i] = new Message(); 12 System.out.println("----------添加電話本----------"); 13 System.out.println("姓名:"); 14 String str1 = s.next(); 15 m[i].setName(str1); 16 System.out.println("性别:"); 17 String str2 = s.next(); 18 m[i].setSex(str2); 19 System.out.println("年齡:"); 20 int i1 = s.nextInt(); 21 m[i].setAge(i1); 22 System.out.println("電話:"); 23 int i2 = s.nextInt(); 24 m[i].setTelephone(i2); 25 System.out.println("QQ:"); 26 int i3 = s.nextInt(); 27 m[i].setQq(i3); 28 System.out.println("位址:"); 29 String str3 = s.next(); 30 m[i].setAdress(str3); 31 System.out.println(m[i].toString()); 32 System.out.println("添加成功"); 33 break; 34 } 35 } 36 37 } 38 39 public void delete(Message[] m) { 40 System.out.println("----------删除電話本----------"); 41 System.out.println("請輸入要删除人的名字:"); 42 String str = s.next(); 43 boolean flag = false; 44 for (int i = 0; i < m.length; i++) { 45 if (m[i] != null) { 46 if (str.equals(m[i].getName())) { 47 m[i] = null; 48 flag = true; 49 break; 50 } 51 } 52 } 53 if (flag) { 54 System.out.println("删除成功"); 55 } else { 56 System.out.println("此聯系人不存在"); 57 } 58 59 } 60 61 public void upDate(Message[] m) {// 修改 62 // for (int i = 0; i < ma.length; i++) { 63 // ma[i] = new Message(); 64 System.out.println("----------修改電話本----------"); 65 System.out.println("請輸入要修改的姓名:"); 66 String str = s.next(); 67 for (int i = 0; i < m.length; i++) { 68 if (m[i] != null) { 69 // m[i] = new Message(); 70 if (str.equals(m[i].getName())) { 71 System.out.println(m[i].toString()); 72 73 System.out.println("請重新輸入資訊"); 74 System.out.println("姓名:"); 75 String str1 = s.next(); 76 m[i].setName(str1); 77 System.out.println("性别:"); 78 String str2 = s.next(); 79 m[i].setSex(str2); 80 System.out.println("年齡:"); 81 int i1 = s.nextInt(); 82 m[i].setAge(i1); 83 System.out.println("電話:"); 84 int i2 = s.nextInt(); 85 m[i].setTelephone(i2); 86 System.out.println("QQ:"); 87 int i3 = s.nextInt(); 88 m[i].setQq(i3); 89 System.out.println("位址:"); 90 String str3 = s.next(); 91 m[i].setAdress(str3); 92 System.out.println(m[i].toString()); 93 System.out.println("修改成功"); 94 break; 95 } 96 } 97 } 98 } 99 100 public void printAll(Message[] m) {// 列印 101 System.out.println("----------列印電話本----------"); 102 for (int i = 0; i < m.length; i++) { 103 if (m[i] != null) { 104 System.out.println(m[i].toString()); 105 } 106 } 107 // 108 } 109 110 public void nameQuery(Message[] m) { 111 System.out.println("----------根據姓名查詢----------"); 112 System.out.println("請輸入要查找的人的姓名"); 113 String str = s.next(); 114 for (int i = 0; i < m.length; i++) { 115 if (m[i] != null) { 116 if (str.equals(m[i].getName())) { 117 System.out.println(m[i].toString()); 118 } 119 } 120 } 121 } 122 }
1 package kai1; 2 3 import java.util.Scanner; 4 5 public class TestAddPhone { 6 public static void main(String[] args) { 7 Method me = new Method(); 8 Message[] m = new Message[10]; 9 Scanner s = new Scanner(System.in); 10 while (true) { 11 System.out.println("---------------電話本管理系統---------------"); 12 System.out.println("1.添加 2.删除 3.修改 4.列印所有 5.根據姓名查詢 6.退出"); 13 System.out.println("---------------電話本管理系統---------------"); 14 System.out.println("請選擇業務:"); 15 int input = s.nextInt(); 16 switch(input) { 17 case 1: 18 me.add(m); 19 break; 20 case 2: 21 me.delete(m); 22 break; 23 case 3: 24 me.upDate(m); 25 break; 26 case 4: 27 me.printAll(m); 28 break; 29 case 5: 30 31 break; 32 case 6: 33 System.exit(0); 34 break; 35 default : 36 System.out.println("您輸入的資訊有誤"); 37 break; 38 } 39 } 40 } 41 }