对象序列化实现学生管理系统
项目要有用对象序列化实现对学生信息进行增删查改,并且用文件进行保存
第二次能够有之前的信息。
具体实现功能
用到了文件的输入和输出流,和对象序列化进行信息输入和输出
和一些基本的java基础
项目实施
- 建立一个学生类并且对他进行序列化(Serializable)。
- 建立一个学生管理类,t添加增删查改功能,用ArrayList进行信息零时存储。
- 创建一个文件写入和获取文件类容的类,对信息永久保存的实现。
- 建立程序入口,对管理功能的补充,和控制台显示的完善。
具体实现的类
- 学生类Student
- 学生管理类 StudentManage
- 文件写入和获取类FileLian
- 程序入口Test
学生类Student
package com.softeem.project4;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 6576600158133403115L;
//学号,姓名,性别,年龄
private int no ;
private String name;
private String sex;
private int age;
public Student(int no, String name, String sex, int age) {
super();
this.no = no;
this.name = name;
this.sex = sex;
this.age = age;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return no + "\t" + name + "\t" + sex + "\t" + age;
}
}
学生管理类 StudentManage __
package com.softeem.project4;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Objects;
public class StudentMange {
static ArrayList<Student> list = new ArrayList<>();
static{
try {
list=FileLian.getList(); //冲文件中获取学生列表信息
} catch (EOFException e) {
list=new ArrayList<Student>();
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//添加学生信息
public void add(Student s) throws FileNotFoundException, IOException{
list.add(s);
paixu();
FileLian.insert(list);
}
//按学号查找学生信息
public Student find(int no){
for (Student s : list) {
if (Objects.nonNull(s) && s.getNo() == no) {
return s;
}
}
return null;
}
//按学号删除学生信息
public void delete(int no) throws FileNotFoundException, IOException{
list.remove(find(no));
paixu();
FileLian.insert(list);
}
//显示列表信息
public void listOut(){
for (Student s : list) {
System.out.println(s);
}
}
//按年龄排序
public void paixu(){
Collections.sort(list,new Comparator<Student>() {
@Override
public int compare(Student a, Student b) {
return a.getAge()- b.getAge();
}
});
}
}
文件写入和获取类FileLian
package com.softeem.project4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class FileLian {
private static final String FAF ="src/aaa.dat"; //目标文件
//将学生序列化信息写入文件中
public static void insert(ArrayList<Student> list) throws FileNotFoundException, IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(FAF)));
oos.writeObject(list);
oos.close();
}
//从文件中获取学生序列化信息
public static ArrayList<Student> getList() throws FileNotFoundException, IOException, ClassNotFoundException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(FAF)));
ArrayList<Student> list =(ArrayList<Student>)ois.readObject();
ois.close();
return list;
}
}
程序入口Test
package com.softeem.project4;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Scanner;
public class Test {
StudentMange sm = new StudentMange();
Scanner sc = new Scanner(System.in);
public void mene() throws FileNotFoundException, IOException{
System.out.println("-----学生管理系统-----\n 【1】添加学生信息\n【2】查找学生信息\n【3】"
+ "修改学生信息\n【4】删除学生信息\n【5】显示学生信息\n【6】退出\n");
start();
}
private void start() throws FileNotFoundException, IOException {
sc = new Scanner(System.in);
int key = sc.nextInt();
switch (key) {
case 1:
addR();//添加学生信息
mene();
break;
case 2:
findR(); //按学号查找学生信息
mene();
break;
case 3:
editR();//按学号修改学生信息
mene();
break;
case 4:
deleteR();//按学号删除学生信息
mene();
break;
case 5:
listR();//显示列表信息
mene();
break;
case 6:
return;//结束程序
}
}
private void deleteR() throws FileNotFoundException, IOException {
System.out.println("请输入删除学生的学号");
sc = new Scanner(System.in);
int no=sc.nextInt();
sm.delete(no);
System.out.println("删除成功!");
}
private void editR() {
System.out.println("请输入查找的学生学号");
sc = new Scanner(System.in);
int no=sc.nextInt();
System.out.println("请输入:学号/姓名/性别/年龄");
sc = new Scanner(System.in);
String s =sc.nextLine();
String [] info =s.split("/");
Student stu = sm.find(no);
if(sm.find(Integer.parseInt(info[0])) != null ) {
System.out.println("学号不能相同!!!");
}else {
stu.setNo(Integer.parseInt(info[0]));
stu.setName(info[1]);
stu.setSex(info[2]);
stu.setAge(Integer.parseInt(info[3]));
System.out.println("修改成功!");
}
}
private void findR() {
System.out.println("请输入查找的学生学号");
sc = new Scanner(System.in);
int no=sc.nextInt();
System.out.println("学号\t姓名\t性别\t年龄");
System.out.println(sm.find(no));
}
private void listR() {
System.out.println("学号\t姓名\t性别\t年龄");
for (Student s : sm.list) {
System.out.println(s);
}
}
private void addR() throws FileNotFoundException, IOException {
System.out.println("请输入:学号/姓名/性别/年龄");
sc = new Scanner(System.in);
String s =sc.nextLine();
String [] info =s.split("/");
if(sm.find(Integer.parseInt(info[0])) != null ) {
System.out.println("学号已存在!!!");
}else {
Student stu = new Student(Integer.parseInt(info[0]),
info[1],
info[2],
Integer.parseInt(info[3]));
sm.add(stu);
System.out.println("添加成功!");
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
Test t =new Test();
t.mene();
}
}
在这里我们的教师学生日报管理系统已经写完了~~!
来看看成果吧 ^ v ^!
成果展示