diff --git a/src/Calc/App.java b/src/Calc/App.java index 4101058..75f9a61 100644 --- a/src/Calc/App.java +++ b/src/Calc/App.java @@ -7,8 +7,15 @@ public class App { - public static void main(String[] args) { - new Calculator().setVisible(true); + public static void main(String[] args) { + //deleting this line to test the singleton --> new Calculator().setVisible(true); + //testing the sigelton + javax.swing.SwingUtilities.invokeLater(() -> { + Calculator calculator1 = Calculator.getInstance(); + Calculator calculator2 = Calculator.getInstance(); + System.out.println("same instance? " + (calculator1 == calculator2)); + calculator1.setVisible(true); + }); } } diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index 7e2a37c..a8561d8 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -13,10 +13,61 @@ public final class Calculator extends javax.swing.JFrame { private String currentOperand; private String previousOperand; private String operation; - private int x, y; + + // --- Singleton Method --- + private static Calculator INSTANCE; + + public static Calculator getInstance() { + if (INSTANCE == null) { + INSTANCE = new Calculator(); + } + return INSTANCE; + } + + // --- Factory Method --- + private interface Operation { float apply(float a, float b); } + + private static class AddOperation implements Operation { + @Override + public float apply(float a,float b){ + return a+b;} +} + + private static class SubOperation implements Operation { + @Override + public float apply(float a,float b){ + return a-b;} + } + + private static class MultOperation implements Operation { + @Override + public float apply(float a,float b){ + return a*b;} + } + + private static class DivOperation implements Operation { + @Override + public float apply(float a,float b){ + if (b == 0f) { + throw new ArithmeticException("Error! Division by zero is not accepted"); + } + return a/b;} + } - public Calculator() { + private static class OperationFactory { + static Operation getOperation(String op) { + switch (op) { + case "+": return new AddOperation(); + case "-": return new SubOperation(); + case "×": return new MultOperation(); + case "÷": return new DivOperation(); + default: throw new IllegalArgumentException("Unknown operation: " + op); + } + } + } + + private Calculator() { initComponents(); getContentPane().setSize(400, 700); this.clear(); @@ -109,17 +160,13 @@ public void chooseOperation(String operation) { } public void compute() { - float computation; + if (this.currentOperand.equals("") || this.previousOperand.equals("")) { return; } - - float curr = Float.parseFloat(this.currentOperand); - float prev = Float.parseFloat(this.previousOperand); - if (Float.isNaN(curr) || Float.isNaN(prev)) { - return; - } - + + //deleting the switch, we already have the operation interface + /*.. switch (this.operation) { case "+" -> computation = prev + curr; @@ -139,8 +186,23 @@ public void compute() { return; } } - - this.currentOperand = (computation - (int) computation) != 0 ? Float.toString(computation) : Integer.toString((int) computation); +..*/ + try { + float curr = Float.parseFloat(this.currentOperand); + float prev = Float.parseFloat(this.previousOperand); + Operation op = OperationFactory.getOperation(this.operation); + float result = op.apply(prev, curr); + + this.currentOperand = (result - (int) result ) != 0 + ? Float.toString(result ) + : Integer.toString((int) result ); + + } catch (IllegalArgumentException | ArithmeticException ex) { + this.clear(); + this.currentOperand = "Error"; + return; + } + this.previousOperand = ""; this.operation = ""; }