package com.itheima;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*
* @title Test1
* @description 第一题:有五个学生,每个学生有3门课(语文、数学、英语)的成绩,
* 写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩),
* 然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。
* 要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
* @author Chen_zyan
* @version 1.0
* @create_date 2013-5-5
* @copyright (c) ITHEIMA
* @modifyBy Chen_zyan
*
*/
public class Test1
{
/**
*
* @title main
* @description 测试入口
* @author Chen_zyan
* @create_date 2013-5-5
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
//使用ComparatorStudent 比较器
List<Student> list = StudentUtils.sysInputStream(new ArrayList<Student>());
ComparatorStudent comparator = new ComparatorStudent("cmpa");
//反转比较器
Collections.sort(list, Collections.reverseOrder(comparator));
StudentUtils.writeToFile(list,"stu.txt");
}
}
/**
*
* @title Student
* @description 学生类
* @author Chen_zyan
* @version
* @create_date 2013-5-5
* @copyright (c) ITHEIMA
* @modifyBy Chen_zyan
*
*/
class Student implements Comparable<Student>
{
private String name;
private int chinese;
private int maths;
private int english;
private int totalPoints;
//构造方法
Student(String name, int chinese, int maths, int english)
{
this.name = name;
this.chinese = chinese;
this.maths = maths;
this.english = english;
this.totalPoints = chinese + maths + english;
}
//姓名
public String getName()
{
return name;
}
//语文
public int getChinese()
{
return chinese;
}
//数学
public int getMaths()
{
return maths;
}
//英语
public int getEnglish()
{
return english;
}
public int getTotalPoints()
{
return totalPoints;
}
// 为了打印效果,有必要覆盖toString方法
public String toString()
{
return "" + name + ", " + chinese + ", " + maths + ", " + english;
}
public int compareTo(Student o)
{
//先比总分
int totalPoints = new Integer(this.getTotalPoints()).compareTo(new Integer(o.getTotalPoints()));
if(totalPoints==0)
return this.name.compareTo(o.name);//如果总分相等,则比较姓名
return totalPoints;//如果总分不相等,则比较总分
}
}
/**
*
* @title StudentUtils
* @description 学生的工具类
* @author Chen_zyan
* @version 1.0
* @create_date 2013-5-5
* @copyright (c) ITHEIMA
* @modifyBy Chen_zyan
*
*/
class StudentUtils {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> T sysInputStream(T obj) throws Exception {
System.out.println("请输入数据: (格式 name,90,90,90);并请输入\"over\"结束");
//通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//定义一个字符串
String line = null;
Collection objColl = null;
// Collection与object比较
boolean isFather = Collection.class.isAssignableFrom(obj.getClass());
if (isFather)
{
objColl = (Collection) obj;
}
else
{
throw new Error("参数不是Collection子类");
}
// 循环读
while ((line = bufr.readLine()) != null) {
// 如果输入over则结束
if ("over".equals(line))
break;
// 用逗号把数据隔开
String[] info = line.split(",");
// 将info封装成学生对象,将各成绩都转换成整数
Student stu = new Student(info[0], Integer.parseInt(info[1]),Integer.parseInt(info[2]), Integer.parseInt(info[3]));
objColl.add(stu);// 将字符串数组添加到集合中
}
bufr.close();
return (T) objColl;
}
/**
*
* @title writeToFile
* @description 存放文件的输出流
* @author Chen_zyan
* @create_date 2013-5-5
* @param students
* @param fileName
* @throws IOException
*/
public static void writeToFile(Collection<Student> students,String fileName) throws IOException
{
BufferedWriter bufw = new BufferedWriter(new FileWriter(fileName));
for (Student stu : students) {
bufw.write(stu.toString());// 将学生的姓名和各成绩写出去
bufw.write("|" + stu.getTotalPoints());// 将学生的总分写到文件
bufw.newLine();// 换行一次
bufw.flush();// 字符流缓冲区需要刷新
}
bufw.close();
System.out.println("写入文件已成功");
}
}
/**
*
* @title ComparatorStudent
* @description 比较器
* @author Chen_zyan
* @version 1.0
* @create_date 2013-5-5
* @copyright (c) ITHEIMA
* @modifyBy Chen_zyan
*
*/
class ComparatorStudent implements Comparator<Student>
{
public ComparatorStudent(String cmpa)
{
}
public int compare(Student o1, Student o2)
{
// 先比总分
int totalPoints = new Integer(o1.getTotalPoints()).compareTo(new Integer(o2.getTotalPoints()));
if (totalPoints == 0)
return o1.getName().compareTo(o2.getName());// 如果总分相等,则比较姓名
return totalPoints;// 如果总分不相等,则比较总分
}
}
|
|