学生成绩排序系统

package newstudent;

public class Student implements Comparable<Student>{
	private String code;
	private String name;
	private String subject;
	private int grade;
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public Student() {
	}
	public Student(String code, String name, String subject, int grade) {
		super();
		this.code = code;
		this.name = name;
		this.subject = subject;
		this.grade = grade;
	}
	@Override
	public String toString() {
		return "Student [code=" + code + ", name=" + name + ", subject=" + subject + ", grade=" + grade + "]";
	}
	@Override
	public int compareTo(Student o) {
		int grade1 = this.grade;
		int grade2 = o.getGrade();
		int grade = 0;
		if(grade1 > grade2){
			grade = -1;
		}else if(grade1 < grade2){
			grade = 1;
		}
		return grade;
	}
	
}
package newstudent;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class FileOpreate {
	public FileOpreate() {
		Student s1 = new Student("1","x0","y",23);
		Student s2 = new Student("2","x1","y",25);
		Student s3 = new Student("3","x2","y",21);
		Student s4 = new Student("4","x3","y",20);
		Student s5 = new Student("5","x4","y",29);
		List<Student> list = new ArrayList<Student>();
		list.add(s1);
		list.add(s2);		
		list.add(s3);
		list.add(s4);
		list.add(s5);
		try {
			FileOutputStream fos = new FileOutputStream("123.txt");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			FileInputStream fis = new FileInputStream("123.txt");
			ObjectInputStream ois = new ObjectInputStream(fis);
			
			oos.writeObject(list);
			oos.writeBoolean(true);
			oos.flush();
			// 读对象信息
			try {
				List list1 = (List) ois.readObject();
				System.out.println(list1);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			System.out.println(ois.readBoolean());//读取的顺序要和写入的顺序一致

			fos.close();
			oos.close();
			fis.close();
			ois.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

package newstudent;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
	public static void main(String[] args) {
		Student s1 = new Student("1","x0","y",23);
		Student s2 = new Student("2","x1","y",25);
		Student s3 = new Student("3","x2","y",21);
		Student s4 = new Student("4","x3","y",20);
		Student s5 = new Student("5","x4","y",29);
		List<Student> list = new ArrayList<Student>();
		list.add(s1);
		list.add(s2);		
		list.add(s3);
		list.add(s4);
		list.add(s5);
	
	

		Collections.sort(list);
		System.out.println("排序后:");
		for(Student student : list){
			System.out.println(student);
		}
		Collections.sort(list, new Comparator<Student>(){

			@Override
			public int compare(Student o1, Student o2) {
				int s1 = o1.getGrade();
				int s2 = o2.getGrade();
				return s2-s1;
			}
			
		});
		for(Student s : list) {
			System.out.println(s);
		}
	}
	
} 


你可能感兴趣的:(学生成绩排序系统)