学生类
import java.io.Serializable;
/**
* @author
* @ClassName Student
* @description: TODO
* @datetime 2023年 04月 04日 15:55
* @version: 1.0
*/
public class Student implements Serializable {
private int id; //学号
private String name; //姓名
private int score; //成绩
public Student() {
}
public Student(int id, String name, int score) {
this.id = id;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", score=" + score +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
管理类
import java.io.*;
import java.util.*;
/**
* @author 李政
* @ClassName Manager
* @description: TODO
* @datetime 2023年 04月 04日 15:57
* @version: 1.0
*/
public class Manager {
ArrayList<Student> list = new ArrayList<Student>();
Scanner input = new Scanner(System.in);
public long getsize(){
long len= list.size();
return len;
}
//获取文件内容
public ArrayList<Student> getTxt(){
ObjectInputStream ois = null;
InputStreamReader oiss = null;
try {
ois = new ObjectInputStream(new FileInputStream(new File("D:/1.txt")));
oiss = new InputStreamReader(ois);
char[] chars = new char[1024];
if (oiss.read(chars)!=0){
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (ois!=null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
//添加学生
public void add(Student s){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("D:/1.txt")));
oos.writeObject(list.add(s));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (oos!=null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//修改学生信息
public void update(int id){
if (getsize()>0){
list = getTxt();
}
for (int i = 0; i < list.size() ; i++) {
if (list.get(i).getId()==id){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("D:/1.txt")));
System.out.println("请输入修改后的姓名:");
String name = input.next();
System.out.println("请输入修改后的成绩:");
int score = input.nextInt();
list.get(i).setName(name);
list.get(i).setScore(score);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (oos!=null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
//删除学生
public void delete(int id){
if (getsize()>0){
list = getTxt();
}
for (int i = 0; i <list.size() ; i++) {
if (list.get(i).getId()==id){
list.remove(i);
}
}
}
//查询一个学生
public void findByID(int id){
if (getsize()>0){
list = getTxt();
}
for (int i = 0; i < list.size(); i++) {
int id1 = list.get(i).getId();
if (id1==id){
System.out.println(list.get(i));
}
}
}
//查看所有学生
public void findAll(){
if (getsize()>0){
list = getTxt();
}
for (int i = 0; i < list.size() ; i++) {
System.out.println(list.get(i));
}
}
}
测试类
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import javax.print.DocFlavor;
import java.util.Scanner;
/**
* @author 李政
* @ClassName Test
* @description: TODO
* @datetime 2023年 04月 04日 15:38
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
Manager mm = new Manager();
Scanner input = new Scanner(System.in);
String a = "y";
while (a.equals("y")) {
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("*****************");
System.out.println("请选择:");
int b = input.nextInt();
switch (b){
case 1:
System.out.println("添加学生");
System.out.println("请输入要添加的学生学号:");
int id = input.nextInt();
System.out.println("请输入要添加的学生姓名:");
String name = input.next();
System.out.println("请输入要添加的学生成绩:");
int score = input.nextInt();
Student student = new Student(id,name,score);
mm.add(student);
break;
case 2:
System.out.println("修改学生信息");
System.out.println("请输入要修改的学生学号:");
int id2 = input.nextInt();
mm.update(id2);
break;
case 3:
System.out.println("删除学生");
System.out.println("请输入要删除的学生学号:");
int id3 = input.nextInt();
mm.delete(id3);
break;
case 4:
System.out.println("查询一个学生");
System.out.println("请输入要查询的学生学号:");
int id4 = input.nextInt();
mm.findByID(id4);
break;
case 5:
System.out.println("查询所有学生");
mm.findAll();
break;
}
}
}
}