50,530
社区成员
发帖
与我相关
我的任务
分享
import java.io.File;
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.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* 用java双向链表作数据结构,编写一个通讯录管理系统。设计内容:本系统应完成以下几方面的功能:
* 1) 输入信息——enter();
* 2) 显示信息———display( );
* 3) 查找以姓名作为关键字 ———search( );
* 4) 删除信息———delete( );
* 5) 存盘———save ( );
* 6) 装入———load( ) ;
* @author bree06
*
*/
public class Demo20 {
/** 电话薄 */
LinkedList<AddressBook> entities = null;
/**
* 实例化的时候就装入通迅录
*/
public Demo20() {
super();
load();
}
/**
* 输入信息
* @param ab
* @return
*/
public boolean enter(AddressBook ab) {
// check
if (ab == null || nullOrEmpty(ab.getName())) {
throw new IllegalArgumentException("通讯录用户名必须.");
}
int index = entities.indexOf(ab);
// 如果不存在则追加到最后否则替换
if (index == -1) {
entities.add(ab);
} else {
entities.add(index, ab);
}
return true;
}
/**
* 显示信息所有通讯录
*/
public void display() {
if (entities.isEmpty()) {
System.out.println("通讯录为空.");
return;
}
int index = 0;
for (Iterator<AddressBook> it = entities.iterator(); it.hasNext();) {
System.out.println("<---------------"+index+++"------------>\r\n" + it.next());
}
System.out.println("<---------------END------------>");
}
/**
* 查找以姓名作为关键字
* @param name
* @return
*/
public List<AddressBook> search(String name) {
if (entities.isEmpty()) {
return null;
}
// 查询条件为空时返回全部数据
if (nullOrEmpty(name)) {
return entities;
}
// 部分一致查询,返回所有包含关键字的用户
List<AddressBook> result = new LinkedList<AddressBook>();
for (Iterator<AddressBook> it = entities.iterator(); it.hasNext();) {
AddressBook ab = it.next();
String _name = ab.getName();
if (_name.length() - (_name.replace(name, "").length()) > 0) {
result.add(ab);
}
}
return result;
}
/**
* 存盘
*/
public synchronized void save() {
if (entities == null)
return;
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("AddressBooks.data")));
oos.writeObject(entities);
} catch (IOException e) {
} finally {
try {
if (oos != null) oos.close();
} catch (IOException e) {
}
}
}
/**
* 装入
*/
@SuppressWarnings("unchecked")
public synchronized void load() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("AddressBooks.data"));
if (ois != null) {
entities = (LinkedList<AddressBook>) ois.readObject();
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} catch (ClassNotFoundException e) {
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException e) {
}
// none entity
if (entities == null) {
entities = new LinkedList<AddressBook>();
}
}
}
/**
* 删除信息
* @param ab
* @return
*/
public void delete(AddressBook ab) {
if (entities.isEmpty()) {
return;
}
entities.remove(ab);
}
/**
* 删除信息(删除相同名字的通讯录)
* @param ab
* @return
*/
public void delete(String name) {
if (entities.isEmpty()) {
return;
}
AddressBook ab = new AddressBook();
ab.setName(name);
entities.remove(ab);
}
/**
* 空判断
* @param s
* @return
*/
public boolean nullOrEmpty(String s) {
return s == null || "".equals(s);
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
Demo20 demo = new Demo20();
demo.display();
// demo.delete("abc");
// demo.save();
// // enter
// AddressBook ab = new AddressBook();
// ab.setName("abc");
// ab.setPhone_number("123456789");
// demo.enter(ab);
// demo.display();
// // search
// List<AddressBook> list = demo.search("cua");
// System.out.println("抽出结果:");
// if (list.isEmpty()) {
// System.out.println("满足条件的通讯录为空.");
// return;
// }
// int index = 0;
// for (Iterator<AddressBook> it = list.iterator(); it.hasNext();) {
// System.out.println("<---------------"+index+++"------------>");
// System.out.println(it.next());
// }
// System.out.println("<---------------END------------>");
// // enter
// ab = new AddressBook();
// ab.setName("eee");
// ab.setPhone_number("000000000");
// demo.enter(ab);
// demo.display();
//
// demo.save();
}
}
/**
* 通讯录
* @author bree06
*
*/
class AddressBook implements Serializable {
private static final long serialVersionUID = 1L;
/** 姓名 */
private String name;
/** 电话号码 */
private String phone_number;
/** 住址 */
private String address;
/** 邮箱地址 */
private String email;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the phone_number
*/
public String getPhone_number() {
return phone_number;
}
/**
* @param phone_number the phone_number to set
*/
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return String.format("姓名:%-10s电话:%-11s\r\n地址:%s\r\n邮箱:%s", name, phone_number, address, email);
}
@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof AddressBook)) {
return false;
}
return name.equals(((AddressBook) o).getName());
}
}
[/quote]
这个程序有排序功能吗?import java.io.File;
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.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* 用java双向链表作数据结构,编写一个通讯录管理系统。设计内容:本系统应完成以下几方面的功能:
* 1) 输入信息——enter();
* 2) 显示信息———display( );
* 3) 查找以姓名作为关键字 ———search( );
* 4) 删除信息———delete( );
* 5) 存盘———save ( );
* 6) 装入———load( ) ;
* @author bree06
*
*/
public class Demo20 {
/** 电话薄 */
LinkedList<AddressBook> entities = null;
/**
* 实例化的时候就装入通迅录
*/
public Demo20() {
super();
load();
}
/**
* 输入信息
* @param ab
* @return
*/
public boolean enter(AddressBook ab) {
// check
if (ab == null || nullOrEmpty(ab.getName())) {
throw new IllegalArgumentException("通讯录用户名必须.");
}
int index = entities.indexOf(ab);
// 如果不存在则追加到最后否则替换
if (index == -1) {
entities.add(ab);
} else {
entities.add(index, ab);
}
return true;
}
/**
* 显示信息所有通讯录
*/
public void display() {
if (entities.isEmpty()) {
System.out.println("通讯录为空.");
return;
}
int index = 0;
for (Iterator<AddressBook> it = entities.iterator(); it.hasNext();) {
System.out.println("<---------------"+index+++"------------>\r\n" + it.next());
}
System.out.println("<---------------END------------>");
}
/**
* 查找以姓名作为关键字
* @param name
* @return
*/
public List<AddressBook> search(String name) {
if (entities.isEmpty()) {
return null;
}
// 查询条件为空时返回全部数据
if (nullOrEmpty(name)) {
return entities;
}
// 部分一致查询,返回所有包含关键字的用户
List<AddressBook> result = new LinkedList<AddressBook>();
for (Iterator<AddressBook> it = entities.iterator(); it.hasNext();) {
AddressBook ab = it.next();
String _name = ab.getName();
if (_name.length() - (_name.replace(name, "").length()) > 0) {
result.add(ab);
}
}
return result;
}
/**
* 存盘
*/
public synchronized void save() {
if (entities == null)
return;
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("AddressBooks.data")));
oos.writeObject(entities);
} catch (IOException e) {
} finally {
try {
if (oos != null) oos.close();
} catch (IOException e) {
}
}
}
/**
* 装入
*/
@SuppressWarnings("unchecked")
public synchronized void load() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("AddressBooks.data"));
if (ois != null) {
entities = (LinkedList<AddressBook>) ois.readObject();
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} catch (ClassNotFoundException e) {
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException e) {
}
// none entity
if (entities == null) {
entities = new LinkedList<AddressBook>();
}
}
}
/**
* 删除信息
* @param ab
* @return
*/
public void delete(AddressBook ab) {
if (entities.isEmpty()) {
return;
}
entities.remove(ab);
}
/**
* 删除信息(删除相同名字的通讯录)
* @param ab
* @return
*/
public void delete(String name) {
if (entities.isEmpty()) {
return;
}
AddressBook ab = new AddressBook();
ab.setName(name);
entities.remove(ab);
}
/**
* 空判断
* @param s
* @return
*/
public boolean nullOrEmpty(String s) {
return s == null || "".equals(s);
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
Demo20 demo = new Demo20();
demo.display();
// demo.delete("abc");
// demo.save();
// // enter
// AddressBook ab = new AddressBook();
// ab.setName("abc");
// ab.setPhone_number("123456789");
// demo.enter(ab);
// demo.display();
// // search
// List<AddressBook> list = demo.search("cua");
// System.out.println("抽出结果:");
// if (list.isEmpty()) {
// System.out.println("满足条件的通讯录为空.");
// return;
// }
// int index = 0;
// for (Iterator<AddressBook> it = list.iterator(); it.hasNext();) {
// System.out.println("<---------------"+index+++"------------>");
// System.out.println(it.next());
// }
// System.out.println("<---------------END------------>");
// // enter
// ab = new AddressBook();
// ab.setName("eee");
// ab.setPhone_number("000000000");
// demo.enter(ab);
// demo.display();
//
// demo.save();
}
}
/**
* 通讯录
* @author bree06
*
*/
class AddressBook implements Serializable {
private static final long serialVersionUID = 1L;
/** 姓名 */
private String name;
/** 电话号码 */
private String phone_number;
/** 住址 */
private String address;
/** 邮箱地址 */
private String email;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the phone_number
*/
public String getPhone_number() {
return phone_number;
}
/**
* @param phone_number the phone_number to set
*/
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return String.format("姓名:%-10s电话:%-11s\r\n地址:%s\r\n邮箱:%s", name, phone_number, address, email);
}
@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof AddressBook)) {
return false;
}
return name.equals(((AddressBook) o).getName());
}
}