package com.mytest;
public class Student implements Comparable<Student>{
private String name;
private int chinese;
private int math;
private int english;
private int score;
public Student(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.score = chinese+math+english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int compareTo(Student o2) {
// TODO Auto-generated method stub
int num = new Integer(this.score).compareTo(o2.getScore());
if(num>0){
return -1;
}
if(num<0){
return 1;
}
if(num==0){
return this.name.compareTo(o2.name);
}
return num;
}
public int hashCode(){
return name.hashCode()+score*78;
}
public boolean equals(Object obj){
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student stu = (Student) obj;
return (this.name.equals(stu.getName()))&&(this.score==stu.getScore());
}
public String toString(){
return "["+"name"+this.name+"\t"+"语文"+this.chinese+"\t"+"数学"+this.math+"\t"+"英语"+this.english+"\t"+"score"+this.score+"]";
}
}
package com.mytest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class TestTree{
/*要求录入一个班的成绩(语,数,外),求总分,按最高分排序输入OVER结束
*
*
* */
private Set<Student> getStudentScore(){
Set<Student> stu = new TreeSet<Student>();
BufferedReader fis = new BufferedReader(new InputStreamReader(System.in));
String str;
try {
while((str=fis.readLine())!=null){
if(str.equals("over")){
break;
}
String []strs = str.split(",");
stu.add(new Student(strs[0], Integer.parseInt(strs[1]) , Integer.parseInt(strs[2]), Integer.parseInt(strs[3])));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return stu;
}
private void writeToDoc(Set<Student> set,String name){
Set<Student> stu = set;
BufferedWriter fos =null;
try {
OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(name));
fos = new BufferedWriter(output);
Iterator< Student> it = stu.iterator();
while(it.hasNext()){
Student stu1 = it.next();
try {
fos.write(stu1.toString());
fos.newLine();
fos.flush();
System.out.println(stu1.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
TestTree test = new TestTree();
//test.getStudentScore();
test.writeToDoc(test.getStudentScore(), "woshiku.txt");
// Set set = new TreeSet();
// set.add(10);
// set.add(5);
// set.add(-2);
// Iterator it = set.iterator();
// while(it.hasNext()){
// System.out.println(it.next());
// }
// }
}
}
|
|