有五个学生,每个学生有3门课的成绩,定义一种比较直观的文本文件格式, 输入学生姓名和成绩,输入的格式:name,30,30,30从键盘输入以上数据(包括姓名,三门课成绩), 按总分数从高到低的顺序将学

package cn.itcast.io.p8.test;


import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;


/*
有五个学生,每个学生有3门课的成绩,定义一种比较直观的文本文件格式,
输入学生姓名和成绩,输入的格式:name,30,30,30从键盘输入以上数据(包括姓名,三门课成绩),
按总分数从高到低的顺序将学生信息存放在磁盘文件"stu.txt"中。


思路:
1,3门课的成绩都是数据,为了便于操作,将其封装到学生对象中。
学生本身就是问题领域中涉及的对象,对学生描述。 
学生:
姓名,语文成绩,英语成绩,数学成绩,总分.
2,数据来源于键盘录入,将这些数据都封装到每一个学生对象中。
3,按照总分排个序,很熟,但是这些数据都存储到了学生对象中,其实是学生对象排序。
学生对象很多,先想到的就是存起来--集合-不重复排序-TreeSet。
4,将排序后的信息写入到一个文件中。定义操作文件的输出流。
  将信息写入到文件中。 


aa,10,30,50
zz,30,60,30
qq,30,90,70
cc,70,80,90
pp,80,80,80
*/


public class StudentInfoTest {


/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {


Comparator<Student> comp = Collections.reverseOrder();
comp = Collections.reverseOrder(new ComparatorByMath());
Set<Student> set = StudentInfoTool.getStudents(comp);


File file = new File("stuinfo.txt");
StudentInfoTool.write2File(set, file);
}


}


——————————————————————————————————————————————————————————————

package cn.itcast.io.p8.test;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;


public class StudentInfoTool {


/**
* 定义功能,获取键盘录入的信息。 并将信息封装成学生对象。存储到容器中。
* 按照学生的自然排序完成排序动作。 
* @return
* @throws IOException
*/
public static Set<Student> getStudents() throws IOException {

return getStudents(null);
}
/**
* 定义功能,获取键盘录入的信息。 并将信息封装成学生对象。存储到容器中。
* 按照指定比较器完成排序的动作。

* @throws IOException
*/


public static Set<Student> getStudents(Comparator<Student> comp) throws IOException {


// 获取键盘录入。
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));


// 创建一个集合对象。TreeSet.
Set<Student> set = null;

if(comp==null)
set = new TreeSet<Student>();
else
set = new TreeSet<Student>(comp);
String line = null;


while ((line = bufr.readLine()) != null) {


if ("over".equals(line))// 定义键盘录入的结束标记。
break;


// 对获取的信息进行切割,获取指定的数据内容。
String[] info_arr = line.split(",");
Student stu = new Student(info_arr[0],
Integer.parseInt(info_arr[1]),
Integer.parseInt(info_arr[2]),
Integer.parseInt(info_arr[3]));


// 把学生对象存储到集合中去。
set.add(stu);
}


return set;
}


/**
* 定义功能,将集合中的对象信息写入到指定文件中进行存储。

* @throws IOException
*/
public static void write2File(Set<Student> set, File file)
throws IOException {


BufferedWriter bufw = null;
try {
bufw = new BufferedWriter(new FileWriter(file));


for (Student stu : set) {
bufw.write(stu.toString() + "\t"+stu.getSum());
bufw.newLine();
bufw.flush();
}
} finally {
if (bufw != null)
bufw.close();
}


}
}


——————————————————————————————————————————————————————


package cn.itcast.io.p8.test;


public class Student implements Comparable<Student> {


private String name;
private int cn,en,ma;
private int sum;
public Student(String name, int cn, int en, int ma) {
super();
this.name = name;
this.cn = cn;
this.en = en;
this.ma = ma;
sum = cn + en + ma;
}



@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + sum;
return result;
}






@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sum != other.sum)
return false;
return true;
}






public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCn() {
return cn;
}
public void setCn(int cn) {
this.cn = cn;
}
public int getEn() {
return en;
}
public void setEn(int en) {
this.en = en;
}
public int getMa() {
return ma;
}
public void setMa(int ma) {
this.ma = ma;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}






@Override
public int compareTo(Student o) {

int temp = this.sum-o.sum;
return temp==0?this.name.compareTo(o.name):temp;
}






@Override
public String toString() {

return "Student:["+name+","+cn+","+en+","+ma+"]";
}





}


————————————————————————————————————————————————————————————————————


package cn.itcast.io.p8.test;


import java.util.Comparator;


public class ComparatorByMath implements Comparator<Student> {


@Override
public int compare(Student o1, Student o2) {


int temp = o1.getMa() - o2.getMa();

return temp==0?o1.getName().compareTo(o2.getName()):temp;
}


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值