본문 바로가기

Programming/[JAVA]

[JAVA] Generic & Vector를 이용한 성적 출력 프로그램(GUI)

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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package pack02;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
class Name {
    String name;
 
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}
 
class Score {
    int score;
 
    public void setScore(int score) {
        this.score = score;
    }
    public int getScore() {
        return score;
    }
}
 
class Manager {
    Name name = new Name();
    //Score score = new Score();
    Score kor = new Score();
    Score math = new Score();
    Score eng = new Score();
 
    int sum;
    float avr;
 
    public Manager(String name, int kor, int math, int eng) {
        // TODO Auto-generated constructor stub
        this.name.setName(name);
        this.kor.setScore(kor);
        this.math.setScore(math);;
        this.eng.setScore(eng);;
    }
    public void setName(String name) {
        this.name.setName(name);
    }
    public String getName() {
        return this.name.getName();
    }
    public void setKor(int score) {
        this.kor.setScore(score);
    }
    public int getKor() {
        return this.kor.getScore();
    }
    public void setMath(int score) {
        this.math.setScore(score);
    }
    public int getMath() {
        return this.math.getScore();
    }
    public void setEng(int score) {
        this.eng.setScore(score);
    }
    public int getEng() {
        return this.eng.getScore();
    }
    public int getSum() {
        return sum = kor.getScore() + eng.getScore() + math.getScore();
    }
    public float getAvr() {
        return sum/3f;
    }
    @Override
    public String toString() {
        return "이름 : "+getName()+" 국어 : "+getKor()+" 영어 : "+getEng()+" 수학 : "+getMath() + "\n" + " 총점 : "+getSum()+" 평균 : "+getAvr()+"\n";
    }
}
 
public class GenericStudyGrade {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //이름 국어 영어 수학 총점 평균
        //입력 = 학생수50
        //한 사람씩 입력
        //출력 = 전체 출력
        //검색 & 성적(ex 80점 그이상만 출력 성적 순 정렬해서)
        //삭제
        JFrame jframe = new JFrame();
        jframe.setBounds(5050500300);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);
 
        JPanel jpanel = new JPanel();
        jframe.add(jpanel);
        jpanel.setLayout(null);
 
        JTextField tf = new JTextField();
        jpanel.add(tf);
        tf.setSize(30030);
        tf.setLocation(55);
 
        JTextArea ta = new JTextArea();
        jpanel.add(ta);
        ta.setSize(300200);
        ta.setLocation(550);
        ta.setText("입력 시 : 이름, 국어, 수학, 영어" + "\n" + "검색과 수정, 삭제할 시 이름을 입력하세요" + "\n");
 
        JButton btn1 = new JButton("입력");
        jpanel.add(btn1);
        btn1.setBounds(3502010030);
 
        JButton btn2 = new JButton("출력");
        jpanel.add(btn2);
        btn2.setBounds(3506010030);
 
        JButton btn3 = new JButton("검색");
        jpanel.add(btn3);
        btn3.setBounds(35010010030);
 
        JButton btn4 = new JButton("수정");
        jpanel.add(btn4);
        btn4.setBounds(35014010030);
 
        JButton btn5 = new JButton("삭제");
        jpanel.add(btn5);
        btn5.setBounds(35018010030);
 
        JButton btn6 = new JButton("종료");
        jpanel.add(btn6);
        btn6.setBounds(35022010030);
 
        Vector<Manager> list = new Vector<Manager>();
 
        btn1.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub    
 
                String[] temp = tf.getText().split(" ");
                int kor = Integer.parseInt(temp[1]);
                int math = Integer.parseInt(temp[2]);
                int eng = Integer.parseInt(temp[3]);
 
                list.add(new Manager(temp[0], kor, math, eng));
                ta.append(tf.getText() + "\n");
            }
        });
 
        btn2.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
 
                for (int i = 0; i < list.size(); i++) {
                    ta.append(list.get(i).toString() + "\n");
                }
            }
        });
 
        btn3.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
 
                String name = tf.getText(); //TextField.getText()
                Iterator<Manager> it = list.iterator();
                while (it.hasNext()) {
                    Manager temp = it.next();
                    if (temp.getName().equals(name)) {
                        ta.setText(temp.toString() + "\n");
                    }    
                }
            }
        });
 
        btn4.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
 
                String[] name1 = tf.getText().split(" ");
                int kor = Integer.parseInt(name1[1]);
                int math = Integer.parseInt(name1[2]);
                int eng = Integer.parseInt(name1[3]);
                Iterator<Manager> it1 = list.iterator();
                while (it1.hasNext()) {
                    Manager temp = it1.next();
                    if (temp.getName().equals(name1[0])) {
                        temp.setKor(kor);
                        temp.setMath(math);
                        temp.setEng(eng);
                    }    
                }
        
            }
        });
 
        btn5.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                int num = 0;
                String name2 = tf.getText();
                Iterator<Manager> it2 = list.iterator();
                while (it2.hasNext()) {
                    Manager temp = it2.next();
                    if (temp.getName().equals(name2)) {
                        list.remove(num);
                    }
                    num++;
                }
            }
        });
 
        btn6.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.exit(0);
            }
        });
 
        Scanner sc = new Scanner(System.in);
 
        while (true) {
            System.out.println("1.입력 2.출력 3.검색 4.수정  5.삭제 6.종료");
            switch (sc.nextInt()) {
            case 1:
                System.out.println("이름, 국어, 수학, 영어");
                list.add(new Manager( sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt()));
                break;
 
            case 2:
                for (int i = 0; i < list.size(); i++) {
                    System.out.println(list.get(i).toString());
                }
                break;
 
            case 3:
                System.out.println("검색할 이름");
                String name = sc.next();
                Iterator<Manager> it = list.iterator();
                while (it.hasNext()) {
                    Manager temp = it.next();
                    if (temp.getName().equals(name)) {
                        System.out.println(temp);
                    }    
                }
                break;
 
            case 4:
                System.out.println("수정할 이름");
                String name1 = sc.next();
                Iterator<Manager> it1 = list.iterator();
                while (it1.hasNext()) {
                    Manager temp = it1.next();
                    if (temp.getName().equals(name1)) {
                        System.out.println("1.국어수정 2.수학수정 3.영어수정");
                        int n = sc.nextInt();
                        switch (n) {
                        case 1:
                            temp.setKor(sc.nextInt());
                            break;
                        case 2:
                            temp.setMath(sc.nextInt());
                            break;
                        case 3:
                            temp.setEng(sc.nextInt());
                            break;
                        }
                    }    
                }
                break;
            case 5:
                int num = 0;
                System.out.println("삭제할 이름");
                String name2 = sc.next();
                Iterator<Manager> it2 = list.iterator();
                while (it2.hasNext()) {
                    Manager temp = it2.next();
                    if (temp.getName().equals(name2)) {
                        list.remove(num);
                    }    
                    num++;
                }
                break;
 
            case 6:
                System.out.println("종료합니다");
                return;
            }
        }
    }
}
 
cs