Skip to content

Commit 694dd8c

Browse files
committed
add exam02
1 parent 5ce6c71 commit 694dd8c

3 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.eomjinyoung.lesson01.exam02.client;
2+
3+
import java.io.PrintStream;
4+
import java.net.Socket;
5+
import java.util.Scanner;
6+
7+
public class CalculatorAgent {
8+
String ip;
9+
int port;
10+
11+
public CalculatorAgent(String ip, int port) {
12+
this.ip = ip;
13+
this.port = port;
14+
}
15+
16+
public double compute(String operator, double a, double b) throws Exception {
17+
Socket socket = null;
18+
PrintStream out = null;
19+
Scanner in = null;
20+
21+
try {
22+
socket = new Socket(ip, port);
23+
out = new PrintStream(socket.getOutputStream());
24+
in = new Scanner(socket.getInputStream());
25+
26+
out.println(operator);
27+
out.println(a);
28+
out.println(b);
29+
out.flush();
30+
31+
String state = in.nextLine();
32+
if (state.equals("success")) {
33+
return Double.parseDouble(in.nextLine());
34+
} else {
35+
throw new Exception(in.nextLine());
36+
}
37+
} catch (Exception e) {
38+
throw e;
39+
40+
} finally {
41+
try {out.close();} catch(Exception e) {}
42+
try {in.close();} catch(Exception e) {}
43+
try {socket.close();} catch(Exception e) {}
44+
}
45+
}
46+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.eomjinyoung.lesson01.exam02.client;
2+
3+
import java.awt.Container;
4+
import java.awt.Dimension;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.ActionListener;
7+
8+
import javax.swing.Box;
9+
import javax.swing.BoxLayout;
10+
import javax.swing.JButton;
11+
import javax.swing.JComboBox;
12+
import javax.swing.JFrame;
13+
import javax.swing.JOptionPane;
14+
import javax.swing.JTextField;
15+
16+
@SuppressWarnings("serial")
17+
public class CalculatorFrame extends JFrame implements ActionListener{
18+
CalculatorAgent calcAgent = new CalculatorAgent("localhost", 8888);
19+
JTextField operand1 = new JTextField(4);
20+
JTextField operand2 = new JTextField(4);
21+
String[] operatorData = {"+", "-", "*", "/"};
22+
JComboBox<String> operator = new JComboBox<String>(operatorData);
23+
JButton equal = new JButton("=");
24+
JTextField result = new JTextField(6);
25+
JButton clear = new JButton("Clear");
26+
27+
public CalculatorFrame() {
28+
this.setTitle("Lesson01-Exam01");
29+
30+
Container contentPane = this.getContentPane();
31+
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
32+
33+
contentPane.add(Box.createVerticalGlue());
34+
contentPane.add(this.createInputForm());
35+
contentPane.add(this.createToolBar());
36+
contentPane.add(Box.createVerticalGlue());
37+
38+
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39+
this.pack();
40+
this.setLocationRelativeTo(null);
41+
}
42+
43+
@Override
44+
public void actionPerformed(ActionEvent event) {
45+
if (event.getSource() == equal) {
46+
compute();
47+
} else {
48+
clearForm();
49+
}
50+
}
51+
52+
private void compute() {
53+
double a = Double.parseDouble(operand1.getText());
54+
double b = Double.parseDouble(operand2.getText());
55+
double r = 0;
56+
57+
try {
58+
r = calcAgent.compute(operator.getSelectedItem().toString(), a, b);
59+
result.setText(Double.toString(r));
60+
61+
} catch (Exception err) {
62+
JOptionPane.showMessageDialog(
63+
null, err.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
64+
}
65+
}
66+
67+
private void clearForm() {
68+
this.operand1.setText("");
69+
this.operand2.setText("");
70+
this.result.setText("");
71+
}
72+
73+
74+
private Box createInputForm() {
75+
Box box = Box.createHorizontalBox();
76+
box.setMaximumSize(new Dimension(300, 30));
77+
box.setAlignmentY(Box.CENTER_ALIGNMENT);
78+
box.add(operand1);
79+
box.add(operator);
80+
box.add(operand2);
81+
box.add(equal);
82+
box.add(result);
83+
equal.addActionListener(this);
84+
return box;
85+
}
86+
87+
private Box createToolBar() {
88+
Box box = Box.createHorizontalBox();
89+
box.add(clear);
90+
clear.addActionListener(this);
91+
return box;
92+
}
93+
94+
public static void main(String[] args) {
95+
CalculatorFrame app = new CalculatorFrame();
96+
app.setVisible(true);
97+
}
98+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.eomjinyoung.lesson01.exam02.server;
2+
3+
import java.io.PrintStream;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
import java.util.Scanner;
7+
8+
public class CalculatorServer {
9+
private int port;
10+
11+
public CalculatorServer(int port) {
12+
this.port = port;
13+
}
14+
15+
public void service() throws Exception {
16+
ServerSocket serverSocket = new ServerSocket(port);
17+
System.out.println("CalculatorServer startup:");
18+
19+
Socket socket = null;
20+
21+
while(true) {
22+
try {
23+
System.out.println("waiting client...");
24+
25+
socket = serverSocket.accept();
26+
System.out.println("connected to client.");
27+
28+
processRequest(socket);
29+
System.out.println("closed client.");
30+
31+
} catch (Throwable e) {
32+
System.out.println("connection error!");
33+
}
34+
}
35+
36+
}
37+
38+
private void processRequest(Socket socket) throws Exception {
39+
Scanner in = new Scanner(socket.getInputStream());
40+
PrintStream out = new PrintStream(socket.getOutputStream());
41+
42+
String operator = in.nextLine();
43+
double a = Double.parseDouble(in.nextLine());
44+
double b = Double.parseDouble(in.nextLine());
45+
double r = 0;
46+
47+
try {
48+
switch (operator) {
49+
case "+": r = a + b; break;
50+
case "-": r = a - b; break;
51+
case "*": r = a * b; break;
52+
case "/":
53+
if (b == 0) throw new Exception("0 으로 나눌 수 없습니다!");
54+
r = a / b; break;
55+
default:
56+
throw new Exception("해당 연산을 지원하지 않습니다!");
57+
}
58+
out.println("success");
59+
out.println(r);
60+
61+
} catch (Exception err) {
62+
out.println("failure");
63+
out.println(err.getMessage());
64+
}
65+
66+
67+
out.close();
68+
in.close();
69+
socket.close();
70+
}
71+
72+
public static void main(String[] args) throws Exception {
73+
CalculatorServer app = new CalculatorServer(8888);
74+
app.service();
75+
}
76+
}
77+

0 commit comments

Comments
 (0)