*这是运行包*
package Application;
import fangfabao.Menu;
public class App {
public static void main(String[] args) {
Menu menu=new Menu();
menu.mainMenu();//调用主菜单
}
}
*这是方法包*
*Menu类(存储菜单)*
package fangfabao;
import java.util.Scanner;
//一级菜单
/*1:添加记录
2:查找记录
3:修改记录
4:删除记录
5:排序记录
6:退出系统
*/
public class Menu {
Operate o=new Operate();
public void mainMenu() {
//这是主菜单
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.退出系统 **");
o.mainLogic();
}
// 二级菜单添加记录子菜单
/*1:添加新记录
2:查看全纪录
3:返回上一级
*/
public void addUserMenu(){
System.out.println("**********************");
System.out.println("** 1.添加新记录 **");
System.out.println("** 2.查看全纪录 **");
System.out.println("** 3.返回上一级 **");
System.out.println("** 请输入数字1-3 **");
}
// 二级查找记录子菜单
/*1:按姓名查找
2:按年龄查找
3:按性别查找
4:按号码查找
5:按住址查找
6:查看全记录
7:返回上一级
*/
public void searchMenu(){
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("** 请输入数字1-7 **");
}
// 二级修改记录子菜单
/*1:查看全纪录
2:修改指定记录
3:返回上一级
*/
public void modifyMenu(){
System.out.println("**********************");
System.out.println("** 1.查看全纪录 **");
System.out.println("** 2.修改指定记录 **");
System.out.println("** 3.返回上一级 **");
System.out.println("** 请输入数字1-3 **");
}
// 二级删除记录子菜单
/*1:查看全记录
2:修改指定记录
3:返回上一级
*/
public void deleteMenu(){
System.out.println("*******************************");
System.out.println("** 1.查看全纪录 **");
System.out.println("** 2.删除指定记录 **");
System.out.println("** 3.删除全纪录 **");
System.out.println("** 4.返回上一级 **");
System.out.println("** 请输入数字1-4 **");
}
// 二级排序记录子菜单
/*1:按姓名排序
2:按年龄排序
3:按性别排序
4:查找全纪录
5:返回上一级
*/
public void orderMenu(){
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("** 请输入数字1-5 **");
//调用下一级菜单
//o.
}
// 三级修改指定记录子菜单
public void sumodifyMenu(){
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("** 请输入数字1-6 **");
}
}
*Person类(存储用户属性)*
package fangfabao;
public class Person {
private int id;//用户ID
private String name;//用户姓名
private int age;//用户年龄
private String sex;//用户性别
private String telNum;//用户电话号码属性
private String address;//用户地址属性
public Person(){
super();
//System.out.println("无参的调用方法");
}
public Person(int id, String name, int age, String sex, String telNum, String address) {
super();
//System.out.println("有参的调用方法");
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;
}
//需重写
@Override
public String toString() {
return "序号 +getID+# 姓名 +getName()+ 年龄 +getAge()+ 性别 +getSex()+ 电话号码 +getTelNum()+ 住址 +getAddress()";
}
}
*Operate类(各种操作集合)*
package fangfabao;
import java.util.*;
import org.omg.Messaging.SyncScopeHelper;
public class Operate {
static TelNoteRegex regex=new TelNoteRegex();
static Vector<Person> persons=new Vector<Person>();
static Scanner reader=new Scanner(System.in);
static Menu menu=new Menu();//创建显示页面对象
static int id=0;
public static void mainLogic(){
while(true){
int close=reader.nextInt();
switch(close){
case 1:
addLogic();
break;
case 2:
searchLogic();
break;
case 3:
modifyLogic();
break;
case 4:
deleteLogic();
break;
case 5:
orderLogic();
break;
case 6:
System.exit(0);
default:
System.out.println("你输入的数字不合法,请重新输入");
break;
}
}
}
/*用户添加信息业务逻辑控制*/
public static void addLogic() {
//进入添加用户选择菜单界面
menu.addUserMenu();
int search=reader.nextInt();
switch(search){
case 1:
addUser();
break;
case 2:
showAll();
addLogic();
break;
case 3:
new Menu().mainMenu();
default:
System.out.println("输入数字不合法请重新输入!");
addLogic();
}
}
public static void addUser() {
/* 添加用户方法*/
//添加姓名
while(true){
Person person=new Person();
person.setId(id+1);
System.out.println("请输入姓名(1-10个字母)");
String name=reader.next();
//判断姓名是否合法TelNoteRegex()
person.setName(regex.nameRegex(name));
//添加年龄
System.out.println("请输入年龄(数字0-138)");
int age=reader.nextInt();
//判断年龄是否合法
person.setAge(regex.ageRegex(age));
//添加性别
System.out.println("请输入性别(男 m or M)(女 f or F)");
String sex=reader.next();
//判断性别是否合法
person.setSex(regex.sexRegex(sex));
//添加电话号码
System.out.println("请输入电话号码(6-10个数字)");
String telNum=reader.next();
//判断电话号码是否合法
person.setTelNum(regex.telNumRegex(telNum));
System.out.println("请输入:1-50位字母或数字");
String address=reader.next();
//判断地址是否合法
person.setAddress(regex.addressRegex(address));
persons.add(person);
System.out.println("是否需要再次添加:是:1 OR 否:0");
if(reader.nextInt()==1){
addUser();
}else{
break;
}
}
addLogic();
}
/*遍历全记录*/
public static void showAll(){
int id=1;
Iterator<Person> it=persons.iterator();
while(it.hasNext()){
Person User=it.next();
User.setId(id++);
System.out.println("序号:"+User.getId()+"#"+" 姓名:"+User.getName()+" 年龄:"+User.getAge()+" 性别:"+User.getSex()+" 电话号码:"+User.getTelNum()+" 住址:"+User.getAddress());
}
}
/*查询逻辑控制*/
public static void searchLogic(){
menu.searchMenu();
int search=reader.nextInt();
switch(search){
case 1:
searchByName();
break;
case 2:
searchByAge();
break;
case 3:
searchBySex();
break;
case 4:
searchTelNum();
break;
case 5:
searchByAddress();
break;
case 6:
showAll();
searchLogic();
break;
case 7:
new Menu().mainMenu();
break;
default:
System.out.println("你输入数字不合规范,请重新输入!");
searchLogic();
}
}
//按姓名查询
public static void searchByName(){
System.out.println("请输入查询的姓名:");
String name=reader.next();
Iterator<Person> it=persons.iterator();
while(it.hasNext()){
Person User=it.next();
if(User.getName().equals(name)){
System.out.println("序号:"+User.getId()+"#"+" 姓名:"+User.getName()+" 年龄:"+User.getAge()+" 性别:"+User.getSex()+" 电话号码:"+User.getTelNum()+" 住址:"+User.getAddress());
}else{
continue;
}
}
searchLogic();
}
//按年龄查询
public static void searchByAge(){
System.out.println("请输入查询的年龄:");
int Age=reader.nextInt();
Iterator<Person> it=persons.iterator();
while(it.hasNext()){
Person User=it.next();
if(User.getAge()==Age){
System.out.println("序号:"+User.getId()+"#"+" 姓名:"+User.getName()+" 年龄:"+User.getAge()+" 性别:"+User.getSex()+" 电话号码:"+User.getTelNum()+" 住址:"+User.getAddress());
}else{
continue;
}
}
searchLogic();
}
//按性别查询
public static void searchBySex(){
System.out.println("请输入查询的性别:");
String Sex=reader.next();
Iterator<Person> it=persons.iterator();
while(it.hasNext()){
Person User=it.next();
if(User.getSex().equals(Sex)){
System.out.println("序号:"+User.getId()+"#"+" 姓名:"+User.getName()+" 年龄:"+User.getAge()+" 性别:"+User.getSex()+" 电话号码:"+User.getTelNum()+" 住址:"+User.getAddress());
}else{
continue;
}
}
searchLogic();
}
//按电话号码查询
public static void searchTelNum(){
System.out.println("请输入查询的电话号码:");
String TelNum=reader.next();
Iterator<Person> it=persons.iterator();
while(it.hasNext()){
Person User=it.next();
if(User.getTelNum().equals(TelNum)){
System.out.println("序号:"+User.getId()+"#"+" 姓名:"+User.getName()+" 年龄:"+User.getAge()+" 性别:"+User.getSex()+" 电话号码:"+User.getTelNum()+" 住址:"+User.getAddress());
}else{
continue;
}
}
searchLogic();
}
//按地址查询用户
public static void searchByAddress(){
System.out.println("请输入查询的地址:");
String Address=reader.next();
Iterator<Person> it=persons.iterator();
while(it.hasNext()){
Person User=it.next();
if(User.getAddress().equals(Address)){
System.out.println("序号:"+User.getId()+"#"+" 姓名:"+User.getName()+" 年龄:"+User.getAge()+" 性别:"+User.getSex()+" 电话号码:"+User.getTelNum()+" 住址:"+User.getAddress());
}else{
continue;
}
}
searchLogic();
}
/*修改逻辑控制*/
public static void modifyLogic(){
menu.modifyMenu();
int search=reader.nextInt();
switch(search){
case 1:
showAll();
modifyLogic();
case 2:
sumodifyLogic();
break;
case 3:
new Menu().mainMenu();
default:
System.out.println("你输入数字不合规范,请重新输入!");
modifyLogic();
}
}
public static void sumodifyLogic(){
menu.sumodifyMenu();
System.out.println("请输入你要修改的类型:");
int search=reader.nextInt();
switch(search){
case 1:
modifyByName();
break;
case 2:
modifyByAge();
break;
case 3:
modifyBySex();
break;
case 4:
modifyByTelNum();
break;
case 5:
modifyByAddress();
break;
case 6:
modifyLogic();
break;
default:
System.out.println("你输入的数字不合法,请重新输入!");
sumodifyLogic();
}
}
public static void modifyByName(){
System.out.println("请输入你需要修改人员的ID");
int ID=reader.nextInt();
System.out.println("请输入修改后的姓名:");
String afename=reader.next();
persons.get(ID-1).setName(afename);
System.out.println("修改成功,正在返回······");
sumodifyLogic();
}
public static void modifyByAge(){
System.out.println("请输入你需要修改人员的ID");
int ID=reader.nextInt();
System.out.println("请输入修改后的年龄:");
int afeAge=reader.nextInt();
persons.get(ID-1).setAge(afeAge);
System.out.println("修改成功,正在返回······");
sumodifyLogic();
}
public static void modifyBySex(){
System.out.println("请输入你需要修改人员的ID");
int ID=reader.nextInt();
System.out.println("请输入修改后的性别:");
String afesex=reader.next();
persons.get(ID-1).setName(afesex);
System.out.println("修改成功,正在返回······");
sumodifyLogic();
}
public static void modifyByTelNum(){
System.out.println("请输入你需要修改人员的ID");
int ID=reader.nextInt();
System.out.println("请输入修改后的电话号码:");
String afetelnum=reader.next();
persons.get(ID-1).setName(afetelnum);
System.out.println("修改成功,正在返回······");
sumodifyLogic();
}
public static void modifyByAddress(){
System.out.println("请输入你需要修改人员的ID");
int ID=reader.nextInt();
System.out.println("请输入修改后的地址:");
String afeaddress=reader.next();
persons.get(ID-1).setName(afeaddress);
System.out.println("修改成功,正在返回······");
sumodifyLogic();
}
public static void deleteLogic(){
menu.deleteMenu();
int search=reader.nextInt();
switch(search){
case 1:
showAll();
deleteLogic();
break;
case 2:
sudelete();
break;
case 3:
deleteAll();
break;
case 4:
new Menu().mainMenu();
default:
System.out.println("你输入的数字不合法,请重新输入!");
deleteLogic();
}
}
public static void sudelete(){
System.out.println("输入你需要删除的用户ID");
int ID=reader.nextInt();
int count=0;
Iterator<Person> it=persons.iterator();
while(it.hasNext()){
Person User=it.next();
if((User.getId())==ID){
count=count+1;
persons.remove(User);
System.out.println("删除成功!");
System.out.println("是否需要再次删除\n是:Y 否:N(字母大小写都可以)");
String close=reader.next();
if(close=="Y"||close=="y"){
sudelete();
}else{
deleteLogic();
}
}
}
if(count==0){
System.out.println("没有要删除的用户,请输入正确的ID");
deleteLogic();
}
}
public static void deleteAll(){
persons.removeAll(persons);
deleteLogic();
}
public static void orderLogic(){
menu.orderMenu();
System.out.println("请输入你选择的功能");
int search=reader.nextInt();
switch(search){
case 1:
orderName();
break;
case 2:
orderAge();
break;
case 3:
orderSex();
break;
case 4:
showAll();
break;
case 5:
new Menu().mainMenu();
default:
System.out.println("你输入的数字不合法,请重新输入");
break;
}
}
public static void orderName(){
Collections.sort(persons,new NameComparator());
System.out.println("已按照姓名进行排序,下面是排序结果");
showAll();
System.out.println("准备返回······");
orderLogic();
}
public static void orderAge(){
Collections.sort(persons,new AgeComparator());
System.out.println("已按照年龄进行排序,下面是排序结果");
showAll();
System.out.println("准备返回······");
orderLogic();
}
public static void orderSex(){
Collections.sort(persons,new SexComparator());
System.out.println("已按照性别进行排序,下面是排序结果");
showAll();
System.out.println("准备返回······");
orderLogic();
}
}
class NameComparator implements Comparator<Person>{
public int compare(Person p1,Person p2){
return p1.getName().compareTo(p2.getName());
}
}
class AgeComparator implements Comparator<Person>{
public int compare(Person p1,Person p2){
return Integer.valueOf(p1.getAge()).compareTo(Integer.valueOf(p2.getAge()));
}
}
class SexComparator implements Comparator<Person>{
public int compare(Person p1,Person p2){
return p1.getSex().compareTo(p2.getSex());
}
}
*TelNoteRegex(判断输入是否合格)*
package fangfabao;
import java.util.Scanner;
public class TelNoteRegex {
// 菜单输入选项的验证
Scanner reader=new Scanner(System.in);
// 用户输入姓名的验证
public String nameRegex(String name) {
int count=0;
while(true){
if(count<5){
if((name.length()<1)||(name.length()>10)){
System.out.println("你的输入不合法,请重新输入");
name=reader.next();
count++;
}else{
return name;
}
}else{
System.out.println("因为你的操作不规范,将退出系统");
System.exit(0);
}
}
}
// 用户输入年龄的验证
public int ageRegex(int age){
int count=1;
while(true){
if(count<5){
if(age<0||age>138){
System.out.println("你的输入不合法,请重新输入");
age=reader.nextInt();
count++;
}else{
return age;
}
}else{
System.out.println("因为你的操作不规范,将退出系统");
System.exit(0);
}
}
}
// 用户输入性别的验证
public String sexRegex(String sex){
int count=1;
while(true){
if(count<5){
if(sex.equals("m")||sex.equals("M")||sex.equals("f")||sex.equals("F")){
return sex;
}else{
count++;
System.out.println("你的输入不合法,请重新输入");
sex=reader.next();
}
}else{
System.out.println("因为你的操作不规范,将退出系统");
System.exit(0);
}
}
}
// 用户输入电话号码的验证
public String telNumRegex(String telNum){
int count=1;
while(true){
if(count<5){
if((telNum.length()>=6)&&(telNum.length()<=10)){
return telNum;
}else{
count++;
System.out.println("你的输入不合法,请重新输入");
telNum=reader.next();
}
}else{
System.out.println("因为你的操作不规范,将退出系统");
System.exit(0);
}
}
}
// 用户输入地址的验证
public String addressRegex(String address){
int count=1;
while(true){
if(count<5){
if(address.length()>=1&&address.length()<=50){
return address;
}else{
count++;
System.out.println("你的输入不合法,请重新输入");
address=reader.next();
}
}else{
System.out.println("因为你的操作不规范,将退出系统");
System.exit(0);
}
}
}
}