Calculator Application with Library
1.1.3
A simple library for performing basic arithmetic operations with application
Toggle main menu visibility
Loading...
Searching...
No Matches
calculator-app
src
main
java
com
ucoruh
calculator
CalculatorApp.java
Go to the documentation of this file.
1
12
package
com.ucoruh.calculator;
13
14
import
org.slf4j.LoggerFactory;
15
16
import
ch.qos.logback.classic.Logger;
17
30
public
class
CalculatorApp {
34
private
static
final
Logger logger = (Logger) LoggerFactory.getLogger(CalculatorApp.class);
35
40
static
final
String USAGE =
"Usage: CalculatorApp <number> <+|-|*|/> <number>"
;
41
42
private
CalculatorApp() {
43
// Utility/entry-point class: not meant to be instantiated.
44
}
45
56
public
static
void
main
(String[] args) {
57
System.out.println(run(args));
58
}
59
77
static
String run(String[] args) {
78
if
(args ==
null
|| args.length != 3) {
79
logger.warn(
"Expected 3 arguments, got {}"
, args ==
null
? 0 : args.length);
80
return
USAGE;
81
}
82
83
final
int
left;
84
final
int
right;
85
86
try
{
87
left = Integer.parseInt(args[0]);
88
right = Integer.parseInt(args[2]);
89
}
catch
(NumberFormatException e) {
90
logger.error(
"Invalid operand: {}"
, e.toString());
91
return
"Error: operands must be integers. "
+ USAGE;
92
}
93
94
String operator = args[1];
95
Calculator calculator =
new
Calculator();
96
97
switch
(operator) {
98
case
"+"
:
99
return
String.valueOf(calculator.add(left, right));
100
101
case
"-"
:
102
return
String.valueOf(calculator.subtract(left, right));
103
104
case
"*"
:
105
return
String.valueOf(calculator.multiply(left, right));
106
107
case
"/"
:
108
try
{
109
return
String.valueOf(calculator.divide(left, right));
110
}
catch
(ArithmeticException e) {
111
logger.error(
"Division by zero requested"
);
112
return
"Error: division by zero"
;
113
}
114
115
default
:
116
logger.warn(
"Unknown operator: {}"
, operator);
117
return
"Error: unknown operator '"
+ operator +
"'. "
+ USAGE;
118
}
119
}
120
}
com.ucoruh.calculator.CalculatorApp.main
static void main(String[] args)
The main entry point of the Calculator App.
Definition
CalculatorApp.java:56
Generated by
1.18.0