Java项目作业-----电话簿系统

package hello;

import java.io.IOException;

public class APP {
	public static void main(String[] args) throws IOException {
		Menu m = new Menu();
		m.mainMenu();
	}

}

package hello;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;

public class Menu {
	Scanner in = new Scanner(System.in);
	Operate o = new Operate();

	public void mainMenu() throws IOException {
		System.out.println("       主菜单              ");
		System.out.println("******************");
		System.out.println("**    1 添加记录       **");
		System.out.println("**    2 查找记录       **");
		System.out.println("**    3 修改记录       **");
		System.out.println("**    4 删除记录       **");
		System.out.println("**    5 排序记录       **");
		System.out.println("**    6 退出系统       **");
		System.out.println("******************");
		System.out.println("请输入正确数字,最大6,最小1");
		int selection1 = 0;
		while (true) {
			try {
				selection1 = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			switch (selection1) {
			case 1:
				addMenu();
				break;
			case 2:
				searchMenu();
				break;
			case 3:
				modifyMenu();
				break;
			case 4:
				deleteMenu();
				break;
			case 5:
				orderMenu();
				break;
			case 6:
				End();
				System.exit(0);
			default:
				System.out.println("输入数字错误,请重新输入");
				break;
			}
		}

	}

	public void End() throws IOException {// 退出保存文件

		FileOutputStream fos = new FileOutputStream("text.txt", true);
		Iterator<Person> it = Operate.list.iterator();
		while (it.hasNext()) {
			Person p = it.next();
			String str = p.toString() + "\r\n";
			byte buff[] = str.getBytes();
			fos.write(buff);
		}
		fos.flush();
		fos.close();
		System.out.println("退出系统成功");
	}

	// 添加用户信息菜单
	public void addMenu() throws IOException {
		System.out.println("******************");
		System.out.println("**    1 添加新记录       **");
		System.out.println("**    2 查看全记录       **");
		System.out.println("**    3 返回上一级       **");
		System.out.println("******************");
		System.out.println("请输入正确数字,最大3,最小1");
		o.addLogic();
	}

	// 查找用户信息菜单
	public void searchMenu() throws IOException {
		System.out.println("******************************************");
		System.out.println("**   请输入需要查询人员信息的方式:	**");
		System.out.println("**     1 按姓名查找	  **");
		System.out.println("**     2 按年龄查找	  **");
		System.out.println("**     3 按性别查找	  **");
		System.out.println("**     4 按号码查找	  **");
		System.out.println("**     5 按住址查找	  **");
		System.out.println("**     6 查看全纪录	  **");
		System.out.println("**     7 返回上一级	  **");
		System.out.println("******************************************");
		System.out.println("请输入正确地数字,最小是:1	最大是:7");
		o.searchLogic();
	}

	// 修改用户信息菜单
	public void modifyMenu() throws IOException {
		System.out.println("**********************************");
		System.out.println("**    修改人员信息子菜单   	**");
		System.out.println("**   1 查看全纪录	    **");
		System.out.println("**   2 修改指定纪录	    **");
		System.out.println("**   3 返回上一级		**");
		System.out.println("**********************************");
		System.out.println("请输入正确地数字,最小是:1	最大是:3");
		o.modifyLogic();
	}

	// 修改用户信息子菜单
	public void subModifyMenu() throws IOException {
		System.out.println("******************************************");
		System.out.println("**    修改指定记录子菜单		**");
		System.out.println("**   1 修改姓名		  **");
		System.out.println("**   2 修改年龄		  **");
		System.out.println("**   3 修改性别		  **");
		System.out.println("**   4 修改号码		  **");
		System.out.println("**   5 修改住址		  **");
		System.out.println("**   6 返回上一级	  **");
		System.out.println("******************************************");
		System.out.println("请输入正确地数字,最小是:1	最大是:6");
		o.modify();
	}

	// 删除用户信息菜单
	public void deleteMenu() throws IOException {
		System.out.println("******************************");
		System.out.println("**   删除人员子菜单 		**");
		System.out.println("**   1-查看全纪录		**");
		System.out.println("**   2-删除指定纪录		**");
		System.out.println("**   3-删除全部纪录		**");
		System.out.println("**   4-返回上一级		**");
		System.out.println("******************************");
		System.out.println("请输入正确地数字,最小是:1	最大是:4");
		o.deleteLogic();
	}

	// 排序信息菜单
	public void orderMenu() throws IOException {
		System.out.println("******************************************");
		System.out.println("**     请选择排序人员信息的方式:	**");
		System.out.println("**     1-按姓名排序		**");
		System.out.println("**     2-按年龄排序		**");
		System.out.println("**     3-按性别排序		**");
		System.out.println("**     4-查看全纪录		**");
		System.out.println("**     5-返回上一级		**");
		System.out.println("******************************************");
		System.out.println("请输入正确地数字,最小是:1	最大是:5");
		o.orderLogic();
	}
}

package hello;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;

public class Operate {
	Scanner in = new Scanner(System.in);
	static Menu m = new Menu();
	static Person pp = new Person();
	static ArrayList<Person> list = new ArrayList<Person>();

	// 用户添加信息业务逻辑控制
	public void addLogic() throws IOException {
		while (true) {
			int way = 0;
			try {
				way = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			switch (way) {
			case 1:
				addOperation();
				break;
			case 2:
				showAll();
				m.addMenu();
				break;
			case 3:
				m.mainMenu();
				break;
			default:
				System.out.println("输入错误,请重新输入");
				break;
			}
		}

	}

	// 用户查询信息业务逻辑控制
	public void searchLogic() throws IOException {
		while (true) {
			int way = 0;
			try {
				way = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			switch (way) {
			case 1:
				searchByName();
				break;
			case 2:
				searchByAge();
				break;
			case 3:
				searchBySex();
				break;
			case 4:
				searchByTelNum();
				break;
			case 5:
				searchByAdd();
				break;
			case 6:
				showAll();
				m.searchMenu();
				break;
			case 7:
				m.mainMenu();
				break;
			default:
				System.out.println("输入错误,请重新输入");
				break;
			}

		}

	}

	// 用户修改信息业务逻辑控制
	public void modifyLogic() throws IOException {
		while (true) {
			int way = 0;
			try {
				way = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			switch (way) {
			case 1:
				showAll();
				m.modifyMenu();
				break;
			case 2:
				m.subModifyMenu();
				break;
			case 3:
				m.mainMenu();
				break;
			default:
				System.out.println("输入错误,请重新输入");
				modifyLogic();
				break;
			}
		}

	}

	// 用户删除信息业务逻辑控制
	public void deleteLogic() throws IOException {
		while (true) {
			int way = 0;
			try {
				way = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			switch (way) {
			case 1:
				showAll();
				m.deleteMenu();
				break;
			case 2:
				delete();
				break;
			case 3:
				deleteAll();
				break;
			case 4:
				m.mainMenu();
				break;
			default:
				System.out.println("输入错误,请重新输入");
				break;
			}
		}
	}

	// 用户排序信息业务逻辑控制
	public void orderLogic() throws IOException {
		while (true) {
			int way = 0;
			try {
				way = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			switch (way) {
			case 1:
				orderName();
				m.orderMenu();
				break;
			case 2:
				orderAge();
				m.orderMenu();
				break;
			case 3:
				orderSex();
				m.orderMenu();
				break;
			case 4:
				showAll();
				m.orderMenu();
				break;
			case 5:
				m.mainMenu();
				break;
			default:
				System.out.println("输入错误,请重新输入");
				break;
			}
		}

	}

	// 添加新记录
	public void addOperation() throws IOException {
		// FileInputStream fis= new FileInputStream("text.txt");
		while (true) {
			// FileOutputStream fos= new FileOutputStream("text.txt",true);
			System.out.println("请输入人员信息:");
			System.out.println("输入姓名:1~10位字母");
			String name = in.nextLine();
			name = TelNoteRegex.nameRegex(name);
			System.out.println("输入年龄:1~100+");
			int age = Integer.parseInt(in.nextLine());
			age = TelNoteRegex.ageRegex(age);
			System.out.println("请输入性别:男m或M女f或F");
			String sex = in.nextLine();
			sex = TelNoteRegex.sexRegex(sex);
			System.out.println("请输入电话号码:6-11位数字");
			String telNum = in.nextLine();
			telNum = TelNoteRegex.telNumRegex(telNum);
			System.out.println("请输入地址:1-50位字母或数字");
			String address = in.nextLine();
			address = TelNoteRegex.addressRegex(address);
			// 创建Person对象,并设置值
			Person p1 = new Person();
			int id = 1;
			p1.setId(id);
			p1.setName(name);
			p1.setAge(age);
			p1.setSex(sex);
			p1.setTelNum(telNum);
			p1.setAddress(address);
			// 将p1添加进集合list中
			list.add(p1);
			System.out.println("录入成功");
			System.out.println("是否继续输入?1- 是              0- 否 ");

			int message1 = Integer.parseInt(in.nextLine());
			if (message1 != 1) {
				break;
			}
			id = id + 1;
		}

		m.addMenu();
	}

	// 删除指定用户信息
	public void delete() throws IOException {
		System.out.println("请输入所要删除人员的序号:(1-" + list.size() + ")");
		int ID = Integer.parseInt(in.nextLine());
		if (ID < 1 || ID > list.size())
			System.out.println("输入序号错误");
		else {
			list.remove(ID - 1);
			System.out.println("删除成功!");
		}
		m.deleteMenu();
		int id=1;
		Iterator<Person> it = list.iterator();
		while(it.hasNext()) {
			it.next().setId(id);
			id++;
		}
	}

	// 删除全部用户信息
	public void deleteAll() throws IOException {
		list.clear();
		System.out.println("已清空电话簿");
		m.deleteMenu();
	}

	// 用户修改信息子功能
	public void modify() throws IOException {
		while (true) {
			int way = 0;
			try {
				way = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			switch (way) {
			case 1:
				name();
				m.subModifyMenu();
				break;

			case 2:
				age();
				m.subModifyMenu();
				break;

			case 3:
				sex();
				m.subModifyMenu();
				break;

			case 4:
				telNum();
				m.subModifyMenu();
				break;

			case 5:
				address();
				m.subModifyMenu();
				break;

			case 6:
				m.modifyMenu();
				break;

			}
		}

	}

	// 修改姓名
	public void name() {
		System.out.println("请输入所要修改人员的序号:(1-"+list.size()+")");
		int ID;
		while (true) {
			try {
				ID = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			if (ID > list.size()||ID<1) {
				System.out.println("无此序号");
				break;
			} else {
				System.out.println("请输入新的姓名:1~10位字母");
				String name = in.nextLine();
				name = TelNoteRegex.nameRegex(name);
				list.get(ID - 1).setName(name);
				System.out.println("修改成功!");
				break;
			}
		}

	}

	// 修改年齡
	public void age() {
		System.out.println("请输入所要修改人员的序号:(1-" + list.size() + ")");
		int ID;
		boolean flag=false;
		while (true) {
			try {
				ID = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			if (ID < 1 || ID > list.size()) {
				System.out.println("输入序号错误");
				break;
			}
			else {
				Iterator<Person> it = list.iterator();
				while (it.hasNext()) {
					Person p = it.next();
					if (p.getId() == ID) {
						System.out.println("请输入新的年龄:1~100+");
						int age = Integer.parseInt(in.nextLine());
						age = TelNoteRegex.ageRegex(age);
						p.setAge(age);
						System.out.println("修改成功!");
						flag=true;
						break;
					}
				}
			}
			if(flag)
			break;
		}

	}

	// 修改性別
	public void sex() {

		System.out.println("请输入所要修改人员的序号:(1-" + list.size() + ")");
		int ID;
		boolean flag=false;
		while (true) {
			try {
				ID = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			if (ID < 1 || ID > list.size()){
				System.out.println("输入序号错误");
				break;
			}
			else {
				Iterator<Person> it = list.iterator();
				while (it.hasNext()) {
					Person p = it.next();
					if (p.getId() == ID) {
						System.out.println("请输入新的性别:(男 m or M) (女 f or F)");
						String sex = in.nextLine();
						sex = TelNoteRegex.sexRegex(sex);
						p.setSex(sex);
						System.out.println("修改成功!");
						flag=true;
						break;
						
					}
				}
				
			}
			if(flag)
				break;
		}
	}

	// 修改电话号码
	public void telNum() {
		System.out.println("请输入所要修改人员的序号:(1-" + list.size() + ")");
		int ID;
		boolean flag=false;
		while (true) {
			try {
				ID = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			if (ID < 1 || ID > list.size()) {
				System.out.println("输入序号错误");
				break;
			}
				
			else {
				Iterator<Person> it = list.iterator();
				while (it.hasNext()) {
					Person p = it.next();
					if (p.getId() == ID) {
						System.out.println("请输入新的号码:6~11位数字");
						String telNum = in.nextLine();
						telNum = TelNoteRegex.telNumRegex(telNum);
						p.setTelNum(telNum);
						System.out.println("修改成功!");
						flag=true;
						break;
					}
				}
			}
			if(flag)
		    	break;
		}
    
	}

	// 修改地址
	public void address() {
		System.out.println("请输入所要修改人员的序号:(1-" + list.size() + ")");
		int ID;
		boolean flag=false;
		while (true) {
			try {
				ID = Integer.parseInt(in.nextLine().trim());
			} catch (Exception e) {
				System.out.println("你输入的数据类型不对,请重新输入");
				continue;
			}
			if (ID < 1 || ID > list.size()) {
				System.out.println("输入序号错误");
				break;
			}
				
			else {
				Iterator<Person> it = list.iterator();
				while (it.hasNext()) {
					Person p = it.next();
					if (p.getId() == ID) {
						System.out.println("请输入新的住址:1~50位字母或数字");
						String address = in.nextLine();
						address = TelNoteRegex.addressRegex(address);
						p.setAddress(address);
						System.out.println("修改成功!");
						flag= true;
						break;
					}
				}
			}
			if(flag)
				break;
		}

	}

	// 按姓名查找
	public void searchByName() throws IOException {
		System.out.println("请输入要查找的姓名:1-10个字母");
		String name = in.nextLine();
		name = TelNoteRegex.nameRegex(name);
		Iterator<Person> it = list.iterator();
		boolean flag = false;
		while (it.hasNext()) {
			Person person = it.next();
			if (person.getName().equals(name)) {
				System.out.println(person);
				flag = true;
			}
		}
		if (!flag) {
			System.out.println("查无此人");
		}
		m.searchMenu();
	}

	// 按年齡查找
	public void searchByAge() throws IOException {
		System.out.println("请输入要查找的年龄:1-100+");
		int age = Integer.parseInt(in.nextLine());
		age = TelNoteRegex.ageRegex(age);
		Iterator<Person> it = list.iterator();
		boolean flag = false;
		while (it.hasNext()) {
			Person person = it.next();
			if (person.getAge() == age) {
				System.out.println(person);
				flag = true;
			}
		}
		if (!flag) {
			System.out.println("查无此人");
		}
		m.searchMenu();
	}

	// 按电话号码查找
	public void searchByTelNum() throws IOException {
		System.out.println("请输入要查找的电话号码:");
		String telNum = in.nextLine();
		telNum = TelNoteRegex.telNumRegex(telNum);
		Iterator<Person> it = list.iterator();
		boolean flag = false;
		while (it.hasNext()) {
			Person person = it.next();
			if (person.getTelNum().equals(telNum)) {
				System.out.println(person);
				flag = true;
			}
		}
		if (!flag) {
			System.out.println("查无此人");
		}
		m.searchMenu();
	}

	// 按性别查找
	public void searchBySex() throws IOException {
		System.out.println("请输入要查找的性别:");
		String sex = in.nextLine();
		sex = TelNoteRegex.sexRegex(sex);
		Iterator<Person> it = list.iterator();
		boolean flag = false;
		while (it.hasNext()) {
			Person person = it.next();
			if (person.getSex().equals(sex)) {
				System.out.println(person);
				flag = true;
			}
		}
		if (!flag) {
			System.out.println("查无此人");
		}
		m.searchMenu();
	}

	// 按地址查找
	public void searchByAdd() throws IOException {
		System.out.println("请输入要查找的地址:");
		String address = in.nextLine();
		address = TelNoteRegex.addressRegex(address);
		Iterator<Person> it = list.iterator();
		boolean flag = false;
		while (it.hasNext()) {
			Person person = it.next();
			if (person.getAddress().equals(address)) {
				System.out.println(person);
				flag = true;
			}
		}
		if (!flag) {
			System.out.println("查无此人");
		}
		m.searchMenu();
	}

	// 查看所有记录,用Iterator对集合中的元素进行遍历
	public void showAll() throws IOException {
		Iterator<Person> it = list.iterator();
		int id = 1;
		while (it.hasNext()) {
			Person person = it.next();
			person.setId(id++);
			System.out.println(" 序号:" + person.getId() + "#" + person);
		}
		
	}

	// 按用户姓名排序信息
	public void orderName() {
		Collections.sort(list, new NameComparator());
		System.out.println("排序成功!");
	}

	// 按用户年龄排序信息
	public void orderAge() {
		Collections.sort(list, new AgeComparator());
		System.out.println("排序成功!");
	}

	// 按用户性别排序信息
	public void orderSex() {
		Collections.sort(list, new SexComparator());
		System.out.println("排序成功!");
	}

}

// 自定义姓名排序类
class NameComparator implements Comparator<Person> {
	public int compare(Person p1, Person p2) {
		return p1.getName().compareTo(p2.getName());
	}
}

// 自定义性别排序类
class SexComparator implements Comparator<Person> {
	public int compare(Person p1, Person p2) {
		return p1.getSex().compareTo(p2.getSex());
	}
}

// 自定义年龄排序类
class AgeComparator implements Comparator<Person> {
	public int compare(Person p1, Person p2) {
		return Integer.valueOf(p1.getAge()).compareTo(Integer.valueOf(p2.getAge()));
	}
}
package hello;

public class Person {
	Operate o = new Operate();
	private int id;// 用户ID 属性
	private String name; // 用户姓名属性
	private int age; // 用户年龄属性
	private String sex; // 用户性别属性
	private String telNum; // 用户电话号码属性
	private String address; // 用户地址属性

	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Person(int id, String name, int age, String sex, String telNum, String address) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.telNum = telNum;
		this.address = address;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getTelNum() {
		return telNum;
	}

	public void setTelNum(String telNum) {
		this.telNum = telNum;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	public String toString() {
		return " 姓名 :" + name + "\t" + " 年龄 :" + age + "\t" + " 性别 :" + sex + "\t" + " 电话号码:" + telNum + "\t" + " 住址:"
				+ address;
	}
}

package hello;

import java.util.Scanner;

public class TelNoteRegex {
	static Scanner in = new Scanner(System.in);

	public static String nameRegex(String name) {
		
		while (true) {
			if (!name.matches("[a-zA-Z]{1,10}")) {
				System.out.println("您的输入操作不正确!请重新输入");
				name = in.nextLine();
			} else {
				break;
			}
		}
		return name;
	}

	public static int ageRegex(int age) {
		while (true) {
			if (age < 1 || age > 199) {
				System.out.println("您的输入操作不正确!请重新输入");
				age = Integer.parseInt(in.nextLine());
			} else {
				break;
			}
		}
		return age;
	}

	public static String sexRegex(String sex) {
		while (true) {
			if (!sex.matches("[FMfm]")) {
				System.out.println("您的输入操作不正确!请重新输入");
				sex = in.nextLine();
			} else {
				break;
			}
		}
		return sex;
	}

	public static String telNumRegex(String telNum) {// 用户添加信息业务逻辑控制
		while (true) {
			if (!telNum.matches("[0-9]{6,11}")) {
				System.out.println("您的输入操作不正确!请重新输入");
				telNum = in.nextLine();
			} else {
				break;
			}
		}
		return telNum;
	}

	public static String addressRegex(String address) {
		while (true) {
			if (!address.matches("[0-9A-Za-z]{1,50}")) {
				System.out.println("您的输入操作不正确!请重新输入");
				address = in.nextLine();
			} else {
				break;
			}
		}
		return address;
	}
}

发布了57 篇原创文章 · 获赞 69 · 访问量 6290

猜你喜欢

转载自blog.csdn.net/zp1455604302/article/details/103819448