用java中的Arraylist实现电话本系统管理
大致思路:创建一个电话本条目的类,在主类中实例化。用实例化的对象调用构造参数接收输入值,然后将此对象存入Arraylist的对象中,实现动态添加电话本条目。
该系统具备添加、删除、修改、查询所有和按姓名查询等。
1 //定义PersonIitm类
2 public class PersonItem {
3 private String pname;
4 private char psex;
5 private int page;
6 private String phonenum;
7 private String qnumber;
8 private String paddress;
9 //构造函数
10 public void setName(String pname)
11 {
12 this.pname = pname;
13 }
14
15 public void setSex(char psex)
16 {
17 this.psex = psex;
18 }
19
20 public void setAge(int page)
21 {
22 this.page = page;
23 }
24
25 public void setPhone(String phonenum)
26 {
27 this.phonenum = phonenum;
28 }
29
30 public void setQQ(String qnumber)
31 {
32 this.qnumber = qnumber;
33 }
34
35 public void setAddress(String paddress)
36 {
37 this.paddress = paddress;
38 }
39
40 public String getName()
41 {
42 return pname;
43 }
44
45 public char getSex()
46 {
47 return psex;
48 }
49
50 public int getAge()
51 {
52 return page;
53 }
54
55 public String getPhone()
56 {
57 return phonenum;
58 }
59
60 public String getQQ()
61 {
62 return qnumber;
63 }
64
65 public String getAddress()
66 {
67 return paddress;
68 }
69
70 }
1 import java.util.ArrayList;
2 import java.util.Scanner;
3
4 public class MainPage {
5 public static void main(String[] args)
6 {
7 int cho = 0;
8 @SuppressWarnings("resource")
9 Scanner sc = new Scanner(System.in);
10 do
11 {
12 System.out.println("--------------------电话本管理系统"
13 + "--------------------\n"
14 + "1.添加\t2.删除\t3.修改\t4.查询所有\t5.根据姓名查询\t0.退出\n"
15 + "--------------------电话本管理业务"
16 + "--------------------\n请选择业务");
17 cho = sc.nextInt();
18 switch(cho)
19 {
20 case 0:
21 System.out.println("退出系统");
22 break;
23 case 1:
24 InsertItem();
25 break;
26 case 2:
27 RemoveItem();
28 break;
29 case 3:
30 UpdateItem();
31 break;
32 case 4:
33 SelectAll();
34 break;
35 case 5:
36 SelectItem();
37 break;
38 default:
39 System.out.println("没有此选项!");
40 break;
41 }
42 }
43 while(cho != 0);
44
45 }
46
47 static ArrayList<PersonItem> item = new ArrayList<PersonItem> ();
48 //定义添加函数
49 public static void InsertItem()
50 {
51 System.out.print("------------添加电话本------------\n姓名:");
52 @SuppressWarnings("resource")
53 Scanner sc = new Scanner(System.in);
54 PersonItem pi = new PersonItem(); //类实例化
55 pi.setName(sc.next());
56 System.out.print("性别:");
57 pi.setSex(sc.next().charAt(0));
58 System.out.print("年龄:");
59 pi.setAge(sc.nextInt());
60 System.out.print("电话:");
61 pi.setPhone(sc.next());
62 System.out.print("QQ:");
63 pi.setQQ(sc.next());
64 System.out.print("地址:");
65 pi.setAddress(sc.next());
66 item.add(pi);
67 System.out.println("姓名:"+pi.getName()+",性别:"+pi.getSex()+",年龄:"+pi.getAge()+",电话:"
68 +pi.getPhone()+",QQ:"+pi.getQQ()+",地址:"+pi.getAddress()+"\n添加成功");
69 }
70
71 //定义查找人名函数
72 public static void SelectItem()
73 {
74 System.out.print("------------查找电话本------------\n"
75 + "请输入姓名:");
76 @SuppressWarnings("resource")
77 Scanner sc = new Scanner(System.in);
78 String name = sc.next();
79 int i = -1;
80 for(int j = 0;j < item.size();j++)
81 {
82 if(item.get(j).getName().equals(name)) //切记不用“==”比较,它比较的是地址
83 {
84 i = j;
85 break;
86 }
87 }//遍历查询人名
88 if(i == -1)
89 System.out.println("此人不存在");
90 else
91 System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
92 +item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
93 }
94
95 //定义查找所有函数
96 public static void SelectAll()
97 {
98 System.out.println("------------打印所有电话本------------");
99 for(int i = 0;i < item.size();i++)
100 {
101 System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
102 +item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
103 }
104 }
105
106 //删除函数
107 public static void RemoveItem()
108 {
109 System.out.print("------------删除电话本------------\n"
110 + "请输入删除的姓名 ");
111 @SuppressWarnings("resource")
112 Scanner sc = new Scanner(System.in);
113 String name = sc.next();
114 int i = -1;
115 for(int j = 0;j < item.size();j++)
116 {
117 if(item.get(j).getName().equals(name))
118 {
119 i = j;
120 break;
121 }
122 }
123 if(i == -1)
124 System.out.println("此人不存在");
125 else
126 {
127 System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
128 +item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
129 System.out.println("确定吗?1(是)0(否)");
130 if(sc.nextInt() == 1)
131 {
132 item.remove(i);
133 System.out.println("删除成功");
134 }
135 }
136
137 }
138
139 //修改函数
140 public static void UpdateItem()
141 {
142 System.out.print("------------修改电话本------------\n"
143 + "输入姓名:");
144 @SuppressWarnings("resource")
145 Scanner sc = new Scanner(System.in);
146 String name = sc.next();
147 int i = -1;
148 for(int j = 0;j < item.size();j++)
149 {
150 if(item.get(j).getName().equals(name))
151 {
152 i = j;
153 break;
154 }
155 }
156 if(i == -1)
157 System.out.println("此人不存在");
158 else
159 {
160 System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
161 +item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
162 System.out.print("请输入信息\n姓名:");
163 item.get(i).setName(sc.next());
164 System.out.print("性别:");
165 item.get(i).setSex(sc.next().charAt(0));
166 System.out.print("年龄:");
167 item.get(i).setAge(sc.nextInt());
168 System.out.print("电话:");
169 item.get(i).setPhone(sc.next());
170 System.out.print("QQ:");
171 item.get(i).setQQ(sc.next());
172 System.out.print("地址:");
173 item.get(i).setAddress(sc.next());
174 System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
175 +item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
176 System.out.println("修改成功");
177
178 }
179 }
180 }