学生管理系统(把数据存储到本地)
public class Demo1 {
/*
* 学生管理系统
* 1、增加学生
* 2、删除学生
* 3、修改学生
* 4、查询学生
* 5、显示所有学生
* 6、退出
* 学生属性:姓名、年龄、成绩
* */
public static void main(String[] args) {
new Menu().menu();
}
}
import java.util.Scanner;
//此类主要用于调用StuManageImp类中的方法的调用
public class Menu {
//实例化StuManageImp类
StuManageImp stuManageImp = new StuManageImp();
Scanner sc = new Scanner(System.in);
public void menu(){
System.out.println("-----学生信息管理系统-----");
stuManageImp.out();
//通过一个循环可以让用户一直增加信息
while (true){
System.out.println("1、增加学生 2、删除学生 3、修改学生 4、查询学生 5、显示所有学生 6、退出");
System.out.println("请你输入你需要完成的内容");
int key = sc.nextInt();
switch (key){
case 1:
stuManageImp.add();
break;
case 2:
stuManageImp.delete();
break;
case 3:
stuManageImp.remove();
break;
case 4:
stuManageImp.findByName();
break;
case 5:
stuManageImp.showAll();
break;
case 6:
stuManageImp.save();
stuManageImp.exit();
break;
default:
System.out.println("你输入的信息有误");
}
}
}
}
import java.io.Serializable;
//学生类
public class Student implements Serializable {
// 学生属性:姓名、年龄、成绩
private String name;
private int age;
private int score;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +"岁"+
", score=" + score +
'}';
}
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
/* 1、增加学生
* 2、删除学生
* 3、修改学生
* 4、查询学生
* 5、显示所有学生
* 6、退出*/
public class StuManageImp {
ArrayList<Student> collection = new ArrayList<>();
//定义全局的扫描器
Scanner sc = new Scanner(System.in);
//1、增加学生
public void add(){
//增加学生之前,我们需要创建一个容器,去存储学生的信息,这个容器我们定义在上面
//在增加学生之前我们先给用户一些提示
System.out.println("请你输入你要增加的学生的姓名:");
String name = sc.next();
System.out.println("请你输入你要增加的学生的年龄");
int age = sc.nextInt();
System.out.println("请你输入你要增加的学生的分数");
int score = sc.nextInt();
Student student = new Student(name, age, score);
//把用户增加的学生的信息存储到集合中
collection.add(student);
System.out.println("学生信息增加成功!!!");
}
//2、删除学生
public void delete(){
//删除学生信息之前,我们需要先找到这个学生,循环集合里的内容
for (int i = 0;i < collection.size();i++){
//给与用户提示要删除学生的名字
System.out.println("请你输入要删除学生的名字");
String name = sc.next();
if (collection.get(i).equals(name)){
//如果相等说明能够找到要删除学生的信息
collection.remove(i);
System.out.println("学生成功删除");
}else{
System.out.println("对不起,该校没有这个学生!!!");
}
}
}
//3、修改学生
public void remove(){
//在修改学生之前需要先找到该学生
for (int i = 0;i<collection.size();i++){
Student student = collection.get(i);
//提示用户要修改学生的信息
System.out.println("1、姓名 2、年龄 3、分数");
int key = sc.nextInt();
switch (key){
case 1:
System.out.println("请输入要修改的学生的姓名:");
String name = sc.next();
student.setName(name);
System.out.println("信息修改成功");
break;
case 2:
System.out.println("请输入要修改的学生的年龄:");
int age = sc.nextInt();
student.setAge(age);
System.out.println("信息修改成功");
break;
case 3:
System.out.println("请输入要修改的学生的分数");
int score = sc.nextInt();
student.setScore(score);
System.out.println("信息修改成功");
break;
default:
System.out.println("你输入的信息有误");
}
}
}
//4、查询学生
public void findByName(){
//在查询学生之前,先获取该学生
for (int i = 0;i < collection.size();i++){
Student student = collection.get(i);
//提示用户输入要修改的学生的姓名
System.out.println("请你输入要修改的学生的姓名:");
String name = sc.next();
//判断这个集合中是否有该学生
if (student.getName().equals(name)){
//此时说明已经找到该学生
System.out.println(student);
}else{
System.out.println("学校没有该学生,无法修改");
}
}
}
//5、显示所有学生
public void showAll(){
//修改学生之前先获取先获取集合中的学生
for (int i = 0;i<collection.size();i++){
Student student = collection.get(i);
System.out.println(student);
}
}
//6、退出
public void exit(){
//直接停止程序运行
System.exit(0);
}
//7、把集合中的数据存储到文件中
public void save() {
try (ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\Users\\fuwei\\Desktop\\Test\\pp\\aa.txt"))){
oos.writeObject(collection);
oos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//8、把文件中的数据写入到集合中
public void out(){
ObjectInputStream objectInputStream = null;
FileInputStream fileInputStream=null;
//先实例化一个通道
try {
fileInputStream=new FileInputStream("C:\\Users\\fuwei\\Desktop\\Test\\pp\\aa.txt");
objectInputStream = new ObjectInputStream(fileInputStream);
collection = (ArrayList<Student>)objectInputStream.readObject();
objectInputStream.close();
} catch (FileNotFoundException e) {
File file = new File("C:\\Users\\fuwei\\Desktop\\Test\\pp\\aa.txt");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}