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;publicclassOperate{
Scanner in=newScanner(System.in);static Menu m =newMenu();static Person pp =newPerson();static ArrayList<Person> list =newArrayList<Person>();// 用户添加信息业务逻辑控制publicvoidaddLogic() throws IOException {while(true){
int way =0;try{
way = Integer.parseInt(in.nextLine().trim());}catch(Exception e){
System.out.println("你输入的数据类型不对,请重新输入");continue;}switch(way){case1:addOperation();break;case2:showAll();
m.addMenu();break;case3:
m.mainMenu();break;default:
System.out.println("输入错误,请重新输入");break;}}}// 用户查询信息业务逻辑控制publicvoidsearchLogic() throws IOException {while(true){
int way =0;try{
way = Integer.parseInt(in.nextLine().trim());}catch(Exception e){
System.out.println("你输入的数据类型不对,请重新输入");continue;}switch(way){case1:searchByName();break;case2:searchByAge();break;case3:searchBySex();break;case4:searchByTelNum();break;case5:searchByAdd();break;case6:showAll();
m.searchMenu();break;case7:
m.mainMenu();break;default:
System.out.println("输入错误,请重新输入");break;}}}// 用户修改信息业务逻辑控制publicvoidmodifyLogic() throws IOException {while(true){
int way =0;try{
way = Integer.parseInt(in.nextLine().trim());}catch(Exception e){
System.out.println("你输入的数据类型不对,请重新输入");continue;}switch(way){case1:showAll();
m.modifyMenu();break;case2:
m.subModifyMenu();break;case3:
m.mainMenu();break;default:
System.out.println("输入错误,请重新输入");modifyLogic();break;}}}// 用户删除信息业务逻辑控制publicvoiddeleteLogic() throws IOException {while(true){
int way =0;try{
way = Integer.parseInt(in.nextLine().trim());}catch(Exception e){
System.out.println("你输入的数据类型不对,请重新输入");continue;}switch(way){case1:showAll();
m.deleteMenu();break;case2:delete();break;case3:deleteAll();break;case4:
m.mainMenu();break;default:
System.out.println("输入错误,请重新输入");break;}}}// 用户排序信息业务逻辑控制publicvoidorderLogic() throws IOException {while(true){
int way =0;try{
way = Integer.parseInt(in.nextLine().trim());}catch(Exception e){
System.out.println("你输入的数据类型不对,请重新输入");continue;}switch(way){case1:orderName();
m.orderMenu();break;case2:orderAge();
m.orderMenu();break;case3:orderSex();
m.orderMenu();break;case4:showAll();
m.orderMenu();break;case5:
m.mainMenu();break;default:
System.out.println("输入错误,请重新输入");break;}}}// 添加新记录publicvoidaddOperation() 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 =newPerson();
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();}// 删除指定用户信息publicvoiddelete() 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++;}}// 删除全部用户信息publicvoiddeleteAll() throws IOException {
list.clear();
System.out.println("已清空电话簿");
m.deleteMenu();}// 用户修改信息子功能publicvoidmodify() throws IOException {while(true){
int way =0;try{
way = Integer.parseInt(in.nextLine().trim());}catch(Exception e){
System.out.println("你输入的数据类型不对,请重新输入");continue;}switch(way){case1:name();
m.subModifyMenu();break;case2:age();
m.subModifyMenu();break;case3:sex();
m.subModifyMenu();break;case4:telNum();
m.subModifyMenu();break;case5:address();
m.subModifyMenu();break;case6:
m.modifyMenu();break;}}}// 修改姓名publicvoidname(){
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;}}}// 修改年齡publicvoidage(){
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;}}// 修改性別publicvoidsex(){
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;}}// 修改电话号码publicvoidtelNum(){
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;}}// 修改地址publicvoidaddress(){
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;}}// 按姓名查找publicvoidsearchByName() 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();}// 按年齡查找publicvoidsearchByAge() 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();}// 按电话号码查找publicvoidsearchByTelNum() 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();}// 按性别查找publicvoidsearchBySex() 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();}// 按地址查找publicvoidsearchByAdd() 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对集合中的元素进行遍历publicvoidshowAll() 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);}}// 按用户姓名排序信息publicvoidorderName(){
Collections.sort(list,newNameComparator());
System.out.println("排序成功!");}// 按用户年龄排序信息publicvoidorderAge(){
Collections.sort(list,newAgeComparator());
System.out.println("排序成功!");}// 按用户性别排序信息publicvoidorderSex(){
Collections.sort(list,newSexComparator());
System.out.println("排序成功!");}}// 自定义姓名排序类classNameComparatorimplementsComparator<Person>{public int compare(Person p1, Person p2){return p1.getName().compareTo(p2.getName());}}// 自定义性别排序类classSexComparatorimplementsComparator<Person>{public int compare(Person p1, Person p2){return p1.getSex().compareTo(p2.getSex());}}// 自定义年龄排序类classAgeComparatorimplementsComparator<Person>{public int compare(Person p1, Person p2){return Integer.valueOf(p1.getAge()).compareTo(Integer.valueOf(p2.getAge()));}}