【Java】集合实现学生管理系统

学生管理系统

正文

运行系统:
提示:欢迎来到学生管理系统
系统列表展示

(1)查看所有学生 封装方法
(2)添加学生 封装方法
(3)删除学生 封装方法
(4)修改学生 封装方法
(5)退出系统 封装方法

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Scanner;
class Student {
	private String sno;
	private String name;
	private int age;
	private String home;
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	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 getHome() {
		return home;
	}
	public void setHome(String home) {
		this.home = home;
	}

	//重写了hashCode()
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((home == null) ? 0 : home.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sno == null) ? 0 : sno.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (home == null) {
			if (other.home != null)
				return false;
		} else if (!home.equals(other.home))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (sno == null) {
			if (other.sno != null)
				return false;
		} else if (!sno.equals(other.sno))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Student [sno=" + sno + ", name=" + name + ", age=" + age + ", home=" + home + "]";
	}	
}
public class StudentManageMent {
//	定义学生数组长度,初始长度=0;
	public static int length=0;	
//	展示学生
	public static void showStudents(ArrayList<Student> student,int length){
		Student s=new Student();
		if(student.isEmpty()){
			System.out.println("没有学生");
			theShow(student,length);
			System.out.println("请选择其他功能");
		}else{
			for(int j=0;j<length;j++){
//				System.out.println(student.get(j).toString());
				System.out.println("学生"+(j+1)+"的年龄是: "+student.get(j).getAge()+" 名字是: "+student.get(j).getName()+
						" 学号是: "+student.get(j).getSno()+" 家庭住址是: "+student.get(j).getHome());
				
			}
			theShow(student,length);
		}
	}	
//	添加学生
	public static void addStudents(ArrayList<Student> student,int length){
		System.out.println("请输入学号");
		Scanner inSno =new Scanner(System.in);
		String sno =inSno.next();
		for(int b=0;b<length;b++){
			if(sno.equals(student.get(b).getSno())){
				System.out.println("你输入的学号已经被占用,请重新输入");
				addStudents(student,length);
			}	
		}
		Student stu=new Student();
		stu.setSno(sno);
     	System.out.println("请输入姓名");
		Scanner inName =new Scanner(System.in);
		String name =inSno.next();
		stu.setName(name);	
		System.out.println("请输入年龄");
		Scanner Inage =new Scanner(System.in);
		int age =inSno.nextInt();
		stu.setAge(age);	
		System.out.println("请输入居住地");
		Scanner inHome =new Scanner(System.in);
		String home =inSno.next();
		stu.setHome(home);
		student.add(stu);
		length++;
		theShow(student,length);
	}	
//	删除学生
	public static void delStudents(ArrayList<Student> a,int length){
		System.out.println("选择要删除学生的学号");		
		Scanner inSno =new Scanner(System.in);
		String sno =inSno.next();
		int time =0;//锁
		for(int b=0;b<length;b++){
			if(sno.equals(a.get(b).getSno())){
				System.out.println("你输入的学号存在");
				a.remove(b);
				length--;
				time =1;
			}			
		}
		if(time ==1){
			System.out.println(",已经删除");			
		}		
		theShow(a,length);
		System.out.println("请选择其他功能");			
	}	
//	修改学生
	public static void setStudents(ArrayList<Student> student,int length){
		System.out.println("选择要修改学生的学号");		
		Student stu =new Student();
		Scanner inSno =new Scanner(System.in);
		String sno =inSno.next();
		int time =0;//锁
		for(int b=0;b<length;b++){
			if(sno.equals(student.get(b).getSno())){
				System.out.println("你输入的学号存在");
				student.remove(b);
				time =1;
				stu.setSno(sno);			
				System.out.println("请输入姓名");
				Scanner inName =new Scanner(System.in);
				String name =inSno.next();
				stu.setName(name);		
				System.out.println("请输入年龄");
				Scanner Inage =new Scanner(System.in);
				int age =inSno.nextInt();
				stu.setAge(age);		
				System.out.println("请输入居住地");
				Scanner inHome =new Scanner(System.in);
				String home =inSno.next();
				stu.setHome(home);
				student.add(stu);			
			}	
			if(time ==1){
				System.out.println("修改成功");
			}		
		}		
		theShow(student,length);
		System.out.println("请选择其他功能");		
	}
//	退出系统
	public static void outOfTheSystem(ArrayList<Student> a){	
	}
//	theShow()操作系统的小入口,会被多次调用
	public static void theShow(ArrayList<Student> student,int length){
		
		while(true){
			Scanner sc=new Scanner(System.in);
			int in=sc.nextInt();
			switch(in){
			case 1:
				showStudents(student,length);
			    break;
			case 2:
				addStudents(student,length++);
			    break;
			case 3:
				delStudents(student,length);
				break;
			case 4:
				setStudents(student,length);
				break;
			case 5:
				return ;					
			}		
			return ;
		}		
	}	
//	主界面
	public static void managementView(){
		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("===========================");
	}	
//	主函数
	public static void main(String[] args) {
		ArrayList<Student> student=new ArrayList<>();	
		managementView();
		theShow(student,length);	
	}	
}

你可能感兴趣的:(Java,学生管理系统,java,学生管理系统)