From 0f95cdbf9bf5717f850c52ce39f9215ec6308992 Mon Sep 17 00:00:00 2001 From: angel Date: Fri, 9 Jan 2026 09:05:07 -0600 Subject: [PATCH] Update src/main/java/dev/sillyangel/calc/Calculator.java --- .../java/dev/sillyangel/calc/Calculator.java | 244 +++--------------- 1 file changed, 41 insertions(+), 203 deletions(-) diff --git a/src/main/java/dev/sillyangel/calc/Calculator.java b/src/main/java/dev/sillyangel/calc/Calculator.java index d58f97c..99b9954 100644 --- a/src/main/java/dev/sillyangel/calc/Calculator.java +++ b/src/main/java/dev/sillyangel/calc/Calculator.java @@ -36,16 +36,17 @@ public class Calculator extends JFrame implements KeyListener { private static final String PREF_FONT_SIZE = "fontSize"; private static final String PREF_ALWAYS_ON_TOP = "alwaysOnTop"; public static final Preferences prefs = Preferences.userRoot().node(PREF_NODE_NAME); - public static String APPLICATION_VERSION = "1.0.0pre"; - private JTextField display; - private String operand1; - private String operator; - private String operand2; - protected boolean resultVisible; - protected double result; - private final CalculatorHistory history; - private final List undoStack = new ArrayList<>(); - private int undoIndex = -1; + public static String APPLICATION_VERSION = "1.1.0"; + public JTextField display; + public String operand1; + public String operator; + public String operand2; + public boolean resultVisible; + public double result; + public final CalculatorHistory history; + public final List undoStack = new ArrayList<>(); + public int undoIndex = -1; + public utils calculatorUtils; public static String getVersion() { return APPLICATION_VERSION; @@ -93,6 +94,7 @@ public class Calculator extends JFrame implements KeyListener { } public Calculator() { history = new CalculatorHistory(); + calculatorUtils = new utils(this); setBackground(new Color(32, 32, 32)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -189,15 +191,15 @@ public class Calculator extends JFrame implements KeyListener { JMenuItem copy = new JMenuItem("Copy"); JMenuItem paste = new JMenuItem("Paste"); JMenuItem delete = new JMenuItem("Delete"); - delete.addActionListener(e -> clearEntry()); + delete.addActionListener(e -> calculatorUtils.clearEntry()); JMenuItem preferencesm = new JMenuItem("Preferences"); preferencesm.setMnemonic('P'); preferencesm.addActionListener(e -> PreferencesAction()); - undo.addActionListener(e -> undoAction()); - redo.addActionListener(e -> redoAction()); - cut.addActionListener(e -> cutAction()); - copy.addActionListener(e -> copyAction()); - paste.addActionListener(e -> pasteAction()); + undo.addActionListener(e -> calculatorUtils.undoAction()); + redo.addActionListener(e -> calculatorUtils.redoAction()); + cut.addActionListener(e -> calculatorUtils.cutAction()); + copy.addActionListener(e -> calculatorUtils.copyAction()); + paste.addActionListener(e -> calculatorUtils.pasteAction()); edit.add(undo); edit.add(redo); @@ -275,7 +277,7 @@ public class Calculator extends JFrame implements KeyListener { public void actionPerformed(ActionEvent e) { String n = display.getText(); if (n.contains(".")) return; - processDigit(e.getActionCommand()); + calculatorUtils.processDigit(e.getActionCommand()); } }); @@ -293,12 +295,12 @@ public class Calculator extends JFrame implements KeyListener { } }); - btnClear.addActionListener(e -> clearAll()); - btnReciprocal.addActionListener(e -> reciprocal()); - btnSquare.addActionListener(e -> square()); - btnSquareRoot.addActionListener(e -> squareRoot()); - btnEq.addActionListener(e -> math()); - btnBackspace.addActionListener(e -> backspace()); + btnClear.addActionListener(e -> calculatorUtils.clearAll()); + btnReciprocal.addActionListener(e -> calculatorUtils.reciprocal()); + btnSquare.addActionListener(e -> calculatorUtils.square()); + btnSquareRoot.addActionListener(e -> calculatorUtils.squareRoot()); + btnEq.addActionListener(e -> calculatorUtils.math()); + btnBackspace.addActionListener(e -> calculatorUtils.backspace()); btnClearEntry.addActionListener(e -> display.setText("")); JButton[] buttons = { @@ -315,10 +317,10 @@ public class Calculator extends JFrame implements KeyListener { }; // Add action listeners to each digit button to process the number input for (JButton button : digitButtons) - button.addActionListener(e -> processDigit(e.getActionCommand())); + button.addActionListener(e -> calculatorUtils.processDigit(e.getActionCommand())); // Add action listeners to each operator to process what operator button has been clicked for (JButton button : operatorButtons) - button.addActionListener(e -> setOperator(e.getActionCommand())); + button.addActionListener(e -> calculatorUtils.setOperator(e.getActionCommand())); // add the buttons to the main display panel for(JButton button: buttons) { button.setFocusable(false); @@ -331,69 +333,9 @@ public class Calculator extends JFrame implements KeyListener { } // Undo/Redo functionality - private void saveToUndoStack(String value) { - // Remove any states after current index (if user undid and then made a new action) - if (undoIndex < undoStack.size() - 1) { - undoStack.subList(undoIndex + 1, undoStack.size()).clear(); - } - undoStack.add(value); - undoIndex = undoStack.size() - 1; - } + - private void undoAction() { - if (undoIndex > 0) { - undoIndex--; - display.setText(undoStack.get(undoIndex)); - } else { - System.out.println("Nothing to undo"); - } - } - - private void redoAction() { - if (undoIndex < undoStack.size() - 1) { - undoIndex++; - display.setText(undoStack.get(undoIndex)); - } else { - System.out.println("Nothing to redo"); - } - } - - // Clipboard operations - private void cutAction() { - copyAction(); - display.setText(""); - } - - private void copyAction() { - String text = display.getText(); - if (text != null && !text.isEmpty()) { - StringSelection selection = new StringSelection(text); - Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); - clipboard.setContents(selection, null); - System.out.println("Copied: " + text); - } - } - - private void pasteAction() { - try { - Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); - String text = (String) clipboard.getData(DataFlavor.stringFlavor); - if (text != null && !text.isEmpty()) { - // Validate that pasted content is a valid number - try { - Double.parseDouble(text); - display.setText(text); - saveToUndoStack(text); - } catch (NumberFormatException ex) { - JOptionPane.showMessageDialog(this, - "Pasted content is not a valid number", - "Invalid Input", JOptionPane.ERROR_MESSAGE); - } - } - } catch (Exception ex) { - System.err.println("Error pasting: " + ex.getMessage()); - } - } + // History dialog private void showHistoryDialog() { @@ -609,109 +551,6 @@ public class Calculator extends JFrame implements KeyListener { } } - private void backspace() { - String tmp = display.getText(); - if (tmp.length()>1) // if there is more than 1 character - display.setText( tmp.substring(0, tmp.length()-1) ); - else // otherwise just make display empty - display.setText(""); - } - - private void clearEntry() { - // Clear only the current display (entry) - display.setText(""); - } - - private void clearAll() { - // Clear everything including operands and operator - display.setText(""); - operand1 = ""; - operand2 = ""; - operator = ""; - resultVisible = false; - } - - private void reciprocal() { - String tmp = display.getText(); - if (tmp == null || tmp.isEmpty()) return; - double value = Double.parseDouble(tmp); - if (value != 0) { - result = 1.0 / value; - display.setText("" + result); - resultVisible = true; - } - } - - private void square() { - String tmp = display.getText(); - if (tmp == null || tmp.isEmpty()) return; - double value = Double.parseDouble(tmp); - result = value * value; - display.setText("" + result); - resultVisible = true; - } - - private void squareRoot() { - String tmp = display.getText(); - if (tmp == null || tmp.isEmpty()) return; - double value = Double.parseDouble(tmp); - if (value >= 0) { - result = Math.sqrt(value); - display.setText("" + result); - resultVisible = true; - } - } - - private void processDigit(String actionCommand) { - if (resultVisible) { - display.setText(""); - resultVisible = false; - } - String newValue = display.getText() + actionCommand; - display.setText(newValue); - saveToUndoStack(newValue); - } - private void setOperator(String daop) { - operand1 = display.getText(); - operator = daop; - display.setText(""); - } - - private void math() { - operand2 = display.getText(); - double op1 = Double.parseDouble(operand1); - double op2 = Double.parseDouble(operand2); - - if ("+".equals(operator)) { - result = op1+op2; - } else if ("-".equals(operator)) { - result = op1-op2; - } else if ("*".equals(operator)) { - result = op1*op2; - } else if ("/".equals(operator)) { - result = op1/op2; - } else if ("%".equals(operator)) { - result = op1 % op2; - } else { - result = op2; - System.out.println("Op: " + op1); - System.out.println("Op2: " + op2); - } - - // Save calculation to history - if (!operator.isEmpty()) { - String calculation = operand1 + " " + operator + " " + operand2 + " = " + result; - history.saveToHistory(calculation); - } - - operator = ""; - operand1 = ""; - operand2 = ""; - - resultVisible = true; - display.setText(""+result); - saveToUndoStack(""+result); - } private void loadPreferences() { int fontSize = prefs.getInt(PREF_FONT_SIZE, 22); @@ -794,10 +633,10 @@ public class Calculator extends JFrame implements KeyListener { int keyCode = e.getKeyCode(); // System.out.println("Key Pressed: " + KeyEvent.getKeyText(keyCode)); if ("Enter".equals(KeyEvent.getKeyText(keyCode))) { - math(); - } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { - backspace(); - } + calculatorUtils.math(); + } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { + calculatorUtils.backspace(); + } } // Implement the keyReleased method @@ -814,20 +653,19 @@ public class Calculator extends JFrame implements KeyListener { // System.out.println("Key Typed: " + keyChar); System.out.println(c + " KT"); if (c >= '0' && c <= '9') { - processDigit(String.valueOf(c)); + calculatorUtils.processDigit(String.valueOf(c)); } if (c == '+') { - setOperator("+"); + calculatorUtils.setOperator("+"); } else if (c == '*') { - setOperator("*"); + calculatorUtils.setOperator("*"); } else if (c == '/') { - setOperator("/"); + calculatorUtils.setOperator("/"); } else if (c == '-') { - setOperator("-"); + calculatorUtils.setOperator("-"); } if (c == '=') { - math(); - } - + calculatorUtils.math(); + } } -} +} \ No newline at end of file