学生管理系统(附源码)

简单学生管理系统进行优化,主要内容是将集合信息保存到文件

在StudentDao类中

建一个文件 将集合中的学生信息保存到文件
private void saveStudentInfoToFile(ArrayList studentList) {
}

指定文件保存的位置
这个文件会生成在该工程下

String path="student.txt";

创建文件对象

File file=new File(path);

创建字符流输出对象

FileWriter out=new FileWriter(file);

有一个异常
用try{}块包裹
在catch{语句块中}打印堆栈,得到具体异常

	try {
		//1.指定文件保存位置
		String path="student.txt";
		//2.创建文件对象
		File file=new File(path);
		//3创建字符输出流对象
		BufferedWriter  out=new BufferedWriter(new FileWriter(file,true));	
	    	}catch(Exception e) {
	    		e.printStackTrace();
	    	}

调用写出方法输出学生信息到文件
学生信息在集合中,集合中保存的每一个对象就是学生信息,要遍历集合
首先要判断集合是否为空
转换成Student对象
拼接字符串

if(studentList.size()>0) {
	    	for(Object obj:studentList) {
	    		Student  student=(Student)obj;
	    		String  studentinfo=student.getStuid()+"-"+
	    				student.getStuname()+"-"+student.getStuage()+"-"+
	    				student.getStuaddress();
	    		out.write(studentinfo);
	    		out.newLine();
	    		
	    	}
	    	out.flush();
	    	out.close();
	    	}

写出字符串
out.write(studentinfo);

这个不能换行,创建高级流BufferedWriter
调用newLine();方法
为了实现一直写
需要给输出流进行追加

BufferedWriter  out=new BufferedWriter(new FileWriter(file,true));	

刷新和关流

out.flash();
out.close();
	//定义保存学生信息的文件对象
	private static File file=new File("student.txt");
	//将集合中信息保存到文件
	private void saveStudentInfoToFile(ArrayList studentList) {	
		try {
		//3创建字符输出流对象
		BufferedWriter  out=new BufferedWriter(new FileWriter(file,true));	
		if(studentList.size()>0) {
	    	for(Object obj:studentList) {
	    		Student  student=(Student)obj;
	    		String  studentinfo=student.getStuid()+"-"+
	    				student.getStuname()+"-"+student.getStuage()+"-"+
	    				student.getStuaddress();
	    		out.write(studentinfo);
	    		out.newLine();
	    	}
	    	out.flush();
	    	out.close();
	    	}
	    	}catch(Exception e) {
	    		e.printStackTrace();
	    	}
	}
可以把创建文件对象过程写在集合信息保存到文件【saveStudentInfoToFile】方法外定义成全局变量。

添加学生信息的数据访问方法中

必须先添加数据到集合

boolean flag=studentList.add(student);

在将集合中的数据写入文件

saveStudentInfoToFile(studentList);

清除集合
为什么要添加集合,如果不清除集合。
重新添加数据时会将集合原有的数据在文件中增加一次。
例如:
第一次操作添加学生张三,第二次操作添加学生对象李四
文件里面会保存三个学生对象,其中张三重复一次,是因为第二次添加的时候集合没有被清空,学生对象张三还在集合中。
在这里插入图片描述

 studentList.clean();

返回

return flag;

读取

读取文件中的数据到集合

因为输出输入流有异常,直接写好try{}catch{}

创建字符输入流对象

BufferedReader in=new BufferedReader(new FileReader(file));

定义读取来的每一条学生信息

接下来遍历每一条信息
需要用while循环,因为不知道有多少条信息

解析String类型的学生信息成学生对象
需要一个参数为String类型的信息

学生信息用“-”划分,利用split("-");拆分,拆分成String类型数组
接下来准备一个Student的对象收值。

Student student=new Student();

要将String类型转成int型

student.setStuid(Interger.parseInt(stuarray[0]));
	//解析String类型的学生信息成学生对象
	public Student jiexiStringStuInfo(String stuinfo) {
		String stuarray[]=stuinfo.split("-");
		Student student=new Student();
		student.setStuid(Integer.parseInt(stuarray[0]));
		student.setStuname(stuarray[1]);
		student.setStuage(Integer.parseInt(stuarray[2]));
		student.setStuaddress(stuarray[3]);
		return student;
		
	}

调用解析方法,将解析后的数据放入集合。然后关流。

	//读取文件到集合
	public void readFileToList() {
		try {
			//创建字符输入流对象
			BufferedReader in=new BufferedReader(new FileReader(file));
			//定义读取来的每一条学生信息
			String stuinfo=null;
			while((stuinfo=in.readLine())!=null) {
				//解析String类型的学生信息成学生对象
				studentList.add(jiexiStringStuInfo(stuinfo));
			}
			in.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

然后将readFileToList();放到查询所有学生信息的数据访问方法

因为没有清空,查询时会把上次放在集合中的数据打印出。因此要在读取文件中的数据到集合之前清空集合里面的数据,把 studentList.clear(); 添加在readFileToList()方法里。

但是,如果添加一条学生信息再查询,会把上一次文件里的学生信息添加进去,因为此时我们没有清空集合。所以要在查询所有学生信息的数据访问方法时清空集合。

//查询所有学生信息的数据访问方法
	public ArrayList selectAll() {
		//清空集合
		studentList.clear();
		readFileToList();
		return studentList;
	}

根据学号查询学生信息的数据方法

清空集合readFileToList();
直接调用readFileToList();方法


	//根据学号查询学生信息的数据方法
	public Student selectByStuid(int stuid) {
		//清空集合
		studentList.clear();
		readFileToList();
		Student student=null;
		for(Object obj:studentList) {
			Student stu=(Student)obj;
			if(stu.getStuid()==stuid) {
				student=stu;
				break;
			}
		}

根据学号修改学生信息的数据方法

清空集合readFileToList();
直接调用readFileToList();方法查询到学生对象然后替换

在这里会出现数组下标越界错误。
原因是

Object obj=studentList.set(studentList.indexOf(student),newStudent);

中的

studentList.indexOf(student)

出现错误
因为对象是上一个集合对象,被清除了
解决方法
再去查一次

//获得了id
Student newListstudent=selectByStuid(student.getStuid());

Object obj=studentList.set(studentList.indexOf(newListstudent), newStudent);

接下来保存

这是以前的对象
集合里面有值,此时不能清空,因为一清空集合就没值,不能保存,所以只能清空文件。
如果文件存在,及删除文件。最后将集合写入文件。

//根据学号修改学生信息数据访问数据方法
	public boolean updateByStuid(Student student,Student newStudent) {
		//清空集合
		studentList.clear();
		readFileToList();
		Student newListstudent=selectByStuid(student.getStuid());
		Object obj=studentList.set(studentList.indexOf(newListstudent), newStudent);
		if(file.exists()) {
			file.delete();
		}
		saveStudentInfoToFile(studentList);
		if(obj!=null) {
			return true;
		}
		return false;
	}

根据学号删除学生信息数据访问方法

已经把id传到dao层的操作方法上了

	//根据学号删除学生信息数据访问方法
	public boolean deleteByStuid(Student student) {
		//清空集合
		studentList.clear();
		readFileToList();
		Student newListstudent=selectByStuid(student.getStuid());
		boolean flag=studentList.remove(newListstudent);
		if(file.exists()) {
			file.delete();
		}
		saveStudentInfoToFile(studentList);
		return flag;
	}

删除所有学生信息数据访问方法

直接删除文件

//删除所有学生信息数据访问方法
	public boolean deleteAll() {
		boolean flag=false;
		if(file.exists()) {
			flag=file.delete();
			try {
				file.createNewFile();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

源代码:

package com.jindi.bean;
/**
 * 保存学生信息的java类
 */
public class Student {
	//学生学号
		private int stuid;
		//学生姓名
		private String stuname;
		//学生年龄
		private int stuage;
		//学生地址
		private String stuaddress;
		//由于上面定义的保存学生信息的变量时私有的
		//我们就需要为这些私有的变量提供可被访问的公共方法
		//setXXXX(参数)---设置变量值
		//getXXXX()---获取变量值
		public int getStuid() {
			return stuid;
		}
		public void setStuid(int stuid) {
			this.stuid=stuid;
		}
		
		public String getStuname() {
			return stuname;
		}
		public void setStuname(String stuname) {
			this.stuname=stuname;
		}
		
		public int getStuage() {
			return stuage;
		}
		public void setStuage(int stuage) {
			this.stuage=stuage;
		}
		
		public String getStuaddress() {
			return stuaddress;
		}
		public void setStuaddress(String stuaddress) {
			this.stuaddress=stuaddress;
		}
}

package com.jindi.service;

import java.util.ArrayList;

import com.jindi.bean.Student;
import com.jindi.dao.StudentDao;
import com.jindi.util.StudentHelper;

/**
 * 学生信息的业务访问类
 * @author wemme
 *
 */
public class StudentService {
	//添加学生信息的具体业务处理方法
	private void insertStudent(Student student) {
		StudentDao studao=new StudentDao();
		boolean flag=studao.insert(student);
		if(flag) {
			System.out.println("学生信息添加成功");
		}else {
			System.out.println("学生信息添加失败");
		}
	}
	//查询所有学生信息的具体业务处理方法
	private void selectAllStudent() {
		StudentDao studao=new StudentDao();
		ArrayList studentList=studao.selectAll();
		if(studentList.size()>0) {
			System.out.println("--------------");
			for(Object obj:studentList) {
				Student stu=(Student)obj;
				System.out.println("--"+stu.getStuid()+"-"+
						stu.getStuname()+"-"+stu.getStuage()+"-"+stu.getStuaddress()+"--");
				System.out.println("----------------");
			}
		}else {
			System.out.println("---没有记录---");
		}
	}
	//根据学号查询学生信息的具体业务处理方法
	private void selectStudentByStuid(int stuid) {
		StudentDao studao=new StudentDao();
		Student student=studao.selectByStuid(stuid);
		if(student!=null) {
			System.out.println("--"+student.getStuid()+"-"+
					student.getStuname()+"-"+student.getStuage()+"-"+student.getStuaddress()+"--");
			System.out.println("----------------");
		}else {
			System.out.println("---没有记录---");
		}
	}
	//根据学号修改学生信息的具体业务处理方法
	private void updateStudentByStuid(int stuid) {
		StudentDao studao=new StudentDao();
		Student student=studao.selectByStuid(stuid);
		if(student!=null) {
			Student newStudent=StudentHelper.getInputStudent();
			boolean flag=studao.updateByStuid(student,newStudent);
			if(flag) {
			System.out.println("修改成功");
		}else {
			System.out.println("修改失败");
		}
	}else{
		System.out.println("----没有记录----");
	}
	}
	//根据学号删除学生信息的具体业务处理方法
	private void deleteStudentByStuid(int stuid) {
		StudentDao studao=new StudentDao();
		Student student=studao.selectByStuid(stuid);
		if(student!=null) {
			boolean flag=studao.deleteByStuid(student);
			if(flag) {
				System.out.println("删除成功");
			}else {
				System.out.println("删除失败");
			}
		}else {
			System.out.println("---没有记录---");
		}
	}
	//删除所有学生信息的具体业务处理方法
	private void deleteAll() {
		StudentDao studao=new StudentDao();
		boolean flag=studao.deleteAll();
		if(flag) {
			System.out.println("删除所有信息成功");
		}else {
			System.out.println("删除所有信息失败");
		}
	}
	/**
	 * 具体业务处理方法
	 * 【根据键盘输入操作具体编号】
	 */
	public void service(int setvice_code) {
		switch(setvice_code) {
		case 1:System.out.println("添加学生信息");
			insertStudent(StudentHelper.getInputStudent());
			break;
		case 2:System.out.println("查询所有学生信息");
			selectAllStudent();
			break;
		case 3:System.out.println("根据学号查询学生信息");
			selectStudentByStuid(StudentHelper.getIntValue("请输入要查询的学生学号"));
			break;
		case 4:System.out.println("根据学号修改学生信息");
			updateStudentByStuid(StudentHelper.getIntValue("请输入要修改的学生学号"));
			break;
		case 5:System.out.println("根据学号删除学生信息");
			deleteStudentByStuid(StudentHelper.getIntValue("请输入要删除学生的学号"));
			break;
		case 6:System.out.println("删除所有学生信息");
			deleteAll();
			break;
		case 0:System.out.println("退出系统");
			System.exit(0);
			break;
		default:System.out.println("请输入正确编号");
			break;
		}
	}
}

package com.jindi.util;

import java.util.Scanner;

import com.jindi.bean.Student;

/**
 * 学生信息处理帮助类
 * @author wemme
 *
 */
public class StudentHelper {
	public static void getMenu() {
		System.out.println("---简单的学生管理系统---");
		System.out.println("1=添加学生信息,2=查询所有学生信息,3=根据学号查询学生信息,4=根据学号修改学生信息5=根据学号删除学生信息,6=删除所有学生信息,0=退出学生系统");
	}
	//键盘输入得到一个整数值
	public static int getIntValue(String tipinfo) {
		int code=0;
		try {
			System.out.println(tipinfo);
			Scanner input=new Scanner(System.in);
			code=input.nextInt();
		}catch(Exception e) {
			System.out.println("输入有误,请重新输入");
			code=getIntValue(tipinfo);
		}
		return code;
	}
	
	//从键盘得到一个String
	public static String getStringValue(String tipinfo) {
		String info=null;
		try {
			System.out.println(tipinfo);
			Scanner input=new Scanner(System.in);
			info=input.nextLine();
			info=info.trim();
			if(info==null ||info.length()<=0) {
				throw new Exception("String不能为空");
			}
		}catch(Exception e) {
			System.out.println("不能为空,请重新输入");
			info=getStringValue(tipinfo);
		}
		return info;
	}
	//键盘输入一个学生信息
	public static Student getInputStudent() {
		Student student=null;
		//得到学生学号
		int stuid=getIntValue("--请输入学生学号--");
		//得到学生姓名
		String stuname=getStringValue("--请输入学生姓名--");
		//得到学生年龄
		int stuage=getIntValue("--请输入学生年龄--");
		//得到学生地址
		String stuaddress=getStringValue("--请输入学生地址--");
		student=new Student();
		student.setStuid(stuid);
		student.setStuname(stuname);
		student.setStuage(stuage);
		student.setStuaddress(stuaddress);
		return student;
	}
}

package com.jindi.dao;

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

import com.jindi.bean.Student;

	/**
	 * 提供学生信息的数据访问类
	 */
public class StudentDao {
	//保存信息的具体集合
	private static ArrayList studentList=new ArrayList();
	
//定义保存学生信息的文件对象
	private static File file=new File("student.txt");
//将集合中信息保存到文件
	private void saveStudentInfoToFile(ArrayList studentList) {
//开始		
		try {
		//3创建字符输出流对象
		BufferedWriter  out=new BufferedWriter(new FileWriter(file,true));	
		if(studentList.size()>0) {
	    	for(Object obj:studentList) {
	    		Student  student=(Student)obj;
	    		String  studentinfo=student.getStuid()+"-"+
	    				student.getStuname()+"-"+student.getStuage()+"-"+
	    				student.getStuaddress();
	    		out.write(studentinfo);
	    		out.newLine();
	    		
	    	}
	    	out.flush();
	    	out.close();
	    	}
	    	}catch(Exception e) {
	    		e.printStackTrace();
	    	}
	}

	//读取文件到集合
	public void readFileToList() {
		try {
			//清空集合
			studentList.clear();
			//创建字符输入流对象
			BufferedReader in=new BufferedReader(new FileReader(file));
			//定义读取来的每一条学生信息
			String stuinfo=null;
			while((stuinfo=in.readLine())!=null) {
				//解析String类型的学生信息成学生对象
				studentList.add(jiexiStringStuInfo(stuinfo));
			}
			in.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
//解析String类型的学生信息成学生对象
	public Student jiexiStringStuInfo(String stuinfo) {
		String stuarray[]=stuinfo.split("-");
		Student student=new Student();
		student.setStuid(Integer.parseInt(stuarray[0]));
		student.setStuname(stuarray[1]);
		student.setStuage(Integer.parseInt(stuarray[2]));
		student.setStuaddress(stuarray[3]);
		return student;
		
	}
	
//开始	
	
	//添加学生信息的数据访问方法
	public boolean insert(Student student) {
		//清空集合
		studentList.clear();
		//先添加到数据到集合
		boolean flag=studentList.add(student);
		//将集合数据写入到文件
		saveStudentInfoToFile(studentList);
		//清空集合
		studentList.clear();
		return flag;
	}
	
	//查询所有学生信息的数据访问方法
	public ArrayList selectAll() {
		//清空集合
		studentList.clear();
		readFileToList();
		return studentList;
	}
	
	//根据学号查询学生信息的数据方法
	public Student selectByStuid(int stuid) {
		//清空集合
		studentList.clear();
		readFileToList();
		Student student=null;
		for(Object obj:studentList) {
			Student stu=(Student)obj;
			if(stu.getStuid()==stuid) {
				student=stu;
				break;
			}
		}
		return student;
	}
	
	//根据学号修改学生信息数据访问数据方法
	public boolean updateByStuid(Student student,Student newStudent) {
		//清空集合
		studentList.clear();
		readFileToList();
		Student newListstudent=selectByStuid(student.getStuid());
		Object obj=studentList.set(studentList.indexOf(newListstudent), newStudent);
		if(file.exists()) {
			file.delete();
		}
		saveStudentInfoToFile(studentList);
		if(obj!=null) {
			return true;
		}
		return false;
	}
	
	//根据学号删除学生信息数据访问方法
	public boolean deleteByStuid(Student student) {
		//清空集合
		studentList.clear();
		readFileToList();
		Student newListstudent=selectByStuid(student.getStuid());
		boolean flag=studentList.remove(newListstudent);
		if(file.exists()) {
			file.delete();
		}
		saveStudentInfoToFile(studentList);
		return flag;
	}
	
	//删除所有学生信息数据访问方法
	public boolean deleteAll() {
		boolean flag=false;
		if(file.exists()) {
			flag=file.delete();
			try {
				file.createNewFile();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}
}

package com.jindi.main;

import com.jindi.service.StudentService;
import com.jindi.util.StudentHelper;

public class StudentMain {

	public static void main(String[] args) {
		while(true) {
			StudentHelper.getMenu();
			int setvice_code=StudentHelper.getIntValue("请输入对应的编号");
			StudentService studentService=new StudentService();
			studentService.service(setvice_code);
					
		}
	}

}

运行结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值