用Java写一个学生管理系统(添加、删除、修改、查询学生信息)

不废话,上代码:

package keeper;

public class Student {

	//Java封装
	private String id;
	private String name;
	private String mobile;
	private String address;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", mobile=" + mobile + ", address=" + address + "]";
	}
	
	
	
}

package keeper;

import java.util.Scanner;

public class Main {

	static int index = 0;	//下面main()方法是static,所以此处也用static,这样才可以直接调用
	static Student [] students = new Student[45]; 
	
	public static void main(String[] args) { 
		
		System.out.println("***********************************");
		System.out.println("*      欢迎使用学生管理系统        *");
		System.out.println("***********************************");
		
		for(;;) {	//死循环
			information();
		}
		
	}
	
	public static void information() {
		System.out.println("1、添加学生信息");
		System.out.println("2、删除学生信息");
		System.out.println("3、修改学生信息");
		System.out.println("4、查询学生信息");
		
		System.out.println("请输入操作项,点击enter键");
		Scanner scanner = new Scanner(System.in);
		int option = scanner.nextInt();
		
		switch (option) {
			case 1:{
				if(index>students.length-1) {	//判断是否满员
					System.out.println("座位已满");
					return;
				}
				System.out.println("·添加学生信息:");
				Student student = new Student();
				System.out.println("请输入学号,点击enter键: ");
				String id = scanner.next();
				student.setId(id);
				if(index>0) {
					for (Student stu : students) {
						if(stu == null) {	//当某个位置为null时,其后的位置都为null,不用再查找了
							break;
						}
						if(stu.getId().equals(student.getId())) {//判断学号是否重复
							System.out.println("学号不能重复,请重新输入学号:");
							student.setId(scanner.next());
							break;
						}
					}
				}
				System.out.println("请输入姓名,点击enter键");
				student.setName(scanner.next());
				System.out.println("请输入手机号,点击enter键");
				student.setMobile(scanner.next());
				System.out.println("请输入家庭地址,点击enter键");
				student.setAddress(scanner.next());
				
				students[index] = student;	//把学生的信息依次填入数组中
				index++;
				System.out.println("添加成功");
				break;
			}
			case 2:{
				System.out.println("·删除学生信息");
				System.out.println("请输入要删除学生的学号:");
				String id = scanner.next();
				
				index=0;
				for(Student student:students) {
					if(student == null) {
						break;
					}
					if(student.getId().equals(id)) { //找到了要删除的学生
						while(index<students.length-1) {
							students[index] = students[index+1]; //把要删除学生其后的学生依次向前移一位
							index++;
						}
						students[students.length-1] = null; //最后一个学生向前移一位了,所以最后一位就是null;若没有学生,此处虽看似多余,但却可以减少判断每次循环的判断
						System.out.println("删除成功,已返回!");
						return;
					}
					index++;
				}
				
				System.out.println("未找到学号为"+id+"的学生,无法删除");
				return;
			}
			case 3:{			
				System.out.println("·修改学生信息");
				System.out.println("请输入要修改的学生的学号:");
				String id = scanner.next();
				
				for(Student student:students) {
					if(student == null) {
						break;
					}
					if(student.getId().equals(id)) {
						System.out.println("请输入新的姓名,点击enter键");
						student.setName(scanner.next());
						System.out.println("请输入新的手机号,点击enter键");
						student.setMobile(scanner.next());
						System.out.println("请输入新的家庭地址,点击enter键");
						student.setAddress(scanner.next());
						System.out.println("修改成功,已返回!");
						return;
					}
				}
				
				System.out.println("未查到学号为"+id+"的学生,无法修改!");
				return;
			}
			case 4:{
				
				boolean flag = false;
				System.out.println("·查询学生信息");
				System.out.println("请输入要查询学生的姓名:");
				String name = scanner.next();
				
				for(Student student: students) {
					if(student == null) {
						break;
					}
					if(student.getName().equals(name)) {
						System.out.println(student);
						flag = true;
					}
				}
				
				if(!flag) {
					System.out.println("姓名为"+name+"的学生不存在");
					return;
				}		
				break;
			}
			default:
				System.out.println("系统未找到"+option+"操作项");
		}
		
	}

}

你可能感兴趣的:(用Java写一个学生管理系统(添加、删除、修改、查询学生信息))