电话本管理系统HashMap实现

本文介绍了一个简单的电话本管理系统,包括添加、删除、修改联系人信息,显示所有联系人及根据姓名搜索的功能,并通过序列化技术将数据持久化存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本实验用Contact对象作为HashMap的value,name作为key保存在HashMap中,最终通过序列化将其存在contact1.txt中

现在还没学会数据库也没学会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;
}
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值