这是我的第四篇文章

面向对象

实现通讯录系统

Contact类

java
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
67
68
69
import java.io.Serializable;

class Contaxt implements Serializable {
int id;
String name;
String tel;
String address;
boolean flag;

public Contaxt(int id, String name, String tel, String address) {
this.id = id;
this.name = name;
this.tel = tel;
this.address = address;
flag = false;
}

public boolean getFlag() {
return flag;
}

public void setFlag(boolean flag) {
this.flag = flag;
}

public void setId(int id) {
this.id = id;
}

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

public void setTel(String tel) {
this.tel = tel;
}

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

public int getId() {
return id;
}

public String getName() {
return name;
}

public String getTel() {
return tel;
}

public String getAddress() {
return address;
}

@Override
public String toString() {
return "Contaxt{" +
"id=" + id +
", name='" + name + '\'' +
", tel='" + tel + '\'' +
", address='" + address + '\'' +
'}';
}
}


AddressBook

java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.io.*;
import java.util.ArrayList;

import java.util.Scanner;

public class AddressBook {

ArrayList<Contaxt> contaxts = new ArrayList<>();

public void addContaxt() {
System.out.println("请输入姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.next();

System.out.println("请输入电话:");
String tel = sc.next();

System.out.println("请输入地址:");
String address = sc.next();

int id = contaxts.size() == 0 ? 1 : contaxts.get(contaxts.size()-1).getId() + 1;
Contaxt contaxt = new Contaxt(id,name,tel,address);
contaxts.add(contaxt);

System.out.println("添加成功");
System.out.println(contaxt);
save();
}

public void showAll() {
for ( Contaxt contaxt : contaxts) {
if(contaxt.getFlag()==false)
System.out.println(contaxt);
}
}

public void searchID() {
System.out.println("请输入ID:");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();

for (Contaxt contaxt:contaxts) {
if(id == contaxt.id && contaxt.getFlag() == false) {
System.out.println(contaxt);
return ;
}
}

System.out.println("ID:"+id+" 不存在");
}

public void deleteID() {
System.out.println("请输入ID:");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();

for (Contaxt contaxt:contaxts) {
if(id == contaxt.id && contaxt.getFlag() == false) {
System.out.println("已删除");
System.out.println(contaxt);
contaxt.setFlag(true);
save();
return ;
}
}

System.out.println("ID:"+id+" 不存在");
}

public void changeID() {
System.out.println("请输入ID:");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();

for (Contaxt contaxt:contaxts) {
if(id == contaxt.id && contaxt.getFlag() == false) {
System.out.println(contaxt);

int op = sc.nextInt();


System.out.println("请输入姓名:");
String name = sc.next();
contaxt.setName(name);

System.out.println("请输入电话:");
String tel = sc.next();
contaxt.setTel(tel);

System.out.println("请输入地址:");
String address = sc.next();
contaxt.setAddress(address);

System.out.println("修改成功");
System.out.println(contaxt);
save();
return ;
}
}

System.out.println("ID:"+id+" 不存在");

}

public void save() {
ObjectOutputStream objectOutputStream = null;

try {
objectOutputStream = new ObjectOutputStream(
new FileOutputStream("C:\\Users\\Lenovo\\IdeaProjects\\通讯录系统\\src\\data.txt"));
objectOutputStream.writeObject(contaxts);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void init(){

ObjectInputStream objectInputStream = null;

try {
objectInputStream = new ObjectInputStream( new FileInputStream("C:\\Users\\Lenovo\\IdeaProjects\\通讯录系统\\src\\data.txt"));
ArrayList<Contaxt> data = (ArrayList<Contaxt> )objectInputStream.readObject();
contaxts = data;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
objectInputStream.close();
} catch (Exception e){
e.printStackTrace();
}
}
}

}

TextDemo

java
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
import java.util.Scanner;

import static java.lang.System.exit;

public class TextDemo {
public static void main(String[] args) {
AddressBook MyBook = new AddressBook();
MyBook.init();

while(true)
{
System.out.println("1.添加联系人");
System.out.println("2.删除联系人");
System.out.println("3.修改联系人");
System.out.println("4.查找联系人");
System.out.println("5.查看所有联系人");
System.out.println("6.退出");

Scanner sc = new Scanner(System.in);
int op = sc.nextInt();

switch (op)
{
case 1:
MyBook.addContaxt();
break;
case 2:
MyBook.deleteID();
break;
case 3:
MyBook.changeID();
break;
case 4:
MyBook.searchID();
break;
case 5:
MyBook.showAll();
break;
case 6:
System.out.println("退出");
System.exit(0);
default:
System.out.println("输入错误");
}
}
}
}