Last active
June 4, 2016 21:04
-
-
Save tinkerstudent/9edc723e3ccb5501132d8f78b54058a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.tinkeracademy.projects; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import java.math.RoundingMode; | |
| import java.text.DecimalFormat; | |
| import java.util.Stack; | |
| import javax.swing.JButton; | |
| import javax.swing.JTextArea; | |
| public class Calculator implements ActionListener { | |
| public Stack<String> inputs = new Stack<String>(); | |
| public JTextArea output; | |
| public static void main(String[] args) { | |
| Calculator calculator = new Calculator(); | |
| javax.swing.SwingUtilities.invokeLater(new Runnable() { | |
| public void run() { | |
| calculator.createAndShowGUI(); | |
| } | |
| }); | |
| } | |
| public void createAndShowGUI() { | |
| Window.show(); | |
| Window.addLabel("Calculator Application Developed By: Tinker Academy v1.0"); | |
| output = Window.addTextArea("0", 2, 10, false); | |
| String[][] controls = { | |
| { "7", "8", "9", "AC" }, | |
| { "4", "5", "6", "-" }, | |
| { "1", "2", "3", "+" }, | |
| { "0", "\u00F7", "*", "=" } | |
| }; | |
| JButton[][] buttons = Window.addGridButtons(controls); | |
| for (int i = 0; i < buttons.length; i++) { | |
| JButton[] innerArray = buttons[i]; | |
| for (int j = 0; j < innerArray.length; j++) { | |
| JButton button = innerArray[j]; | |
| button.addActionListener(this); | |
| } | |
| } | |
| } | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| System.out.println(e.getActionCommand()); | |
| if ("AC".equals(e.getActionCommand())) { | |
| output.setText("0"); | |
| inputs.clear(); | |
| } else if ("=".equals(e.getActionCommand())) { | |
| String answer = calculate(); | |
| output.setText(answer); | |
| inputs.clear(); | |
| } else { | |
| if (inputs.isEmpty()) { | |
| output.setText(e.getActionCommand()); | |
| } else { | |
| output.setText(output.getText() + e.getActionCommand()); | |
| } | |
| inputs.push(e.getActionCommand()); | |
| } | |
| } | |
| public String calculate() { | |
| // actually do the calculation | |
| ArithmeticParser parser = new ArithmeticParser("+", "-", "*", "\u00F7"); | |
| Double result = parser.evaluate(inputs); | |
| if (result == null) { | |
| return "<ERROR>"; | |
| } | |
| if (result.intValue() == result) { | |
| return String.valueOf(result.intValue()); | |
| } | |
| DecimalFormat f = new DecimalFormat("##.00"); | |
| f.setMinimumIntegerDigits(1); | |
| f.setMaximumFractionDigits(14); | |
| f.setRoundingMode(RoundingMode.HALF_UP); | |
| String roundedValue = f.format(result.doubleValue()); | |
| return roundedValue; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment