본문 바로가기
Programming/JAVA

[JAVA] LINKEDLIST를 이용한 성적처리

성적 처리







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
package Grade;
 
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
 
class Student {
    private String name;
    private int kor;
    private int eng;
    private int math;
    private int total;
    private float avg;
 
    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() {
        total = kor + math + eng;
        return total;
    }
 
    public void setTotal(int total) {
        this.total = total;
    }
 
    public float getAvg() {
        avg = total / 3.f;
        return avg;
    }
 
    public void setAvg(float avg) {
        this.avg = avg;
    }
 
    @Override
    public String toString() {
        return "이름 : " + this.getName() + " 국어 : " + this.getKor() + " 영어 : " + this.getEng() + " 수학 : "
                + this.getMath() + " 총점 : " + this.getTotal() + " 평균 : " + this.getAvg() + "\n";
    }
}
 
public class Grade {
    LinkedList<Student> list = new LinkedList<Student>();
    Iterator<Student> it = list.iterator();
    Student stu;
 
    public void input() {
        Scanner sc = new Scanner(System.in);
        stu = new Student();
        System.out.print("이름 : ");
        stu.setName(sc.next());
        System.out.print("국어 : ");
        stu.setKor(sc.nextInt());
        System.out.print("영어 : ");
        stu.setEng(sc.nextInt());
        System.out.print("수학 : ");
        stu.setMath(sc.nextInt());
        list.add(stu);
    }
 
    public void output() {
        for (Student stu : list) {
            System.out.println(stu); //toString()함수 호출
        }
    }
 
    public void search() {
        Scanner sc = new Scanner(System.in);
        System.out.print("이름 검색 : ");
        String n = sc.next();
        it = list.iterator(); //위에 선언한 iterator을 불러옴
        while (it.hasNext()) { //찾을 떄까지 넘김
            Student stu = it.next();
            if (stu.getName().equals(n)) { //입력한 n이 같으면
                System.out.println(stu); //n이 들어있는 list 호출
            }
        }
    }
 
    public void fix() {
        Scanner sc = new Scanner(System.in);
        System.out.print("수정 할 이름 입력 : ");
        String n = sc.next();
        it = list.iterator();
        while (it.hasNext()) {
            Student stu = it.next();
            if (stu.getName().equals(n)) { 
                System.out.print("수정 - 1.국어  2.영어  3.수학 : ");
                int f = sc.nextInt();
                switch (f) {
                case 1// 국어 
                    System.out.print("국어 : ");
                    stu.setKor(sc.nextInt());
                    break;
                case 2// 영어
                    System.out.print("영어 : ");
                    stu.setEng(sc.nextInt());
                    break;
                case 3// 수학 
                    System.out.print("수학 : "); 
                    stu.setMath(sc.nextInt());
                    break;
                }
            }
        }
    }
 
    public void delete() {
        Scanner sc = new Scanner(System.in);
        if (stu == null) {
            System.out.println("삭제할 내역이 없습니다");
        } else {
            System.out.print("삭제 할 이름 검색 : ");
            String n = sc.next();
            it = list.iterator();
            while (it.hasNext()) {
                Student stu = it.next();
                if (stu.getName().equals(n)) {
                    list.remove(stu);
                }
            }
        }
    }
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Grade g = new Grade();
        while (true) {
            System.out.println("1.입력  2.출력  3.검색  4.수정  5.삭제  6.끝내기");
            System.out.print("선택 : ");
            int choose;
            choose = sc.nextInt();
            while (choose != 6) {
                switch (choose) {
                case 1// 입력
                    g.input();
                    break;
 
                case 2// 출력
                    g.output();
                    break;
 
                case 3// 검색
                    g.search();
                    break;
 
                case 4// 수정
                    g.fix();
                    break;
 
                case 5// 삭제
                    g.delete();
                    break;
 
                case 6// 끝내기
                    break;
                }
                break;
            }
        }
    }
}
 
cs


'Programming > JAVA' 카테고리의 다른 글

[JAVA] 중첩 클래스의 접근 제한  (1) 2018.06.09
[JAVA] 중첩 클래스와 중첩 인터페이스  (3) 2018.06.07
[JAVA] 경로  (1) 2018.04.15
[JAVA] 이칙(+,-) 계산기  (0) 2018.04.15
[JAVA] 성적 처리  (0) 2018.04.15