1. 程式人生 > 其它 >集合到檔案資料排序改進版本(比較器)

集合到檔案資料排序改進版本(比較器)

package com.czie.iot1913.lps.IO.BufferStream.Better;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

/**
* FileName: ArrayListToFile 集合到檔案(資料排序進階版本)
* Author: lps
* Date: 2022/3/26 18:50
* Sign:劉品水 Q:1944900433
*/
public class ArrayListToFile {


public static void main(String[] args) throws IOException {
TreeSet<Student> treeset = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//int sum=(s2.getChinese()+s2.getMaths()+s2.getEnglish())-(s1.getChinese()+s1.getEnglish()+s1.getMaths());
int sum = s2.getSum() - s1.getSum();
int sum01 = sum == 0 ? s1.getName().compareTo(s2.getName()) : sum;
//三目運算
return sum01;
}
});
// Student s0 = new Student("臭男人",28,60,50,90);
// Student s1 = new Student("劉品水", 21, 100, 100, 100);
// Student s2 = new Student("王得發", 29, 65, 100, 100);
// treeset.add(s0);
// treeset.add(s1);
// treeset.add(s2);
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入第" + (i + 1) + "學生的資訊");
System.out.println("姓名:");
String name = sc.next();
System.out.println("年齡:");
int age = sc.nextInt();
System.out.println("語文成績:");
int chinese = sc.nextInt();
System.out.println("數學成績:");
int maths = sc.nextInt();
System.out.println("英語成績:");
int english = sc.nextInt();
//賦值給學生
Student s = new Student();
s.setName(name);
s.setAge(age);
s.setChinese(chinese);
s.setMaths(maths);
s.setEnglish(english);
treeset.add(s);
}

BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\JavaCode\\array.txt"));
for (Student s : treeset) {
bw.write(s.getName()+","+s.getAge()+","+s.getChinese()+","+s.getMaths()+","+s.getEnglish()+","+s.getSum());
bw.newLine();
bw.flush();
}
bw.close();

}
}



package com.czie.iot1913.lps.IO.BufferStream.Better;

import java.util.Objects;

/**
* FileName: Student
* Author: lps
* Date: 2022/3/26 18:51
* Sign:劉品水 Q:1944900433
*/
public class Student {
private String name;
private int age;
private int chinese;
private int maths;
private int english;

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", chinese=" + chinese +
", maths=" + maths +
", english=" + english +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && chinese == student.chinese && maths == student.maths && english == student.english && Objects.equals(name, student.name);
}

@Override
public int hashCode() {
return Objects.hash(name, age, chinese, maths, english);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getChinese() {
return chinese;
}

public void setChinese(int chinese) {
this.chinese = chinese;
}

public int getMaths() {
return maths;
}

public void setMaths(int maths) {
this.maths = maths;
}

public int getEnglish() {
return english;
}

public void setEnglish(int english) {
this.english = english;
}

public Student(String name, int age, int chinese, int maths, int english) {
this.name = name;
this.age = age;
this.chinese = chinese;
this.maths = maths;
this.english = english;
}

public Student() {
}
public int getSum(){
return this.chinese+this.maths+this.english;
}
}