일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- ERWin
- jsp
- 데이터베이스
- 객체지향프로그래밍
- 객제지향
- 오라클
- html
- 객제지향프로그래밍
- javascript
- 프로그래밍
- DB
- 웹프로그래밍
- 코딩
- CSS
- 테이블
- 공부
- orcle
- 주말이다..
- sql
- squery
- UI
- 공부를열심히
- Java
- 객체지향
- 프로젝트
- 자바
- 웹
- web
- Oracle
- Project
Archives
- Today
- Total
햄찌개
JavaCollectionFramework - List Sort 예제 - 학생 관리 프로그램 본문
* 문제 ) 학번, 이름, 국어점수, 영어점수, 수학점수, 총점, 등수를 멤버로 갖는
* Student 클래스를 만든다.
* 생성자는 학번, 이름, 국어, 영어, 수학 점수만 매개변수를 받아서 처리한다.
*
* 이 Student 객체들은 List에 저장하여 관리한다.
* List에 저장한 데이터들을 학번의 오름차순으로 정렬하여 출력하는 부분과
* 총점의 역순으로 정렬하는 부분을 프로그램 하시오.
* (총점이 같으면 학번의 내림차순으로 정렬되도록 한다.)
* (학번 정렬 기준은 Student클래스 자체에서 제공하도록 하고,
* 총점 정렬기준은 외부클래스에서 제공하도록 한다.)
package kr.or.ddid.basic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class T04_StudentTest {
public static void main(String[] args) {
List<Student> List = new ArrayList<>();
List.add(new Student("1560050", "전진원",100,100,100));
List.add(new Student("1560049", "박찬", 100, 100, 100));
List.add(new Student("1560005", "김성준", 40, 75, 85));
List.add(new Student("1560023", "김선준", 30, 22, 56));
List.add(new Student("1560001", "홍길동", 35, 75, 90));
System.out.println("정렬전");
for(Student student : List) {
System.out.println(student);
}
System.out.println("=============================================================");
Collections.sort(List);// 정렬
System.out.println("학번 오름차순 정렬 후 : ");
for(Student student : List) {
System.out.println(student.toString());
}
System.out.println("=========================================================");
Collections.sort(List, new StudentTotal() );//정렬
System.out.println("총점의 역순 정렬 : ");
int a = 1;
for(Student student : List) {
int b = List.get(a-1).total;
for (int i = 0; i <List.size() ; i++) {
if(b< List.get(i).total) {
int c =student.getGrade();
student.setGrade(c+1);
}
}a++;
System.out.println(student.toString());
}
System.out.println("==========================================================");
}
}
class StudentTotal implements Comparator<Student>{
@Override
public int compare(Student st1, Student st2) {
if(new Integer(st1.getTotal()).compareTo(st2.getTotal()) ==0) { //총점이 같으면 학번 순
return new String(st1.getHak()).compareTo(st2.getHak()) -1;
}else {
return new Integer(st1.getTotal()).compareTo(st2.getTotal())*-1 ; //총점 내림차순
}
}
}
class Student implements Comparable<Student>{
String hak;
String name;
int kor;
int eng;
int math;
int total;
int grade =1;
@Override
public String toString() {
return "Student [hak=" + hak + ", name=" + name + ", kor=" + kor + ", eng=" + eng + ", math=" + math
+ ", total=" + total + ", grade=" + grade + "]";
}
public Student(String hak, String name, int kor, int eng, int math) {
super();
this.hak = hak;
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
total = kor+eng+math;
}
@Override
public int compareTo(Student st1) {
return hak.compareTo(st1.getHak());
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getHak() {
return hak;
}
public void setHak(String hak) {
this.hak = hak;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
'고급 JAVA' 카테고리의 다른 글
JavaCollectionFramework - 해시 함수 - equals(), hashCode() (0) | 2020.09.15 |
---|---|
JavaCollectionFramework - TreeSet 예제 (0) | 2020.09.15 |
JavaCollectionFramework - Set (0) | 2020.09.15 |
JavaCollectionFramework - ListSort - 정렬 Comparable, Comparator (0) | 2020.09.15 |
STACK & QUEUE (0) | 2020.09.15 |