请用面向对象方法设计并实现一个简单电话簿,包括如下功能:
1.能设置并以文件保存若干姓名、联系电话和电邮地址。
2.根据输入的姓名,在已经保存的文件中查询相应的联系电话和电邮地址,并显示查询结果。
1.能设置并以文件保存若干姓名、联系电话和电邮地址。
2.根据输入的姓名,在已经保存的文件中查询相应的联系电话和电邮地址,并显示查询结果。
3.根据输入的电话号码,在已经保存的文件中查询相应的联系人姓名,并显示查询结果。
将输入的数据保存到文件中。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class IOUtil {
public static Map<String, PhoneBook> phoneDatas = new HashMap<String, PhoneBook>();
public static final String DATA_FILE = "data.bat";
static {
load();
}
public static boolean save(String name,String phoneNum,String email){
phoneDatas.put(name, new PhoneBook(name,phoneNum,email));
try {
FileOutputStream out = new FileOutputStream(DATA_FILE);
out.write((name+" "+phoneNum+" "+email).getBytes());
out.flush();
out.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static boolean load() {
try {
FileInputStream in = new FileInputStream(DATA_FILE);
byte[] bs = new byte[1024];
int len = 0;
StringBuilder dataStr = new StringBuilder();
while ((len = in.read(bs)) != -1) {
dataStr.append(new String(bs, 0, len));
}
String[] dataStrs = dataStr.toString().trim().split("\\s+");
if (dataStrs.length > 1) {
for (int i = 0; i < dataStrs.length; i += 3) {
phoneDatas.put(dataStrs[i],new PhoneBook(dataStrs[i],dataStrs[i+1],dataStrs[i+2]));
}
}
return true;
} catch (FileNotFoundException e) {
try {
new File(DATA_FILE).createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
电话簿类
public class PhoneBook {
private String name;
private String phoneNum;
private String email;
public PhoneBook() {
}
public PhoneBook(String name, String phoneNum, String email) {
super();
this.name = name;
this.phoneNum = phoneNum;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
import java.util.Collection;
import java.util.Scanner;
public class Main {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String name ;
do {
System.out.println("可进行的操作有:");
System.out.println(" 1.存入信息");
System.out.println(" 2.通过姓名查询其他信息");
System.out.println(" 3.通过电话查询其他信息");
System.out.print("请输入您要进行的操作:");
switch (input.nextInt()) {
case 1:
System.out.print("请输入姓名:");
name = input.next();
System.out.print("请输入电话号码:");
String phoneNum = input.next();
System.out.print("请输入邮箱:");
String email = input.next();
IOUtil.save(name, phoneNum, email);
break;
case 2:
System.out.println("通过姓名查询其他信息");
findByName();
break;
case 3:
System.out.println("通过电话查询其他信息");
findByPhoneNum();
break;
default:
System.out.println("没有该操作!");
}
System.out.print("是否继续操作(y/n):");
} while ("y".equals(input.next()));
}
public static void findByName() {
String name ;
System.out.print("请输入需要查询的姓名:");
name = input.next();
if(IOUtil.phoneDatas.containsKey(name)){
System.out.println(name+"的电话号码是:"+IOUtil.phoneDatas.get(name).getPhoneNum()+",邮箱是:"+IOUtil.phoneDatas.get(name).getEmail());
}else{
System.out.println("没有名为"+name+"的信息。");
}
}
public static void findByPhoneNum(){
System.out.print("请输入需要查询的号码:");
String phoneNum = input.next();
Collection<PhoneBook> pbs = IOUtil.phoneDatas.values();
for(PhoneBook pb:pbs){
if(phoneNum.equals(pb.getPhoneNum())){
System.out.print("电话号码为"+phoneNum+"的姓名是:"+pb.getName()+",邮箱是:"+pb.getEmail());
return;
}
}
System.out.println("没有电话号码为"+phoneNum+"的信息。");
}
}