人员列表排序输出

问题描述:
编写一个程序实现人员列表排序输出的功能,请创建Person类,属性包括编号number、姓名name、年龄age。

一个职员类Staff继承Person类,增加薪水Salarys属性。

一个Student类,增加英语englishScore、数学mathScore、程序设计programScore三门成绩,三门课的学分数分别为6,5,4分。

在Person类创建一个抽象函数getCompareVaue函数,并在Staff和Student实现该函数,Staff类返回薪水的值,Student返回GPA的值 要求:

(1)从给定的文件中读出多个人员信息放在一个对象数组里,如果一行包含四个数据 则认为是 staff的信息,否则认为是学生数据。
(2)按照年龄和编号从小到大顺序(先按年龄,再按编号,既如果年龄相同则按照编号从小到大顺序)输出人员信息的列表,要求输出格式尽量规范。
(3)按照getCompareVaue的从大到小的顺序staff和Student人员信息,要求staff和Student不交叉,各自成组。

import java.io.*;
import java.util.*;

abstract class Person {
	private int number;

	private String name;

	private int age;

	abstract double getCompareVaue();

	public Person(int number, String name, int age) {
		super();
		this.number = number;
		this.name = name;
		this.age = age;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	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;
	}
}

//职员类
class Staff extends Person implements Comparable<Staff> {
	private double Salarys;

	public Staff(int number, String name, int age, double salarys) {
		super(number, name, age);
		Salarys = salarys;
	}

	public double getSalarys() {
		return Salarys;
	}

	public void setSalarys(double salarys) {
		Salarys = salarys;
	}

	//排序方法,根据工资从小到大排序
	public int compareTo(Staff s) {
		if (Salarys == s.Salarys) {
			return 0;
		}
		return Salarys < s.Salarys ? -1 : 1;
	}

	@Override
	double getCompareVaue() {
		return Salarys;
	}

	@Override
	public String toString() {
		return "name:  " + getName() + "  number:  " + getNumber()
				+ "  age:  " + getAge() + "  salarys:  " + Salarys;
	}
}

//学生类
class Student extends Person implements Comparable<Student> {
	private double englishScore, mathScore, programScore;

	public Student(int number, String name, int age, double englishScore,
			double mathScore, double programScore) {
		super(number, name, age);
		this.englishScore = englishScore;
		this.mathScore = mathScore;
		this.programScore = programScore;
	}

	public double getEnglishScore() {
		return englishScore;
	}

	public void setEnglishScore(double englishScore) {
		this.englishScore = englishScore;
	}

	public double getMathScore() {
		return mathScore;
	}

	public void setMathScore(double mathScore) {
		this.mathScore = mathScore;
	}

	public double getProgramScore() {
		return programScore;
	}

	public void setProgramScore(double programScore) {
		this.programScore = programScore;
	}

	/**
	 * 一般大学采用之计分法
	 * 
	 * A90 - 100 4 点
	 * B80 - 89  3 点
	 * C70 - 79  2 点
	 * D60 - 69  1 点
	 * E0 - 59   0 点
	 * 
	 * 例如:某位学生修习三门课,其课目、学分及成绩分别为: 
	 * 英文:三学分、92 分;
	 * 化学:五学分、80 分;
	 * 数学:二学分、60分,则GPA的算法如下:
	 * 
	 * 科目 学分 分数 点数 分数×点数 
	 * 英文 3    92   4    12 
	 * 化学 5    80   3    15 
	 * 数学 2    60   1    2 
	 * 合计 10             29
	 * 29/10=2.9 2.9即为某生的GPA
	 */
	//此方法用于计算点数
	public int point(double score) {
		if (score >= 90 && score <= 100) {
			return 4;
		} else if (score >= 80 && score <= 89) {
			return 3;
		} else if (score >= 70 && score <= 79) {
			return 2;
		} else if (score >= 60 && score <= 69) {
			return 1;
		} else {
			return 0;
		}
	}
	
	//此方法用于计算GPA
	public double calculateGPA(double english, double math, double program) {
		return (6 * point(english) + 5 * point(math) + 4 * point(program))
				/ (6 + 5 + 4);
	}
	
	//排序方法,根据GPA从小到大排序
	public int compareTo(Student s) {
		if (getCompareVaue() == s.getCompareVaue()) {
			return 0;
		}
		return getCompareVaue() < s.getCompareVaue() ? -1 : 1;
	}

	@Override
	double getCompareVaue() {
		return calculateGPA(englishScore, mathScore, programScore);
	}

	@Override
	public String toString() {
		return "name:  " + getName() + "  number:  " + getNumber()
				+ "  age:  " + getAge() + "  GPA:  " + getCompareVaue();
	}
}

// Person类的排序方法,先按年龄,再按编号,如果年龄相同则按照编号从小到大顺序)输出人员信息的列表
class comparator implements Comparator<Person> {
	public int compare(Person o1, Person o2) {
		if (o1.getAge() == o2.getAge()) {
			return o1.getNumber() < o2.getNumber() ? -1 : 1;
		}
		return o1.getAge() < o2.getAge() ? -1 : 1;
	}
}

//PersonInformation类
public class PersonInformation {
	public static void main(String[] args) {
		String path = "d:/information.txt";
		String str;
		String[] information;
		List<Staff> staff = new ArrayList<Staff>();//用于保存Staff
		List<Student> student = new ArrayList<Student>();//用于保存Student
		List<Person> person = new ArrayList<Person>();//用于保存Person
		try {
			BufferedReader br = new BufferedReader(new FileReader(path));
			try {
				while ((str = br.readLine()) != null) {
					information = str.split("\\s+");
					if (information.length == 4) {//是Staff
						staff.add(new Staff(Integer.parseInt(information[0]),
								information[1], Integer
										.parseInt(information[2]), Double
										.parseDouble(information[3])));
					} else {//是Student
						student.add(new Student(Integer
								.parseInt(information[0]), information[1],
								Integer.parseInt(information[2]), Double
										.parseDouble(information[3]), Double
										.parseDouble(information[4]), Double
										.parseDouble(information[5])));
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		person.addAll(staff);
		person.addAll(student);
		Collections.sort(staff);//对Staff排序
		Collections.sort(student);//对Student排序
		Collections.sort(person, new comparator());//对Person排序
		Iterator<Person> p = person.iterator();
		Iterator<Staff> s = staff.iterator();
		Iterator<Student> stu = student.iterator();
		System.out.println("Staff Information:");
		while (s.hasNext()) {
			System.out.println(s.next());
		}
		System.out.println("\n\n\n");
		System.out.println("Student Information:");
		while (stu.hasNext()) {
			System.out.println(stu.next());
		}
		System.out.println("\n\n\n");
		System.out.println("Person Information:");
		while (p.hasNext()) {
			System.out.println(p.next());
		}
	}
}


/**
 * 打开文件的内容: 
 * 1 jim 18 80 90 90 
 * 2 tom 20 2000 
 * 3 lilei 29 70 60 30 
 * 4 hanmei 30 5000
 * 5 lili 18 5000
 * 
 * 
 * 
 * 运行结果: 
 * Staff Information: 
 * name: tom number: 2 age: 20 salarys: 2000.0 
 * name:hanmei number: 4 age: 30 salarys: 5000.0 
 * name: lili number: 5 age: 18 salarys: 5000.0
 * 
 * 
 * 
 * Student Information: 
 * name: lilei number: 3 age: 29 GPA: 1.0 
 * name: jim number: 1 age: 18 GPA: 3.0
 *
 * 
 * 
 * Person Information: 
 * name: jim number: 1 age: 18 GPA: 3.0 
 * name: lili number: 5 age: 18 salarys: 5000.0
 * name: tom number: 2 age: 20 salarys: 2000.0 
 * name:lilei number: 3 age: 29 GPA: 1.0 
 * name: hanmei number: 4 age: 30 salarys:5000.0
 */

你可能感兴趣的:(java)