java 学生管理系统_Java——简单实现学生管理系统

import java.io.*;

import java.util.ArrayList;

import java.util.Scanner;

class MyObjectOutputStream extends ObjectOutputStream{

public MyObjectOutputStream() throws IOException{

super();

}

public MyObjectOutputStream(OutputStream out) throws IOException {

super(out);

}

public void writeStreamHeader() throws IOException {

return;

}         //以上为解决对象流输入输出读写文件时的一些问题,重写库里的方法。

}

class Student implements Serializable {    //序列化这个类,只有成员变量无方法,作为结构体使用。

long studentid = 1803050200;                  //学号,姓名,性别,年龄,备注。

String studentname = "noname";

String Sexual = "unknow";

int age = 18;

String ps = "unknow";

}

public class DemoSystem {

public static void main (String args []) {

boolean end = true;

String c ;

c = "请选择系统功能项:"+"\n"+"\t"+"a.从文件中读入学生的基本信息"+"\n"+"\t"+"b.添加新学生的基本信息"+"\n"+"\t"+"c.学生基本信息显示"+"\n"+"\t"+"d.学生信息保存至文件"+"\n"+"\t"+"e.学生基本信息删除"+"\n"+"\t"+"f.学生基本信息的修改"+"\n"+"\t"+"g.学生基本信息查询"+"\n"+"\t"+"\t"+"1.按学号查询"+"\n"+"\t"+"\t"+"2.按姓名查询"+"\n"+"\t"+"h.退出系统"+"\n";   //方便打印管理系统界面。

Student stu[] = new Student[10];    //实例化“结构体”数组。

ArrayList al = new ArrayList();   //泛型数组,方便保存。单纯的对象流输入输出,并不方便。

for(int i = 0;i<10;i++) {       //初始化

stu[i] = new Student();

}

Scanner sc = new Scanner(System.in);    //用户输入

File f = new File("D:\\javawork","stu1.txt");  //文件创建

try {                 //因为文件读写可能出现异常,所以把语句放入try语句块内,方便捕捉异常

while(end) {        //循环开始,end为前面设的布尔值,初始值为true

System.out.println(c);   //打印管理系统菜单(界面)

char d = sc.next().charAt(0);   //等待用户输入对应的菜单项字母

switch(d) {       //匹配相应的用户输入的菜单字母,以执行其功能

case 'a':         //从文件读取学生信息

FileInputStream fileIn = new FileInputStream(f);   //初始化对象流。

ObjectInputStream objectIn = new ObjectInputStream(fileIn);

try {    //用try捕捉异常,可能出现找不到文件的现象

ArrayList al1 =  (ArrayList)objectIn.readObject();  //用对象流读取,并打印学生信息

for(int i = 0;i<10;i++) {

stu[i] = al1.get(i);

System.out.println("name:"+stu[i].studentname+"  Sexual:"+stu[i].Sexual+"  age:"+stu[i].age+"  id:"+stu[i].studentid+"  ps:"+stu[i].ps);

}

}

catch(ClassNotFoundException e) {

System.out.println(e);

}

objectIn.close();  //关闭对象流

fileIn.close();

break;

case 'b':    //添加新学生功能

int q = 0;

int flag = 0;

boolean end0 = true;

String name = "noname";

while(end0&&flag<10) {  //运用flag,以防数组已满,不能添加

if(stu[flag].studentname.compareTo(name)==0) {

q=flag;

end0 = false;

}

flag++;

}

for(;q<10;q++) {

if(stu[q].studentname.compareTo(name)==0) {

System.out.println("请输入新学生姓名:");

stu[q].studentname = sc.next();

System.out.println("请输入新学生学号:");

stu[q].studentid = sc.nextLong();

System.out.println("请输入新学生年龄:");

stu[q].age = sc.nextInt();

System.out.println("请输入新学生性别:");

stu[q].Sexual = sc.next();

System.out.println("请输入对新学生的备注:"+"\n");

stu[q].ps = sc.next();

System.out.println("添加新学生信息完毕!"+"\n");

}

System.out.println("是否继续?yes/no");

String anwser = sc.next();

if(anwser.compareTo("yes")==0) {

q=q;

}

if(anwser.compareTo("no")==0) {

q = 10;

}

}

break;

case 'c':      //显示基本学生信息

String s;

for(int i = 0;i<10;i++) {

s= "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;

System.out.println(s);

}

break;

case 'd':   //保存学生信息

FileOutputStream fileOut = new FileOutputStream(f);

ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);

if(f.length()<1) {

for(int i=0;i<10;i++) {

al.add(stu[i]);

}

objectOut.writeObject(al);

objectOut.flush();

}

else {

MyObjectOutputStream mos = new MyObjectOutputStream(fileOut);

for(int i=0;i<10;i++) {

al.add(stu[i]);

}

mos.writeObject(al);

mos.flush();

}

objectOut.close();

fileOut.close();

break;

case 'e':    //删除学生信息

boolean start1= true;

while(start1) {

System.out.println("请输入你想操作的学号:");

long si = sc.nextLong();

for(int i = 0;i<10;i++) {

if(stu[i].studentid==si) {

String str1 = "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;

System.out.println(str1);

System.out.println("\t"+"1.姓名"+"\n"+"2.性别"+"\n"+"3.年龄"+"\n"+"4.学号"+"\n"+"5.备注"+"\n");

System.out.println("请输入您要操作的序号:");

int l = sc.nextInt();

switch(l) {

case 1:

stu[i].studentname = "noname";

break;

case 2:

stu[i].Sexual="unknow";

break;

case 3:

stu[i].age=18;

break;

case 4:

stu[i].studentid = 1803050200;

break;

case 5:

stu[i].ps="unknow";

break;

default :

System.out.println("输入序号错误!");

}

}

}

System.out.println("是否继续删除学生信息?yes/no");

String me1 = sc.next();

if(me1.compareTo("yes")==0) {

start1 = true;

}

if(me1.compareTo("no")==0) {

start1 = false;

}

}

break;

case 'f':   //更改学生信息

boolean end2 = true;

System.out.println("请输入你想操作的学号:");

long si1 = sc.nextLong();

for(int i = 0;i<10;i++) {

if(stu[i].studentid==si1) {

String str2 = "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;

System.out.println(str2);

System.out.println("\t"+"1.姓名"+"\n"+"2.性别"+"\n"+"3.年龄"+"\n"+"4.学号"+"\n"+"5.备注"+"\n");

System.out.println("请输入您要操作的序号:");

int l1 = sc.nextInt();

while(end2) {

switch(l1) {

case 1:

System.out.println("请输入你更改后的姓名:");

stu[i].studentname = sc.next();

break;

case 2:

System.out.println("请输入你更改后的性别:");

stu[i].Sexual = sc.next();

break;

case 3:

System.out.println("请输入你更改后的年龄:");

stu[i].age = sc.nextInt();

break;

case 4:

System.out.println("请输入你更改后的学号:");

stu[i].studentid = sc.nextLong();

break;

case 5:

System.out.println("请输入你更改后的备注::");

stu[i].ps = sc.next();

break;

default :

System.out.println("输入序号错误!");

}

System.out.println("是否继续修改?yes/no");

String an1=sc.next();

if(an1.compareTo("yes")==0) {

end2 = true;

}

if(an1.compareTo("no")==0) {

end2 = false;

}

}

}

}

break;

case 'g':  //查询某个学生信息

boolean end4 = true;

while(end4) {

System.out.println("(1).按学号查询"+"\n"+"(2).按姓名查询"+"\n"+"输入操作序号,继续"+"\n");

int l3 = sc.nextInt();

if(l3==1) {

System.out.println("请输入查询学号:");

long stid2=sc.nextLong();

for(int i = 0;i<10;i++) {

if(stu[i].studentid==stid2) {

String str3 = "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;

System.out.println(str3);

}

}

}

if(l3==2) {

System.out.println("请输入查询姓名:");

String stna2 = sc.next();

for(int i = 0;i<10;i++) {

if(stna2.compareTo(stu[i].studentname)==0) {

String str4 = "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;

System.out.println(str4);

}

}

}

System.out.println("是否继续查询?yes/no");

String an4 = sc.next();

if(an4.compareTo("yes")==0) {

end4 =true;

}

if(an4.compareTo("no")==0) {

end4 =false;

}

}

break;

case 'h':

System.out.println("退出系统!");

end =false;

break;

default :

System.out.println("输入的操作字母错误!");

}

}

}

catch(IOException e) {

System.out.println(e.toString());

}

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值