本实验用Contact对象作为HashMap的value,name作为key保存在HashMap中,最终通过序列化将其存在contact1.txt中
现在还没学会数据库也没学会swing,以后可以继续改进,出个电话本2.0和3.0
实体类Contact
操作类ContactPro
交互
现在还没学会数据库也没学会swing,以后可以继续改进,出个电话本2.0和3.0
实体类Contact
package TelDirectory;
import java.io.Serializable;
//序列化
public class Contact implements Serializable {
private String name;
private String sex;
private int age;
private String phonenum;
private String QQnum;
private String address;
//构造函数
public Contact(){}
public Contact(String name,String sex,int age,
String phonenum,String QQnum,String address){
this.name=name;
this.sex=sex;
this.age=age;
this.phonenum=phonenum;
this.QQnum=QQnum;
this.address=address;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
public String getQQnum() {
return QQnum;
}
public void setQQnum(String QQnum) {
QQnum = QQnum;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//toString方法
public String toString() {
return "Contact [姓名:" + name + ", 性别:" + sex + ", 年龄:" + age
+ ", 电话:" + phonenum + ", QQ:" + QQnum + ", 地址:"
+ address + "]";
}
}
操作类ContactPro
package TelDirectory;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class ContactPro {
private Scanner sc;
HashMap hm=new HashMap();
//增加
public void add(){
sc = new Scanner(System.in);
System.out.println("------添加电话本------");
System.out.println("姓名:");
String name=sc.nextLine();
System.out.println("性别:");
String sex=sc.nextLine();
System.out.println("电话:");
String phonenum=sc.nextLine();
System.out.println("QQ:");
String QQnum=sc.nextLine();
System.out.println("地址:");
String adress=sc.nextLine();
System.out.println("年龄:");
int age=sc.nextInt();
Contact newOne=new Contact(name,sex,age,phonenum,QQnum,adress);//读到的数据构造一个Contact对象
hm.put(name, newOne);//添加到HashMap
System.out.println("添加成功");
}
//删除
public void del(){
sc = new Scanner(System.in);
System.out.println("------删除电话本------");
System.out.println("请输入要删除的姓名:");
String name=sc.nextLine();
if(hm.containsKey(name)){ //判断是否有这个人
System.out.println(hm.get(name).toString());
System.out.println("确定要删除么 1(是)0(否)");
int flag=sc.nextInt();
if(flag==1){
hm.remove(name);
System.out.println("删除成功");
}else{
System.out.println("取消删除");
}
}else{
System.out.println("不存在此人");
}
}
//修改
public void change(){
sc = new Scanner(System.in);
System.out.println("------修改电话本------");
System.out.println("请输入要修改的姓名:");
String name=sc.nextLine();
if(hm.containsKey(name)){ //判断是否有这个人
System.out.println(hm.get(name).toString());
hm.remove(name); //先删除这个人
System.out.println("请重新输入信息");
System.out.println("姓名:");
String name1=sc.nextLine();
System.out.println("性别:");
String sex=sc.nextLine();
System.out.println("电话:");
String phonenum=sc.nextLine();
System.out.println("QQ:");
String QQnum=sc.nextLine();
System.out.println("地址:");
String adress=sc.nextLine();
System.out.println("年龄:");
int age=sc.nextInt();
Contact newOne=new Contact(name1,sex,age,phonenum,QQnum,adress);
hm.put(name,newOne); //再加上新的
System.out.println("修改成功");
}else{
System.out.println("不存在此人");
}
}
//打印所有
public void show(){
System.out.println("------显示所有-----");
Iterator iter = hm.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
System.out.println(entry.getValue().toString());
}
}
//按姓名搜索
public void search(){
sc = new Scanner(System.in);
System.out.println("------删除电话本------");
System.out.println("请输入要修改的姓名:");
String name=sc.nextLine();
if(hm.containsKey(name)){
System.out.println(hm.get(name).toString());
}else{
System.out.println("不存在此人");
}
}
//清空hm
public void clear(){
hm.clear();
}
//最初用DataInputStream和DataOutputStream读取和存储电话本信息
/*public void open() throws IOException{
System.out.println("读取......");
DataInputStream in=new DataInputStream(new FileInputStream("contact.txt"));
boolean flag=true;
while(flag){
try{
String name=in.readUTF();
String sex=in.readUTF();
String phonenum=in.readUTF();
String QQnum=in.readUTF();
String adress=in.readUTF();
int age=in.readInt();
Contact newOne=new Contact(name,sex,age,phonenum,QQnum,adress);//读到的数据构造一个Contact对象
hm.put(name, newOne);//添加到HashMap
}catch(EOFException e){
flag=false;
}
}
in.close();
}
public void save() throws IOException{
System.out.println("保存......");
DataOutputStream out=new DataOutputStream(new FileOutputStream("contact.txt"));
Iterator iter = hm.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Contact val = (Contact) entry.getValue();
out.writeUTF(val.getName());
out.writeUTF(val.getSex());
out.writeUTF(val.getPhonenum());
out.writeUTF(val.getQQnum());
out.writeUTF(val.getAddress());
out.writeInt(val.getAge());
}
out.close();
System.out.println("保存成功");
}
*/
//序列化进行存储和读取
//读取
public void open1() throws Exception{
System.out.println("读取......");
ObjectInputStream in=new ObjectInputStream(new FileInputStream("contact1.txt"));
boolean flag=true;
while(flag){
try{
Contact newOne=(Contact) in.readObject();
hm.put(newOne.getName(), newOne);//添加到HashMap
}catch(EOFException e){
flag=false;
}
}
in.close();
}
//存储
public void save1() throws Exception{
System.out.println("保存......");
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("contact1.txt"));
Iterator iter = hm.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Contact val = (Contact) entry.getValue();
out.writeObject(val);
}
out.close();
System.out.println("保存成功");
}
}
交互
package TelDirectory;
import java.io.IOException;
import java.util.Scanner;
public class ContactTest {
public static void main(String[] args) throws Exception {
ContactPro con =new ContactPro();
int flag=0;//循环出口
while(flag==0){
System.out.println("----------------------电话本管理系统----------------------");
System.out.println("1.添加\t2.删除\t3.修改\t4.显示所有\t5.根据姓名查询\t6.读取并显示\t7.保存电话本\t8.清空\t0.退出");
System.out.println("----------------------电话本管理系统----------------------");
System.out.println("请选择操作");
Scanner sc1 = new Scanner(System.in);
switch (sc1.nextInt()) {
case 1:
con.add();
break;
case 2:
con.del();
break;
case 3:
con.change();
break;
case 4:
con.show();
break;
case 5:
con.search();
break;
case 6:
con.open1();con.show();
break;
case 7:
con.save1();
break;
case 8:
con.clear();
break;
case 0:
flag=1;
break;
default:
break;
}
}
}
}