diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Adder.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Adder.java new file mode 100644 index 0000000..8611bb1 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Adder.java @@ -0,0 +1,31 @@ +package src; + +public class Adder extends CalculateBase implements MathProcessing { + + public Adder() { + } + + public Adder(double leftVal, double rightVal) { + super(leftVal, rightVal); + } + + @Override + public void calculate() { + setResult(getLeftVal() + getRightVal()); + } + + // Using interfaces + @Override + public String getKeyWord() { + return "add"; + } + + @Override + public double doCalculation(double leftVal, double rightVal) { + setLeftVal(leftVal); + setRightVal(rightVal); + calculate(); + + return getResult(); + } +} diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/CalculateBase.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/CalculateBase.java new file mode 100644 index 0000000..82e0a00 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/CalculateBase.java @@ -0,0 +1,43 @@ +package src; + +public abstract class CalculateBase { + private double leftVal; + private double rightVal; + private double result; + + public abstract void calculate(); + + public CalculateBase() { + } + + public CalculateBase(double leftVal, double rightVal) { + this.leftVal = leftVal; + this.rightVal = rightVal; + } + + // Getters and setters + public double getLeftVal() { + return leftVal; + } + + public void setLeftVal(double leftVal) { + this.leftVal = leftVal; + } + + public double getRightVal() { + return rightVal; + } + + public void setRightVal(double rightVal) { + this.rightVal = rightVal; + } + + public double getResult() { + return result; + } + + public void setResult(double result) { + this.result = result; + } + +} diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Divider.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Divider.java new file mode 100644 index 0000000..ae83d0d --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Divider.java @@ -0,0 +1,16 @@ +package src; + +public class Divider extends CalculateBase { + + public Divider() { + } + + public Divider(double leftVal, double rightVal) { + super(leftVal, rightVal); + } + + @Override + public void calculate() { + setResult(getLeftVal() / getRightVal()); + } +} diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/DynamicHelper.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/DynamicHelper.java new file mode 100644 index 0000000..3cf6f36 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/DynamicHelper.java @@ -0,0 +1,31 @@ +package src; + +public class DynamicHelper { + + private final MathProcessing[] handlers; + + public DynamicHelper(MathProcessing[] handlers) { + this.handlers = handlers; + } + + public void process(String statement) { + String[] parts = statement.split(MathProcessing.SEPARATOR); + String keyword = parts[0]; + double leftVal = Double.parseDouble(parts[1]); + double rightVal = Double.parseDouble(parts[2]); + + MathProcessing theHandler = null; + for (MathProcessing handler : handlers) { + if (keyword.equalsIgnoreCase(handler.getKeyWord())) { + theHandler = handler; + break; + } + } + + double result = theHandler.doCalculation(leftVal, rightVal); + if (theHandler.doFormatting() != null) + System.out.println(theHandler.doFormatting()); + else + System.out.println("result = " + result); + } +} diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/MathEquation.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/MathEquation.java new file mode 100644 index 0000000..16ded90 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/MathEquation.java @@ -0,0 +1,113 @@ +package src; + +public class MathEquation { + private double leftVal; + private double rightVal; + private char opCode; + private double result; + + private static int numOfCalculations; + private static double sumOfResults; + + // Empty constructor to allow us to use MathEquation without needing to pass in + // anything + public MathEquation() { + } + + // This only exists as a demo for chaining constructors + public MathEquation(char opCode) { + this.opCode = opCode; + } + + // Demonstrating how chaining constructors works + public MathEquation(char opCode, double leftVal, double rightVal) { + this(opCode); + this.leftVal = leftVal; + this.rightVal = rightVal; + } + + // Method to execute the calculations + public void execute() { + switch (this.opCode) { + case 'a': + this.result = this.leftVal + this.rightVal; + break; + case 's': + this.result = this.leftVal - this.rightVal; + break; + case 'm': + this.result = this.leftVal * this.rightVal; + break; + case 'd': + this.result = this.rightVal != 0 ? this.leftVal / this.rightVal : 0.0d; + break; + default: + System.out.println("Invalid opCode: " + this.opCode); + this.result = 0.0d; + break; + } + numOfCalculations++; + sumOfResults += result; + } + + public void execute(double leftVal, double rightVal) { + this.leftVal = leftVal; + this.rightVal = rightVal; + + execute(); + } + + public void execute(int leftVal, int rightVal) { + this.leftVal = leftVal; + this.rightVal = rightVal; + + execute(); + + result = (int) result; + } + + public static double getAverageResult() { + return sumOfResults / numOfCalculations; + } + + public MathEquation(double leftVal, double rightVal, char opCode, double result) { + this.leftVal = leftVal; + this.rightVal = rightVal; + this.opCode = opCode; + this.result = result; + } + + // Setters + + public double getLeftVal() { + return leftVal; + } + + public void setLeftVal(double leftVal) { + this.leftVal = leftVal; + } + + public double getRightVal() { + return rightVal; + } + + public void setRightVal(double rightVal) { + this.rightVal = rightVal; + } + + public char getOpCode() { + return opCode; + } + + public void setOpCode(char opCode) { + this.opCode = opCode; + } + + public double getResult() { + return result; + } + + public void setResult(double result) { + this.result = result; + } +} diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/MathOperation.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/MathOperation.java new file mode 100644 index 0000000..c9aa4a8 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/MathOperation.java @@ -0,0 +1,5 @@ +package src; + +public enum MathOperation { + ADD, SUBTRACT, DIVIDE, MULTIPLY +} \ No newline at end of file diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/MathProcessing.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/MathProcessing.java new file mode 100644 index 0000000..0d6dde3 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/MathProcessing.java @@ -0,0 +1,14 @@ +package src; + +public interface MathProcessing { + + String SEPARATOR = " "; + + String getKeyWord(); + + double doCalculation(double leftVal, double rightVal); + + default String doFormatting() { + return null; + } +} diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Multiplier.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Multiplier.java new file mode 100644 index 0000000..c725625 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Multiplier.java @@ -0,0 +1,16 @@ +package src; + +public class Multiplier extends CalculateBase { + + public Multiplier() { + } + + public Multiplier(double leftVal, double rightVal) { + super(leftVal, rightVal); + } + + @Override + public void calculate() { + setResult(getLeftVal() * getRightVal()); + } +} diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/NewCalcEngine.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/NewCalcEngine.java new file mode 100644 index 0000000..1ee22a5 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/NewCalcEngine.java @@ -0,0 +1,120 @@ +package src; + +import java.util.Scanner; + +public class NewCalcEngine { + + public static void main(String[] args) { + // 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(); + in.close(); + + 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(); + in.close(); + + 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()); + } + } + + 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); + mathEquation[1] = new MathEquation('a', 25.0d, 92.0d); + mathEquation[2] = new MathEquation('s', 225.0d, 17.0d); + mathEquation[3] = new MathEquation('m', 11.0d, 3.0d); + + for (MathEquation equation : mathEquation) { + equation.execute(); + System.out.println("Result = " + equation.getResult()); + } + + 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()); + + } +} \ No newline at end of file diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/PowerOf.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/PowerOf.java new file mode 100644 index 0000000..8f977a4 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/PowerOf.java @@ -0,0 +1,23 @@ +package src; + +public class PowerOf implements MathProcessing { + + private String formattedOutput; + + @Override + public String getKeyWord() { + return "power"; + } + + @Override + public String doFormatting() { + return formattedOutput; + } + + @Override + public double doCalculation(double leftVal, double rightVal) { + double product = Math.pow(leftVal, rightVal); + formattedOutput = leftVal + " Has Been Raised To The Power Of " + rightVal + " Which Results In: " + product; + return product; + } +} diff --git a/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Subtractor.java b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Subtractor.java new file mode 100644 index 0000000..37b2609 --- /dev/null +++ b/Java Classes & Interfaces/Pluralsite/New Calc Engine/src/Subtractor.java @@ -0,0 +1,16 @@ +package src; + +public class Subtractor extends CalculateBase { + + public Subtractor() { + } + + public Subtractor(double leftVal, double rightVal) { + super(leftVal, rightVal); + } + + @Override + public void calculate() { + setResult(getLeftVal() - getRightVal()); + } +}