2022年10月10日Java上机

通讯录管理

AddressBook

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AddressBook {
private static List<Contact> contactList = new ArrayList<>();
public static void main(String[] args) {
initContact();
String choice = null;
String name,addres, phone;
Scanner stdin = new Scanner(System.in);
while (choice != "X") {
System.out.println("L—列出通讯录中的所有联系人");
System.out.println("A—添加一个联系人到通讯录中");
System.out.println("D—删除通讯录中指定的联系人");
System.out.println("F查找通讯录中符合条件的联系人");
System.out.println("X退出程序");
choice = stdin.next();
System.out.println("");
switch (choice) {
case "L":
System.out.println("通信录中所有联系人的信息如下:");
for (int i = 0; i < contactList.size(); i++) {
System.out.println(contactList.get(i).getName()+" " + contactList.get(i).getAddress() +" "+ contactList.get(i).getTelephone());
}
break;
case "A":
System.out.println("输入姓名:");
name = stdin.next();
System.out.println("输入手号");
phone = stdin.next();
System.out.println("输入地址");
addres = stdin.next();
Contact contact = new Contact(name, addres,phone);
contactList.add(contact);
break;
case "D":
System.out.println("输入姓名:");
name = stdin.next();
for (int i = 0; i < contactList.size(); i++) {
if (contactList.get(i).getName().equals(name)) {
contactList.remove(i);
}
}
break;
case "F":
System.out.println("输入姓名:");
name = stdin.next();
for (int i = 0; i < contactList.size(); i++) {
if (contactList.get(i).getName().equals(name)) {
System.out.println(contactList.get(i).getName()+" " + contactList.get(i).getAddress() +" "+ contactList.get(i).getTelephone());
}
}
break;
case "X":
System.out.println("退出系统......");
return;
}
}
}

private static void initContact() {
contactList.add(new Contact("张三","监狱","1101"));
contactList.add(new Contact("李四","监狱","1102"));
contactList.add(new Contact("王五","监狱","1103"));
}
}

Contact

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Contact {
private String name;
private String address;
private String telephone;
private int itemCount;
private Contact contact;
public Contact() {
}

public Contact(String name, String address, String telephone) {
this.name = name;
this.address = address;
this.telephone = telephone;
}

public Contact(Contact contact) {
this.contact = contact;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getTelephone() {
return telephone;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public void setItemCount(int itemCount) {
this.itemCount = itemCount;
}

public int getItemCount() {
return itemCount;
}
}

2022年10月10日Java上机
https://www.eldpepar.com/coding/63892/
作者
EldPepar
发布于
2022年10月9日
许可协议