From bbd59fa8d6d71ea3010728dc79e5da517de9a9c9 Mon Sep 17 00:00:00 2001 From: sultanax100 Date: Sun, 28 Sep 2025 12:00:06 +0300 Subject: [PATCH 01/12] singleton --- src/Calc/Calculator.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index 7e2a37c..d5196d1 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -3,6 +3,7 @@ import java.awt.Color; import java.awt.event.*; import javax.swing.JButton; +import java.beans.Beans; /** * @@ -15,8 +16,26 @@ public final class Calculator extends javax.swing.JFrame { private String operation; private int x, y; + + // --- Singleton --- + private static Calculator INSTANCE; + + public static Calculator getInstance() { + if (INSTANCE == null) { + INSTANCE = new Calculator(); + } + return INSTANCE; + } public Calculator() { + + if (INSTANCE != null && !Beans.isDesignTime()) { + throw new IllegalStateException("Use Calculator.getInstance()"); + } + if (INSTANCE == null) { + INSTANCE = this; + } + initComponents(); getContentPane().setSize(400, 700); this.clear(); From 314ab90a6e39d597b7fee5b623a1352be94c7382 Mon Sep 17 00:00:00 2001 From: sultanax100 Date: Sun, 28 Sep 2025 22:58:59 +0300 Subject: [PATCH 02/12] singleton and factorial --- src/Calc/App.java | 6 ++++-- src/Calc/Calculator.java | 44 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/Calc/App.java b/src/Calc/App.java index 4101058..dc15a1d 100644 --- a/src/Calc/App.java +++ b/src/Calc/App.java @@ -7,8 +7,10 @@ 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 + Calculator.getInstance().setVisible(true); } } diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index d5196d1..bec9da6 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -17,7 +17,7 @@ public final class Calculator extends javax.swing.JFrame { private int x, y; - // --- Singleton --- + // --- Singleton Method --- private static Calculator INSTANCE; public static Calculator getInstance() { @@ -26,6 +26,29 @@ public static Calculator getInstance() { } return INSTANCE; } + + // --- Factory Method --- +@FunctionalInterface +private interface Operation {double apply(double a, double b); +} + +private Operation makeOperation(String symbol) { + switch (symbol) { + case "+" -> { + return (a, b) -> a + b; + } + case "-" -> { + return (a, b) -> a - b; + } + case "×", "*" -> { + return (a, b) -> a * b; + } + case "÷", "/" -> { + return (a, b) -> (b == 0) ? Double.NaN : a / b; + } + default -> throw new IllegalArgumentException("Unknown operation: " + symbol); + } +} public Calculator() { @@ -138,7 +161,9 @@ public void compute() { if (Float.isNaN(curr) || Float.isNaN(prev)) { return; } - + + //deleting the switch + /*.. switch (this.operation) { case "+" -> computation = prev + curr; @@ -158,6 +183,21 @@ public void compute() { return; } } +..*/ + + try { + if (("÷".equals(this.operation) || "/".equals(this.operation)) && curr == 0f) { + this.clear(); + this.currentOperand = "Error"; + return; + } + + Operation op = makeOperation(this.operation); + computation = (float) op.apply(prev, curr); + } catch (IllegalArgumentException ex) { + return; + } + this.currentOperand = (computation - (int) computation) != 0 ? Float.toString(computation) : Integer.toString((int) computation); this.previousOperand = ""; From 0699874870eb5b682cfedcf917aa5152eee5f7f1 Mon Sep 17 00:00:00 2001 From: sultanax100 Date: Mon, 29 Sep 2025 22:57:22 +0300 Subject: [PATCH 03/12] singleton and factorial --- src/Calc/App.java | 7 ++- src/Calc/Calculator.java | 99 +++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/Calc/App.java b/src/Calc/App.java index dc15a1d..75f9a61 100644 --- a/src/Calc/App.java +++ b/src/Calc/App.java @@ -10,7 +10,12 @@ public class App { public static void main(String[] args) { //deleting this line to test the singleton --> new Calculator().setVisible(true); //testing the sigelton - Calculator.getInstance().setVisible(true); + 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 bec9da6..a8561d8 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -3,7 +3,6 @@ import java.awt.Color; import java.awt.event.*; import javax.swing.JButton; -import java.beans.Beans; /** * @@ -14,7 +13,6 @@ public final class Calculator extends javax.swing.JFrame { private String currentOperand; private String previousOperand; private String operation; - private int x, y; // --- Singleton Method --- @@ -28,37 +26,48 @@ public static Calculator getInstance() { } // --- Factory Method --- -@FunctionalInterface -private interface Operation {double apply(double a, double b); + 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 Operation makeOperation(String symbol) { - switch (symbol) { - case "+" -> { - return (a, b) -> a + b; - } - case "-" -> { - return (a, b) -> a - b; - } - case "×", "*" -> { - return (a, b) -> a * b; - } - case "÷", "/" -> { - return (a, b) -> (b == 0) ? Double.NaN : 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"); } - default -> throw new IllegalArgumentException("Unknown operation: " + symbol); + return a/b;} } -} - public Calculator() { - - if (INSTANCE != null && !Beans.isDesignTime()) { - throw new IllegalStateException("Use Calculator.getInstance()"); - } - if (INSTANCE == null) { - INSTANCE = this; + 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(); @@ -151,18 +160,12 @@ 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 + //deleting the switch, we already have the operation interface /*.. switch (this.operation) { case "+" -> @@ -184,22 +187,22 @@ public void compute() { } } ..*/ - try { - if (("÷".equals(this.operation) || "/".equals(this.operation)) && curr == 0f) { - this.clear(); - this.currentOperand = "Error"; - return; - } - - Operation op = makeOperation(this.operation); - computation = (float) op.apply(prev, curr); - } catch (IllegalArgumentException ex) { + 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.currentOperand = (computation - (int) computation) != 0 ? Float.toString(computation) : Integer.toString((int) computation); + this.previousOperand = ""; this.operation = ""; } From b43b5ca7d502904a88bc7c32a6e637db526a1ccb Mon Sep 17 00:00:00 2001 From: Leen Alghamdi Date: Wed, 5 Nov 2025 01:18:02 +0300 Subject: [PATCH 04/12] add the facade pattern class and the decorator --- src/Calc/CalculatorFacade.java | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Calc/CalculatorFacade.java diff --git a/src/Calc/CalculatorFacade.java b/src/Calc/CalculatorFacade.java new file mode 100644 index 0000000..2672add --- /dev/null +++ b/src/Calc/CalculatorFacade.java @@ -0,0 +1,46 @@ +package Calc; + +// Facade Pattern: Interface بسيطة للتحكم بحسابات الآلة فقط (بدون GUI) +public class CalculatorFacade { + + private final Calculator calculator; + + public CalculatorFacade() { + this.calculator = Calculator.getInstance(); // نستخدم Singleton + } + + // إدخال رقم أو نقطة + public void enterNumber(String number) { + calculator.appendNumber(number); + } + + // اختيار عملية (+, -, ×, ÷) + public void chooseOperation(String operation) { + calculator.chooseOperation(operation); + } + + // = (تنفيذ العملية وعرض الناتج) + public void calculate() { + calculator.compute(); + calculator.updateDisplay(); + } + + // C (مسح كل شيء) + public void clear() { + calculator.clear(); + } + + // نقرأ الرقم الظاهر حالياً + public String getCurrentDisplay() { + return calculator.getCurrentOperand(); // لازم نضيف getter بسيط إذا غير موجود + } + + // نقرأ الرقم السابق والعملية (مثلاً "5 +") + public String getPreviousDisplay() { + return calculator.getPreviousOperand(); // Getter بسيط أيضاً + } + // نعرض النافذة لو بغينا GUI (ما يمس الواجهة) + public void showUI() { + calculator.setVisible(true); + } +} From 9c5adc39a1177d1c9fbb27d4e1d355027e3b448d Mon Sep 17 00:00:00 2001 From: Leen Alghamdi Date: Wed, 5 Nov 2025 01:21:36 +0300 Subject: [PATCH 05/12] add the facade pattern class and the decorator calculator.java --- src/Calc/Calculator.java | 100 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index a8561d8..476c7e9 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -27,6 +27,74 @@ public static Calculator getInstance() { // --- Factory Method --- private interface Operation { float apply(float a, float b); } + + + + + + + // --- Decorator Pattern (History) --- + +// 1) Abstract Decorator +private abstract static class OperationDecorator implements Operation { + protected Operation decoratedOperation; + + public OperationDecorator(Operation decoratedOperation) { + this.decoratedOperation = decoratedOperation; + } + + @Override + public float apply(float a, float b) { + return decoratedOperation.apply(a, b); + } +} + +// 2) Concrete Decorator - History +private static class HistoryOperation extends OperationDecorator { + + private static final java.util.List history = new java.util.ArrayList<>(); + + public HistoryOperation(Operation decoratedOperation) { + super(decoratedOperation); + } + + @Override + public float apply(float a, float b) { + float result = super.apply(a, b); + history.add(a + " " + getSymbol(decoratedOperation) + " " + b + " = " + result); + return result; + } + + private String getSymbol(Operation op) { + if (op instanceof AddOperation) return "+"; + if (op instanceof SubOperation) return "-"; + if (op instanceof MultOperation) return "×"; + if (op instanceof DivOperation) return "÷"; + return "?"; + } + + public static java.util.List getHistory() { + return history; + } +} + + + + + +// Getters needed for Facade +public String getCurrentOperand() { + return currentOperand; +} + +public String getPreviousOperand() { + return previousOperand; +} + + + + + private static class AddOperation implements Operation { @Override @@ -191,6 +259,21 @@ public void compute() { float curr = Float.parseFloat(this.currentOperand); float prev = Float.parseFloat(this.previousOperand); Operation op = OperationFactory.getOperation(this.operation); + + + + + + // نغلف العملية بالـ History Decorator + op = new HistoryOperation(op); + + + + + + + + float result = op.apply(prev, curr); this.currentOperand = (result - (int) result ) != 0 @@ -672,6 +755,23 @@ private void btnDivActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST: private void btnEqualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEqualActionPerformed this.compute(); this.updateDisplay(); + + + + + System.out.println("---- History ----"); +for (String h : HistoryOperation.getHistory()) { + System.out.println(h); +} + + + + + + + + + if (this.currentOperand.equals("Error")) this.currentOperand = ""; }//GEN-LAST:event_btnEqualActionPerformed From 5ed564aa7106318db257624159430822cc7c2d88 Mon Sep 17 00:00:00 2001 From: Leen Alghamdi Date: Wed, 5 Nov 2025 01:22:09 +0300 Subject: [PATCH 06/12] add the facade pattern class and the decorator app.java --- src/Calc/App.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Calc/App.java b/src/Calc/App.java index 75f9a61..053537f 100644 --- a/src/Calc/App.java +++ b/src/Calc/App.java @@ -8,13 +8,16 @@ public class App { public static void main(String[] args) { - //deleting this line to test the singleton --> new Calculator().setVisible(true); - //testing the sigelton javax.swing.SwingUtilities.invokeLater(() -> { + + // اختبار الـ Singleton Calculator calculator1 = Calculator.getInstance(); Calculator calculator2 = Calculator.getInstance(); System.out.println("same instance? " + (calculator1 == calculator2)); - calculator1.setVisible(true); + + // بدل ما نستخدم calculator1.setVisible → نستخدم Facade + CalculatorFacade calc = new CalculatorFacade(); + calc.showUI(); // هذا يعرض الواجهة }); } From 2aacff7c57e08cbb7fb0fc66547067a160675244 Mon Sep 17 00:00:00 2001 From: Leen Alghamdi Date: Wed, 5 Nov 2025 15:29:43 +0300 Subject: [PATCH 07/12] rollback --- src/Calc/App.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Calc/App.java b/src/Calc/App.java index 053537f..598c9ac 100644 --- a/src/Calc/App.java +++ b/src/Calc/App.java @@ -15,9 +15,8 @@ public static void main(String[] args) { Calculator calculator2 = Calculator.getInstance(); System.out.println("same instance? " + (calculator1 == calculator2)); - // بدل ما نستخدم calculator1.setVisible → نستخدم Facade - CalculatorFacade calc = new CalculatorFacade(); - calc.showUI(); // هذا يعرض الواجهة + calculator1.setVisible(true); + }); } From 632d8914579da412744139d458d2dea480d0f1d3 Mon Sep 17 00:00:00 2001 From: Leen Alghamdi Date: Wed, 5 Nov 2025 15:29:59 +0300 Subject: [PATCH 08/12] rollback2 --- src/Calc/Calculator.java | 48 ++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index 476c7e9..749ef06 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -26,7 +26,9 @@ public static Calculator getInstance() { } // --- Factory Method --- - private interface Operation { float apply(float a, float b); } + private interface Operation { + float apply(float a, float b); + } @@ -65,37 +67,19 @@ public float apply(float a, float b) { return result; } - private String getSymbol(Operation op) { - if (op instanceof AddOperation) return "+"; - if (op instanceof SubOperation) return "-"; - if (op instanceof MultOperation) return "×"; - if (op instanceof DivOperation) return "÷"; - return "?"; - } +private String getSymbol(Operation op) { + if (op instanceof AddOperation) return "+"; + if (op instanceof SubOperation) return "-"; + if (op instanceof MultOperation) return "×"; + if (op instanceof DivOperation) return "÷"; + return "?"; +} public static java.util.List getHistory() { return history; } } - - - - -// Getters needed for Facade -public String getCurrentOperand() { - return currentOperand; -} - -public String getPreviousOperand() { - return previousOperand; -} - - - - - - private static class AddOperation implements Operation { @Override public float apply(float a,float b){ @@ -135,12 +119,12 @@ static Operation getOperation(String op) { } } - private Calculator() { - initComponents(); +private Calculator() { + initComponents(); getContentPane().setSize(400, 700); - this.clear(); - this.addEvents(); - } + this.clear(); + this.addEvents(); +} public void addEvents() { JButton[] btns = { @@ -180,7 +164,7 @@ public void mouseExited(MouseEvent e) { } }); } - } + } public void clear() { this.currentOperand = ""; From d1077577336e8da3e866247c23bd29fe31674ec3 Mon Sep 17 00:00:00 2001 From: Leen Alghamdi Date: Wed, 5 Nov 2025 17:02:15 +0300 Subject: [PATCH 09/12] the facade pattern again --- src/Calc/App.java | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Calc/App.java b/src/Calc/App.java index 598c9ac..a2cd664 100644 --- a/src/Calc/App.java +++ b/src/Calc/App.java @@ -1,23 +1,18 @@ package Calc; -/** - * - * @author youcefhmd - */ - public class App { - - public static void main(String[] args) { + public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(() -> { - // اختبار الـ Singleton - Calculator calculator1 = Calculator.getInstance(); - Calculator calculator2 = Calculator.getInstance(); - System.out.println("same instance? " + (calculator1 == calculator2)); + CalculatorFacade calcFacade = new CalculatorFacade(); - calculator1.setVisible(true); + // بدل ما نستدعي Calculator مباشرة: + calcFacade.showCalculator(); + // مثال تشغيل: + float result = calcFacade.performOperation("+", 5, 3); + System.out.println("Result = " + result); }); } - } + From 2d3ae729212dcedc84335b5433732b1c8b309b18 Mon Sep 17 00:00:00 2001 From: Leen Alghamdi Date: Wed, 5 Nov 2025 17:02:25 +0300 Subject: [PATCH 10/12] the facade pattern again2 --- src/Calc/Calculator.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index 749ef06..e04df33 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -26,7 +26,7 @@ public static Calculator getInstance() { } // --- Factory Method --- - private interface Operation { + public interface Operation { float apply(float a, float b); } @@ -52,7 +52,7 @@ public float apply(float a, float b) { } // 2) Concrete Decorator - History -private static class HistoryOperation extends OperationDecorator { +public static class HistoryOperation extends OperationDecorator { private static final java.util.List history = new java.util.ArrayList<>(); @@ -80,6 +80,7 @@ public static java.util.List getHistory() { } } + private static class AddOperation implements Operation { @Override public float apply(float a,float b){ @@ -107,7 +108,7 @@ public float apply(float a,float b){ return a/b;} } - private static class OperationFactory { + public static class OperationFactory { static Operation getOperation(String op) { switch (op) { case "+": return new AddOperation(); From dda6df296355a50fb27b790cc34a70f92c36e024 Mon Sep 17 00:00:00 2001 From: Leen Alghamdi Date: Wed, 5 Nov 2025 17:02:35 +0300 Subject: [PATCH 11/12] the facade pattern again3 --- src/Calc/CalculatorFacade.java | 46 +++++++++++++--------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/src/Calc/CalculatorFacade.java b/src/Calc/CalculatorFacade.java index 2672add..027afd8 100644 --- a/src/Calc/CalculatorFacade.java +++ b/src/Calc/CalculatorFacade.java @@ -1,46 +1,34 @@ package Calc; -// Facade Pattern: Interface بسيطة للتحكم بحسابات الآلة فقط (بدون GUI) public class CalculatorFacade { - private final Calculator calculator; + private Calculator calculator; public CalculatorFacade() { - this.calculator = Calculator.getInstance(); // نستخدم Singleton + calculator = Calculator.getInstance(); // Singleton } - // إدخال رقم أو نقطة - public void enterNumber(String number) { - calculator.appendNumber(number); - } - - // اختيار عملية (+, -, ×, ÷) - public void chooseOperation(String operation) { - calculator.chooseOperation(operation); + // تفتح واجهة الآلة الحاسبة + public void showCalculator() { + calculator.setVisible(true); } - // = (تنفيذ العملية وعرض الناتج) - public void calculate() { - calculator.compute(); - calculator.updateDisplay(); + // تنفّذ عملية رياضية وتُرجع الناتج + public float performOperation(String operation, float a, float b) { + Calculator.Operation op = Calculator.OperationFactory.getOperation(operation); + if (op == null) { + throw new IllegalArgumentException("Invalid operation: " + operation); + } + return op.apply(a, b); } - // C (مسح كل شيء) - public void clear() { + // تمسح كل شي من الشاشة (Clear) + public void clearCalculator() { calculator.clear(); } - // نقرأ الرقم الظاهر حالياً - public String getCurrentDisplay() { - return calculator.getCurrentOperand(); // لازم نضيف getter بسيط إذا غير موجود - } - - // نقرأ الرقم السابق والعملية (مثلاً "5 +") - public String getPreviousDisplay() { - return calculator.getPreviousOperand(); // Getter بسيط أيضاً - } - // نعرض النافذة لو بغينا GUI (ما يمس الواجهة) - public void showUI() { - calculator.setVisible(true); + // تعرض الـ History (من Decorator) + public java.util.List getHistory() { + return Calculator.HistoryOperation.getHistory(); } } From 6be6e0fa843e43a0904313dcd304f196913a267c Mon Sep 17 00:00:00 2001 From: Leen Alghamdi Date: Wed, 5 Nov 2025 17:18:00 +0300 Subject: [PATCH 12/12] new decorator --- src/Calc/Calculator.java | 119 +++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 61 deletions(-) diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index e04df33..c2cdc17 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -70,8 +70,8 @@ public float apply(float a, float b) { private String getSymbol(Operation op) { if (op instanceof AddOperation) return "+"; if (op instanceof SubOperation) return "-"; - if (op instanceof MultOperation) return "×"; - if (op instanceof DivOperation) return "÷"; + if (op instanceof MultOperation) return "*"; + if (op instanceof DivOperation) return "/"; return "?"; } @@ -81,6 +81,32 @@ public static java.util.List getHistory() { } + +// concrete Decorator – Logging +private static class LoggingOperation extends OperationDecorator { + + public LoggingOperation(Operation decoratedOperation) { + super(decoratedOperation); + } + + @Override + public float apply(float a, float b) { + float result = super.apply(a, b); + System.out.println("[LOG] " + a + " " + getSymbol(decoratedOperation) + " " + b + " = " + result); + return result; + } + + private String getSymbol(Operation op) { + if (op instanceof AddOperation) return "+"; + if (op instanceof SubOperation) return "-"; + if (op instanceof MultOperation) return "*"; + if (op instanceof DivOperation) return "/"; + return "?"; + } +} + + + private static class AddOperation implements Operation { @Override public float apply(float a,float b){ @@ -212,69 +238,40 @@ public void chooseOperation(String operation) { this.updateDisplay(); } - public void compute() { - - if (this.currentOperand.equals("") || this.previousOperand.equals("")) { - return; - } - - //deleting the switch, we already have the operation interface - /*.. - switch (this.operation) { - case "+" -> - computation = prev + curr; - case "-" -> - computation = prev - curr; - case "×" -> - computation = prev * curr; - case "÷" -> { - if (curr == 0) { - this.clear(); - this.currentOperand = "Error"; - return; - } - computation = prev / curr; - } - default -> { - return; - } - } -..*/ - try { - float curr = Float.parseFloat(this.currentOperand); - float prev = Float.parseFloat(this.previousOperand); - Operation op = OperationFactory.getOperation(this.operation); - - - - - - // نغلف العملية بالـ History Decorator - op = new HistoryOperation(op); - - - - - - - - - float result = op.apply(prev, curr); - - this.currentOperand = (result - (int) result ) != 0 +public void compute() { + + if (this.currentOperand.equals("") || this.previousOperand.equals("")) { + return; + } + + try { + float curr = Float.parseFloat(this.currentOperand); + float prev = Float.parseFloat(this.previousOperand); + + // نجيب العملية الأساسية من الفاكتوري + Operation op = OperationFactory.getOperation(this.operation); + + // نغلفها بـ History + Logging (Decorator Pattern) + op = new HistoryOperation(op); + op = new LoggingOperation(op); + + 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 = ""; + + } catch (IllegalArgumentException | ArithmeticException ex) { + this.clear(); + this.currentOperand = "Error"; + return; } + this.previousOperand = ""; + this.operation = ""; +} + + public void updateDisplay() { current.setText(this.currentOperand); previous.setText(previousOperand + " " + this.operation);