Skip to content

Commit 5df99fa

Browse files
iamshwetabhardwajmaibin
authored andcommitted
BAEL-3506 - Refactoring. (eugenp#8238)
* BAEL-3506 * BAEL-3506 * BAEL-3506 - Added java-math-2 to parent pom. * BAEL-3506 - Refactoring.
1 parent d6c9bfe commit 5df99fa

File tree

2 files changed

+29
-40
lines changed

2 files changed

+29
-40
lines changed

java-math-2/src/main/java/com/baeldung/maths/calculator/basic/BasicCalculatorIfElse.java renamed to core-java-modules/core-java-lang-math/src/main/java/com/baeldung/calculator/basic/BasicCalculatorIfElse.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.maths.calculator.basic;
1+
package com.baeldung.calculator.basic;
22

33
import java.util.InputMismatchException;
44
import java.util.Scanner;
@@ -7,19 +7,14 @@ public class BasicCalculatorIfElse {
77

88
public static void main(String[] args) {
99

10-
System.out.println("---------------------------------- \n" +
11-
"Welcome to Basic Calculator \n" +
12-
"----------------------------------");
13-
System.out.println("Following operations are supported : \n" +
14-
"1. Addition (+) \n" +
15-
"2. Subtraction (-) \n" +
16-
"3. Multiplication (* OR x) \n" +
17-
"4. Division (/) \n");
10+
System.out.println("---------------------------------- \n" + "Welcome to Basic Calculator \n" + "----------------------------------");
11+
System.out.println("Following operations are supported : \n" + "1. Addition (+) \n" + "2. Subtraction (-) \n" + "3. Multiplication (* OR x) \n" + "4. Division (/) \n");
1812

1913
Scanner scanner = new Scanner(System.in);
2014
try {
2115
System.out.println("Enter an operator: (+ OR - OR * OR /) ");
22-
char operation = scanner.next().charAt(0);
16+
char operation = scanner.next()
17+
.charAt(0);
2318

2419
if (!(operation == '+' || operation == '-' || operation == '*' || operation == 'x' || operation == '/')) {
2520
System.err.println("Invalid Operator. Please use only + or - or * or /");
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
package com.baeldung.maths.calculator.basic;
1+
package com.baeldung.calculator.basic;
22

33
import java.util.InputMismatchException;
44
import java.util.Scanner;
55

66
public class BasicCalculatorSwitchCase {
77
public static void main(String[] args) {
88

9-
System.out.println("---------------------------------- \n"
10-
+ "Welcome to Basic Calculator \n"
11-
+ "----------------------------------");
12-
System.out.println("Following operations are supported : \n" +
13-
"1. Addition (+) \n" +
14-
"2. Subtraction (-) \n" +
15-
"3. Multiplication (* OR x) \n" +
16-
"4. Division (/) \n");
9+
System.out.println("---------------------------------- \n" + "Welcome to Basic Calculator \n" + "----------------------------------");
10+
System.out.println("Following operations are supported : \n" + "1. Addition (+) \n" + "2. Subtraction (-) \n" + "3. Multiplication (* OR x) \n" + "4. Division (/) \n");
1711

1812
Scanner scanner = new Scanner(System.in);
1913
try {
2014
System.out.println("Enter an operator: (+ OR - OR * OR /) ");
21-
char operation = scanner.next().charAt(0);
15+
char operation = scanner.next()
16+
.charAt(0);
2217

2318
if (!(operation == '+' || operation == '-' || operation == '*' || operation == 'x' || operation == '/')) {
2419
System.err.println("Invalid Operator. Please use only + or - or * or /");
@@ -35,30 +30,29 @@ public static void main(String[] args) {
3530
}
3631

3732
switch (operation) {
38-
case '+':
39-
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
40-
break;
41-
case '-':
42-
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
43-
break;
44-
case '*':
45-
System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));
46-
break;
47-
case 'x':
48-
System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));
49-
break;
50-
case '/':
51-
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
52-
break;
53-
default:
54-
System.err.println("Invalid Operator Specified.");
55-
break;
33+
case '+':
34+
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
35+
break;
36+
case '-':
37+
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
38+
break;
39+
case '*':
40+
System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));
41+
break;
42+
case 'x':
43+
System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));
44+
break;
45+
case '/':
46+
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
47+
break;
48+
default:
49+
System.err.println("Invalid Operator Specified.");
50+
break;
5651
}
5752
} catch (InputMismatchException exc) {
5853
System.err.println(exc.getMessage());
5954
} finally {
6055
scanner.close();
6156
}
6257
}
63-
}
64-
58+
}

0 commit comments

Comments
 (0)