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<Student> al = new ArrayList<Student>(); //泛型數組,方便保存。單純的對象流輸入輸出,並不方便。
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<Student> al1 = (ArrayList<Student>)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());
}
}
}
