quilt code
[고급자바] Student 과제 본문
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | package study; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; public class StudentTest { public static void main(String[] args) { List<Student> stuList = new ArrayList<Student>(); stuList.add(new Student("201500214", "정재현", 95, 80, 90)); stuList.add(new Student("201600523", "차은우", 95, 95, 100)); stuList.add(new Student("201200718", "이태민", 70, 75, 80)); stuList.add(new Student("201000458", "김준면", 65, 95, 90)); stuList.add(new Student("201100506", "변백현", 75, 90, 85)); ranking(stuList); System.out.println("정렬 전"); for(Student stu : stuList) { System.out.println(stu); } System.out.println("-------------------------------------"); Collections.sort(stuList); System.out.println("학번의 오름차순으로 정렬 후"); for(Student stu : stuList) { System.out.println(stu); } System.out.println("-------------------------------------"); Collections.sort(stuList, new totalScore()); System.out.println("총점의 역순으로 정렬"); for(Student stu : stuList) { System.out.println(stu); } System.out.println("-------------------------------------"); } public static void ranking(List<Student> stuList) { for(Student stu : stuList) { int rank = 1; for(Student stu2 : stuList) { if(stu.getTotal() < stu2.getTotal()) { rank++; } } stu.setRank(rank); } } } // 학생정보 class Student implements Comparable<Student>{ public Student(String num, String name, int kor, int eng, int mat) { super(); this.num = num; this.name = name; this.kor = kor; this.eng = eng; this.mat = mat; this.total = kor + eng + mat; } private String num; private String name; private int kor; private int eng; private int mat; private int total; private int rank; public String getNum() { return num; } public void setNum(String num) { this.num = num; } 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 getMat() { return mat; } public void setMat(int mat) { this.mat = mat; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public int getRank() { return rank; } public void setRank(int rank) { this.rank = rank; } //학번 오름차순 정렬 @Override public int compareTo(Student stu) { return num.compareTo(stu.getNum()); } @Override public String toString() { return "studentInfo [num=" + num + ", name=" + name + ", kor=" + kor + ", eng=" + eng + ", mat=" + mat + ", total=" + total + ", rank=" + rank + "]"; } } //총점 역순 정렬 class totalScore implements Comparator<Student>{ @Override public int compare(Student stu1, Student stu2) { if(stu1.getTotal() == stu2.getTotal()) { return stu1.getNum().compareTo(stu2.getNum()) * -1; } else { return Integer.compare(stu1.getTotal(), stu2.getTotal()) * -1; } } } | cs |
'daily > 고급자바' 카테고리의 다른 글
| [고급자바] 제너릭 (1) (0) | 2023.02.02 |
|---|---|
| [고급자바] 컬렉션 프레임워크(4) (0) | 2023.02.02 |
| [고급자바] Lotto 과제 (0) | 2023.02.02 |
| [고급자바] 컬렉션 프레임워크 (3) (0) | 2023.02.01 |
| [고급자바] 컬렉션 프레임워크 (2) (0) | 2023.01.31 |