Update src/main/java/dev/sillyangel/calc/Calculator.java
All checks were successful
Build the Jar / build (push) Successful in 8s
All checks were successful
Build the Jar / build (push) Successful in 8s
This commit is contained in:
@@ -36,16 +36,17 @@ public class Calculator extends JFrame implements KeyListener {
|
|||||||
private static final String PREF_FONT_SIZE = "fontSize";
|
private static final String PREF_FONT_SIZE = "fontSize";
|
||||||
private static final String PREF_ALWAYS_ON_TOP = "alwaysOnTop";
|
private static final String PREF_ALWAYS_ON_TOP = "alwaysOnTop";
|
||||||
public static final Preferences prefs = Preferences.userRoot().node(PREF_NODE_NAME);
|
public static final Preferences prefs = Preferences.userRoot().node(PREF_NODE_NAME);
|
||||||
public static String APPLICATION_VERSION = "1.0.0pre";
|
public static String APPLICATION_VERSION = "1.1.0";
|
||||||
private JTextField display;
|
public JTextField display;
|
||||||
private String operand1;
|
public String operand1;
|
||||||
private String operator;
|
public String operator;
|
||||||
private String operand2;
|
public String operand2;
|
||||||
protected boolean resultVisible;
|
public boolean resultVisible;
|
||||||
protected double result;
|
public double result;
|
||||||
private final CalculatorHistory history;
|
public final CalculatorHistory history;
|
||||||
private final List<String> undoStack = new ArrayList<>();
|
public final List<String> undoStack = new ArrayList<>();
|
||||||
private int undoIndex = -1;
|
public int undoIndex = -1;
|
||||||
|
public utils calculatorUtils;
|
||||||
|
|
||||||
public static String getVersion() {
|
public static String getVersion() {
|
||||||
return APPLICATION_VERSION;
|
return APPLICATION_VERSION;
|
||||||
@@ -93,6 +94,7 @@ public class Calculator extends JFrame implements KeyListener {
|
|||||||
}
|
}
|
||||||
public Calculator() {
|
public Calculator() {
|
||||||
history = new CalculatorHistory();
|
history = new CalculatorHistory();
|
||||||
|
calculatorUtils = new utils(this);
|
||||||
|
|
||||||
setBackground(new Color(32, 32, 32));
|
setBackground(new Color(32, 32, 32));
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
@@ -189,15 +191,15 @@ public class Calculator extends JFrame implements KeyListener {
|
|||||||
JMenuItem copy = new JMenuItem("Copy");
|
JMenuItem copy = new JMenuItem("Copy");
|
||||||
JMenuItem paste = new JMenuItem("Paste");
|
JMenuItem paste = new JMenuItem("Paste");
|
||||||
JMenuItem delete = new JMenuItem("Delete");
|
JMenuItem delete = new JMenuItem("Delete");
|
||||||
delete.addActionListener(e -> clearEntry());
|
delete.addActionListener(e -> calculatorUtils.clearEntry());
|
||||||
JMenuItem preferencesm = new JMenuItem("Preferences");
|
JMenuItem preferencesm = new JMenuItem("Preferences");
|
||||||
preferencesm.setMnemonic('P');
|
preferencesm.setMnemonic('P');
|
||||||
preferencesm.addActionListener(e -> PreferencesAction());
|
preferencesm.addActionListener(e -> PreferencesAction());
|
||||||
undo.addActionListener(e -> undoAction());
|
undo.addActionListener(e -> calculatorUtils.undoAction());
|
||||||
redo.addActionListener(e -> redoAction());
|
redo.addActionListener(e -> calculatorUtils.redoAction());
|
||||||
cut.addActionListener(e -> cutAction());
|
cut.addActionListener(e -> calculatorUtils.cutAction());
|
||||||
copy.addActionListener(e -> copyAction());
|
copy.addActionListener(e -> calculatorUtils.copyAction());
|
||||||
paste.addActionListener(e -> pasteAction());
|
paste.addActionListener(e -> calculatorUtils.pasteAction());
|
||||||
|
|
||||||
edit.add(undo);
|
edit.add(undo);
|
||||||
edit.add(redo);
|
edit.add(redo);
|
||||||
@@ -275,7 +277,7 @@ public class Calculator extends JFrame implements KeyListener {
|
|||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
String n = display.getText();
|
String n = display.getText();
|
||||||
if (n.contains(".")) return;
|
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());
|
btnClear.addActionListener(e -> calculatorUtils.clearAll());
|
||||||
btnReciprocal.addActionListener(e -> reciprocal());
|
btnReciprocal.addActionListener(e -> calculatorUtils.reciprocal());
|
||||||
btnSquare.addActionListener(e -> square());
|
btnSquare.addActionListener(e -> calculatorUtils.square());
|
||||||
btnSquareRoot.addActionListener(e -> squareRoot());
|
btnSquareRoot.addActionListener(e -> calculatorUtils.squareRoot());
|
||||||
btnEq.addActionListener(e -> math());
|
btnEq.addActionListener(e -> calculatorUtils.math());
|
||||||
btnBackspace.addActionListener(e -> backspace());
|
btnBackspace.addActionListener(e -> calculatorUtils.backspace());
|
||||||
btnClearEntry.addActionListener(e -> display.setText(""));
|
btnClearEntry.addActionListener(e -> display.setText(""));
|
||||||
|
|
||||||
JButton[] buttons = {
|
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
|
// Add action listeners to each digit button to process the number input
|
||||||
for (JButton button : digitButtons)
|
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
|
// Add action listeners to each operator to process what operator button has been clicked
|
||||||
for (JButton button : operatorButtons)
|
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
|
// add the buttons to the main display panel
|
||||||
for(JButton button: buttons) {
|
for(JButton button: buttons) {
|
||||||
button.setFocusable(false);
|
button.setFocusable(false);
|
||||||
@@ -331,69 +333,9 @@ public class Calculator extends JFrame implements KeyListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Undo/Redo functionality
|
// 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
|
// History dialog
|
||||||
private void showHistoryDialog() {
|
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() {
|
private void loadPreferences() {
|
||||||
int fontSize = prefs.getInt(PREF_FONT_SIZE, 22);
|
int fontSize = prefs.getInt(PREF_FONT_SIZE, 22);
|
||||||
@@ -794,10 +633,10 @@ public class Calculator extends JFrame implements KeyListener {
|
|||||||
int keyCode = e.getKeyCode();
|
int keyCode = e.getKeyCode();
|
||||||
// System.out.println("Key Pressed: " + KeyEvent.getKeyText(keyCode));
|
// System.out.println("Key Pressed: " + KeyEvent.getKeyText(keyCode));
|
||||||
if ("Enter".equals(KeyEvent.getKeyText(keyCode))) {
|
if ("Enter".equals(KeyEvent.getKeyText(keyCode))) {
|
||||||
math();
|
calculatorUtils.math();
|
||||||
} else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
|
} else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
|
||||||
backspace();
|
calculatorUtils.backspace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implement the keyReleased method
|
// Implement the keyReleased method
|
||||||
@@ -814,20 +653,19 @@ public class Calculator extends JFrame implements KeyListener {
|
|||||||
// System.out.println("Key Typed: " + keyChar);
|
// System.out.println("Key Typed: " + keyChar);
|
||||||
System.out.println(c + " KT");
|
System.out.println(c + " KT");
|
||||||
if (c >= '0' && c <= '9') {
|
if (c >= '0' && c <= '9') {
|
||||||
processDigit(String.valueOf(c));
|
calculatorUtils.processDigit(String.valueOf(c));
|
||||||
}
|
}
|
||||||
if (c == '+') {
|
if (c == '+') {
|
||||||
setOperator("+");
|
calculatorUtils.setOperator("+");
|
||||||
} else if (c == '*') {
|
} else if (c == '*') {
|
||||||
setOperator("*");
|
calculatorUtils.setOperator("*");
|
||||||
} else if (c == '/') {
|
} else if (c == '/') {
|
||||||
setOperator("/");
|
calculatorUtils.setOperator("/");
|
||||||
} else if (c == '-') {
|
} else if (c == '-') {
|
||||||
setOperator("-");
|
calculatorUtils.setOperator("-");
|
||||||
}
|
}
|
||||||
if (c == '=') {
|
if (c == '=') {
|
||||||
math();
|
calculatorUtils.math();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user