打包;
importjava.io.*;
importjava.util.*;
publicclassTreemap{
staticTreeMaptm=newTreeMap();//实例化一个树对象
staticStudents;//声明一个静态变量S
publicstaticvoidmain(Stringargs[]){
try{
try{
fis=new("students.txt");//实例化一个对象,读取文件Students.txt
ois=new(fis);
while((s=(Student)(ois.readObject()))!=null)
{
tm.put(s.stunum,s);
}
ois.close();
}catch(IOExceptionioe){
}catch(c){
}
尝试{
while(true){
System.out.println("[0]主菜单[1]查找[2]输入[3]删除[4]全部列出[5]退出");//显示菜单
System.out.print("请选择进程:");
is=new(System.in);
br=new(is);
Stringch=br.readLine();
intc=Integer.parseInt(ch);
switch(c){
case0:
break;
case1:
search();
break;
case2:
input();
break;
case3:
delete();
break;
情况4:
listAll();
break;
情况5:
tm.clear();
System.exit(0);
break;
默认:
System.out.println("输入错误!");
}
}
}catch(IOExceptione){
}catch(e){
System.out.println("输入错误!");
}
}
staticvoidlistAll()
{
tm.clear();
尝试{
fis=new("学生.dat");
ois=new(fis);
while((s=(Student)(ois.readObject()))!=null)
{
tm.put(s.stunum,s);
System.out.println(s.stunum""s.stuname);
}
ois.close();
}catch(IOExceptionioe){
}catch(c){
}
}
staticvoidinput(){
Stringstr1=null,str2=null;
尝试{
InputStreamReaderis=newInputStreamReader(System.in);
BufferedReaderbr=newBufferedReader(is);
System.out.print("请输入学号:");
str1=br.readLine();
System.out.print("请输入您的姓名:");
str2=br.readLine();
}catch(IOExceptione){
System.out.println(e);
}
s=newStudent(str1,str2);
tm.put(s.stunum,s);
//写文件
intn=1;
试试{
FileOutputStreamfos=newFileOutputStream("students.dat");
ObjectOutputStreamoos=newObjectOutputStream(fos);
for(Iteratorit=tm.values().iterator();it.hasNext();)
{
s=(Student)it.next();
oos.写对象;
}
oos.close();
}catch(IOExceptione){
System.out.println(e);
}
}
staticvoidsearch()
{
Stringkey=null;
try{
InputStreamReaderis=newInputStreamReader(System.in);
BufferedReaderbr=newBufferedReader(is);
System.out.print("请输入学生编号:");
key=br.readLine();
}catch(IOExceptione){
System.out.println(e);
}
if(tm.containsKey(key))
{
s=(学生)tm.get(key);
System.out.println(s.stunum""s.stuname);
}
else
<系统。 out.println("否");}
staticvoiddelete()
{
Stringkey=null
try{
InputStreamReaderis=newInputStreamReader(System.in);
BufferedReaderbr=newBufferedReader(is);
System.out输入您的学号:“);
key=br.readLine();
}catch(IOExceptione){
}
if(tm.remove(key).equals(null))
{
System.out.println("否");
}
尝试{
FileOutputStreamfos=newFileOutputStream("students.dat");
ObjectOutputStreamoos=newObjectOutputStream(fos);
for(Iteratorit=tm.values().iterator();it.hasNext();)
{
s=(Student)it.next();
oos.writeObject(s);
}
oos.close();
}catch(IOExceptione){
System.out.println(e);
}
}
}
classStudentimplementsSerialized{
Stringstunum;
Stringstuname;
publicStudent(Stringstunum,Stringstuname){
this.stunum=stunum;
this.stuname=stuname;
}
}
定义一个类“Student”,代表学生信息。 要求如下:
(1)Student类的成员变量:
sNO代表学号;sSex代表性别;sAge代表年龄;sJava:代表Java课程成绩。
(2)Student类带参数的构造方法:
构造方法中,通过形参完成对成员变量的赋值。
(3)Student类的方法成员:
getNo():获取学号
getName():获取姓名;p>
getSex():获取性别;
getAge()获取年龄;
getJava():获取Java课程成绩
(4)根据班级学生定义,创建该类的五个对象,输出每个学生的信息,计算并输出五个学生Java语言成绩的平均值,并计算他们的Java语言成绩的最大值和最小值以及输出你。 */
上一篇:java学生信息管理系统代码
下一篇:java酒店管理系统代码