Student类
package first;
import java.io.Serializable;
class Student implements Serializable{//实现接口Serializable
private int id;
private String name;
private String jiguan;
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 getJiguan() {
return jiguan;
}
public void setJiguan(String jiguan) {
this.jiguan = jiguan;
}
@Override
public String toString() {
return "Studet [id=" + id + ", name=" + name + ", jiguan=" + jiguan + "]";
}
public Student(int id,String name,String jiguan) {
super();
this.id=id;
this.name=name;
this.jiguan=jiguan;
}
}
测试类
package first;
import java.io.*;
import java.util.*;
import java.util.*;
public class test {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
System.out.println("请输入登录密码:");
Scanner key =new Scanner(System.in);
int key1 = key.nextInt();
if(key1==123) {
menu();
}
else {
System.out.println("密码错误,请退出重新输入密码!");
}
}
private final static String absolutePath = "D:\\test\\jiguan.dat";
public static void menu() throws Exception {
while(true){
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("*6、删除 *");
System.out.println("*7、修改 *");
System.out.println("*8、退出程序 *");
System.out.println("请输入你要实行操作的号码:");
Scanner sc =new Scanner(System.in);
int xz = sc.nextInt();
switch(xz){
case 1:input();break;
case 2:show_jg();break;
case 3:inquire();break;
case 4:jiguan();break;
case 5:add();break;
case 6:delete();break;
case 7:revise();break;
case 8:System.exit(0);
default:
System.out.println("请输入正确的选项!");
Scanner sc1 = new Scanner(System.in);
String q = sc1.next();
input();
break;
}
}
}
public static void determine() throws FileNotFoundException, IOException {//判断文件是否存在
File file=new File(absolutePath);
if (!file.exists()) {
file.createNewFile();
List <Student> studentlist=new ArrayList<Student>();
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(studentlist);
oos.flush();
oos.close();
}
}
public static void input()throws Exception {//输入学生信息
determine();
List<Student> studentlist=getStudent();
System.out.println("开始输入信息:");
for(int i=0;i<2;i++) {
Scanner scanner=new Scanner(System.in);
System.out.print("请输入第"+(i+1)+"个学生的学号:");
int id = scanner.nextInt();
System.out.print("请输入第"+(i+1)+"个学生的姓名:");
String name = scanner.next();
System.out.print("请输入第"+(i+1)+"个学生的籍贯:");
String jg = scanner.next();
Student stu=new Student(id,name,jg);
studentlist.add(stu);
}
System.out.println("学生信息添加成功!");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(absolutePath));
oos.writeObject(studentlist);
oos.close();
}
public static void show() throws Exception {//显示所有学生的所有信息
List<Student> stus=getStudent();
System.out.println(stus.toString());
}
public static void show_jg() throws Exception {//显示所有学生的籍贯信息
List<Student> stus=getStudent();
for (Student student : stus) {
System.out.println(student.getName()+":"+student.getJiguan());
}
}
public static List<Student> getStudent() throws Exception{//将read得到的object数据转为List<Student>型
List<Student> stus = (List<Student>) read();
return stus;
}
public static Object read() throws Exception {//读取文件数据,返回object型数据
Object obj = null;
ObjectInputStream inputStream = null;
try {
inputStream = new ObjectInputStream(new FileInputStream(absolutePath));
obj=inputStream.readObject();
System.out.println("数据载入成功");
} catch (EOFException e) {
obj = new ArrayList<Student>();
System.out.println("无数据,删除文件重新写入数据");
}catch (Exception e) {
e.printStackTrace();
System.out.println("数据载入失败");
}finally {
if(inputStream != null) {
inputStream.close();
}
}
return obj;
}
public static void write(List<Student> stu) throws Exception {//输出信息
ObjectOutputStream outputstream=new ObjectOutputStream(new FileOutputStream("d:/test/jiguan.dat"));
outputstream.writeObject(stu);
System.out.println(stu.toString());
outputstream.close();
}
public static void inquire() throws Exception {//通过学号或者姓名查询籍贯
System.out.println("请选择你要查询学生籍贯的方式:");
System.out.println("如果通过学号查询 请输入1:");
System.out.println("如果通过姓名查询 请输入2:");
Scanner sc=new Scanner(System.in);
int choice =sc.nextInt();
switch(choice) {
case 1:xuehao();break;
case 2:xingming();break;
default:break;
}
}
public static void xuehao() throws Exception {//用学号查询籍贯
List<Student> stus=getStudent();
System.out.println("请输入你要查找的学号:");
Scanner sc=new Scanner(System.in);
int sc1=sc.nextInt();
for(int i=0;i<stus.size();i++)
{
if(stus.get(i).getId()==sc1) {
System.out.println("该学生籍贯为:"+stus.get(i).getJiguan());
}
}
}
public static void xingming() throws Exception {//用姓名查询籍贯
List<Student> stus=getStudent();
System.out.println("请输入你要查找的姓名:");
Scanner name=new Scanner(System.in);
String sc1=name.next();
for(int i=0;i<stus.size();i++)
{
if(stus.get(i).getName().equals(sc1)) {
System.out.println("该学生籍贯为:"+stus.get(i).getJiguan());
}
}
}
public static void jiguan() throws Exception {//籍贯查询到所有学生
List<Student> stus=getStudent();
System.out.println("请输入你要查找的籍贯");
Scanner jiguan=new Scanner(System.in);
String sc1=jiguan.next();
for(int i=0;i<stus.size();i++)
{
if(stus.get(i).getJiguan().equals(sc1)) {
System.out.println("该籍贯的学生有:"+stus.get(i).getName()+" ");
}
}
}
public static void revise() throws Exception {//修改学生籍贯信息
List<Student> stus=getStudent();
show();
System.out.println("以上是学生所有信息");
System.out.println("如果你想修改某学生籍贯信息,请输入2:");
Scanner num=new Scanner(System.in);
int sc2=num.nextInt();
if(sc2==2) {
System.out.println("请输入你要查找的学号");
Scanner sc=new Scanner(System.in);
int sc1=sc.nextInt();
for(int i=0;i<stus.size();i++)
{
if(stus.get(i).getId()==sc1) {
System.out.println("请输入你要修改的籍贯信息");
Scanner message=new Scanner(System.in);
String new_message=message.next();
stus.get(i).setJiguan(new_message);
}
}
System.out.println("修改信息成功!新数据如下");
write(stus);
System.out.println("如果你想继续修改信息,请选择1:如果你不想继续修改信息,请选择2:");
Scanner num1=new Scanner(System.in);
int sc3=num.nextInt();
if(sc3==1) {
revise();
}
if(sc1==2) {
menu();
}
}
}
public static void delete() throws Exception {//删除学生籍贯信息
List<Student> stus=getStudent();
show();
System.out.println("以上是学生所有信息");
System.out.println("如果你想删除某学生籍贯信息,请输入1:");
Scanner num=new Scanner(System.in);
int sc2=num.nextInt();
if(sc2==1) {
System.out.println("请输入你要查找的学号:");
Scanner sc=new Scanner(System.in);
int sc1=sc.nextInt();
for(int i=0;i<stus.size();i++)
{
if(stus.get(i).getId()==sc1) {
System.out.println("请输入null,将原有籍贯信息用null空表示");
Scanner message=new Scanner(System.in);
String new_message=message.next();
stus.get(i).setJiguan(new_message);
}
}
}
System.out.println("删除成功!新数据如下");
write(stus);
System.out.println("如果你想继续删除信息,请选择1:;如果你不想继续删除信息,请选择2:");
Scanner num1=new Scanner(System.in);
int sc1=num.nextInt();
if(sc1==1) {
delete();
}
if(sc1==2) {
menu();
}
}
public static void add() throws Exception {//添加信息
List<Student> stus=getStudent();
show();
System.out.println("以上是学生所有信息");
System.out.println("如果你想添加某学生籍贯信息,请输入3:");
Scanner num=new Scanner(System.in);
int sc2=num.nextInt();
if(sc2==3) {
System.out.println("请输入你要添加的学生信息:");
Scanner scanner=new Scanner(System.in);
System.out.print("请输入学生的学号:");
int id = scanner.nextInt();
System.out.print("请输入学生的姓名:");
String name = scanner.next();
System.out.print("请输入学生的籍贯:");
String jg = scanner.next();
Student s=new Student(id, name, jg);
s.setId(id);
s.setName(name);
s.setJiguan(jg);
stus.add(s);
}
System.out.println("添加成功!新数据如下");
write(stus);
System.out.println("如果你想继续追加信息,请选择1:如果你不想继续追加信息,请选择2:");
Scanner num1=new Scanner(System.in);
int sc1=num.nextInt();
if(sc1==1) {
add();
}
if(sc1==2) {
menu();
}
}
}