学生信息系统

来源:互联网 发布:微商城怎么查询数据库 编辑:程序博客网 时间:2024/04/30 11:33
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.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;




public class TestInformation implements Serializable{

Map map1 = new TreeMap();
File file =new File("d:\\1.dat");
public TestInformation() {
if(file.exists()){
readFromFile();
  
}
}

     
public void addInformation() {
System.out.println("请输入学习基本信息,格式如下:");
System.out.println("学号 姓名 年龄 性别 专业方向 ");
System.out.println("输入完成请回车");
try {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
String[] strs = new String[5];
strs = str.split(" ");
map1.put(strs[0],(strs[1] + " " + strs[2] + " " + strs[3] + " " + strs[4]));

} catch (Exception e) {
System.out.println("error");
}
getAllInformation();
}


public void getInformation() {
System.out.println("请选择查询方式:");
System.out.println("1 按学号查询");
System.out.println("2 按姓名查询");
System.out.println("3 按性别查询");
System.out.println("4 查询所有人信息");
Scanner input = new Scanner(System.in);
int b = input.nextInt();
if (b == 1) {
System.out.println("请输入学号:");
Scanner input1 = new Scanner(System.in);
String str1 = input1.nextLine();
if (str1 != null && map1.get(str1) != null) {
System.out.println("学号 姓名 年龄 性别 专业方向");
System.out.println(str1 + " " + map1.get(str1));
} else {
System.out.println("学号输入错误!");
}
mainMenu();


}
if (b == 2) {
System.out.println("请输入姓名:");
Scanner input1 = new Scanner(System.in);
String str1 = input1.nextLine();
Set kSet = map1.keySet();
Iterator kits = kSet.iterator();
System.out.println("学号 姓名 年龄 性别 专业方向");
while (kits.hasNext()) {
Object objk = kits.next();
String str = (String) map1.get(objk);
String[] strs = new String[4];
strs = str.split(" ");
if (str1 != null && str1.equals(strs[0])) {
System.out.print(objk + " ");
System.out.println(map1.get(objk));
}
}
mainMenu();
}
if (b == 3) {
System.out.println("请输入性别:");
Scanner input1 = new Scanner(System.in);
String str1 = input1.nextLine();
Set kSet = map1.keySet();
Iterator kits = kSet.iterator();
System.out.println("学号 姓名 年龄 性别 专业方向");
while (kits.hasNext()) {
Object objk = kits.next();
String str = (String) map1.get(objk);
String[] strs = new String[4];
strs = str.split(" ");
if (str1 != null && str1.equals(strs[2])) {
System.out.print(objk + " ");
System.out.println(map1.get(objk));
}
}
mainMenu();
}
if(b==4){
getAllInformation();
}


}


public void changeInformation() {
System.out.println("请输入你要修改的学生信息的学号");
Scanner input = new Scanner(System.in);
String str = input.nextLine();
if (str != null && map1.get(str) != null) {
try {
System.out.println("输入修改后的信息:");
System.out.println("学号 姓名 年龄 性别 专业方向(格式)");
Scanner input1 = new Scanner(System.in);
String str1 = input1.nextLine();
String[] strs = new String[5];
strs = str1.split(" ");
map1.remove(str);
map1.put(
strs[0],(strs[1] + " " + strs[2] + " " + strs[3] + " " + strs[4]));
System.out.println("修改成功!");
} catch (Exception e) {
System.out.println("error");
}
getAllInformation();
} else {
System.out.println("学号错误!");
}


mainMenu();


}


public void deleteInformation() {
System.out.println("请输入你要删除的学生信息的学号:");
Scanner input = new Scanner(System.in);
String str = input.nextLine();
if (str != null && map1.get(str) != null) {
map1.remove(str);
System.out.println("删除成功!");
getAllInformation();
} else {
System.out.println("学号错误!");
}


mainMenu();
}


public void quit() {
saveToFile();

System.exit(0);


}
public void saveToFile(){
        FileOutputStream fos=null;
        ObjectOutputStream oos=null;
        try {
fos=new FileOutputStream("d:\\1.dat");
oos=new ObjectOutputStream(fos);
oos.writeObject(map1);
oos.flush();
System.out.println("数据已保存!");


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{

try {
if(oos!=null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fos!=null)
try {
fos.close();
} catch (IOException e) {

e.printStackTrace();
}



}



}public void readFromFile(){

FileInputStream fis=null;
ObjectInputStream ois=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
Object obj=ois.readObject();
if(obj!=null){
map1=(Map)obj;
}
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if(ois!=null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}


}




}


public void mainMenu() {
 
   System.out.println("*****************欢迎访问学生信息系统***************");
System.out.println("请选择操作:");
System.out.println("1 添加");
System.out.println("2 修改");
System.out.println("3 删除");
System.out.println("4 查询");
System.out.println("5 退出");
Scanner input = new Scanner(System.in);
int a = input.nextInt();


if (a == 1) {
addInformation();


}
if (a == 2) {
changeInformation();


}
if (a == 3) {
deleteInformation();


}
if (a == 4) {
getInformation();
}
if (a == 5) {
quit();
}
}


public void getAllInformation() {
System.out.println("所有学生信息:");
System.out.println("学号 姓名 年龄 性别 专业方向 ");
Set kSet =map1.keySet();
Iterator kits = kSet.iterator();
while (kits.hasNext()) {
Object objk=kits.next();
System.out.print(objk + " ");
System.out.println(map1.get(objk));
}
mainMenu();

}


public static void main(String[] args) {
TestInformation t = new TestInformation();
t.mainMenu();


}


}
0 0
原创粉丝点击