本帖最后由 杨兴庭 于 2013-7-13 15:22 编辑
import java.io.*;
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private String sex;
private int java,c_shar,php;
private int sum;
private int avg;
Student(String name,String sex,int java,int c_shar,int php)
{
this.name = name;
this.sex = sex;
this.java = java;
this.c_shar = c_shar;
this.php = php;
}
public int compareTo(Student s)
{
int num = new Integer(this.sum).compareTo(new Integer(s.sum));
if(num==0)
this.name.equals(s.name);
return num;
}
public int getSum()
{
return sum = java+c_shar+php;
}
public int getAvg()
{
return sum/3;
}
public boolean equalse(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student)obj;
return this.name.equals(s.name) && this.sum==s.sum;
}
public int hashCode()
{
return name.hashCode()+sum*78;
}
public String toString()
{
return name+"\t"+sex+"\t"+java+"\t"+c_shar+"\t"+php;
}
}
class StudentInfoToos
{
public static Set<Student> getStudnetInfo()throws IOException
{
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入学生基本信息");
String line = null;
Set<Student> stus = new TreeSet<Student>();
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
String[] info = line.split(",");
Student stu = new Student(info[0],info[1]
,Integer.parseInt(info[2])
,Integer.parseInt(info[3])
,Integer.parseInt(info[4]));
stus.add(stu);
}
bufr.close();
return stus;
}
public static void write2File(Set<Student> stus)throws IOException
{
BufferedWriter bufw = new BufferedWriter(new FileWriter("Info.txt"));
String msg = "姓名"+"\t"+"性别"+"\t"+"Java"+"\t"+"C#"+"\t"+"Php"+"\t"+"总分"+"\t"+"平均分";
bufw.write(msg);
bufw.newLine();
bufw.flush();
for(Student stu : stus)
{
bufw.write(stu.toString()+"\t");
bufw.write(stu.getSum()+""+"\t");
bufw.write(stu.getAvg()+"");
bufw.newLine();
bufw.flush();
}
bufw.close();
}
}
class StudentInfo
{
public static void main(String[] args)throws IOException
{
Set<Student> stus = StudentInfoToos.getStudnetInfo();
StudentInfoToos.write2File(stus);
}
}
|