java集合类的学生管理系统

java集合类的学生管理系统

菜单类


import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
/*@auther kxd
 * 
 * 
chapter1为学生信息管理系统,要求完成以下基本任务:
1.改写程序为良好程序风格(文档注释,函数注释,语句注释)。
2.将功能补充完全(基于文件处理,完成刷新和保存功能)。
3.将学生信息改为更好的数据组织,而非离散形式(结构体)。
提高要求:
可将程序中的不足(如数据验证)等根据自己的能力与理解完成.
本人使用 java里的Collection集合类的子类TreeSet来组织数据,java 集合也是目前企业中最实用的类容器 

 * */
public class Demo4 {

	public static void main(String[] args) {
		
		TreeSet<StudentTree> ts = new TreeSet<StudentTree>(new Comparator<StudentTree>(){
			@Override
			public int compare(StudentTree s1, StudentTree s2) {
				//主要条件,按总分
				int i = s2.getSum()-s1.getSum();
				//数学
				int ii = i == 0?s2.getMath()-s2.getMath():i;
				
				return ii;
			}
			
		});
	//public StudentTree(String name, int id, int age, String sex, int cProgram, int english, int math) {
		
		StudentTree st1= new  StudentTree("moom", 1001,20, "男",88, 89, 45);
		StudentTree st2= new  StudentTree("李安", 1002,20, "男",78, 89, 65);
		StudentTree st3= new  StudentTree("潘森", 1003,20, "男",96, 86, 76);
		StudentTree st4= new  StudentTree("卡特", 1004,20, "女",87, 89, 65);
		StudentTree st5= new  StudentTree("林那", 1005,20, "女",69, 89, 82);
		
		ts.add(st1);
		ts.add(st2);
		ts.add(st3);
		ts.add(st4);
		ts.add(st5);
	
		for (StudentTree s:ts) {
			System.out.println(s.toString());
		}
		
	
		boolean boo=true;
		while(boo) {
		System.out.println("******************************************************");
		System.out.println("***************************系统功能菜单************************");
		System.out.println("            0.系统帮助及说明  * *  1.刷新学生信息 ");
		System.out.println("******************************************************");
		System.out.println("            2.查询学生信息    * *  3.修改学生信息");
		System.out.println("******************************************************");
		System.out.println("           4.增加学生信息    * *  5.按学号删除信息");
		System.out.println("******************************************************");
		System.out.println("           6.显示当前信息    * *   7.退出系统");
		System.out.println("******************************************************");
		
		
	/*	  
		printf("请选择菜单编号:");
		scanf("%d",&num);
		switch(num)
		{ 
		case 0:help();break;
		case 2:seek();break;
		case 3:modify();break;
		case 4:insert();break;
	 	case 5:del();break;
	 	case 6:display();break;
		case 8:k=0;break;
		default:printf("请在0-8之间选择\n");
		}
		
		
	}
*/
		System.out.println("请选择菜单编号:");
		Scanner s= new Scanner(System.in);
		int i= s.nextInt();
		switch(i)
		{ 
		case 0:help();break;
		case 1:display(ts);break;
		case 2:seek(ts);break;
		case 3:modify(ts);break;
		case 4:insert(ts);break;
	 	case 5:del(ts);break;
	 	case 6:display(ts);break;
		case 7:System.exit(0);
		default:System.out.println("请在0-8之间选择");
		
		}
		
	}
		
		
		
		
}
	public  static void  help(){//帮助
		System.out.println("欢迎使用系统帮助!");
		System.out.println("按照菜单提示键入数字代号");
		System.out.println("增加修改学生信息后,切记保存");
		System.out.println("谢谢您的使用");

	}
	
	public static void seek( TreeSet<StudentTree> ts) {//查询
		
		System.out.println("按学号查询, 请输入要查询的学生的学号 ");
		System.out.println("x=");
		Scanner s= new Scanner (System.in);
		int  coid=s.nextInt();
		
		for(StudentTree st:ts) {
			if(st.getId()==coid) {
				System.out.println(st.toString());
			}
		}
	}
	public static void modify(TreeSet<StudentTree> ts) {/*3修改信息*/
		System.out.println("按学号查询, 请输入要修改的学生的学号 ");
		System.out.println("x=");
		Scanner s= new Scanner (System.in);
		int  coid=s.nextInt();
		
		for(StudentTree st:ts) {
			if(st.getId()==coid) {
				boolean boo=true;
				while(boo) {
				
				System.out.println("************************************************************************************************");
				System.out.println(" 1修改学号 2修改姓名  3 修改年龄 4 修改性别 5 修改数学6 修改英语   7 修改C语言  8 保存信息 9返回上一级     ");
				System.out.println("************************************************************************************************");
				
				
				System.out.println("请选择菜单编号:");
				Scanner sc= new Scanner(System.in);
				int i= sc.nextInt();
				switch(i)
				{ 
				case 1:{
					System.out.println("请输入修改学号");
				System.out.println("ID=");
				Scanner s1= new Scanner (System.in);
				int  id=s1.nextInt();
				st.setId(id);
				break;
				}
				case 2:{
					System.out.println("   请输入修改修改姓名");
					System.out.println("name=");
					Scanner s1= new Scanner (System.in);
					String name = s1.nextLine();
					st.setName(name);
					break;
				}
				case 3:{
					System.out.println("请输入修改年龄");
					System.out.println("age=");
					Scanner s1= new Scanner (System.in);
					int  id=s1.nextInt();
					st.setAge(id);
					break;
					
				}
				case 4:{
					System.out.println("   请输入修改修改姓别");
					System.out.println("sex=");
					Scanner s1= new Scanner (System.in);
					String sex = s1.nextLine();
					st.setSex(sex);
					break;
				}
			 	case 5:{
			 		System.out.println("请输入修改数学成绩");
					System.out.println("math=");
					Scanner s1= new Scanner (System.in);
					int  id=s1.nextInt();
					st.setMath(id);
					break;
			 		
			 	}
			 	case 6:{
			 		System.out.println("请输入修改英语成绩");
				System.out.println("English=");
				Scanner s1= new Scanner (System.in);
				int  id=s1.nextInt();
				st.setEnglish(id);
				break;
				}
				case 7:{
					System.out.println("请输入修改C语言成绩");
				System.out.println("C语言=");
				Scanner s1= new Scanner (System.in);
				int  id=s1.nextInt();
				st.setcProgram(id);
				break;
				}
				case 8:{
					ts.add(st);
					System.out.println("   保存成功!");
					break;
				}
				case 9:{
					boo=false;
					break;
				}
				
				default:System.out.println("请在1-9之间选择");
				break;
				}
				
				}	
				
				
				
			}
		}
		
	}
	public static void insert(TreeSet<StudentTree> ts) {/*插入函数*/
		
		StudentTree st= new  StudentTree();
		System.out.println("   请输姓名");
		System.out.println("name=");
		Scanner s1= new Scanner (System.in);
		String name = s1.nextLine();
		st.setName(name);
		
		System.out.println("请输年龄");
		System.out.println("age=");
		Scanner s2= new Scanner (System.in);
		int  id=s2.nextInt();
		st.setAge(id);
		
		System.out.println("   请输性别");
		System.out.println("sex=");
		Scanner s3= new Scanner (System.in);
		String sex = s3.nextLine();
		st.setSex(sex);
		
		System.out.println("请输入数学成绩");
		System.out.println("math=");
		Scanner s4= new Scanner (System.in);
		int  id2=s4.nextInt();
		st.setMath(id2);
		
		System.out.println("请输入英语成绩");
	System.out.println("English=");
	Scanner s5= new Scanner (System.in);
	int  id1=s5.nextInt();
	st.setEnglish(id1);
	
	System.out.println("请输入C语言成绩");
		System.out.println("C语言=");
	Scanner s6= new Scanner (System.in);
	int  id4=s6.nextInt();
	st.setcProgram(id4);

	ts.add(st);
		System.out.println("   创建成功!");
		
	}
	public static void del(TreeSet<StudentTree> ts) {//删除
		
		System.out.println("请输入要删除学生的学号 ");
		System.out.println("ID=");
		Scanner s= new Scanner (System.in);
		int  coid=s.nextInt();
		
		for(StudentTree st:ts) {
			if(st.getId()==coid) {
				ts.remove(st);
				System.out.println("删除成功!");
				break;
			}
		}
		
	}
	public static void display(TreeSet<StudentTree> ts ) {//遍历
		for (StudentTree s:ts) {
			System.out.println(s.toString());
		}
	}
	
	
}
	

 StudentTree类


public class StudentTree {
	private String name ;
	private  int  id;
	private  int  age;
	private  String   sex;
	private  int cProgram;
	private  int  english;
	private  int  math;

	private  int  sum;

	public StudentTree(String name, int id, int age, String sex, int cProgram, int english, int math) {
		super();
		this.name = name;
		this.id = id;
		this.age = age;
		this.sex = sex;
		this.cProgram = cProgram;
		this.english = english;
		this.math = math;
		this.sum = this.cProgram+this.english+this.math;
	}

	public StudentTree() {
		super();
	}

	public String getName() {
		return name;
	}

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

	public int getId() {
		return id;
	}

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

	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 int getcProgram() {
		return cProgram;
	}

	public void setcProgram(int cProgram) {
		this.cProgram = cProgram;
	}

	public int getEnglish() {
		return english;
	}

	public void setEnglish(int english) {
		this.english = english;
	}

	public int getMath() {
		return math;
	}

	public void setMath(int math) {
		this.math = math;
	}

	public int getSum() {
		return sum;
	}

	public void setSum(int sum) {
		this.sum =this.cProgram+this.english+this.math;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + cProgram;
		result = prime * result + english;
		result = prime * result + id;
		result = prime * result + math;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		result = prime * result + sum;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StudentTree other = (StudentTree) obj;
		if (age != other.age)
			return false;
		
		return true;
	}

	@Override
	public String toString() {
		return "StudentTree [name=" + name + ", id=" + id + ", age=" + age + ", sex=" + sex + ", cProgram=" + cProgram
				+ ", english=" + english + ", math=" + math + ", sum=" + sum + "]";
	}
	
	
	
}

 

 

发布了6 篇原创文章 · 获赞 5 · 访问量 3769
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览