欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

电话薄程序期末大作业

程序员文章站 2022-03-07 15:11:54
...

项目:电话薄程序
5个类文件:
App 对应 Application(程序入口方法):主类,创建Menu类的对象,调用Menu类的方法
Menu 对应 Menu (菜单类):定义多个方法,显示各种操作对应的菜单,并调用Operate类中的方法实现菜单中的内容
Operate 对应 Operate(业务处理类):定义每一个具体操作的方法,
Person 对应 Person (实体类):定义属性和方法
TelNoteRegex 对应 TelNoteRegex(用户输入验证类)用于对用户输入的姓名,年龄等属性进行验证。

流程图

电话薄程序期末大作业

package iponeApp;

public class APP {//程序入口
	public static void main(String[] args) {
		Menu m=new Menu();
		m.mainMenu();
	}
	
}

package iponeApp;


import java.util.Scanner;

public class Menu {//选项类
	Scanner in=new Scanner(System.in);
	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:退出系统                            ******");
	System.out.println();
	String selec=in.nextLine();
	while(true){
		if(selec.equals("1")||selec.equals("2")||selec.equals("3")||selec.equals("4")||selec.equals("5")||selec.equals("6")){
			switch (selec) {
			case "1":
				addmenu();
				break;
			case "2":
				search();		
				break;
			case "3":
				modifyLogicLogic();
				break;
			case "4":
				deleteMenu();
				break;
			case "5":
				orderMenu();
				break;
			case "6":
				System.out.println("再见!");
				System.exit(0);
				break;
			default:
				break;
			}
			break;
		}
		else{
			System.out.println("输入非法,请重新输入");
			selec=in.nextLine();
		}
	}//while循环结束
	
}


//选项1中的二级菜单
public void addmenu() {
	System.out.println("******1:添加新记录******");
	System.out.println("******2:查看全记录******");
	System.out.println("******3:返回上一级******");
	System.out.println("输入你的选择");
	o.addlogic();
}
//选项2中的二级菜单
public void search() {
	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:返回上一级******");
	o.searchlogic();
}
//选项3中的二级菜单
public void modifyLogicLogic() {
	System.out.println("***   1:查看全部记录     ***");
	System.out.println("***   2:修改指定记录     ***");
	System.out.println("***   3:返回上一级     ***");
	subModifyMenu ();
	
	
}
//选项3中的二级菜单的子菜单
private void subModifyMenu() {
	System.out.println("请输入选择");
	int a=in.nextInt();
	switch (a) {
	case 1:
		o.showAll(); 
		break;
	case 2:
			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.xiugai();
		break;
	case 3:
		mainMenu();
		break;
	default:
		break;
	}
}

//选项4中的二级菜单
public void deleteMenu() {
	System.out.println("***   1:查看全部记录     ***");
	System.out.println("***   2:删除指定记录     ***");
	System.out.println("***   3:删除所有记录     ***");
	System.out.println("***   4:返回上一级     ***");
	o.deleteLogic();
}
//选项5中的二级菜单
private void orderMenu() {
	System.out.println("***   1:按姓名排序        ***");
	System.out.println("***   2:按年龄排序        ***");
	System.out.println("***   3:按性别排序        ***");
	System.out.println("***   4:查看全记录        ***");
	System.out.println("***   5:返回上一级        ***");
	o.subModifyMenu();
}
}

package iponeApp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class Operate  {
	int count=0;
	//必须要加静态;
	static ArrayList<Person> list=new ArrayList<Person>();
	Scanner sc=new Scanner(System.in);
	static Menu m=new Menu();
	TelNoteRegex tt=new TelNoteRegex();
	//添加
	public void addlogic() {
		int x=sc.nextInt();
		switch (x) {
		case 1:
			addnew();//添加新用户
			break;
		case 2:
			showAll();//显示全部信息	
			break;
		case 3:
			m.mainMenu();
			break;
		default:
			break;
		}
	}
	//添加的具体实现+++++++++++++++++++++++++++++++++++++++++++
	public void addnew() {
		System.out.println("请输入姓名");
		String name=sc.next();
		name=tt.nameRegex(name);//正则表达式判断
		System.out.println("请输入年龄");
		String age=sc.next();
		age=tt.age(age);
		System.out.println("请输入性别    男m   女f");
		String sex=sc.next();
		sex=tt.sexRegex(sex);
		System.out.println("请输入号码");
		String telNum=sc.next();
		telNum=tt.telRegex(telNum);
		System.out.println("请输入住址");
		String address=sc.next();
		address=tt.addressRegex(address);
		count++;
		Person p=new Person(count, name, age, sex,telNum, address);
		list.add(p);
		System.out.println("添加成功");
		m.addmenu();
	}
//查找的具体实现++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public void searchlogic() {
		int search=sc.nextInt();
		switch (search) {
		case 1:
			searchByName();
			break;
		case 2:
			searchByAge();	
			break;
		case 3:
			searchBySex();
			break;
		case 4:
			searchByTelNum();
			break;
		case 5:
			searchByAdd();
			break;
		case 6:
			showAll();
			break;
		case 7:
			m.mainMenu();
			break;
		default:
			break;
		}
		
	}
	//按地址查找
	public void searchByAdd() {
		System.out.println("请输入地址");
		String sadd=sc.next();
		int flag=1;
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(sadd.equals(p1.getAddress())) {
				System.out.println("id: "+p1.getId()+"\t 姓名: "+p1.getName()+"\t 性别:"+p1.getSex()+"\t 年龄:"+p1.getAge()+"\t 手机号:"+p1.getTelNum());
				 flag=0;
			}
			
		}
		if(flag==1) {
			System.out.println("你的输入有误,不存在");
		}
		m.search();//显示菜单
	}
	//按手机号查找
	public void searchByTelNum() {
		System.out.println("请输入手机号");
		String stel=sc.next();
		int flag=1;
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(stel.equals(p1.getTelNum())) {
				System.out.println("id: "+p1.getId()+"\t 姓名: "+p1.getName()+"\t 性别:"+p1.getSex()+"\t 年龄:"+p1.getAge()+"\t 手机号:"+p1.getTelNum());
				 flag=0;
			}
			
		}
		if(flag==1) {
			System.out.println("你的输入有误,不存在");
		}
		m.search();//显示菜单
	}

	//按性别查找
	public void searchBySex() {
		System.out.println("请输入性别");
		String ssex=sc.next();
		int flag=1;
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(ssex.equals(p1.getSex())) {
				System.out.println("id: "+p1.getId()+"\t 姓名: "+p1.getName()+"\t 性别:"+p1.getSex()+"\t 年龄:"+p1.getAge()+"\t 手机号:"+p1.getTelNum());
				 flag=0;
			}
			
		}
		if(flag==1) {
			System.out.println("你的输入有误,不存在");
		}
		m.search();//显示菜单
		
	}

	//按年龄查找
	public void searchByAge() {
		System.out.println("请输入年龄");
		String sage=sc.next();
		int flag=1;
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(sage.equals(p1.getAge())) {
				System.out.println("id: "+p1.getId()+"\t 姓名: "+p1.getName()+"\t 性别:"+p1.getSex()+"\t 年龄:"+p1.getAge()+"\t 手机号:"+p1.getTelNum());
				 flag=0;
			}
			
		}
		if(flag==1) {
			System.out.println("你的输入有误,不存在");
		}
		m.search();//显示菜单
	}
	//按姓名查找
	public void searchByName() {
		System.out.println("请输入查找姓名");
		String sname=sc.next();
		int flag=1;
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(sname.equals(p1.getName())) {
				System.out.println("id: "+p1.getId()+"\t 姓名: "+p1.getName()+"\t 性别:"+p1.getSex()+"\t 年龄:"+p1.getAge()+"\t 手机号:"+p1.getTelNum());
				 flag=0;
			}
		}
		if(flag==1) {
			System.out.println("你的输入有误,不存在");
		}
		m.search();//显示菜单
	}

//修改中的具体修改步骤+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		public void xiugai() {
			int O_xiugai=sc.nextInt();
			switch (O_xiugai) {
			case 1:
				name_xiugai();
				break;
			case 2:
				sex_xiugai();	
				break;
			case 3:
				age_xiugai();
				break;
			case 4:
				tel_xiugai();
				break;
			case 5:
				add_xiugai();
				break;
			case 6:
				m.modifyLogicLogic();
				break;
			default:
				break;
			}
		}
		//修改住址
	public void add_xiugai() {
			System.out.println("请输入ID");
			int xid=sc.nextInt();
			System.out.println("输入修改后的住址");
			String xsex2=sc.next();
			int flag=1;
			Iterator<Person> it=list.iterator();
			while (it.hasNext()) {
				Person p1 =  it.next();
				if(xid==Integer.valueOf(p1.getId())) {
					p1.setAddress(xsex2);;
					 flag=0;
				}
			}
			if(flag==1) {
				System.out.println("你的输入有误,不存在");
			}
			m.modifyLogicLogic();//显示菜单
		
	}
		//修改手机号
	public void tel_xiugai() {
		System.out.println("请输入ID");
		int xid=sc.nextInt();
		System.out.println("输入修改后的手机号");
		String xsex2=sc.next();
		int flag=1;
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(xid==Integer.valueOf(p1.getId())) {
				p1.setTelNum(xsex2);;
				 flag=0;
			}
		}
		if(flag==1) {
			System.out.println("你的输入有误,不存在");
		}
		m.modifyLogicLogic();//显示菜单
		
	}
		//按年龄修改
	public void age_xiugai() {
		System.out.println("请输入ID");
		int xid=sc.nextInt();
		System.out.println("输入修改后的年龄");
		String xsex2=sc.next();
		int flag=1;
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(xid==Integer.valueOf(p1.getId())) {
				p1.setAge(xsex2);
				 flag=0;
			}
		}
		if(flag==1) {
			System.out.println("你的输入有误,不存在");
		}
		m.modifyLogicLogic();//显示菜单
	
	}
		//按性别修改
	public void sex_xiugai() {
		System.out.println("请输入ID");
		int xid=sc.nextInt();
		System.out.println("输入修改后的性别");
		String xsex2=sc.next();
		int flag=1;
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(xid==Integer.valueOf(p1.getId())) {
				p1.setSex(xsex2);;
				 flag=0;
			}
		}
		if(flag==1) {
			System.out.println("你的输入有误,不存在");
		}
		m.modifyLogicLogic();//显示菜单
		
	}
		//按姓名修改
	public void name_xiugai() {
		System.out.println("请输入姓名");
		String xname1=sc.next();
		System.out.println("继续输入修改后的姓名");
		String xname2=sc.next();
		int flag=1;
		tt.nameRegex(xname2);//对用户名再次判断
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(xname1.equals(p1.getName())) {
				p1.setName(xname2);
				 flag=0;
			}
		}
		if(flag==1) {
			System.out.println("你的输入有误,不存在");
		}
		m.modifyLogicLogic();//显示菜单
		
	}
	//删除信息具体实现+++++++++++++++++++++++++++++++++++++++++++++++
	public void deleteLogic() {
		System.out.println("输入你的选择");
		int del=sc.nextInt();
		switch (del) {
		case 1:
			showAll();
			break;
		case 2:
			rm_zhiding();	
			break;
		case 3:
			delAll();
			break;
		case 4:
			m.mainMenu();
			break;
		default:
			break;
		}
		
	}
	public void rm_zhiding() {
		System.out.println("输入需要删除的序号");
		int idd=sc.nextInt();
		Iterator<Person> it=list.iterator();
		while (it.hasNext()) {
			Person p1 =  it.next();
			if(idd==p1.getId()) {
				it.remove();
				System.out.println("删除成功");
			}
		}
		m.deleteMenu();
		
	}
	//删除所有信息
	public void delAll() {
		list.removeAll(list);
		System.out.println("已全部清空");
		m.deleteMenu();
	}
//排序操作++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public void subModifyMenu() {
		System.out.println("输入你的选择");
		int paixu =sc.nextInt();
		switch (paixu) {
		case 1:
			orderName();
			break;
		case 2:
			orderAge();		
			break;
		case 3:
			orderSex();
			break;
		case 4:
			showAll();
			break;
		case 5:
			m.mainMenu();
			break;
		default:
			break;
		}
		
	}
	//按姓名排序
	public void orderName() {
		Collections.sort(list,new NameComparator());
		System.out.println("排序完成");
		subModifyMenu();
		//用到自定义比较器,传入一个比较器
	}
	//按年龄排序
	public void orderAge() {
		Collections.sort(list,new AgeComparator());
		System.out.println("排序完成");
		subModifyMenu();
		
	}
	//按性别排序
	public void orderSex() {
		Collections.sort(list,new SexComparator());
		System.out.println("排序完成");
		subModifyMenu();
	}
//自定义姓名排序类
class NameComparator implements Comparator<Person>{

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

	public int compare(Person o1, Person o2) {
		
		return o1.getSex().compareTo(o2.getSex());
	}
}
class AgeComparator implements Comparator<Person>{

	public int compare(Person o1, Person o2) {
		return o1.getAge().compareTo(o2.getAge());
	}
	
}

//显示全部信息具体实现+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public void showAll() {
		Iterator<Person> it=list.iterator();
		int id=1;
		while (it.hasNext()) {
			Person p1 = (Person) it.next();
			p1.setId(id);
			id++;
			System.out.println("id: "+p1.getId()+"\t姓名: "+p1.getName()+"\t性别:"+p1.getSex()+"\t年龄:"+p1.getAge()+"\t手机号:"+p1.getTelNum()+"\t住址"+p1.getAddress());
		}
		m.addmenu();
	}
	

}

package iponeApp;

public class Person {//实体类,数据
	
	private int id; //用户ID 属性
	private String name; //用户姓名属性
	private String age; //用户年龄属性
	private String sex; //用户性别属性
	private String telNum; //用户电话号码属性
	private String address; //用户地址属性
	Person(){};// 无参数构造方法
	//有参构造方法
	public Person(int id, String name, String age, String sex, String telNum, String address) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		if(sex.equals("m")) {
			this.sex = "男";
		}
		else if(sex.equals("f")) {
			this.sex = "女";
		}
		else {
			this.sex = sex;
		}
//		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 String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		if(sex.equals("m")) {
			this.sex = "男";
		}
		else if(sex.equals("f")) {
			this.sex = "女";
		}
		else {
			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 "id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", telNum=" + telNum
				+ ", address=" + address;
	}

}


package iponeApp;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TelNoteRegex {
	Scanner sc = new Scanner(System.in);
	//对用户名进行判断
	
	
	
	public String nameRegex(String Name) {
		while(!checkName(Name)) {
			  System.out.println("用户名不合法,请重新输入:");
			  Name=sc.next();
			 }
//		必须是6-10位字母、数字、下划线(这里字母、数字、下划线是指任意组合,没有必须三类均包含)
//		不能以数字开头
		return Name;
		  
	}
	public  boolean checkName(String name) {
//		 String regExp = "^[^0-9][\\w_]{5,9}$";
		 String regExp = "^(([\u4e00-\u9fa5]{2,8})|([a-zA-Z]{2,16}))$";
//		 String reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
		 if(name.matches(regExp)) {
		  return true;
		 }else {
		  return false;
		 }
		 }
	
	//对用户性别判断
	public String sexRegex(String sex) {
		while(true){
			if(!(sex.equals("m")||sex.equals("f")||sex.equals("男")||sex.equals("女"))){
				System.out.println("输入有误,请重新输入");
				sex=sc.next();
			}
			else{
				break;
			}
		}
		return sex;
		
	}
	public String age(String age) {
		while(true){
			if(Integer.valueOf(age)<1||Integer.valueOf(age)>138){
				System.out.println("输入有误,请重新输入");
				age=sc.next();
			}
			else{
				break;
			}
		}
		return age;
		
	}
	public String telRegex(String telNum) {
		while(true){
			if(isMobile(telNum)){
			      System.out.println(telNum+"是符合的手机号码");
			      break;
			    }else {
				  System.out.println(telNum+"不符合,请重新输入!");
				  telNum=sc.next();
			    }
			  }
		return telNum;
	}

	  
	  public static boolean isMobile(String str) {
		  Pattern p = null;
	      Matcher m = null;
	      boolean isMatch = false;
		  // 制定验证条件,目前有些新卡会出现类似166开头的,需要适当调整
	      String regex1 = "^[1][3,4,5,7,8][0-9]{9}$";
	      String regex2 = "^((13[0-9])|(14[579])|(15([0-3,5-9]))|(16[6])|(17[0135678])|(18[0-9]|19[89]))\\d{8}$";
	     
	      p = Pattern.compile(regex2);
	      m = p.matcher(str);
	      isMatch = m.matches();
	      return isMatch;
	  }
	public String addressRegex(String address) {
		while(true){
			if(address.length()>3&&address.length()<30){
				break;
			}
			else{
				System.out.println("地址有误,请重新输入");
				address=sc.next();
			}
		}
		return address;
		
	}
	  
	  
}
	
相关标签: 编程