黑马程序员 java基础IOTest

----------- android培训java培训java学习型技术博客、期待与您交流! ------------

 

 /**
 * 有5个学生 每个学生有3门课的成绩 从键盘输入以上数据 (包括 姓名 三门课成绩) 输入的格式 如 张三,3,40,50 计算出总成绩
 * 并把学生的信息和计算出的总分数高低顺序存档到磁盘文件 "stu.txt"中
 * 
 * 1 描述学生对象 2 对应一个可以操作学生对象的工具类
 * 
 * 思想 1 通过获取键盘录入的一行数据 并将改行数据中德信息取出封装成学生对象 2 因为学生有很多 那么就需要存储 使用到集合 因为要对学生的总分排序
 * 所以可以使用TreeSet 3 将集合众的数据写到指定的文件中
 * 
 * @author lazy
 * 
 */


public class IOTest {


public static void main(String[] args) {


Set<Student> stus = StudentInfoTool.getStudents();
try {
StudentInfoTool.writeToFile(stus);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


/**
 * 定义学生类 用于描述学生对象
 * 
 * @author lazy
 * 
 */
class Student implements Comparable<Student> { // 实现Comparable接口使之具有比较性方便后面进行排序


private String name;
private int math, cn, en;
private int sum;


Student(String name, int math, int cn, int en) {
this.name = name;
this.math = math;
this.cn = cn;
this.en = en;
sum = math + cn + en;
}


@Override
public int hashCode() {
// TODO Auto-generated method stub
return name.hashCode() + sum * 13; // 确保hash值唯一
}


@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student) obj;


return this.name.equals(s.name) && this.sum == s.sum;
}


@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub


int num = ((Integer) this.sum).compareTo((Integer) o.sum);
if (num == 0)
return this.name.compareTo(o.name);
return num;
}


@Override
public String toString() {
// TODO Auto-generated method stub
return "stundent[" + name + " ," + math + " " + cn + " ," + en + "]";
}


public String getName() {
return name;
}


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


public int getMath() {
return math;
}


public void setMath(int math) {
this.math = math;
}


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 getSum() {
return sum;
}


public void setSum(int sum) {
this.sum = sum;
}


}


class StudentInfoTool {
public static Set<Student> getStudents() {
BufferedReader bufr = null;
Set<Student> stus = null;
// 读取键盘
try {
bufr = new BufferedReader(new InputStreamReader(System.in));


String line = null;


stus = new TreeSet<Student>();


while ((line = bufr.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);// 添加到set集合中
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bufr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return stus;


}


// 将集合中的信息写到文件中


public static void writeToFile(Set<Student> stus) throws IOException {
BufferedWriter bufw = new BufferedWriter(new FileWriter("stuInfo.txt"));


for (Student stu : stus) {
bufw.write(stu.toString() + "\t");
bufw.write(stu.getSum() + "");
bufw.newLine();
bufw.flush();
}
bufw.close();
}
}

----------------------- android培训java培训java学习型技术博客、期待与您交流! ----------------------

详情请查看:http://edu.csdn.net/heima

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值