//把输入的学生成绩倒叙排列,并存入文本文件中
package com.io;
/**
*
* @author 黑马_王康
*
*/
public class Student implements Comparable<Student>{
private String name;
private int mt,cn,en;
private int sum;
Student( String name,int mt,int cn,int en){
this.name = name;
this.mt = mt;
this.cn = cn;
this.en = en;
sum = mt + cn + en;
}
public String getName(){
return name;
}
public int getSum(){
return sum;
}
public int getEn(){
return en;
}
public int getCn(){
return cn;
}
@Override
public int compareTo(Student stu) { //实现compareTo方法
int num = new Integer(this.sum).compareTo(new Integer(stu.sum));
if(num == 0){
return this.name.compareTo(stu.name);
}
return num;
}
public int hashCode(){ //覆盖hashCode方法
return name.hashCode()+sum*66;
}
public boolean equals(Object obj){ //覆盖equals方法
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student stu = (Student)obj;
return this.name.equals(stu.name)&&this.sum==stu.sum;
}
public String toString(){ //重写toString方法
return "student["+name+","+mt+","+cn+","+en+"]";
}
}
package com.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
/**
*
* @author 王康
*
*/
public class StudentInfo {
public static Set<Student> getStudents() throws IOException{
return getStudents(null);
}
public static Set<Student> getStudents(Comparator<Student> cmp) throws IOException{ //传一个比较器参数,自定义比较器.
BufferedReader buf = 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);
while((line=buf.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);
}
buf.close();
return stus;
}
public static void Write2File(Set<Student> stu) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("D:/testStudent.txt")); //创建输入流,并建立接受文件
for (Student s : stu) {
bw.write(s.toString()+"\t");
bw.write(s.getSum()+"");
bw.newLine();
bw.flush();
}
bw.close();
}
}
package com.io;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
/**
* 测试类
* @author 王康
*
*/
public class StudentInfoTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Comparator<Student> cstu = Collections.reverseOrder(); //定义比较器
Set<Student> stu = StudentInfo.getStudents(cstu);
StudentInfo.Write2File(stu);
}
}
启动程序:并输入如下
lisi,32,43,54
wangwu,56,77,45
zhaoliu,54,66,76
zhouqi,54,88,91
sunba,87,90,83
在D:/testStudent.txt会得到文本文件,内容如下:
student[sunba,87,90,83] 260
student[zhouqi,54,88,91] 233
student[zhaoliu,54,66,76] 196
student[wangwu,56,77,45] 178
黑马程序员_王康把输入的学生成绩倒叙排列,并存入文本文件中 .
最新推荐文章于 2021-05-24 06:03:35 发布
2007

被折叠的 条评论
为什么被折叠?



