Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Calc/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

}
86 changes: 74 additions & 12 deletions src/Calc/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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 = "";
}
Expand Down