java基础学生管理系统

java实现简单的学生管理系统

文件搭建:直接复制到项目下
	Admin.txt
				357886-张三-12345
				357887-李四-12345
				357888-王五-12345
	Student.txt
				101-乔峰-99.0
				102-虚竹-99.2
				103-段誉-98.0
## 实体类的封装
Admin.java
package com.batis.beans;
/*
 * 	学生管理系统:
 * 		管理员类。
 * 	管理员实体类分析:
 * 		管理员具有
 * 			工号	|	姓名	|	密码
 */
public class Admin {
	private Integer id;
	private String name;
	private String pass;
	public Admin() {
		super();
	}

	public Admin(Integer id, String name, String pass) {
		super();
		this.id = id;
		this.name = name;
		this.pass = pass;
	}

	public int getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	@Override
	public String toString() {
		return this.id + "-" + this.name + "-" + this.pass;
	}
}
Student.java
package com.batis.beans;
/*
 * 	学生管理系统:
 * 		学生类。
 * 	学生类分析:
 * 		学生具有的一些属性:
 * 		学号	|	姓名	|	成绩
 */
public class Student {
	private Integer id;
	private String name;
	private double score;
	
	public Student() {
		super();
	}
	
	public Student(Integer id, String name, double score) {
		super();
		this.id = id;
		this.name = name;
		this.score = score;
	}

	public int getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	@Override
	public String toString() {
		return this.id + "-" + this.name + "-" + this.score;
	}
}

测试类

package com.batis.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import com.batis.beans.Admin;
import com.batis.beans.Student;

/*
 * 	学生管理系统:主函数
 * 		这个简单程序的主入口。
 */
public class Test {
	// 静态的全局变量,供全局使用
	private static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		menu1();
	}

	// 定义一个方法,要完成管理员的登录和注册
	public static void menu1() {
		System.out.println("==学生管理系统==");
		System.out.println("1:登录");
		System.out.println("2:注册");
		System.out.println("3:退出");
		System.out.println("请输入你的选择");
		int choose = sc.nextInt();
		switch (choose) {
		case 1:
			login();
			break;
		case 2:
			register();
			break;
		case 3:
			System.out.println("Bye~~");
			System.exit(0);// 固定写法
			break;
		default:
			System.out.println("输入的信息有误");
			break;
		}
		menu1();// 跳出switch循环之后再次近路菜单一。
	}

	public static void login() {
		/*
		 * 实现登录的功能 验证账号和密码是否相等 从上面你的数组中我们得知 账号和密码分别是数组中的第一位和第三位
		 */
		System.out.println("请输入您的工号");
		int id = sc.nextInt();
		System.out.println("请输入您的密码");
		String pass = sc.next();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("Admin.txt"));
			String str = null;
			while ((str = br.readLine()) != null) {
				// 对数据进行下一步的处理,对数据进行切割之后存储到一个数组之中
				String[] split = str.split("-");
				/*
				 * 对数据进行判断 因为数据是字符串类型和基本类型进行比较 需要使用基本类型中包装类的方法将字符串转换为基本数据类型载进行比较
				 * 对于基本数据类型的比较实用的是== 而equals比较的是字符串的是否相等。
				 */
				if (Integer.parseInt(split[0]) == id && split[2].equals(pass)) {
					System.out.println("登录成功");
					// 跳转二级菜单之后,一定要使用return结束现在这个方法的调用,不然方法走完循环还会继续执行下去
					menu2(split[1]);
					return;
				}
			}
			System.out.println("登入失败");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 注册:需要知道注册都需要填写什么,然后通过读写操作对数据的写入 需要对管理员进行判断,如果已经存在在了这个文件当中 工号是管理员唯一的
	 * 信息,只需要判断输入的工号是否存在即可 则输出管理员已存在 不存在则进行写入管理员信息 关闭资源时,先开的后关,后开的先关
	 * 追加写是在这个文本后面加一个true
	 */
	public static void register() {
		System.out.println("请输入您的工号:");
		int id = sc.nextInt();
		System.out.println("请输入姓名:");
		String name = sc.next();
		System.out.println("请输入密码:");
		String pass = sc.next();
		BufferedReader br = null;
		BufferedWriter bw = null;

		try {

			// 1.实例化两个流对象
			br = new BufferedReader(new FileReader("Admin.txt"));
			bw = new BufferedWriter(new FileWriter("Admin.txt", true));

			// 2.判断输入的id是否已存在

			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				if (Integer.parseInt(split[0]) == id) {
					System.out.println("该工号已注册!");
					return;
				}
			}

			// 3.向文件中写出数据
			bw.write(new Admin(id, name, pass).toString());
			bw.newLine();
			bw.flush();
			System.out.println("注册成功!");

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public static void menu2(String x) {
		System.out.println("欢迎" + x + "用户登录");
		System.out.println("1:根据学号查找学生");
		System.out.println("2:展示所有学生");
		System.out.println("3:新增一个学生");
		System.out.println("4:移除一个学生");
		System.out.println("5:修改学生成绩");
		System.out.println("6:返回上级菜单");
		int choose = sc.nextInt();
		switch (choose) {
		case 1:
			findOneStudentById();
			break;
		case 2:
			showAll();
			break;
		case 3:
			addOne();
			break;
		case 4:
			removeOne();
			break;
		case 5:
			changeStudentScore();
			break;
		case 6:
			menu1();
			break;
		default:
			System.out.println("输入有误");
			break;
		}
		menu2(x);
	}

	/**
	 * 根据学号查找学生 : 读取学生信息的文本 对文本进行切割,切割到的第一位就是我们想要的内容 对内容进行判定,如果不存在则输出该生不存在
	 * 如果查找到,则输出该学生的信息
	 * 
	 * @return
	 */
	public static Student findOneStudentById() {
		System.out.println("请输入要查找的学生编号:");
		int id = sc.nextInt();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				if (Integer.parseInt(split[0]) == id) {
					System.out.println("学号\t姓名\t成绩");
					System.out.println(split[0] + "\t" + split[1] + "\t" + split[2]);
					return new Student(Integer.parseInt(split[0]), split[1], Double.parseDouble(split[2]));
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		System.out.println("该学生不存在!");
		return null;
	}

	/**
	 * 展示所有学生信息: 读取文件,输出文件
	 */
	public static void showAll() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			String str = null;
			System.out.println("学号\t姓名\t成绩");
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				System.out.println(split[0] + "\t" + split[1] + "\t" + split[2]);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/*
	 * 添加一个学生信息,需要读取文件,判断学生是否已经存在这个文件之中 如果存在输出该学生已经存在
	 * 不存在则进行天添加的动作,添加应该是追加写,所以在后面需要加一个参数true
	 */
	public static void addOne() {
		System.out.println("请输入添加的学生id");
		int stuId = sc.nextInt();
		System.out.println("请输入添加的学生的姓名");
		String name = sc.next();
		System.out.println("请输入添加的学生的成绩");
		double score = sc.nextDouble();
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			bw = new BufferedWriter(new FileWriter("Student.txt", true));
			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				if (Integer.parseInt(split[0]) == stuId) {
					System.out.println("该学生已经存在");
					return;
				}
			}
			bw.write(new Student(stuId, name, score).toString());
			bw.newLine();
			bw.flush();
			System.out.println("添加成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 删除一个学生,则这个学生可以调用根据id查找学生的方法来使用 查找到之后在进行删除的
	 */
	public static void removeOne() {
		Student stu = findOneStudentById();
		/*
		 * 删除:如果找到的这个对象为空直接结束掉方法 不为空才能进行删除 删除的时候需要进行确认
		 */
		if (stu == null) {
			return;
		}
		System.out.println("是否确定删除(Y/N)");
		String choose = sc.next();
		if (choose.equals("N")) {
			return;
		}
		BufferedReader br = null;
		BufferedWriter bw = null;
		// 定义一个集合用来存储删除之后的其他元素
		ArrayList<Student> list = new ArrayList<Student>();
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				if (stu.getId() == Integer.parseInt(split[0])) {
					continue;
				}
				list.add(new Student(Integer.parseInt(split[0]), split[1], Double.parseDouble(split[2])));
			}
			/*
			 * 已经把其他的学生添加到集合当中,所以只需要遍历集合 将集合中的元素重新写入到文本之中
			 */
			bw = new BufferedWriter(new FileWriter("student.txt"));
			for (Student student : list) {
				bw.write(student.toString());
				bw.newLine();
				bw.flush();
				;
			}
			System.out.println("移除成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/*
	 * 修改学生成绩 可以根据查找到的学生来对他的成绩进行修改
	 * 
	 */
	public static void changeStudentScore() {
		Student student = findOneStudentById();
		if (student == null) {
			return;
		}
		System.out.println("请输入学生新的成绩");
		double newScore = sc.nextDouble();
		BufferedReader br = null;
		BufferedWriter bw = null;
		ArrayList<Student> list = new ArrayList<Student>();
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				//找到这个学生
				if (student.getId() == Integer.parseInt(split[0])) {
					//先添加到集合,即保证这一个修改到。
					list.add(new Student(Integer.parseInt(split[0]), split[1], newScore));
					continue;
				}
				list.add(new Student(Integer.parseInt(split[0]), split[1], Double.parseDouble(split[2])));
			}

			// 将集合中的数据写出到Student.txt中
			bw = new BufferedWriter(new FileWriter("Student.txt"));
			for (Student stu : list) {
				bw.write(stu.toString());
				bw.newLine();
				bw.flush();
			}
			System.out.println("修改成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值