-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kiosk.java
236 lines (196 loc) · 7.83 KB
/
Kiosk.java
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
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Kiosk {
int count = 0;
String show = "";
public Kiosk() {
// 디자인단
// 프레임 설정단
JFrame frame = new JFrame("자동 판매기");
frame.setBounds(0, 0, 625, 1000);
frame.setBackground(Color.green);
// 폰트
Font font = new Font(Font.SERIF, Font.PLAIN, 18);
Font font1 = new Font(Font.MONOSPACED, Font.BOLD, 22);
// 북쪽
Panel pNorth = new Panel();
pNorth.setBackground(new Color(102, 204, 102));
pNorth.setLayout(null);
pNorth.setSize(0, 500);
pNorth.setFont(font);
// 배열 설정 부분
String menu[] = { "카푸치노", "카페라떼", "자몽셔벗", "제주녹차", "티라미수", "샌드위치", "초코케익", "치즈케익" };
int price[] = { 5000, 5500, 6300, 4300, 4800, 6300, 4800, 5500 };
JButton bt[] = new JButton[menu.length];
TextField num[] = new TextField[menu.length];
Button minus[] = new Button[menu.length];
Button plus[] = new Button[menu.length];
JButton ok[] = new JButton[menu.length];
Label l[] = new Label[menu.length];
ImageIcon icon[] = new ImageIcon[menu.length];
// 버튼 설정 부분
for (int i = 0; i < menu.length; i++) {
// 메뉴 버튼
bt[i] = new JButton(menu[i]);
if (i < 4) {
bt[i].setBounds(25 + i * 150, 50, 100, 100);
} else {
bt[i].setBounds(25 + (i - 4) * 150, 300, 100, 100);
}
icon[i] = new ImageIcon(i + ".png");
bt[i].setIcon(icon[i]);
// 숫자 txt 버튼
num[i] = new TextField("0");
num[i].setBackground(Color.white);
num[i].setEditable(false);
num[i].setBounds(bt[i].getX() + 30, bt[i].getY() + 130, 40, 20);
// "-" 버튼
minus[i] = new Button("-");
minus[i].setBounds(bt[i].getX(), num[i].getY(), 20, 20);
minus[i].setEnabled(false);
// "+" 버튼
plus[i] = new Button("+");
plus[i].setBounds(bt[i].getX() + (100 - 20), num[i].getY(), 20, 20);
plus[i].setEnabled(false);
// 가격
l[i] = new Label(price[i] + "원");
l[i].setBounds(bt[i].getX() + 20, num[i].getY() - 25, 100, 20);
// 확인 버튼
ok[i] = new JButton("확인");
ok[i].setBounds(bt[i].getX(), num[i].getY() + 30, 100, 20);
ok[i].setEnabled(false);
pNorth.add(bt[i]);
pNorth.add(num[i]);
pNorth.add(minus[i]);
pNorth.add(plus[i]);
pNorth.add(l[i]);
pNorth.add(ok[i]);
}
// 중앙
TextArea ta = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
ta.setText(" 상품명 단가 수량 합계\n\n");
ta.setBackground(Color.white);
ta.setEditable(false);
ta.setFont(font1);
// 남쪽
Panel pSouth = new Panel();
pSouth.setFont(font);
pSouth.setBackground(new Color(255, 255, 215));
Button bt1 = new Button("order");
Button bt2 = new Button("restart");
Button bt3 = new Button("close");
pSouth.add(bt1);
pSouth.add(bt2);
pSouth.add(bt3);
// 주문버튼
bt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, ta.getText() + " 주문되었습니다. \n이용해주셔서 감사합니다.");
for (int i = 0; i < menu.length; i++) {
bt[i].setEnabled(true);
minus[i].setEnabled(false);
plus[i].setEnabled(false);
num[i].setText("0");
ta.setText(" 상품명 단가 수량 합계\n\n");
}
}
});
// 초기화 버튼
bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < menu.length; i++) {
bt[i].setEnabled(true);
minus[i].setEnabled(false);
plus[i].setEnabled(false);
num[i].setText("0");
ta.setText(" 상품명 단가 수량 합계\n\n");
}
}
});
//bt3 닫기버튼
bt3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 컴포넌트
frame.add(pNorth, BorderLayout.NORTH);
frame.add(ta, BorderLayout.CENTER);
frame.add(pSouth, BorderLayout.SOUTH);
frame.setVisible(true);
// 이벤트단
for (int i = 0; i < menu.length; i++) {
int j = i;
// 메뉴 버튼 이벤트
bt[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
minus[j].setEnabled(true);
plus[j].setEnabled(true);
bt[j].setEnabled(false);
ok[j].setEnabled(true);
count = 0;
}
});
// "-" 버튼 이벤트
minus[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (count > 0) {
count = count - 1;
num[j].setText(count + "");
ok[j].setEnabled(true);
} else {
minus[j].setEnabled(false);
}
}
});
// "+" 버튼 이벤트
plus[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
count = count + 1;
num[j].setText(count + "");
ok[j].setEnabled(true);
if (count > 0) {
minus[j].setEnabled(true);
}
}
});
//확인 버튼 이벤트
ok[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
show = bt[j].getActionCommand();
ta.append(" " + show + " " + price[j] + " " + count + " " + price[j] * count
+ "원" + "\n");
ok[j].setEnabled(false);
}
});
}
// exit
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}