需求:有五個(gè)學(xué)生醉锄,每個(gè)學(xué)生有3門課的成績祝谚,定義一種比較直觀的文本文件格式悠瞬,
輸入學(xué)生姓名和成績,輸入的格式:name,30,30,30從鍵盤輸入以上數(shù)據(jù)(包括姓名宵睦,三門課成績)记罚,
按總分?jǐn)?shù)從高到低的順序?qū)W(xué)生信息存放在磁盤文件"stu.txt"中。
思路:
1壳嚎,3門課的成績都是數(shù)據(jù)毫胜,為了便于操作,將其封裝到學(xué)生對象中诬辈。
學(xué)生本身就是問題領(lǐng)域中涉及的對象酵使,對學(xué)生描述。
學(xué)生:
姓名焙糟,語文成績口渔,英語成績,數(shù)學(xué)成績穿撮,總分.
2缺脉,數(shù)據(jù)來源于鍵盤錄入,將這些數(shù)據(jù)都封裝到每一個(gè)學(xué)生對象中悦穿。
3攻礼,按照總分排個(gè)序,很熟栗柒,但是這些數(shù)據(jù)都存儲到了學(xué)生對象中礁扮,其實(shí)是學(xué)生對象排序。
學(xué)生對象很多,先想到的就是存起來--集合-不重復(fù)排序-TreeSet太伊。
4雇锡,將排序后的信息寫入到一個(gè)文件中。定義操作文件的輸出流僚焦。
將信息寫入到文件中锰提。
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;
}
}
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=" + name + ", cn=" + cn + ", en=" + en + ", ma=" + ma + "]";
}
}
public class StudentInfoTool {
/*
* 定義功能,獲取鍵盤錄入的信息芳悲。 并將信息封裝成學(xué)生對象立肘。存儲到容器中。
* 按照學(xué)生的自然排序完成排序動作名扛。
*/
/*public static Set<Student> getStudents() throws IOException {
return getStudents(null);
}*/
/*
* 定義功能赛不,獲取鍵盤錄入的信息。 并將信息封裝成學(xué)生對象罢洲。存儲到容器中。
* 按照指定比較器完成排序的動作文黎。
*/
public static Set<Student> getStudents(Comparator<Student> comp) throws IOException {
// 獲取鍵盤錄入惹苗。
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
// 創(chuàng)建一個(gè)集合對象。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))// 定義鍵盤錄入的結(jié)束標(biāo)記耸峭。
break;
// 對獲取的信息進(jìn)行切割桩蓉,獲取指定的數(shù)據(jù)內(nèi)容。
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]));
// 把學(xué)生對象存儲到集合中去劳闹。
set.add(stu);
}
return set;
}
/*
* 定義功能院究,將集合中的對象信息寫入到指定文件中進(jìn)行存儲。
*/
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();
}
}
}
public class StudentInfoTest {
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);
}
}
運(yùn)行: