Updated Main Class To Include Functionality for Classes/Enums/ and Interfaces

master
sgoudham 4 years ago
parent ccc6bed7b9
commit dda6f5690b
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

@ -1,10 +1,94 @@
import java.util.Scanner;
public class NewCalcEngine {
public static void main(String[] args) {
performCalculations();
// performCalculations();
// executeCalculations();
// executeInteractively();
dynamicInteractivity();
}
private static void dynamicInteractivity() {
DynamicHelper dynamicHelper = new DynamicHelper(new MathProcessing[] {
new Adder(),
new PowerOf()
});
Scanner in = new Scanner(System.in);
System.out.println("Enter In Your Operation Please: \n");
String userInput = in.nextLine();
dynamicHelper.process(userInput);
}
private static void executeInteractively() {
Scanner in = new Scanner(System.in);
System.out.println("Please Enter In A Calculation: \n");
String userInput = in.nextLine();
String parts[] = userInput.split(" ");
performOperation(parts);
}
private static void performOperation(String[] parts) {
MathOperation mathOperation = MathOperation.valueOf(parts[0].toUpperCase());
double leftVal = Double.parseDouble(parts[1]);
double rightVal = Double.parseDouble(parts[2]);
CalculateBase calculateBase = createCalculation(mathOperation, leftVal, rightVal);
calculateBase.calculate();
System.out.println("\nOperation Performed: " + mathOperation);
System.out.println("Result: " + calculateBase.getResult());
}
private static CalculateBase createCalculation(MathOperation mathOperation, double leftVal, double rightVal) {
CalculateBase calculation = null;
switch (mathOperation) {
case ADD:
calculation = new Adder(leftVal, rightVal);
break;
case MULTIPLY:
calculation = new Multiplier(leftVal, rightVal);
break;
case DIVIDE:
calculation = new Divider(leftVal, rightVal);
break;
case SUBTRACT:
calculation = new Subtractor(leftVal, rightVal);
break;
}
return calculation;
}
private static void executeCalculations() {
CalculateBase[] calculations = {
new Divider(100.0d, 50.0d),
new Adder(25.0d, 92.0d),
new Subtractor(225.0d, 17.0d),
new Multiplier(11.0d, 3.0d)
};
System.out.println("Array Calculations");
for (CalculateBase calculation: calculations) {
calculation.calculate();
System.out.println("Result: " + calculation.getResult());
}
}
static void performCalculations() {
private static void doCalculation(CalculateBase calculate, double leftVal, double rightVal) {
calculate.setLeftVal(leftVal);
calculate.setRightVal(rightVal);
calculate.calculate();
System.out.println("The result of the calculation is: " + calculate.getResult());
}
private static void performCalculations() {
MathEquation[] mathEquation = new MathEquation[4];
mathEquation[0] = new MathEquation('d', 100.0d, 50.0d);
@ -18,5 +102,21 @@ public class NewCalcEngine {
}
System.out.println("Average Result = " + MathEquation.getAverageResult());
System.out.println("\nUsing Execute Method Overloads\n");
MathEquation mathEquationOverload = new MathEquation('d');
double leftDouble = 9.0d;
double rightDouble = 4.0d;
mathEquationOverload.execute(leftDouble, rightDouble);
System.out.println("Overloaded result with doubles: " + mathEquationOverload.getResult());
int leftInt = 9;
int rightInt = 4;
mathEquationOverload.execute(leftInt, rightInt);
System.out.println("Overloaded result with ints: " + mathEquationOverload.getResult());
}
}
Loading…
Cancel
Save