IO练习

来源:互联网 发布:上古世纪最新捏脸数据 编辑:程序博客网 时间:2024/04/30 04:10
/**
 *有五个学生,没个学生有三门课的成绩
 *从键盘输入以上信息,姓名,三门课的成绩
 *输入格式:姓名 30 40 60 计算出总成绩
 *并把学生的信息和计算的总分数高低顺序存放在硬盘文件student.txt;
 *
 *1.描述学生对象
 *2.定义一个可以操作学生对象的工具类
 *
 *思想:
 *1.通过获取键盘录入的数据,并将该行中的数据封装成学生对象
 *2.因为学生有很多,需要存储学生的信息,那么就要用到集合,因为要多学生的总分排序
 *可以使用TreeSet
 *3,将集合的信息写入文件中
 */
/**
 * 实体类
 * */
public class Student implements Comparable<Student> {
private String name;
private int ma,cn,en;
private int num;

public Student(String name, int ma, int cn, int en) {
this.name = name;
this.ma = ma;
this.cn = cn;
this.en = en;
this.num = ma+cn+en;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getMa() {
return ma;
}

public void setMa(int ma) {
this.ma = ma;
}

public int getCn() {
return cn;
}

public void setCn(int cn) {
this.cn = cn;
}

public int getEn() {
return en;
}

public void setEn(int en) {
this.en = en;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

@Override
public int hashCode() {
return name.hashCode()+num*78;
}

@Override
public boolean equals(Object obj) {
if(!(obj instanceof Student)){
throw new ClassCastException("类型不匹配");
}
Student s= (Student)obj;
return this.name.equals(s.name)&&this.num==s.num;
}

@Override
public int compareTo(Student s) {
int sum = new Integer(this.num).compareTo(new Integer(s.num));
if(sum==0)
return this.name.compareTo(s.name);
return sum;
}

@Override
public String toString() {
return "Student [" + name + ", " + ma + ", " + cn + ","
+ en +"]";
}

}


工具类
import java.util.*;
import java.io.*;

public class StudentInfoTool {
public static Set<Student> getStudents(){
return getStudents(null);
}
public static Set<Student> getStudents(Comparator<Student> cmp) {
// 从键盘录入学生信息
System.out.println("请输入学生信息");         
BufferedReader buffr = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
Set<Student> stus =null;
if(cmp==null)
stus=new TreeSet<Student>();
else
stus=new TreeSet<Student>(cmp);
try {
while ((line = buffr.readLine()) != null) {
if ("over".equals(line))
break;
String[] info = line.split(",");
Student stu = new Student(info[0], Integer.parseInt(info[1]),
Integer.parseInt(info[2]), Integer.parseInt(info[3]));
stus.add(stu);

}
} catch (IOException e) {

throw new RuntimeException("从键盘读取信息失败!");
} finally{
try {
if(buffr!=null)
buffr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException("读取流关闭失败!");
}
}

return stus;

}
public static void writeFile(Set<Student> stus){
BufferedWriter buffw=null;
try {
buffw = new BufferedWriter(new FileWriter( new File("D:\\stuInfo.txt")));
for(Student stu:stus){
buffw.write(stu.toString()+"\t");
buffw.write(stu.getNum()+"");
buffw.newLine();
buffw.flush();
}
 
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
buffw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
测试类
 public class StudentInfo {

public static void main(String[] args) {
Comparator<Student> cmp=Collections.reverseOrder();
Set<Student> stus= StudentInfoTool.getStudents(cmp);
StudentInfoTool.writeFile(stus);

}

}


0 0
原创粉丝点击