Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce an OOP approach for Simple-Java-Calculator based on Design Patterns #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions src/simplejavacalculator/AbsCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package simplejavacalculator;

public class AbsCalc implements Calculator {
public double calculate(Buffer buffer) {
return Math.abs(buffer.num1);
}
}
9 changes: 9 additions & 0 deletions src/simplejavacalculator/AddCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package simplejavacalculator;

public class AddCalc implements Calculator {

public double calculate(Buffer buffer) {

return buffer.num1 + buffer.num2;
}
}
23 changes: 23 additions & 0 deletions src/simplejavacalculator/BinaryCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package simplejavacalculator;

import javax.swing.JTextArea;

import simplejavacalculator.Buffer.PreviouslyPressed;

public class BinaryCommand extends Command {

public void execute(Buffer buffer, String label) {
this.sub = CommandSubtype.COM_BINARY;
Buffer.pp = PreviouslyPressed.PRESSED_BINARY;
// Calculation with an abstraction manner
}

public void show(JTextArea text, Buffer buffer, Calculator calc, String label) {
buffer.c = calc; // save the pointer globally
text.setText(label);
if (buffer.c != null && (buffer.num1 != null && buffer.num2 != null)) {
buffer.num1 = (calc.calculate(buffer));
writer(buffer.num1, text);
}
}
}
19 changes: 19 additions & 0 deletions src/simplejavacalculator/Buffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package simplejavacalculator;

public class Buffer {
public Double num1 = null, num2 = null;
public Double buffered_res = null;

public enum PreviouslyPressed {
PRESSED_NONE,
PRESSED_NUMBER,
PRESSED_UNARY,
PRESSED_BINARY,
PRESSED_EQUAL,
PRESSED_CLEAR
};

public static PreviouslyPressed pp = PreviouslyPressed.PRESSED_NONE;
public Object c = null;

}
136 changes: 6 additions & 130 deletions src/simplejavacalculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,130 +1,6 @@
/**
* @name Simple Java Calculator
* @package ph.calculator
* @file Main.java
* @author SORIA Pierre-Henry
* @email [email protected]
* @link http://github.com/pH-7
* @copyright Copyright Pierre-Henry SORIA, All Rights Reserved.
* @license Apache (http://www.apache.org/licenses/LICENSE-2.0)
*/

package simplejavacalculator;

import static java.lang.Double.NaN;
import static java.lang.Math.log;
import static java.lang.Math.log10;
import static java.lang.Math.pow;

public class Calculator {

public enum BiOperatorModes {
normal, add, minus, multiply, divide , xpowerofy
}

public enum MonoOperatorModes {
square, squareRoot, oneDividedBy, cos, sin, tan, log, rate, abs, ln,
}

private Double num1, num2;
private BiOperatorModes mode = BiOperatorModes.normal;

private Double calculateBiImpl() {
if (mode == BiOperatorModes.normal) {
return num2;
}
if (mode == BiOperatorModes.add) {
if (num2 != 0) {
return num1 + num2;
}

return num1;
}
if (mode == BiOperatorModes.minus) {
return num1 - num2;
}
if (mode == BiOperatorModes.multiply) {
return num1 * num2;
}
if (mode == BiOperatorModes.divide) {
return num1 / num2;
}
if (mode == BiOperatorModes.xpowerofy) {
return pow(num1,num2);
}

// never reach
throw new Error();
}

public Double calculateBi(BiOperatorModes newMode, Double num) {
if (mode == BiOperatorModes.normal) {
num2 = 0.0;
num1 = num;
mode = newMode;
return NaN;
} else {
num2 = num;
num1 = calculateBiImpl();
mode = newMode;
return num1;
}
}

public Double calculateEqual(Double num) {
return calculateBi(BiOperatorModes.normal, num);
}

public Double reset() {
num2 = 0.0;
num1 = 0.0;
mode = BiOperatorModes.normal;

return NaN;
}


public Double calculateMono(MonoOperatorModes newMode, Double num) {
if (newMode == MonoOperatorModes.square) {
return num * num;
}
if (newMode == MonoOperatorModes.squareRoot) {
return Math.sqrt(num);
}
if (newMode == MonoOperatorModes.oneDividedBy) {
return 1 / num;
}
if (newMode == MonoOperatorModes.cos) {
return Math.cos(Math.toRadians(num));
}
if (newMode == MonoOperatorModes.sin) {
return Math.sin(Math.toRadians(num));
}
if (newMode == MonoOperatorModes.tan) {
if (num == 0 || num % 180 == 0) {
return 0.0;
}
if (num % 90 == 0 && num % 180 != 0) {
return NaN;
}

return Math.tan(Math.toRadians(num));
}
if (newMode == MonoOperatorModes.log) {
return log10(num);
}
if (newMode == MonoOperatorModes.ln) {
return log(num);
}
if (newMode == MonoOperatorModes.rate) {
return num / 100;
}
if (newMode == MonoOperatorModes.abs){
return Math.abs(num);
}

// never reach
throw new Error();
}

}
package simplejavacalculator;

public interface Calculator {
public abstract double calculate(Buffer buffer);

}
19 changes: 19 additions & 0 deletions src/simplejavacalculator/ClearCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package simplejavacalculator;

import javax.swing.JTextArea;

import simplejavacalculator.Buffer.PreviouslyPressed;

public class ClearCommand extends Command{
public void execute(Buffer buffer, String label) {
this.sub = CommandSubtype.COM_CLEAR;

Buffer.pp = PreviouslyPressed.PRESSED_NONE;
buffer.num1 = null;
buffer.num2 = null;
}

public void show(JTextArea text, Buffer buffer, Calculator calc, String label) {
writer(Double.NaN, text);
}
}
27 changes: 27 additions & 0 deletions src/simplejavacalculator/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package simplejavacalculator;

import javax.swing.JTextArea;

public abstract class Command {

public enum CommandSubtype {
COM_NUMBER,
COM_UNARY,
COM_BINARY,
COM_EQUAL,
COM_CLEAR
}

public CommandSubtype sub;

public abstract void execute(Buffer buffer, String label);
public abstract void show(JTextArea text, Buffer buffer, Calculator calc, String label);

public static void writer(final Double num, JTextArea text) {
if (Double.isNaN(num)) {
text.setText("");
} else {
text.setText(Double.toString(num));
}
}
}
8 changes: 8 additions & 0 deletions src/simplejavacalculator/CosCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package simplejavacalculator;

public class CosCalc implements Calculator {
public double calculate(Buffer buffer) {

return Math.cos(Math.toRadians(buffer.num1));
}
}
11 changes: 11 additions & 0 deletions src/simplejavacalculator/DivideCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package simplejavacalculator;

public class DivideCalc implements Calculator {

public double calculate(Buffer buffer) {

// FIXME
return buffer.num1 / buffer.num2;
}

}
21 changes: 21 additions & 0 deletions src/simplejavacalculator/EqualCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package simplejavacalculator;

import javax.swing.JTextArea;

import simplejavacalculator.Buffer.PreviouslyPressed;

public class EqualCommand extends Command {
public void execute(Buffer buffer, String label) {
this.sub = CommandSubtype.COM_EQUAL;
Buffer.pp = PreviouslyPressed.PRESSED_EQUAL;
}

public void show(JTextArea text, Buffer buffer, Calculator calc, String label) {
if (buffer.c != null && (buffer.num1 != null && buffer.num2 != null)) {
buffer.num1 = ((Calculator)(buffer.c)).calculate(buffer);
writer(buffer.num1, text);
buffer.num2 = null;

}
}
}
10 changes: 10 additions & 0 deletions src/simplejavacalculator/LnCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package simplejavacalculator;
import static java.lang.Math.log;

public class LnCalc implements Calculator {

public double calculate(Buffer buffer) {

return log(buffer.num1);
}
}
11 changes: 11 additions & 0 deletions src/simplejavacalculator/LogCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package simplejavacalculator;

import static java.lang.Math.log10;

public class LogCalc implements Calculator {

public double calculate(Buffer buffer) {

return log10(buffer.num1);
}
}
8 changes: 8 additions & 0 deletions src/simplejavacalculator/MinusCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package simplejavacalculator;

public class MinusCalc implements Calculator {
public double calculate(Buffer buffer) {

return buffer.num1 - buffer.num2;
}
}
10 changes: 10 additions & 0 deletions src/simplejavacalculator/MultiplyCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package simplejavacalculator;

public class MultiplyCalc implements Calculator {

public double calculate(Buffer buffer) {

return buffer.num1 * buffer.num2;
}

}
49 changes: 49 additions & 0 deletions src/simplejavacalculator/NumberCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package simplejavacalculator;

import javax.swing.JTextArea;

import simplejavacalculator.Buffer.PreviouslyPressed;

public class NumberCommand extends Command {

private double get_new_buff_after_num_pressed(Double buffer, Double new_num)
{
Double res = null;

Double temp2 = (double)(buffer * 10 + new_num);

if (temp2 < Integer.MAX_VALUE) {
res = temp2;
}
return res;
}

public void execute(Buffer buffer, String label) {

this.sub = CommandSubtype.COM_NUMBER;

Double temp1 = Double.parseDouble(label);

if (Buffer.pp == PreviouslyPressed.PRESSED_NONE || Buffer.pp == PreviouslyPressed.PRESSED_EQUAL) {
buffer.num1 = temp1;
Buffer.pp = PreviouslyPressed.PRESSED_NUMBER;
} else if (Buffer.pp == PreviouslyPressed.PRESSED_NUMBER) {
if (buffer.num2 != null) {
buffer.num2 = get_new_buff_after_num_pressed(buffer.num2, temp1);
} else {
buffer.num1 = get_new_buff_after_num_pressed(buffer.num1, temp1);
}
} else if (Buffer.pp == PreviouslyPressed.PRESSED_BINARY) {
buffer.num2 = temp1;
Buffer.pp = PreviouslyPressed.PRESSED_NUMBER;
}
}

public void show(JTextArea text, Buffer buffer, Calculator calc, String label) {
if (buffer.num2 != null) {
writer((double)buffer.num2, text);
} else {
writer((double)buffer.num1, text);
}
}
}
8 changes: 8 additions & 0 deletions src/simplejavacalculator/OneDivideByCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package simplejavacalculator;

public class OneDivideByCalc implements Calculator {
public double calculate(Buffer buffer) {

return 1 / buffer.num1;
}
}
Loading