黑马程序员技术交流社区
标题:
自定义泛型方式,解决有五个学生,每个学生有3门课的成绩问题
[打印本页]
作者:
李易烜
时间:
2013-4-13 23:28
标题:
自定义泛型方式,解决有五个学生,每个学生有3门课的成绩问题
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;
import java.util.Set;
import java.util.TreeSet;
/**
* 3、 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,<br/>
* 写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩),<br/>
* 然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。<br/>
* 要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
*
* @author Liyixuan
*/
/*
*
* 思想:
* 1,通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
* 2,因为学生有很多,那么就需要存储,使用到集合。因为要对学生的总分排序。所以可以使用TreeSet。 3,将集合的信息写入到一个文件中。
*
* 处理:2种方式 : list、set
*/
public class Test3 {
/**
* 测试方法 - 线程入口
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//第一种方法,使用ComparatorStudent 比较器
List<Student> list = StudentUtils.sysInputStream(new ArrayList<Student>());
ComparatorStudent comparator = new ComparatorStudent("cmp");
Collections.sort(list, Collections.reverseOrder(comparator));//反转比较器
StudentUtils.writeToFile(list,"stu.txt");
/*
//第二种方法,使用Comparator<Student> 比较器,reverseOrder()反转比较器
Comparator<Student> cmp = Collections.reverseOrder();
//用学生信息工具类调用getStudents方法,因为是静态方法,可以直接调用,返回一个Set集合
Set<Student> stus = StudentUtils.sysInputStream(new TreeSet<Student>(cmp));
//用学生信息工具类调用writeToFile方法,因为是静态方法,可以直接调用
StudentUtils.writeToFile(stus,"stu2.txt"); */
}
}
/**
* 学生的工具类
*
* @author liyx
*
*/
class StudentUtils {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> T sysInputStream(T obj) throws Exception {
System.out.println("请输入数据: (格式 name,90,90,90)");
// 通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
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) { // "C是obj的父类"
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;
}
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("写入文件已成功");
}
}
class ComparatorStudent implements Comparator<Student> {
public ComparatorStudent(String cmp) {
}
@Override
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;// 如果总分不相等,则比较总分
}
}
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;
}
@Override
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;//如果总分不相等,则比较总分
}
}
复制代码
作者:
932759732
时间:
2013-4-23 23:26
你这个覆盖的toString方法好像没起作用呢
作者:
王薪婷
时间:
2013-4-24 10:41
学习学习~
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2