JAVA实现学生信息管理系统

一. 题目要求

对学生信息管理系统,要求完成以下基本任务:
1.有良好程序风格(文档注释,函数注释,语句注释)。
2.将功能补充完全(基于文件处理,完成刷新和保存功能)。
3.将学生信息改为更好的数据组织,而非离散形式(结构体)。

二. 需求分析

1.Main类
实现读取文档初始化学生对象和显示菜单功能
2.Judge类
提供多种静态方法对输入的各种数据进行验证是否合理,正确
3.Student类
以成员变量的形式,用字符串存储学生的姓名,学号(唯一),年龄,性别,C语言成绩,高数成绩,英语成绩。
4.Manage类
①void meau();菜单功能,用户可以选择一项进行操作。
②void help();帮助菜单,为用户提供简单的使用方法。
③void insert();插入学生信息,并将学生信息存入文档。
④void modify();修改学生信息,并将学生信息存入文档。
⑤void display();显示功能,显示当前已有学生的信息。
⑥void del();删除功能,按照学号或者姓名可以删除,若姓名相等,则删 除学号靠前的学生信息。
⑦void seek();查找功能。
⑧void sort();排序工能,将学生对象排序,并按学号升序重新将信息写入文件。
⑨void readFile();读档功能,在未显示菜单之前执行一次,读取已有学生的信息。

三.主要算法流程设计

1.主方法流程图
在这里插入图片描述
2.插入学生信息流程图
在这里插入图片描述

四. 程序说明

1.使用文件功能对程序运行结果进行保存和读取。
2.判断功能,每次输入时都会判断是否合理,若不合理,将给出提示并重新输入。
3.存储结构采用对象数组,顺序存储结构。

五. 算法实现

1.Main类

import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		//建立学生成绩管理系统
		Manage m1 = new Manage();
		//从文档中读取已有学生的信息
		m1.readFile();
		while(m1.flag){
			m1.meau();	
	}
		System.out.println("您已经退出该系统");
	}
}

2.Judge类

public class Judge {
	//判断输入学生的数量是否合法,非法返回false
	static boolean judgeNum(String str) {
		char[] ch = str.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			if (ch[i] < 48 | ch[i] > 57)
				return false;
		}
		if (Integer.parseInt(str) < 0 | Integer.parseInt(str) > Manage.N-Manage.n)
			return false;
		return true;
	}
	//判断学号是否合法与重复,保证学号唯一,非法返回false,合法返回true
	static boolean judgeCode(String str) {
		char[] ch = str.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			if (ch[i] < 48 | ch[i] > 57)
				return false;
		}
		for (int i = 0; i < Manage.n; i++) {
			if (str.equals(Manage.getCode(i)))
				return false;
		}
		return true;
	}
	//判断年龄是否正确合法,非法返回false,合法返回true
	static boolean judgeAge(String str) {
		char[] ch = str.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			if (ch[i] < 48 | ch[i] > 57)
				return false;
		}
		if (Integer.parseInt(str) < 0 | Integer.parseInt(str) > 100)
			return false;
		return true;
	}
	//判断分数是否合法,非法返回false,合法返回true
	static boolean judgeScore(String str) {
		char[] ch = str.toCharArray();
		// 第一位不能为小数点
		if (ch[0] == 46)
			return false;
		for (int i = 0; i < ch.length; i++) {
			// 判断是否为小数点
			if (ch[i] != 46) {
				if (ch[i] < 48 | ch[i] > 57)
					return false;
			}
		}
		return true;
	}
	//判断性别是否合法,非法返回false,合法返回true
	static boolean judgeSex(String str){
		if(str.equals("F") | str.equals("f") | str.equals("M") | str.equals("m"))
			return true;
		return false;
	}

}

3.Student类


public class  Student{
	String code;
	String name;
	String sex;
	String age;
	String scoreOfC;
	String scoreOfMath;
	String scoreOfEnglish;
}

4.Manage类

import java.io.*;
import java.util.Scanner;

public class Manage {
	// 程序运行or结束标志变量
	boolean flag = true;
	static int N = 50;// 学生数量
	static Student[] stu = new Student[N];
	static int n = 0;// n表示当前记录的学生人数
	int m = 0;// m为要增加的人数
	Scanner sc = new Scanner(System.in);

	void meau() throws IOException {
		String num;
		System.out
				.println("****************************************************");
		System.out
				.println("        *             学生信息管理系统                             *");
		System.out
				.println("****************************************************");
		System.out.println("********************系统功能菜单***********************");
		System.out
				.println("*  0.系统帮助及说明  *  * 1.刷新学生信息   *  * 2.查询学生信息    *  ");
		System.out
				.println("****************************************************   ");
		System.out
				.println("*  3.修改学生信息      *  * 4.增加学生信息   *  * 5.按学号删除信息 * ");
		System.out
				.println("*****************************************************     ");
		System.out
				.println("*  6.显示当前信息      *  * 7.保存当前学生信息* *  8.退出系统         *    ");
		System.out
				.println("*****************************************************    ");
		System.out.println("请选择菜单编号:");
		num = sc.next();
		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":
			flag = false;
			sc.close();
			break;
		default:
			System.out.println("请重新在0-8之间选择");
		}
	}

	void help() {
		System.out.println("0.欢迎使用系统帮助!");
		System.out.println("1.初次进入系统后,请先选择增加学生信息;");
		System.out.println("2.按照菜单提示键入数字代号;");
		System.out.println("3.增加学生信息后,切记保存;");
		System.out.println("4.谢谢您的使用!");
	}

	void insert() throws IOException {
		int j = n; // 当前为第j个学生
		System.out.print("请输入待增加的学生数:");
		String str = sc.next();
		if (!Judge.judgeNum(str)) {
			System.out.println("输入数据有误请重新输入");
			return;
		}
		m = Integer.parseInt(str);
		for (int i = j; i < j + m; i++) {
			stu[i] = new Student();
		}
		do {

			// 输入学号并判断
			System.out.println("请输入第" + (j - n + 1) + "个学生的学号:");
			stu[j].code = sc.next();
			if (!Judge.judgeCode(stu[j].code)) {
				System.out.println("输入有误或学号已存在,请重新输入该学生的信息");
				continue;
			}
			// 输入姓名
			System.out.println("请输入第" + (j - n + 1) + "个学生的姓名:");
			stu[j].name = sc.next();
			System.out.println("请输入第" + (j - n + 1) + "个学生的年龄:");
			stu[j].age = sc.next();
			if (!Judge.judgeAge(stu[j].age)) {
				System.out.println("输入有误,请重新输入该学生的信息");
				continue;
			}
			System.out.println("请输入第" + (j - n + 1) + "个学生的性别缩写,仅限F,M,f,m");
			stu[j].sex = sc.next();
			if (!Judge.judgeSex(stu[j].sex)) {
				System.out.println("输入有误,请重新输入该学生的信息");
				continue;
			}
			// 输入成绩并判断
			System.out.println("请输入第" + (j - n + 1) + "个学生的C语言成绩");
			stu[j].scoreOfC = sc.next();
			if (!Judge.judgeScore(stu[j].scoreOfC)) {
				System.out.println("输入有误,请重新输入该学生的信息");
				continue;
			}
			System.out.println("请输入第" + (j - n + 1) + "个学生的大学英语成绩:");
			stu[j].scoreOfEnglish = sc.next();
			if (!Judge.judgeScore(stu[j].scoreOfEnglish)) {
				System.out.println("输入有误,请重新输入该学生的信息");
				continue;
			}
			System.out.println("请输入第" + (j - n + 1) + "个学生的高等数学成绩:");
			stu[j].scoreOfMath = sc.next();
			if (!Judge.judgeScore(stu[j].scoreOfMath)) {
				System.out.println("输入有误,请重新输入该学生的信息");
				continue;
			}
			// 将新输入的信息存入文档
			FileWriter fw = new FileWriter("Student.txt", true);
			String strTemp = new String(stu[j].code + " " + stu[j].name + " "
					+ stu[j].age + " " + stu[j].sex + " " + stu[j].scoreOfC
					+ " " + stu[j].scoreOfEnglish + " " + stu[j].scoreOfMath
					+ "\r\n");
			fw.write(strTemp);
			fw.close();
			j++;
		} while (j < n + m);
		n += m;
		System.out.println("信息增加完毕!");
		// 按照学号升序进行排序
		sort();
	}

	void modify() throws IOException {
		boolean flag = true;
		System.out.print("请输入要要修改的学生的学号:");
		String str = sc.next();
		int t = 0 ; // 循环外的变量记录要修改学生下标值
		String dataOfStudentOld = new String(); // 循环外的变量记录要修改学生的字符数据
		for (int i = 0; i < n; i++) {
			if (stu[i].code.equals(str)) {
				t = i;
				// 定义字符串变量strT存储修改前学生的信息
				String strT = new String(stu[t].code + " " + stu[t].name + " "
						+ stu[t].age + " " + stu[t].sex + " " + stu[t].scoreOfC
						+ " " + stu[t].scoreOfEnglish + " "
						+ stu[t].scoreOfMath + "\r\n");
				dataOfStudentOld = strT;
				flag = false;
				System.out.println("------------------");
				System.out.println("1.修改姓名");
				System.out.println("2.修改年龄");
				System.out.println("3.修改性别");
				System.out.println("4.修改C语言成绩");
				System.out.println("5.修改高等数学成绩");
				System.out.println("6.修改大学英语成绩");
				System.out.println("7.保存修改信息并退出本菜单");
				System.out.println("------------------");
				while (true) {
					System.out.println("请选择子菜单编号:");
					String item = sc.next();
					switch (item) {
					case "1":
						System.out.println("请输入新的姓名:");
						stu[i].name = sc.next();
						break;
					case "2":
						System.out.println("请输入新的年龄:");
						stu[i].age = sc.next();
						if (!Judge.judgeAge(stu[i].age)) {
							System.out.println("输入有误,请重新选择并修改");
						}
						break;
					case "3":
						System.out.println("请输入新的性别:");
						stu[i].sex = sc.next();
						break;
					case "4":
						System.out.println("请输入新的C语言成绩:");
						stu[i].scoreOfC = sc.next();
						if (!Judge.judgeScore(stu[i].scoreOfC)) {
							System.out.println("输入有误,请重新选择并修改");
						}
						break;
					case "5":
						System.out.println("请输入新的高等数学成绩:");
						stu[i].scoreOfMath = sc.next();
						if (!Judge.judgeScore(stu[i].scoreOfMath)) {
							System.out.println("输入有误,请重新选择并修改");
						}
						break;
					case "6":
						System.out.println("请输入新的大学英语成绩:");
						stu[i].scoreOfEnglish = sc.next();
						if (!Judge.judgeScore(stu[i].scoreOfEnglish)) {
							System.out.println("输入有误,请重新选择并修改");
						}
						break;
					case "7":
						// 对文件里该学生的信息进行修改
						if (!flag) {
							// 定义字符串变量dataOfStudentNew存储修改后学生的信息
							String dataOfStudentNew = new String(stu[t].code + " "
									+ stu[t].name + " " + stu[t].age + " " + stu[t].sex + " "
									+ stu[t].scoreOfC + " " + stu[t].scoreOfEnglish + " "
									+ stu[t].scoreOfMath + "\r\n");
							File f = new File("Student.txt");
							long length = f.length();
							FileReader fr = new FileReader("Student.txt");
							char[] ch = new char[(int) length];
							fr.read(ch);
							fr.close();
							String str1 = new String(ch);
							// 进行修改操作
							str1 = str1.replace(dataOfStudentOld, dataOfStudentNew);
							// 将新字符串写入文件
							FileWriter fw = new FileWriter("Student.txt");
							fw.write(str1);
							fw.close();
						}
						return;
					default:
						System.out.println("请在1-7之间选择");
					}
				}
			}

		}
		
		if (flag)
			System.out.println("该学生不在系统内");
	}

	void display() {
		System.out.println("共有" + n + "位学生的信息:");

		if (0 != n) {
			System.out
					.println("学生学号   学生姓名     年龄       性别         C语言成绩        高等数学        大学英语成绩");
			System.out
					.println("--------------------------------------------------------------------");
			for (int i = 0; i < n; i++) {
				System.out.println(stu[i].code + "      " + stu[i].name
						+ "      " + stu[i].age + "    " + stu[i].sex + "     "
						+ stu[i].scoreOfC + "      " + stu[i].scoreOfMath
						+ "         " + stu[i].scoreOfEnglish);
			}
		}

	}

	void del() throws IOException {
		System.out.println("请输入要删除学生的学号:");
		String code = sc.next();
		boolean flag = false;
		for (int i = 0; i < n; i++) {
			if (stu[i].code.equals(code)) {
				flag = true;
				// 定义字符串变量dataOfStudent存储符合条件学生的信息
				String dataOfStudent = new String(stu[i].code + " "
						+ stu[i].name + " " + stu[i].age + " " + stu[i].sex
						+ " " + stu[i].scoreOfC + " " + stu[i].scoreOfEnglish
						+ " " + stu[i].scoreOfMath + "\r\n");
				// 删除的是最后一名学生
				if (i == n - 1) {
					stu[i] = null;
					n -= 1;
				}
				// 删除的不是最后一名学生
				else {
					for (int j = i; j < n - 1; j++) {
						stu[j] = stu[j + 1];
					}
					stu[n - 1] = null;
					n -= 1;
				}
				// 对文件里该学生的信息进行删除
				File f = new File("Student.txt");
				long length = f.length();
				FileReader fr = new FileReader("Student.txt");
				char[] ch = new char[(int) length];
				fr.read(ch);
				fr.close();
				String str = new String(ch);
				// 进行删除操作
				str = str.replace(dataOfStudent, "");
				// 将新字符串写入文件
				FileWriter fw = new FileWriter("Student.txt");
				fw.write(str);
				fw.close();
			}
		}
		if (flag == false)
			System.out.println("该学号不存在!");
		if (flag == true)
			System.out.println("删除成功,显示结果请选择菜单");

	}

	void seek() /* 查找 */
	{
		int i;
		String item, code, name;
		boolean flag = false;
		System.out.println("------------------\n");
		System.out.println("-----1.按学号查询-----\n");
		System.out.println("-----2.按姓名查询-----\n");
		System.out.println("-----3.退出本菜单-----\n");
		System.out.println("------------------\n");
		while (true) {
			System.out.println("请选择子菜单编号:");
			item = sc.next();
			flag = false;
			switch (item) {
			case "1":
				System.out.println("请输入要查询的学生的学号:\n");
				code = sc.next();
				for (i = 0; i < n; i++)
					if (stu[i].code.equals(code)) {
						flag = true;
						System.out
								.println("学生学号   学生姓名     年龄       性别         C语言成绩        高等数学        大学英语成绩");
						System.out
								.println("--------------------------------------------------------------------\n");
						System.out.println(stu[i].code + "      " + stu[i].name
								+ "      " + stu[i].age + "    " + stu[i].sex
								+ "     " + stu[i].scoreOfC + "      "
								+ stu[i].scoreOfMath + "         "
								+ stu[i].scoreOfEnglish);
					}
				if (false == flag)
					System.out.println("该学号不存在!");
				break;
			case "2":
				System.out.println("请输入要查询的学生的姓名:");
				name = sc.next();
				for (i = 0; i < n; i++)
					if (stu[i].name.equals(name)) {
						flag = true;
						System.out
								.println("学生学号   学生姓名     年龄       性别         C语言成绩        高等数学        大学英语成绩");
						System.out
								.println("--------------------------------------------------------------------\n");
						System.out.println(stu[i].code + "      " + stu[i].name
								+ "      " + stu[i].age + "    " + stu[i].sex
								+ "     " + stu[i].scoreOfC + "      "
								+ stu[i].scoreOfMath + "         "
								+ stu[i].scoreOfEnglish);
					}
				if (false == flag)
					System.out.println("该姓名不存在!");
				break;
			case "3":
				return;
			default:
				System.out.println("请在1-3之间选择\n");
			}
		}
	}

	// Judge类中使用
	static String getCode(int i) {
		return stu[i].code;
	}

	void sort() throws IOException {
		int[] code = new int[n];
		int temp;
		Student stuTemp = new Student();
		for (int i = 0; i < n; i++) {
			code[i] = Integer.parseInt(stu[i].code);
		}
		//使用选择排序法对学生对象进行排序
		for (int i = 0; i < n - 1; i++) {
			for (int j = i + 1; j < n; j++) {
				if (code[i] > code[j]) {
					// 对code数组元素进行交换
					temp = code[i];
					code[i] = code[j];
					code[j] = temp;
					// 对Student对象数组元素进行交换
					stuTemp = stu[i];
					stu[i] = stu[j];
					stu[j] = stuTemp;
				}
			}
		}
		//按学号顺序将学生信息写入文件
		FileWriter fw = new FileWriter("Student.txt");
		for(int i=0;i<n;i++){
			String strTemp = new String(stu[i].code + " " + stu[i].name + " "
				+ stu[i].age + " " + stu[i].sex + " " + stu[i].scoreOfC
				+ " " + stu[i].scoreOfEnglish + " " + stu[i].scoreOfMath	+ "\r\n");
					fw.write(strTemp);	
		}
		fw.close();		
	}

	void readFile() throws IOException {
		// 打开Student文件并将所有内容读取到字符串str中
		File f = new File("Student.txt");
		int length = (int) f.length();
		if (length == 0)
			return;
		FileReader fr = new FileReader("Student.txt");
		char[] ch = new char[length];
		fr.read(ch);
		fr.close();
		String str = new String(ch);
		// 将字符串分割为数组并初始化n
		String regex1 = "\\s{2}";
		String regex2 = "\\p{Blank}";
		String[] strArray = str.split(regex1);
		n = strArray.length;
		// 创建实例对象
		for (int i = 0; i < n; i++) {
			stu[i] = new Student();
		}
		// 初始化对象
		for (int i = 0; i < strArray.length; i++) {
			String[] strArray2 = strArray[i].split(regex2);
			stu[i].code = strArray2[0];
			stu[i].name = strArray2[1];
			stu[i].age = strArray2[2];
			stu[i].sex = strArray2[3];
			stu[i].scoreOfC = strArray2[4];
			stu[i].scoreOfEnglish = strArray2[5];
			stu[i].scoreOfMath = strArray2[6];
		}
	}
}

六. 部分测试截图

1.运行程序,显示信息,验证是否已经读档
在这里插入图片描述
2.删除某一学生信息后显示所有学生信息
在这里插入图片描述
在这里插入图片描述
3.对其中一位学生学号进行修改
在这里插入图片描述
在这里插入图片描述
4.增加学生信息,输入错误时,验证其判断功能,增加学号较小的学生信息,验证其排序功能
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
5.验证是否可以正常退出
在这里插入图片描述
6.程序运行后查看文档是否按照学号升序排列
在这里插入图片描述

七. 经验总结

1.文件操作
①使用FileReader类对文件操作时,若要获取整个字符串,可先用File类对象获取文件长度,定义大小刚好合适的字符数组,使用FileReader类对象的read(char[] arr)将文件存储在字符数组中,然后转化为字符串进行操作。
②使用FileWriter类的对象进行写入文件时,可根据不同情况选择从头写入还是末尾追加进行打开文件。写入文件成功后牢记关闭对象引用,防止文件丢失。
2.字符串操作
①使用正则表达式,用于分割字符串。空白字符可用\\s表示。则换行符"\r\n"的对应正则表达式为:regex = “\\s{2}”。
②删除某一字符串str1时可用str = str.replace(str1,"")进行删除。
③从键盘读取一个字符串时可用sc.next()进行读取。
3.其他
设计程序时,若要判断某一段程序是否被执行,可合理设置flag布尔型变量进行判断。

  • 40
    点赞
  • 257
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值