Java上机作业06
编写程序,完成一个简单的通讯录,实现客户信息的录入、查询,具体实现要求如下:
(1)编写Person类,实现客户信息的处理:属性:姓名、电话号码方法:·构造方法,两个参数,设置属性的值。·属性的getter、setter访问器·重写从Object继承的toString方法,返回姓名,电话号码连接成的字符串。
import java.io.*;
public class Person implements Serializable{
private String name;
private String phone;
public Person() {
}
public Person(String name,String phone){
this.name=name;
this.phone=phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString() {
return "姓名为"+this.name+"\t电话号码为"+this.phone;
}
}
编写PhoneBook(电话本)类:属性:phoneList(Person类类型的ArrayList列表,存储多个Person信息)方法:·构造方法,无参,创建空列表·构造方法,1个参数n,创建列表,调用input方法输入并在列表中插入n个Person对象。
·input():使用InputStreamReader、BufferedReader方法实现姓名、电话信息的输入,并创建一个Person对象,插入到phoneList中。·searchByName(String name):根据输入的姓名查询phoneList,返回找到的电话号码,否则返回null;
·searchByPhone(String phone):根据输入的电话号码查询phoneList,返回找到的Person对象,否则返回null;
·writeFile(String filename):遍历phoneList,调用每个Person的toString方法,获得姓名、电话串,输出到文本文件“phone.txt”中。·print():把phoneList中的所有信息输出。选做:
·exportList(String filename):使用对象序列化方法,把phoneList输出到文件·importList(String filename):使用对象序列化方法,从文件中读取内容加载phoneList。
· deleteByName(String name):根据输入的姓名查询phoneList,删除指定的Person对象,若删除成功返回true,否则返回false。
import java.util.*;
import java.io.*;
public class PhoneBook {
ArrayList<Person> phoneList;
Scanner s=new Scanner(System.in);
public PhoneBook() {
phoneList=new ArrayList();
}
public PhoneBook(int n) {
phoneList=new ArrayList();
for(int i=1;i<=n;i++) {
try {
input();
}catch(Exception e) {
e.printStackTrace();
}
}
}
public void input() throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入姓名:");
String n=br.readLine();
System.out.println("请输入电话号码:");
String p=br.readLine();
Person person=new Person(n,p);
phoneList.add(person);
}
public void searchByName01() {
System.out.println("请输入要查找的姓名:");
String name=s.next();
System.out.println("电话号码为:"+searchByName(name));
}
public String searchByName(String name) {
Iterator it=phoneList.iterator();
Person person;
while(it.hasNext()) {
person=(Person)it.next();
if(person.getName().equals(name)) {
return person.getPhone();
}
}
return null;
}
public void searchByPhone01() {
System.out.println("请输入要查找的电话号码:");
String phone=s.next();
System.out.println(searchByPhone(phone).toString());
}
public Person searchByPhone(String phone) {
Iterator it=phoneList.iterator();
Person person;
while(it.hasNext()) {
person=(Person)it.next();
if(person.getPhone().equals(phone)) {
return person;
}
}
return null;
}
public void writeFile (String filename) throws Exception{
FileOutputStream out=new FileOutputStream(new File(filename));
Iterator it=phoneList.iterator();
Person person;
while(it.hasNext()) {
person=(Person)it.next();
byte[] b=person.toString().getBytes();
out.write(b);
}
System.out.println("文件写入成功!");
}
public void print(){
Iterator it=phoneList.iterator();
Person person;
while(it.hasNext()) {
person=(Person)it.next();
System.out.println(person.toString());
}
}
public void exportList(String filename) throws Exception{
ObjectOutputStream oop=new ObjectOutputStream(new FileOutputStream(filename));
Iterator it=phoneList.iterator();
Person person;
while(it.hasNext()) {
person=(Person)it.next();
oop.writeObject(person);
}
}
public void deleteByName(){
System.out.println("请输入要输出的名字:");
String name=s.next();
if(deleteByName(name)) {
System.out.println("删除成功!");
}else {
System.out.println("电话录没有该名字!");
}
}
public boolean deleteByName(String name){
Iterator it=phoneList.iterator();
Person person;
while(it.hasNext()) {
person=(Person)it.next();
if(person.getName().equals(name)) {
phoneList.remove(person);
return true;
}
}
return false;
}
}
(3)编写Demo类创建PhoneBook类对象并进行初始化,接着循环显示菜单:
1、录入信息;2、按姓名查询;3、按电话号码查询;4、写文件;5、输出信息;6、删除信息;7、导出列表;8、导入列表;0、退出*********************************************************************
根据用户的选择调用相应的PhoneBook类的方法进行操作。
import java.util.*;
public class Demo {
public static void main(String[] args) throws Exception{
int i;
PhoneBook p=new PhoneBook();
Scanner s=new Scanner(System.in);
while(true) {
System.out.println("*******************");
System.out.println("1、录入信息;2、按姓名查询;3、按电话号码查询;4、写文件;5、输出信息;6、删除信息;7、导出列表;0、退出");
System.out.println("*******************");
System.out.println("请输入数字实现功能:");
i=s.nextInt();
while(true) {
if(i>=0&&i<=7) {
break;
}
else {
System.out.println("请输入正确的数字!");
}
}
switch(i) {
case 1: p.input();break;
case 2: p.searchByName01();break;
case 3: p.searchByPhone01();break;
case 4: p.writeFile("book.txt");break;
case 5: p.print();break;
case 6: p.deleteByName();break;
case 7: p.exportList("book1.txt");break;
case 0: return ;
}
}
}
}