鍵盤錄入多名學生的信息: 格式:姓名,數學成績,語文成績,英文成績,按總分由高到低 將學生的信息進行排列到文件中

主函數類:

package cn.io;

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

public class iosort {

	/**
	 * @param args
	 * @throws IOException 
	 */
	/*
	 * 例題: 鍵盤錄入多名學生的信息: 格式:姓名,數學成績,語文成績,英文成績。
	 *   按總分由高到低 將學生的信息進行排列到文件中。
	 *思路:
	 * 1,使用鍵盤錄入技術。
	 * 2,操作的學生信息,信息很多,需要將信息封裝成學生對象。
	 * 3,總分由高到低,需要排序,需要對學生對象中的總分排序。需要將多個學生對象進行容器存儲。
	 * 4,將容器中的學生對象的信息寫入到文件中。
	 * 容器:TreeSet
	 * 
	 * 使用操作學生信息的工具類
	 */
	public static void main(String[] args) throws IOException {
		 
		Comparator<Student> comp = Collections.reverseOrder();
		Set<Student> set = GetInfoTool.getStudents(comp);
	    
		File destfile = new File("E:\\3.txt");
		
		GetInfoTool.writrFile(set,destfile);
		
/*	輸入
xiao,13,12,13
ss,13,24,24
de,254,4,2
over
*/
	      

	}

}

操作學生信息的工具類:
<pre class="java" name="code">package cn.io;

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 GetInfoTool {
   
	 public static Set<Student> getStudents() throws IOException
	 {
		 return getStudents(null);
	 }
	public static Set<Student> getStudents(Comparator<Student> comp) throws IOException {
	 
		BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
	
		Set<Student> set = null;
		 if(comp!=null)
		 {
				set=new TreeSet<Student>(comp);
		 }
		 else
		 { 
			 set=new TreeSet<Student>();
		 }
		String line = null;
		while((line=buf.readLine())!=null)
		{ 
			if(line.contains("over")){
				break;
			}
		  String[] strs =line.split(",");
		  Student stu = new Student(strs[0], Integer.parseInt(strs[1]), Integer.parseInt(strs[2]), Integer.parseInt(strs[3]));
		  set.add(stu);
		}

		return set;
	}

	public static void writrFile(Set<Student> set, File destfile) {
		
		BufferedWriter bfw = null;
		
		try {
			bfw = new BufferedWriter(new FileWriter(destfile));
			for(Student stu:set)
			{ 
				bfw.write(stu.getName()+"\t"+stu.getSum());
				bfw.newLine();
				bfw.flush();
			}
			
		 } catch (IOException e) {
			
			e.printStackTrace();
		 }
		finally 
		{ 
			if(bfw!=null){
				 try {
					bfw.close();
				} catch (IOException e) {
				
					e.printStackTrace();
				}
			}
		}
		
		
		
	}
 
	
	
	
}


學生信息類:

package cn.io;

public class Student implements Comparable<Student> {

	 public int getSum() {
		return sum;
	}
	public void setSum(int sum) {
		this.sum = sum;
	}
	private  String name;
	 private int grade1;
	 private int grade2;
	 private int grade3;
	 private int sum;
	public Student(String name, int grade1, int grade2, int grade3) {
		super();
		this.setName(name);
		this.setGrade1(grade1);
		this.setGrade2(grade2);
		this.setGrade3(grade3);
		this.sum=grade1+grade2+grade3 ;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getGrade1() {
		return grade1;
	}
	public void setGrade1(int grade1) {
		this.grade1 = grade1;
	}
	public int getGrade3() {
		return grade3;
	}
	public void setGrade3(int grade3) {
		this.grade3 = grade3;
	}
	public int getGrade2() {
		return grade2;
	}
	public void setGrade2(int grade2) {
		this.grade2 = grade2;
	}

	@Override
	public int compareTo(Student o) {
		// TODO Auto-generated method stub
		int temp = this.sum-o.sum;
		 return temp == 0?this.name.compareTo(o.name):temp; 
	}
}

 
發佈了56 篇原創文章 · 獲贊 14 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章