Skip to content

Commit cba09fa

Browse files
committed
Project with Classes and Interfaces
1 parent dc1f60d commit cba09fa

File tree

9 files changed

+190
-0
lines changed

9 files changed

+190
-0
lines changed

Working with Classes and Interfaces in Java/CalcEngine/.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Working with Classes and Interfaces in Java/CalcEngine/.idea/description.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Working with Classes and Interfaces in Java/CalcEngine/.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Working with Classes and Interfaces in Java/CalcEngine/.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Working with Classes and Interfaces in Java/CalcEngine/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Working with Classes and Interfaces in Java/CalcEngine/.idea/project-template.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Working with Classes and Interfaces in Java/CalcEngine/.idea/vcs.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.pluralsight.calcengine;
2+
3+
import java.time.LocalDate;
4+
import java.util.Scanner;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
double[] leftVals = {100.0d, 25.0d, 225.0d, 11.0d};
10+
double[] rightVals = {50.0d, 92.0d, 17.0d, 3.0d};
11+
char[] opCodes = {'a', 's', 'm', 'd'};
12+
double[] results = new double[opCodes.length];
13+
if (args.length == 0) {
14+
for (int i = 0; i < opCodes.length; i++) {
15+
results[i] = execute(opCodes[i], leftVals[i], rightVals[i]);
16+
}
17+
for(double currentResult : results)
18+
System.out.println(currentResult);
19+
} else if (args.length == 1 && args[0].equals("interactive"))
20+
executeInteractively();
21+
else if (args.length == 3)
22+
handleCommandLine(args);
23+
else
24+
System.out.println("Please provide an operation code and 2 numeric values");
25+
}
26+
27+
static void executeInteractively() {
28+
System.out.println("Enter an operation and two numbers");
29+
Scanner scanner = new Scanner(System.in);
30+
String userInput = scanner.nextLine();
31+
String[] parts = userInput.split(" ");
32+
performOperation(parts);
33+
}
34+
35+
private static void performOperation(String[] parts) {
36+
char opCode = opCodeFromString(parts[0]);
37+
if (opCode == 'w')
38+
handleWhen(parts);
39+
else {
40+
double leftVal = valueFromWord(parts[1]);
41+
double rightVal = valueFromWord(parts[2]);
42+
double result = execute(opCode, leftVal, rightVal);
43+
displayResult(opCode, leftVal, rightVal, result);
44+
}
45+
}
46+
47+
private static void handleWhen(String[] parts) {
48+
LocalDate startDate = LocalDate.parse(parts[1]);
49+
long daysToAdd = (long) valueFromWord(parts[2]);
50+
LocalDate newDate = startDate.plusDays(daysToAdd);
51+
String output = String.format("%s plus %d days is %s", startDate, daysToAdd, newDate);
52+
System.out.println(output);
53+
}
54+
55+
private static void displayResult(char opCode, double leftVal, double rightVal, double result) {
56+
char symbol = symbolFromOpCode(opCode);
57+
// StringBuilder builder = new StringBuilder(20);
58+
// builder.append(leftVal);
59+
// builder.append(" ");
60+
// builder.append(symbol);
61+
// builder.append(" ");
62+
// builder.append(rightVal);
63+
// builder.append(" = ");
64+
// builder.append(result);
65+
// String output = builder.toString();
66+
// System.out.println(output);
67+
68+
String output = String.format("%.3f %c %.3f = %.3f", leftVal, symbol, rightVal, result);
69+
System.out.println(output);
70+
}
71+
72+
private static char symbolFromOpCode(char opCode) {
73+
char[] opCodes = {'a', 's', 'm', 'd'};
74+
char[] symbols = {'+', '-', '*', '/'};
75+
char symbol = ' ';
76+
for (int index = 0; index < opCodes.length; index++) {
77+
if (opCode == opCodes[index]) {
78+
symbol = symbols[index];
79+
break;
80+
}
81+
}
82+
return symbol;
83+
}
84+
85+
private static void handleCommandLine(String[] args) {
86+
char opCode = args[0].charAt(0);
87+
double leftVal = Double.parseDouble(args[1]);
88+
double rightVal = Double.parseDouble(args[2]);
89+
double result = execute(opCode, leftVal, rightVal);
90+
System.out.println(result);
91+
}
92+
93+
static double execute(char opCode, double leftVal, double rightVal) {
94+
double result;
95+
switch (opCode) {
96+
case 'a':
97+
result = leftVal + rightVal;
98+
break;
99+
case 's':
100+
result = leftVal - rightVal;
101+
break;
102+
case 'm':
103+
result = leftVal * rightVal;
104+
break;
105+
case 'd':
106+
result = rightVal != 0 ? leftVal / rightVal : 0.0d;
107+
break;
108+
default:
109+
System.out.println("Invalid opCode: " + opCode);
110+
result = 0.0d;
111+
break;
112+
}
113+
return result;
114+
}
115+
116+
static char opCodeFromString(String operationName) {
117+
char opCode = operationName.charAt(0);
118+
return opCode;
119+
}
120+
121+
static double valueFromWord(String word) {
122+
String[] numberWords = {
123+
"zero", "one", "two", "three", "four",
124+
"five", "six", "seven", "eight", "nine"
125+
};
126+
double value = -1;
127+
for (int index = 0; index < numberWords.length; index++) {
128+
if (word.equals(numberWords[index])) {
129+
value = index;
130+
break;
131+
}
132+
}
133+
if (value == -1d)
134+
value = Double.parseDouble(word);
135+
return value;
136+
}
137+
}

0 commit comments

Comments
 (0)