java学生管理系统项目实战

大致演示

图片[1]-java学生管理系统项目实战 - it同学-it同学

代码实现

//学生类
public class Student {
	private String Name;  //姓名
	private int age;    //年龄
	private String sid;    //学号
	private String birthday;    //生日
	public Student(String name, int age, String sid, String birthday) {
		super();
		Name = name;
		this.age = age;
		this.sid = sid;
		this.birthday = birthday;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSid() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	
	
}
//主函数及方法
package test;
import java.util.ArrayList;
import java.util.Scanner;

public class jiupan {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
			//创建集合
		ArrayList<Student> list = new ArrayList<>();
		lo:while(true) {
			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("请输入你的选择:");
			
			String Choice = sc.next();
			switch(Choice) {
				case "1":
					addStudent(list);
					break;
				case "2":
					delateStudent(list);
					break;
				case "3":
					changeStudent(list);
					break;
				case "4":
					queryStudent(list);
					break;
				case "5":
					System.out.println("欢迎使用");
					break lo;
				default: System.out.println("请重新输入");
					break;
			}
		}
		
    }

	//查找学号在集合中出现的索引
	public static int getIndex(ArrayList<Student> list,String sid) {
		for(int i=0;i<list.size();i++) {
			Student stu = list.get(i);
			if(sid.equals(stu.getSid())) {
				return i;
			}
		}
		return -1;
	}
	//删除学生的方法
	public static void delateStudent(ArrayList<Student> list) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要删除学生的学号");
		String sid = sc.next();
		int index = getIndex(list,sid);
		if(index == -1) {
			System.out.println("没有你要删的人,闲的没事干了???");
			return;
		}else {
			list.remove(index);
			System.out.println("删除成功!!!");
		}
	}
	//查看学生的方法
	public static void queryStudent(ArrayList<Student> list) {
		// 查看学生
		if(list.size() == 0) {
			System.out.println("暂没有学生信息,请添加后再查询!!!");
			return;
		}
		System.out.println("姓名\t年龄\t学号\t生日");
		for(int i=0;i<list.size();i++) {
			Student stu = list.get(i);
			System.out.println(stu.getName() + "\t" + stu.getAge() + "\t" + stu.getSid() + "\t" + stu.getBirthday());
		}
		
	}
	//添加学生的方法
	public static void addStudent(ArrayList<Student> list) {
		String xuehao;
		Scanner sc = new Scanner(System.in);
		while(true) {
			System.out.println("请输入学号");
			xuehao = sc.next();
			int index = getIndex(list,xuehao);
			if(index == -1) {
				break;
			}else {
				System.out.println("已重复");
			}
		}
		System.out.println("请输入姓名");
		String name = sc.next();
		System.out.println("请输入年龄");
		int age = sc.nextInt();
		System.out.println("请输入生日");
		String shengri = sc.next(); 
		Student stu = new Student(name,age,xuehao,shengri);
		list.add(stu);
		System.out.println("添加成功!!!");
	}
    //修改学生的方法
	public static void changeStudent(ArrayList<Student> list) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要修改学生的学号");
		String sid = sc.next();
		int index = getIndex(list,sid);
		if(index == -1) {
			System.out.println("没有你要修改的人,脑子坏掉了???");
			return;
		}else {
			Student stu = list.get(index);
			System.out.println("请输入要修改的名字");
			String name = sc.next();
			stu.setName(name);
			System.out.println("请输入要修改的年龄");
			int age = sc.nextInt();
			stu.setAge(age);
			System.out.println("请输入要修改的学号");
			String sid1 = sc.next();
			stu.setSid(sid1);
			System.out.println("请输入要修改的生日");
			String bir = sc.next();
			stu.setBirthday(bir);
		}
		System.out.println("修改成功!!!");
	}

}

© 版权声明
THE END
喜欢就支持一下吧
点赞7
分享
评论 抢沙发

请登录后发表评论