/**
学生信息类
*/
package com.softeem.project4;
import java.io.Serializable;
public class Student implements Serializable{
int id;
String name;
String sex;
int age;
public Student(){
}
public Student(int id, String name, String sex, int age) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
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 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 "Student [id=" + id + ", name=" + name + ", sex=" + sex
+ ", age=" + age + "]";
}
}
/**
管理接口
*/
public interface Manager {
void add();
void change();
void select();
void delete();
}
/**
输出语句简化类
*/
public class t{
public static void tools(Object object){
System.out.println(object);
}
}
/**
测试类
*/
import java.io.BufferedOutputStream;
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.io.OutputStream;
import java.nio.BufferOverflowException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Test extends Student implements Manager {
int i;
ArrayList<Student> stu = new ArrayList<>();
File file = new File(
"C:\\Documents and Settings\\Administrator\\桌面\\新建 文本文档.txt");
ObjectInputStream ois;
ObjectOutputStream oos;
public void start() {
t.tools("*********");
t.tools("1.添加学生");
t.tools("2.修改学生");
t.tools("3.查询全体学生");
t.tools("4.删除学生");
t.tools("5.退出");
t.tools("请输入:");
manage();
}
public void manage() {
Scanner sc = new Scanner(System.in);
i = sc.nextInt();
switch (i) {
case 1:
add();
break;
case 2:
change();
break;
case 3:
select();
break;
case 4:
delete();
break;
case 5:
t.tools("谢谢使用!");
System.exit(0);
}
}
@Override
public void add() {
FileInputStream fis;
if (file.length() != 0) {
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
stu = (ArrayList<Student>) ois.readObject();
} catch (ClassNotFoundException | IOException e1) {
e1.printStackTrace();
}
}
Scanner sc = new Scanner(System.in);
t.tools("请输入学生学号:");
id = sc.nextInt();
t.tools("请输入学生姓名:");
name = sc.next();
t.tools("请输入学生性别:");
sex = sc.next();
t.tools("请输入学生年龄:");
age = sc.nextInt();
Student s = new Student(id, name, sex, age);
stu.add(s);
t.tools("是否继续添加:");
if (sc.next().equalsIgnoreCase("是")) {
add();
} else {
try {
FileOutputStream fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(stu);
} catch (IOException e) {
e.printStackTrace();
}
t.tools("添加成功!");
}
t.tools("进入主菜单请按1,退出请按2");
if (sc.nextInt() == 1) {
start();
} else {
t.tools("谢谢使用!");
System.exit(0);
}
}
@Override
public void change() {
t.tools("请输入需要修改的学生学号:");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
try {
ois = new ObjectInputStream(new FileInputStream(file));
stu = (ArrayList<Student>) ois.readObject();
FileOutputStream fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
for (Student student : stu) {
if (i == student.getId()) {
t.tools("修改性别请输入1\n修改姓名请输入2\n修改年龄请输入3");
int a = sc.nextInt();
if (a == 1) {
student.setSex(sc.next());
t.tools("修改成功");
} else if (a == 2) {
student.setName(sc.next());
t.tools("修改成功");
} else {
student.setAge(i);
t.tools("修改成功");
}
}
}
oos.writeObject(stu);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
t.tools("进入主菜单请按1,退出请按2");
if (sc.nextInt() == 1) {
start();
} else {
t.tools("谢谢使用!");
System.exit(0);
}
}
@Override
public void select() {
if(file.length() != 0){
try {
ois = new ObjectInputStream(new FileInputStream(file));
stu = (ArrayList<Student>) ois.readObject();
for (Student student : stu) {
t.tools(student);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Scanner sc = new Scanner(System.in);
t.tools("进入主菜单请按1,退出请按2");
if (sc.nextInt() == 1) {
start();
} else {
t.tools("谢谢使用!");
System.exit(0);
}
}
t.tools("无学生信息请添加学生");
Scanner sc = new Scanner(System.in);
t.tools("进入主菜单请按1,退出请按2");
if (sc.nextInt() == 1) {
start();
} else {
t.tools("谢谢使用!");
System.exit(0);
}
}
@Override
public void delete() {
t.tools("请输入需要删除的学生学号:");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
if(file.length()!=0){
try {
ois = new ObjectInputStream(new FileInputStream(file));
stu = (ArrayList<Student>) ois.readObject();
Iterator<Student> it = stu.iterator();
while(it.hasNext()){
if(it.next().getId()==i){
it.remove();
break;
}
}
FileOutputStream fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(stu);
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
t.tools("进入主菜单请按1,退出请按2");
if (sc.nextInt() == 1) {
start();
} else {
t.tools("谢谢使用!");
System.exit(0);
}t.tools("无学生信息");
}
}
public static void main(String[] args) {
new Test().start();
}
}