package File.out.production;
import java.io.Serializable;
/**
* Created by dell on 2017/5/2.
*/
public class Contacts
implements Serializable {
private String
name;
private String
sex;
private String
num;
public Contacts(String name, String sex, String num) {
this.
name = name;
this.
sex = sex;
this.
num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.
name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.
sex = sex;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.
num = num;
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package File.out.production;
import java.io.*;
import java.text.Collator;
import java.util.*;
/**
* Created by shihao on 2017/5/2.
*/
public class Test5 {
static FileOutputStream
f=
null;
static FileInputStream
ff=
null;
static List<Contacts>
list=
null;
static File
fff=
new File(
"F:/java/联系人.txt");
static BufferedReader
bin=
new BufferedReader(
new InputStreamReader(System.
in));
public static void main(String[] args)
throws Exception {
while(
true){
System.
out.println(
"******手机联系人*********");
System.
out.println(
"******1.添加联系人*******");
System.
out.println(
"******2.查看联系人*******");
System.
out.println(
"******3.查找联系人*******");
System.
out.println(
"******4.退出*************");
String s=
bin.readLine();
if(
"1".equals(s)){
add();
}
if(
"2".equals(s)){
look();
}
if(
"3".equals(s)){
fand();
}
if(
"4".equals(s)){
break;
}
}
System.
out.println(
"再见");
}
private static void fand()
throws Exception{
System.
out.println(
"1.根据姓名 2.根据电话");
String aa=
bin.readLine();
if(
"1".equals(aa)){
name();
}
if(
"2".equals(aa)){
nu();
}
}
private static void nu()
throws Exception{
System.
out.println(
"请输入号码");
String a=
bin.readLine();
ff=
new FileInputStream(
fff);
ObjectInputStream in=
new ObjectInputStream(
ff);
list= (List<Contacts>) in.readObject();
int num=
0;
Set<Contacts> set=
new TreeSet<Contacts>(((o1, o2) -> {
Collator c=Collator.
getInstance();
return c.compare(o1.getName(),o2.getName());
}));
for (Contacts pp:
list) {
if(pp.getName().contains(a)){
set.add(pp);
num++;
}
}
for (Contacts pp:set) {
System.
out.println(
"姓名:"+pp.getName()+
"\t性别: "+pp.getSex()+
"\t电话:"+pp.getNum());
}
in.close();
System.
out.println(
"共找到"+num+
"条");
}
private static void name()
throws Exception {
System.
out.println(
"请输入姓名");
String a=
bin.readLine();
ff=
new FileInputStream(
fff);
ObjectInputStream in=
new ObjectInputStream(
ff);
list=(List<Contacts>) in.readObject();
int num=
0;
Set<Contacts> set=
new TreeSet<Contacts>(((o1, o2) -> {
Collator c=Collator.
getInstance();
return c.compare(o1.getName(),o2.getName());
}));
for (Contacts pp:
list) {
if(pp.getName().contains(a)){
num++;
set.add( pp);
}
}
in.close();
System.
out.println(
"共找到"+num+
"条");
for (Contacts pp:set) {
System.
out.println(
"姓名:"+pp.getName()+
"\t性别: "+pp.getSex()+
"\t电话:"+pp.getNum());
}
}
private static void look()
throws Exception{
ff=
new FileInputStream(
fff);
ObjectInputStream in=
new ObjectInputStream(
ff);
list=(List<Contacts>) in.readObject();
for (Contacts pp:
list) {
System.
out.println(
"姓名:"+pp.getName()+
"\t性别: "+pp.getSex()+
"\t电话:"+pp.getNum());
}
in.close();
}
private static void add()
throws Exception{
System.
out.println(
"请输入联系人信息");
System.
out.println(
"姓名:");
String a=
bin.readLine();
System.
out.println(
"性别:");
String b=
bin.readLine();
System.
out.println(
"电话:");
String c=
bin.readLine();
Contacts p=
new Contacts(a,b,c);
if(
fff.length()==
0){
list=
new ArrayList<>();
}
else {
ff=
new FileInputStream(
fff);
ObjectInputStream in=
new ObjectInputStream(
ff);
list=(List<Contacts>) in.readObject();
}
list.add(p);
f=
new FileOutputStream(
fff);
ObjectOutputStream out=
new ObjectOutputStream(
f);
out.writeObject(
list);
//把集合存进去,先判断有没有,有就先去出来,再添加 再放回去
out.close();
}
}