This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCalcClient.java
83 lines (68 loc) · 2.82 KB
/
CalcClient.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import CalcApp.*;
import CalcApp.CalcPackage.DivisionByZero;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import static java.lang.System.out;
public class CalcClient {
static Calc calcImpl;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[]) {
try {
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get the root naming context
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
// Use NamingContextExt instead of NamingContext. This is
// part of the Interoperable naming Service.
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
// resolve the Object Reference in Naming
String name = "Calc";
calcImpl = CalcHelper.narrow(ncRef.resolve_str(name));
// System.out.println(calcImpl);
while (true) {
out.println("1. Sum");
out.println("2. Sub");
out.println("3. Mul");
out.println("4. Div");
out.println("5. exit");
out.println("--");
out.println("choice: ");
try {
String opt = br.readLine();
if (opt.equals("5")) {
break;
} else if (opt.equals("1")) {
out.println("a+b= " + calcImpl.sum(getFloat("a"), getFloat("b")));
} else if (opt.equals("2")) {
out.println("a-b= " + calcImpl.sub(getFloat("a"), getFloat("b")));
} else if (opt.equals("3")) {
out.println("a*b= " + calcImpl.mul(getFloat("a"), getFloat("b")));
} else if (opt.equals("4")) {
try {
out.println("a/b= " + calcImpl.div(getFloat("a"), getFloat("b")));
} catch (DivisionByZero de) {
out.println("Division by zero!!!");
}
}
} catch (Exception e) {
out.println("===");
out.println("Error with numbers");
out.println("===");
}
out.println("");
}
//calcImpl.shutdown();
} catch (Exception e) {
System.out.println("ERROR : " + e);
e.printStackTrace(System.out);
}
}
static float getFloat(String number) throws Exception {
out.print(number + ": ");
return Float.parseFloat(br.readLine());
}
}