通信录的设计(java)

通信录的设计

基本要求:
定义date类,至少包括年月日;
定义person类,至少包括姓名、性别和出生日期;
定义人员staff类,从person类派生,至少包括电话、地址、邮政编码、邮箱、QQ号和类别(例如:同学、朋友等)

功能要求:
1、设计菜单实现功能选择;
2、输入功能:输入人员信息,并保存到文件中;
3、查询功能:
(1)能够根据姓名、电话精确查询人员信息;
(2)能够根据地址进行模糊查询人员信息;
(3)根据人员类别查询人员信息;
4、根据姓名对人员信息排序输出;
5、能根据姓名、电话修改人员信息;
6、能根据姓名、电话删除人员信息。

PS:文件乱码是因为这是以二进流的方式存储数据(需要特定的软件打开才能显示正常)

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PMain {
	static Scanner in = new Scanner(System.in);
	static File fp = new File("D:\\staff.txt");
	List<Staff> list = new ArrayList<Staff>();//动态数组

	PMain() {

	}

	public static void menu() {
		System.out.println("\t1.输入信息");
		System.out.println("\t2.查询信息");
		System.out.println("\t3.排序信息");
		System.out.println("\t4.删除信息");
		System.out.println("\t5.修改信息");
		System.out.println("\t6.退出");
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		int selet;
		PMain p = new PMain();
		while (true) {
			if (!fp.exists()) {
				fp.createNewFile();
			} else {
				p.readO();
			}
			menu();
			System.out.println("请选择:");
			selet = in.nextInt();
			switch (selet) {
			case 1:
				p.in();
				break;
			case 2:
				p.search();
				break;
			case 3:
				p.sort();
				break;
			case 4:
				p.delet();
				break;
			case 5:
				p.modify();
				break;
			case 6:
				System.exit(0);
			}
		}
	}

	// 将文件内容按arraylist格式读出到list
	public void readO() throws FileNotFoundException, IOException, ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fp));//对象输入流
		list = (List<Staff>) ois.readObject();
		ois.close();
	}

	// 将内容按arraylist格式写入文件
	public void writeO() throws FileNotFoundException, IOException, ClassNotFoundException {
		FileOutputStream os = new FileOutputStream(fp);//对象输出流
		ObjectOutputStream oos = new ObjectOutputStream(os);
		oos.writeObject(list);
		oos.flush();//刷新
		oos.close();
	}

	private void in() throws IOException, ClassNotFoundException {
		System.out.println("请输入姓名,性别,出生日,电话、地址、邮政编码、邮箱、QQ号和类别:");
		String n = in.next();
		String s = in.next();
		int y = in.nextInt();
		int m = in.nextInt();
		int d = in.nextInt();
		String t = in.next();
		String address = in.next();
		String yzcode = in.next();
		String mailbox = in.next();
		String QQ = in.next();
		String se = in.next();
		Staff staff = new Staff(n, s, new Data(y, m, d), t, address, yzcode, mailbox, QQ, se);
		list.add(staff);
		writeO();
		System.out.println("OK");
	}

	private void search() throws IOException, ClassNotFoundException {
		System.out.println("请选择:");
		System.out.println("1.根据姓名或电话精确查询人员信息\n2.根据地址进行模糊查询人员信息\n3.根据人员类别查询人员信息");
		switch (in.nextInt()) {
		case 1:
			String sc = in.next();
			for (Staff f : list) {
				if (sc.equals(f.getname()) || sc.equals(f.gettel())) {
					System.out.println(f.toString());
				}
			}
			break;
		case 2:
			String sc1 = in.next();
			for (Staff f : list) {
				if (sc1.equals(f.getadd().substring(0, sc1.length()))) {
					System.out.println(f.toString());
				}
			}
			break;
		case 3:
			String sc2 = in.next();
			for (Staff f : list) {
				if (sc2.equals(f.getse())) {
					System.out.println(f.toString());
				}
			}
		}
		System.out.println("OK");
	}

	private void sort() throws IOException, ClassNotFoundException {
		System.out.println("原信息显示:");
		for (Staff f : list) {
			System.out.println(f);
		}
		System.out.println("按姓名排序后结果:");
		List<Staff> newList = list;
		for (int i = 0; i < newList.size() - 1; i++) {
			for (int z = i + 1; z < newList.size(); z++) {
				if (newList.get(i).getname().compareTo(newList.get(z).getname()) > 0) {
					Staff h = newList.get(i);
					newList.set(i, newList.get(z));
					newList.set(z, h);
				}
			}
		}
		for (Staff f : newList) {
			System.out.println(f);
		}
	}

	private void delet() throws IOException, ClassNotFoundException {
		System.out.println("请输入被删除人的姓名或号码:");
		String sc = in.next();
		Staff z = null;
		for (Staff f : list) {
			if (sc.equals(f.getname()) || sc.equals(f.gettel())) {
				z = f;
			}
		}
		if (z != null) {
			list.remove(z);
			writeO();
			System.out.println("OK");
			return;
		}
		System.out.println("error");

	}

	private void modify() throws IOException, ClassNotFoundException {
		System.out.println("请输入将修改的姓名或号码:");
		String sc = in.next();
		int i = 0;
		for (Staff f : list) {
			if (sc.equals(f.getname()) || sc.equals(f.gettel())) {
				System.out.println("请重新输入姓名,性别,出生日,电话、地址、邮政编码、邮箱、QQ号和类别:");
				String n = in.next();
				String s = in.next();
				int y = in.nextInt();
				int m = in.nextInt();
				int d = in.nextInt();
				String t = in.next();
				String address = in.next();
				String yzcode = in.next();
				String mailbox = in.next();
				String QQ = in.next();
				String se = in.next();
				Staff z = new Staff(n, s, new Data(y, m, d), t, address, yzcode, mailbox, QQ, se);
				list.set(i, z);
				writeO();
				System.out.println("OK");
				return;
			}
			i++;
		}
		System.out.println("error");
	}
}
//Serializable 序列化
class Data implements Serializable {
	private int year, month, day;

	public String toString() {
		return year + "年" + month + "月" + day + "日";
	}

	Data(int y, int m, int d) {
		this.year = y;
		this.month = m;
		this.day = d;
	}

	void sety(int y, int m, int d) {
		year = y;
		month = m;
		day = d;
	}
}

class Person implements Serializable {
	private String name;
	private String sex;
	Data data;

	Person() {

	}

	void setname(String n) {
		this.name = n;
	}

	void setsex(String n) {
		this.sex = n;
	}

	Person(String n, String s, Data d) {
		this.name = n;
		this.sex = s;
		this.data = d;
	}

	String getsex() {
		return sex;
	}

	String getname() {
		return name;
	}

}

class Staff extends Person implements Serializable {
	private String tel;
	private String address;
	private String yzcode;
	private String mailbox;
	private String QQ;
	private String se;

	Staff() {

	}

	void setbirthday(int y, int m, int d) {
		super.data.sety(y, m, d);
	}

	void settel(String s) {
		this.tel = s;
	}

	void setadd(String s) {
		this.address = s;
	}

	void setmail(String s) {
		this.mailbox = s;
	}

	void setyz(String s) {
		this.yzcode = s;
	}

	void setQQ(String s) {
		this.QQ = s;
	}

	void setse(String s) {
		this.se = s;
	}

	String gettel() {
		return tel;
	}

	String getadd() {
		return address;
	}

	String getmail() {
		return mailbox;
	}

	String getyz() {
		return yzcode;
	}

	String getQQ() {
		return QQ;
	}

	String getse() {
		return se;
	}

	public String toString() {
		return "姓名:" + getname() + ",性别:" + getsex() + ",生日:" + data.toString() + ", \n手机号码:" + tel + ",地址:" + address
				+ ",邮政编码" + yzcode + ", \n邮箱:" + mailbox + ",QQ:" + QQ + ",分类:" + se;
	}

	Staff(String name, String sex, Data d, String tel, String a, String yz, String mb, String qq, String se) {
		super(name, sex, d);
		this.tel = tel;
		this.address = a;
		this.yzcode = yz;
		this.mailbox = mb;
		this.QQ = qq;
		this.se = se;
	}
}
  • 12
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小木荣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值