diff --git a/src/main/java/com/mycompany/javacalculator/JavaCalculator.java b/src/main/java/com/mycompany/javacalculator/JavaCalculator.java index 45c9778..ec19553 100644 --- a/src/main/java/com/mycompany/javacalculator/JavaCalculator.java +++ b/src/main/java/com/mycompany/javacalculator/JavaCalculator.java @@ -4,6 +4,12 @@ package com.mycompany.javacalculator; +import com.mycompany.javacalculator.logic.Calculation; +import com.mycompany.javacalculator.ui.RowFactory; +import com.mycompany.javacalculator.logic.Operands; +import com.mycompany.javacalculator.ui.OpButton; +import com.mycompany.javacalculator.ui.NumButton; +import com.mycompany.javacalculator.ui.ButtonRow; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; @@ -12,7 +18,7 @@ import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; -import com.mycompany.javacalculator.Operands.Operand; +import com.mycompany.javacalculator.logic.Operands.Operand; import java.util.Collection; import javax.swing.JButton; @@ -21,16 +27,21 @@ import javax.swing.JButton; * @author mikayladobson */ public class JavaCalculator implements ActionListener { - private float storedNum = 0; - private float receivedNum = 0; + private double storedNum = 0; + private double receivedNum = 0; private String receivedData = ""; - private Operand operand = null; + private Operand operand = Operand.READ_NEXT; + private Operand storedOperand = null; + + private int wholeNums; + private int decimalNums; private final JFrame frame; private final JPanel mainPanel; private final JPanel calculatorScreen; - private final JLabel screenText; + private final JLabel numScreenText; + private final JLabel operandScreenText; private final NumButton btn1; private final NumButton btn2; @@ -50,17 +61,20 @@ public class JavaCalculator implements ActionListener { private final OpButton btnExponent; private final OpButton btnEquals; private final OpButton btnDecimal; + private final OpButton btnClear; public JavaCalculator() { frame = new JFrame(); - screenText = new JLabel("Initial value"); + numScreenText = new JLabel(String.valueOf(storedNum)); + operandScreenText = new JLabel(); mainPanel = new JPanel(); mainPanel.setBorder(BorderFactory.createEmptyBorder(100,100,100,100)); mainPanel.setLayout(new GridLayout(4, 4)); calculatorScreen = new JPanel(); - calculatorScreen.add(screenText); + calculatorScreen.add(numScreenText); + calculatorScreen.add(operandScreenText); calculatorScreen.setBackground(Color.GRAY); mainPanel.add(calculatorScreen); @@ -83,9 +97,10 @@ public class JavaCalculator implements ActionListener { btnExponent = new OpButton(Operand.EXPONENT, this); btnEquals = new OpButton(Operand.EQUALS, this); btnDecimal = new OpButton(Operand.DECIMAL, this); + btnClear = new OpButton(Operand.CLEAR, this); NumButton[] numBtns = { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0 }; - OpButton[] opBtns = { btnPlus, btnMinus, btnMultiply, btnDivide, btnExponent, btnEquals, btnDecimal }; + OpButton[] opBtns = { btnPlus, btnMinus, btnMultiply, btnDivide, btnExponent, btnEquals, btnDecimal, btnClear }; RowFactory rowFactory = new RowFactory(numBtns); Collection btnRows = rowFactory.getRows(); @@ -115,7 +130,84 @@ public class JavaCalculator implements ActionListener { @Override public void actionPerformed(ActionEvent e) { + // get the object declaration and label of the event source JButton source = (JButton)e.getSource(); - screenText.setText(source.getText()); + String targetValue = source.getText(); + String concatenatedNums = ""; + + if (source instanceof NumButton) { + // while decimal mode is active, only add digits after decimal place + // to do: limit to x number of decimal places? + if (this.operand == Operand.DECIMAL) { + if (decimalNums == 0) { + concatenatedNums = String.valueOf(targetValue); + } else { + concatenatedNums = String.valueOf(decimalNums) + String.valueOf(targetValue); + } + + decimalNums = Integer.parseInt(concatenatedNums); + } else { + if (wholeNums == 0) { + concatenatedNums = String.valueOf(targetValue); + } else { + concatenatedNums = String.valueOf(wholeNums) + String.valueOf(targetValue); + } + + wholeNums = Integer.parseInt(concatenatedNums); + } + + storedNum = (float)wholeNums + (float)(decimalNums / 100); + } else if (source instanceof OpButton) { + OpButton opButton = (OpButton)source; + this.operand = opButton.getOperand(); + + switch (this.operand) { + case CLEAR -> { + storedNum = 0; + wholeNums = 0; + decimalNums = 0; + this.operand = null; + operandScreenText.setText(""); + break; + } + case EQUALS -> { + String resultAsString = String.valueOf(storedNum) + " " + String.valueOf(receivedNum) + " " + String.valueOf(storedNum + receivedNum); + operandScreenText.setText(resultAsString); + + double result = new Calculation(receivedNum, storedNum, this.storedOperand).getResult(); + storedNum = result; + receivedNum = 0; + + int idxOfDecimal = resultAsString.indexOf('.'); + wholeNums = Integer.parseInt(resultAsString.substring(0, idxOfDecimal)); + decimalNums = Integer.parseInt(resultAsString.substring(idxOfDecimal + 1)); + this.operand = Operand.READ_NEXT; + break; + } + case DECIMAL, READ_NEXT -> { + break; + } + default -> { + receivedNum = storedNum; + storedNum = 0; + wholeNums = 0; + decimalNums = 0; + this.storedOperand = this.operand; + this.operand = Operand.READ_NEXT; + break; + } + } + } + + + // numScreenText.setText(String.valueOf(storedNum)); + numScreenText.setText(String.format("%s.%s", wholeNums, decimalNums)); + + if (this.operand == null) { + storedNum = 0; + wholeNums = 0; + decimalNums = 0; + this.storedOperand = null; + } } } diff --git a/src/main/java/com/mycompany/javacalculator/logic/Calculation.java b/src/main/java/com/mycompany/javacalculator/logic/Calculation.java new file mode 100644 index 0000000..69b5e1d --- /dev/null +++ b/src/main/java/com/mycompany/javacalculator/logic/Calculation.java @@ -0,0 +1,44 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.mycompany.javacalculator.logic; + +import com.mycompany.javacalculator.logic.Operands.Operand; + +/** + * + * @author mikayladobson + */ +public class Calculation { + private double result; + + public Calculation(double first, double second, Operand operand) { + switch (operand) { + case ADD -> { + this.result = first + second; + break; + } + case SUBTRACT -> { + result = first - second; + break; + } + case MULTIPLY -> { + result = first * second; + break; + } + case DIVIDE -> { + result = first / second; + break; + } + case EXPONENT -> { + result = Math.pow(first, second); + break; + } + } + } + + public double getResult() { + return this.result; + } +} diff --git a/src/main/java/com/mycompany/javacalculator/Operands.java b/src/main/java/com/mycompany/javacalculator/logic/Operands.java similarity index 81% rename from src/main/java/com/mycompany/javacalculator/Operands.java rename to src/main/java/com/mycompany/javacalculator/logic/Operands.java index f132966..1c6e1a3 100644 --- a/src/main/java/com/mycompany/javacalculator/Operands.java +++ b/src/main/java/com/mycompany/javacalculator/logic/Operands.java @@ -2,7 +2,7 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ -package com.mycompany.javacalculator; +package com.mycompany.javacalculator.logic; /** * @@ -11,7 +11,7 @@ package com.mycompany.javacalculator; public class Operands { public enum Operand { ADD, SUBTRACT, MULTIPLY, DIVIDE, EXPONENT, - EQUALS, DECIMAL + EQUALS, DECIMAL, CLEAR, READ_NEXT } public static String toLabel(Operand input) { @@ -37,6 +37,12 @@ public class Operands { case EQUALS -> { return "="; } + case CLEAR -> { + return "c"; + } + case READ_NEXT -> { + return "..."; + } default -> { throw new Error("Invalid operand input"); } diff --git a/src/main/java/com/mycompany/javacalculator/ButtonRow.java b/src/main/java/com/mycompany/javacalculator/ui/ButtonRow.java similarity index 94% rename from src/main/java/com/mycompany/javacalculator/ButtonRow.java rename to src/main/java/com/mycompany/javacalculator/ui/ButtonRow.java index e80fed9..06898dc 100644 --- a/src/main/java/com/mycompany/javacalculator/ButtonRow.java +++ b/src/main/java/com/mycompany/javacalculator/ui/ButtonRow.java @@ -2,7 +2,7 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ -package com.mycompany.javacalculator; +package com.mycompany.javacalculator.ui; import javax.swing.JButton; import javax.swing.JPanel; diff --git a/src/main/java/com/mycompany/javacalculator/NumButton.java b/src/main/java/com/mycompany/javacalculator/ui/NumButton.java similarity index 86% rename from src/main/java/com/mycompany/javacalculator/NumButton.java rename to src/main/java/com/mycompany/javacalculator/ui/NumButton.java index 9fa6590..a763f2c 100644 --- a/src/main/java/com/mycompany/javacalculator/NumButton.java +++ b/src/main/java/com/mycompany/javacalculator/ui/NumButton.java @@ -2,7 +2,8 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ -package com.mycompany.javacalculator; +package com.mycompany.javacalculator.ui; +import com.mycompany.javacalculator.JavaCalculator; import javax.swing.JButton; /** diff --git a/src/main/java/com/mycompany/javacalculator/OpButton.java b/src/main/java/com/mycompany/javacalculator/ui/OpButton.java similarity index 66% rename from src/main/java/com/mycompany/javacalculator/OpButton.java rename to src/main/java/com/mycompany/javacalculator/ui/OpButton.java index 62ed66d..74acd95 100644 --- a/src/main/java/com/mycompany/javacalculator/OpButton.java +++ b/src/main/java/com/mycompany/javacalculator/ui/OpButton.java @@ -2,10 +2,12 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ -package com.mycompany.javacalculator; +package com.mycompany.javacalculator.ui; +import com.mycompany.javacalculator.JavaCalculator; +import com.mycompany.javacalculator.logic.Operands; import javax.swing.JButton; -import com.mycompany.javacalculator.Operands.Operand; +import com.mycompany.javacalculator.logic.Operands.Operand; /** * @@ -19,4 +21,8 @@ public class OpButton extends JButton { this.addActionListener(calculator); this.setText(Operands.toLabel(operand)); } + + public Operand getOperand() { + return operand; + } } diff --git a/src/main/java/com/mycompany/javacalculator/RowFactory.java b/src/main/java/com/mycompany/javacalculator/ui/RowFactory.java similarity index 92% rename from src/main/java/com/mycompany/javacalculator/RowFactory.java rename to src/main/java/com/mycompany/javacalculator/ui/RowFactory.java index 383a896..13ac496 100644 --- a/src/main/java/com/mycompany/javacalculator/RowFactory.java +++ b/src/main/java/com/mycompany/javacalculator/ui/RowFactory.java @@ -2,8 +2,9 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ -package com.mycompany.javacalculator; +package com.mycompany.javacalculator.ui; +import com.mycompany.javacalculator.ui.ButtonRow; import java.util.ArrayList; import java.util.Collection; import javax.swing.JButton;