基于java控制台的学生信息管理系统开发与实现

2024-12-30 22:45:12
推荐回答(2个)
回答1:

package d;
import java.io.*;
import java.util.*;
public class Treemap {
static TreeMap tm=new TreeMap(); //实例化一个树对象
static Student s ;//声明一个静态变量S
public static void main(String args[]){
try{
FileInputStream fis=new FileInputStream("students.txt"); //实例化一个FileInputStream对象并且读取students.txt文件
ObjectInputStream ois=new ObjectInputStream(fis);

while((s=(Student)(ois.readObject()))!=null)
{
tm.put(s.stunum,s);
}
ois.close();
}catch(IOException ioe){
}catch(ClassNotFoundException c){
}
try{
while(true){
System.out.println("[0]主菜单 [1]查找 [2]输入 [3]删除 [4]全部列出 [5]退出"); //显示菜单
System.out.print("请选择操作:");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
String ch=br.readLine();
int c=Integer.parseInt(ch);
switch(c){
case 0:
break;
case 1:
search();
break;
case 2:
input();
break;
case 3:
delete();
break;
case 4:
listAll();
break;
case 5:
tm.clear();
System.exit(0);
break;
default:
System.out.println("输入有误!");
}
}
}catch(IOException e){
}catch(NumberFormatException e){
System.out.println("输入有误!");
}
}
static void listAll()
{
tm.clear();
try{
FileInputStream fis=new FileInputStream("students.dat");
ObjectInputStream ois=new ObjectInputStream(fis);

while((s=(Student)(ois.readObject()))!=null)
{
tm.put(s.stunum,s);
System.out.println(s.stunum+" "+s.stuname);
}
ois.close();
}catch(IOException ioe){
}catch(ClassNotFoundException c){
}
}
static void input(){
String str1=null,str2=null;
try{
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.print("请输入学号:");
str1=br.readLine();
System.out.print("请输入姓名:");
str2=br.readLine();
}catch(IOException e){
System.out.println(e);
}
s=new Student(str1,str2);
tm.put(s.stunum,s);
//写文件
int n=1;
try{
FileOutputStream fos=new FileOutputStream("students.dat");
ObjectOutputStream oos=new ObjectOutputStream(fos);

for(Iterator it=tm.values().iterator();it.hasNext();)
{
s=(Student)it.next();

oos.writeObject(s);
}
oos.close();
}catch(IOException e){
System.out.println(e);
}
}
static void search()
{
String key=null;
try{
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.print("请输入一个学号:");
key=br.readLine();
}catch(IOException e){
System.out.println(e);
}

if(tm.containsKey(key))
{
s=(Student)tm.get(key);
System.out.println(s.stunum+" "+s.stuname);
}
else
System.out.println("没有");
}
static void delete()
{

String key=null;
try{
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.print("请输入学号:");
key=br.readLine();
}catch(IOException e){
}
if(tm.remove(key).equals(null))
{
System.out.println("没有");
}
try{
FileOutputStream fos=new FileOutputStream("students.dat");
ObjectOutputStream oos=new ObjectOutputStream(fos);
for(Iterator it=tm.values().iterator();it.hasNext();)
{
s=(Student)it.next();
oos.writeObject(s);
}
oos.close();
}catch(IOException e){
System.out.println(e);
}
}
}
class Student implements Serializable{
String stunum;
String stuname;
public Student(String stunum,String stuname){
this.stunum=stunum;
this.stuname=stuname;
}
}

回答2:

不需要数据库吗?