java筆記--綜合應(yīng)用

需求:有五個學(xué)生,每個學(xué)生有3門課的成績,定義一種比較直觀的文本文件格式,
輸入學(xué)生姓名和成績,輸入的格式:name,30,30,30從鍵盤輸入以上數(shù)據(jù)(包括姓名,三門課成績),
按總分數(shù)從高到低的順序?qū)W(xué)生信息存放在磁盤文件"stu.txt"中。

思路:
1,3門課的成績都是數(shù)據(jù),為了便于操作,將其封裝到學(xué)生對象中。
學(xué)生本身就是問題領(lǐng)域中涉及的對象,對學(xué)生描述。
學(xué)生:
姓名,語文成績,英語成績,數(shù)學(xué)成績,總分.
2,數(shù)據(jù)來源于鍵盤錄入,將這些數(shù)據(jù)都封裝到每一個學(xué)生對象中。
3,按照總分排個序,很熟,但是這些數(shù)據(jù)都存儲到了學(xué)生對象中,其實是學(xué)生對象排序。
學(xué)生對象很多,先想到的就是存起來--集合-不重復(fù)排序-TreeSet。
4,將排序后的信息寫入到一個文件中。定義操作文件的輸出流。
將信息寫入到文件中。

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)建一個集合對象。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é)束標記。
                break;

            // 對獲取的信息進行切割,獲取指定的數(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;
    }

    /*
     * 定義功能,將集合中的對象信息寫入到指定文件中進行存儲。
     */
    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);
    }
}

運行:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 感謝社區(qū)中各位的大力支持,譯者再次奉上一點點福利:阿里云產(chǎn)品券,享受所有官網(wǎng)優(yōu)惠,并抽取幸運大獎:點擊這里領(lǐng)取 現(xiàn)...
    HetfieldJoe閱讀 900評論 0 3
  • 據(jù)媒體報道,當?shù)貢r間2016年11月12日,美國電影藝術(shù)與科學(xué)學(xué)院為成龍頒發(fā)奧斯卡終身成就獎,這是中國人第一次獲得...
    5bd376ea631b閱讀 813評論 0 0
  • 最近看了一些集成Apple pay 的文章,感覺描述的太復(fù)雜。下面就用最簡約的步聚集成Apple pay 功能。如...
    歐辰_OSR閱讀 308評論 0 2
  • 夜深人靜的時侯想你 我想問你關(guān)于幸福的話題 一想道這里 先是淚流滿面 一個八歲的小男孩 不止一次的跟我說 幸福兩個...
    淡然li閱讀 263評論 0 12
  • 20集的北京女子圖鑒完結(jié)了,那是可可小姐的十年。十年之間,她努力工作,提高工作技能。投資自己,提升生活品質(zhì)。換了兩...
    日京小姐在成都閱讀 1,248評論 0 2

友情鏈接更多精彩內(nèi)容