日期:2014-05-20 浏览次数:20843 次
package test;
//题目:完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部的信息保存在文件中,
//可以实现对学生信息的全面查找,输出全部学生信息的功能。
//
import java.util.*;
import java.io.*;
class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public int num;
public String name;
public double scores[]; // 三门课程成绩
public double aver; // 平均成绩
public Student() {
this.scores = new double[3];
}
public Student(int num, String name, double score1, double score2,
double score3) {
this();
this.setNumber(num);
this.setName(name);
this.setScores(score1, score2, score3);
this.aver = (this.scores[0] + this.scores[1] + this.scores[2]) / 3;
}
public void setNumber(int num) {
this.num = num;
}
public void setName(String name) {
this.name = name;
}
public void setScores(double score1, double score2, double score3) {
this.scores[0] = score1;
this.scores[1] = score2;
this.scores[2] = score3;
}
public int getNumber() {
return num;
}
public String getName() {
return name;
}
public double[] getScores() {
return scores;
}
public double getAver() {
return aver;
}
public String toString() {
return "学号:" + this.num + "姓名:" + this.name + "\n" + "成绩————>" + "语文:"
+ this.scores[0] + "数学:" + this.scores[1] + "英语:"
+ this.scores[2] + "平均成绩:" + this.aver;
}
}
/*
* Scanner sc=new Scanner(System.in); sc.next()即为输入的内容;
*/
public class Test_12 {
public static void main(String[] args) throws Exception {
HashMap<Integer, Student> stu = new HashMap<Integer, Student>();
File file = new File("Stu_info.obj");
Student zhangsan = new Student(18, "张三", 78, 85, 92);
Student lisi = new Student(15, "李四", 85, 87, 74);
Student wangwu = new Student(20, "王五", 75, 98, 96);
Student zhaoliu = new Student(9, "赵六", 93, 98, 93);
stu.put(18, zhangsan);
stu.put(15, lisi);
stu.put(20, wangwu);
stu.put(9, zhaoliu);
// 写
FileOutputStream fos = null;
ObjectOutputStream oos = null;
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(new Inner(stu));
oos.flush();
oos.close();
// 读
FileInputStream fis = null;
ObjectInputStream ois = null;
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
HashMap<Integer, Student> stuRead = null;
//如果读取的对象所属的类是Inner的子类,就···