학생관리-Comparable

이중호·2021년 11월 1일
0

고급자바

목록 보기
4/6

문제)학번(int), 이름(String), 국어점수, 영어점수, 수학점수, 총점, 등수를 멤버로 갖는 Student 클래스를 만든다.
이 클래스의 생성자에서는 학번, 이름, 국어점수, 영어점수, 수학점수만 매개변수로 받아서 초기화 처리를 한다.
이 Student객체는 List에 저장하여 관리한다.
List에 저장된 데이터는 학번의 오름차순으로 정렬할 수 있는 내부정렬 기준을 구현한다.
그리고, 총점의 역순으로 정렬하는데 총점이 같으면 이름의 오름차순으로 정렬이 될 수 있는 외부 정렬 기준 클래스를 작성한다.

위 정렬 기준에 맞춰 정렬되는 프로그램을 작성하시오.
(단, 등수는 List에 전체 데이터가 추가된 후에 저장되도록 한다.)


class Student implements Comparable<Student>{
	private int id;
	private String name;
	private int korScore;
	private int engScore;
	private int mathScore;
	private int sum;
	private int rank;
	
	public Student(int id, String name, int korScore, int engScore, int mathScore) {
		super();
		this.id = id;
		this.name = name;
		this.korScore = korScore;
		this.engScore = engScore;
		this.mathScore = mathScore;
		sum = korScore + engScore + mathScore;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public int getKorScore() {
		return korScore;
	}

	public void setKorScore(int korScore) {
		this.korScore = korScore;
	}

	public int getEngScore() {
		return engScore;
	}

	public void setEngScore(int engScore) {
		this.engScore = engScore;
	}

	public int getMathScore() {
		return mathScore;
	}

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

	public int getSum() {
		return sum;
	}

	public void setSum(int sum) {
		this.sum = sum;
	}

	public int getRank() {
		return rank;
	}

	public void setRank(int rank) {
		this.rank = rank;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", korScore="
				+ korScore + ", engScore=" + engScore + ", mathScore="
				+ mathScore + ", sum=" + sum + ", rank=" + rank + "]";
	}

	@Override
	public int compareTo(Student stu) {
		return Integer.compare(this.getId(), stu.getId());
		//return ((Integer)this.getId()).compareTo(stu.getId());
	}
	
}

class SumDesc implements Comparator<Student>{

	@Override
	public int compare(Student stu1, Student stu2) {
		int result = Integer.compare(stu1.getSum(), stu2.getSum())*-1;
		if(result == 0) result = stu1.getName().compareTo(stu2.getName());
		return result;
	}

}

public class StudentTest {
	
	public void setRanking(List<Student> studentList) {//등수를 구하는 메서드
		for(Student std1 : studentList){ 
			int rank = 1; 
			for(Student std2 : studentList){ 
				if(std1.getSum() < std2.getSum()) rank++;
			}
			std1.setRank(rank);
		}
	}
	
	
	
	public static void main(String[] args) {
		
		List<Student> studentList = new ArrayList<>();
		
		studentList.add(new Student(1544020,"이중호",70,50,30));
		studentList.add(new Student(1544013,"홍길동",60,20,10));
		studentList.add(new Student(1544028,"김충신",100,90,90));
		studentList.add(new Student(1544006,"이순신",50,10,30));
		studentList.add(new Student(1544022,"강아지",60,30,90));
		studentList.add(new Student(1544001,"장독대",30,30,30));
		studentList.add(new Student(1544029,"장보고",10,10,10));
		studentList.add(new Student(1544005,"하동훈",0,20,10));
		studentList.add(new Student(1544007,"김유신",0,0,30));
		studentList.add(new Student(1544031,"박나나",10,0,0));

		System.out.println("정렬하기전");
		for(Student stu : studentList){
			System.out.println(stu);
		}
		System.out.println("---------------------------------------------------------------------------------");

		Collections.sort(studentList);
		System.out.println("학번의 오름차순 정렬후");
		for(Student stu : studentList){
			System.out.println(stu);
		}
		System.out.println("---------------------------------------------------------------------------------");

		Collections.sort(studentList, new SumDesc());
		System.out.println("총점의 역순(총점이 같을경우 이름순) 정렬후");
		for(Student stu : studentList){
			System.out.println(stu);
		}
		System.out.println("---------------------------------------------------------------------------------");

		System.out.println("등수 구하기");
		studentList.get(0).setRank(1);
		System.out.println(studentList.get(0));
		int count = 0;
		for(int i=1; i<studentList.size(); i++){
			count++;
			if(studentList.get(i-1).getSum()==studentList.get(i).getSum()) {
				studentList.get(i).setRank(count--);
			}
			else {
				count = i;
				studentList.get(i).setRank(i+1);
			}
			System.out.println(studentList.get(i));
		}
	
	}
}
profile
배워가는 신입개발자

0개의 댓글