코락 CoRock
코딩하는 락스타
코락 CoRock
  • 분류 전체보기 (393)
    • frameworks (19)
      • spring (19)
      • spring-boot (0)
      • testing (0)
    • languages (94)
      • java (39)
      • kotlin (0)
      • python (42)
      • r (13)
    • libraries (0)
    • programming (239)
      • android (13)
      • c (17)
      • cpp (22)
      • database (18)
      • design-pattern (4)
      • data-structures (11)
      • git (8)
      • hadoop (6)
      • html-css (7)
      • issue (4)
      • javascript (26)
      • jsp (34)
      • os (29)
      • php (6)
      • preferences (19)
      • etc (15)
    • discography (37)
      • k-pop (18)
      • pop (19)
    • blog (3)

블로그 메뉴

  • Programming
  • Java
  • JavaScript
  • Discography
  • K-Pop Songs
  • Pop Songs
  • Blog
  • Guestbook

공지사항

인기 글

태그

  • 파이썬
  • oracle
  • CentOS
  • linux
  • 자바스크립트
  • Java
  • Android
  • jsp
  • Spring
  • javascript
  • python
  • r

최근 댓글

최근 글

티스토리

반응형
hELLO · Designed By 정상우.
코락 CoRock

코딩하는 락스타

languages/java

[Java] 제네릭을 활용한 성적관리 프로그램

2018. 3. 27. 19:27
반응형

수정중


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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package grademanagementsystem;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;
import java.util.function.UnaryOperator;
 
public class GradeMagSystem {
    // 필드
    ArrayList<Student> list;
    Student stu;    
    
    // 생성자
    GradeMagSystem() {
        // ArrayList 생성        
        list = new ArrayList<>();
        stu = null;
    }
    
    // 1: 국영수 점수를 입력하면 총점, 평균을 자동 계산 후 내림차순으로 정렬
    public void input() {
        Scanner sc = new Scanner(System.in);
        
        stu = new Student();
                
        System.out.println("──────────────────");
        System.out.println("학생 정보를 입력합니다.");
        System.out.println("이름을 입력하세요.");
        System.out.println("──────────────────");
        System.out.print(" ──> ");
        stu.setName(sc.next());
        
        System.out.println("──────────────────");
        System.out.println("국어 성적을 입력하세요.");
        System.out.println("──────────────────");
        System.out.print(" ──> ");
        stu.setKor(sc.nextInt());
        
        System.out.println("──────────────────");
        System.out.println("영어 성적을 입력하세요.");
        System.out.println("──────────────────");
        System.out.print(" ──> ");
        stu.setEng(sc.nextInt());
        
        System.out.println("──────────────────");
        System.out.println("수학 성적을 입력하세요.");
        System.out.println("──────────────────");
        System.out.print(" ──> ");
        stu.setMath(sc.nextInt());
        
        // 총점, 평균 계산
        stu.setTotal(stu.getKor() + stu.getEng() + stu.getMath());
        stu.setAvg(stu.getTotal() / 3.f);
        
        // 국영수, 총점, 평균까지 담긴 데이터 삽입
        list.add(stu);
        
        System.out.println("──────────────────");
        System.out.println("학생 정보 등록 완료!");
        
        Descend Descend = new Descend();
        Collections.sort(list, Descend);
    }
    
    // 2: 학생 전체 출력
    public void showAllInfo() {
        // 학생 데이터가 1도 없는 경우
        if(list.isEmpty()) {
            emptyStudentMessage();
            return;
        }
        System.out.println("NAME /  KOR  /  ENG  /  MATH  / TOTAL / AVERAGE");
        for (Student stu : list)
            System.out.println(stu);
    }
    
    // 3번 기능 - 검색
    public void search() {
        // 학생 데이터가 1도 없는 경우
        if(list.isEmpty()) {
            emptyStudentMessage();
            return;
        }
 
        Scanner sc = new Scanner(System.in);
        
        System.out.println("──────────────────");
        System.out.println("학생 이름으로 검색합니다.");
        System.out.println("찾고자 하는 이름을 입력하세요.");
        System.out.println("──────────────────");
        System.out.print(" ──> ");
        String wantToName = sc.next();
 
        // 찾는 이름이 있으면 그 학생에 대한 정보를 출력한다.
        showTargetInfo(wantToName);
    }
    
    public int showTargetInfo(String wantToName) {
        Iterator<Student> it = list.iterator();
        
        while(it.hasNext()) {
            Student target = it.next();
            String name = target.getName();
 
            if(name.equals(wantToName)) {
                int idx = list.indexOf(target);
 
                // 출력
                System.out.println("──────────────────");
                System.out.println("NAME /  KOR  /  ENG  /  MATH  / TOTAL / AVERAGE");
                System.out.println(list.get(idx));
                
                return idx;
                
            } else if(list.contains(it)) {
                System.out.println("──────────────────");
                System.out.println("찾고자 하는 이름이 없습니다.");
            }
        }
        
        return -1;
    }
    
    // 4: 학생 데이터 삭제
    public void deleteInfo() {
        // 학생 데이터가 1도 없는 경우
        if(list.isEmpty()) {
            emptyStudentMessage();
            return;
        }
        
        Scanner sc = new Scanner(System.in);
        System.out.println("──────────────────");
        System.out.println("학생 정보를 삭제합니다.");
        System.out.println("지우고자 하는 이름을 입력하세요.");
        System.out.println("──────────────────");
        System.out.print(" ──> ");
        String wantToName = sc.next();
        
        int idx = showTargetInfo(wantToName);
        
        System.out.println("──────────────────");
        System.out.println("현재 " + wantToName + "의 데이터가 존재합니다.");
        System.out.println("정말 삭제하시겠습니까?");
        System.out.println("1. 네" + "\t" + "2. 아니오");
        System.out.println("──────────────────");
        System.out.print(" ──> ");
        int button = sc.nextInt();
        
        switch(button)
        {
        case 1:
            list.remove(idx);
            
            System.out.println("──────────────────");
            System.out.println("삭제 완료!");
        case 2:
            System.out.println("──────────────────");
            System.out.println("메인 화면으로 돌아갑니다.");
            return;
        default:
            break;
        }
    }    // end of deleteInfo()
    
    
    // 학생 성적 수정
    public void correctGrade() {
        // 학생 데이터가 1도 없는 경우
        if (list.isEmpty()) {
            emptyStudentMessage();
            return;
        }
        
        Scanner sc = new Scanner(System.in);
        int temp = 0;
        
        System.out.println("──────────────────");
        System.out.println("학생 이름으로 검색합니다.");
        System.out.println("찾고자 하는 이름을 입력하세요.");
        System.out.println("──────────────────");
        System.out.print(" ──> ");
        String wantToName = sc.next();
 
        int idx = showTargetInfo(wantToName);
 
        System.out.println("──────────────────");
        System.out.println("현재 " + wantToName + "의 데이터가 다음과 같습니다.");
        System.out.println("어떤 과목의 성적을 수정하시겠습니까?");
        System.out.println("1. 국어" + "\t" + "2. 영어" + "\t" + "3. 수학");
        System.out.println("──────────────────");
        System.out.print(" ──> ");
        int button = sc.nextInt();
 
        switch (button) {
        case 1:
            System.out.println("──────────────────");
            System.out.println("국어 성적을 수정합니다.");
            System.out.println("수정할 점수를 입력하세요.");
            System.out.println("──────────────────");
            System.out.print(" ──> ");
            temp = sc.nextInt();
            list.get(idx).setKor(temp);
 
            break;
 
        case 2:
            System.out.println("──────────────────");
            System.out.println("영어 성적을 수정합니다.");
            System.out.println("수정할 점수를 입력하세요.");
            System.out.println("──────────────────");
            System.out.print(" ──> ");
            temp = sc.nextInt();
            list.get(idx).setEng(temp);
            
            break;
 
        case 3:
            System.out.println("──────────────────");
            System.out.println("수학 성적을 수정합니다.");
            System.out.println("수정할 점수를 입력하세요.");
            System.out.println("──────────────────");
            System.out.print(" ──> ");
            temp = sc.nextInt();
            list.get(idx).setMath(temp);
 
            break;
            
        default:
            System.out.println("──────────────────");
            System.out.println("잘못된 번호를 입력하였습니다.");
            System.out.println("다시 시도해주세요.");
            
            break;
        }
        
        // 총점, 평균 계산
        list.get(idx).setTotal(list.get(idx).getKor() + list.get(idx).getEng() + list.get(idx).getMath());
        list.get(idx).setAvg(list.get(idx).getTotal() / 3.f);
        
        Descend Descend = new Descend();
        Collections.sort(list, Descend);
        
        System.out.println("──────────────────");
        System.out.println("수정이 완료되었습니다!");
        System.out.println("확인해보삼ㅎㅎ");
    }
    
    public void emptyStudentMessage() {
        System.out.println("──────────────────");
        System.out.println("현재 등록된 학생이 1도 없습니다.");
        System.out.println("메인 화면으로 돌아갑니다.");
    }
}
 
//내림차순 정렬 메소드
class Descend implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o2.compareTo(o1);
    }
}
Colored by Color Scripter
cs

반응형
저작자표시 비영리 변경금지 (새창열림)
    'languages/java' 카테고리의 다른 글
    • [Java] 프로젝트 기획 및 설계
    • [Java] 소스코드관리
    • [Java] Swing
    • [Java] Graphic User Interface, GUI
    코락 CoRock
    코락 CoRock
    A COder dreaming of being a ROCKstar

    티스토리툴바