Added files

This commit is contained in:
Norbi Peti 2016-07-13 13:06:54 +02:00
parent 53432cc705
commit 5b48e82dc6
3 changed files with 124 additions and 0 deletions

32
src/calculator/Calc.java Normal file
View file

@ -0,0 +1,32 @@
package calculator;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Calc {
private static final Pattern MULTIPLY = Pattern.compile("([0-9]+)\\*([0-9]+)");
private static final Pattern DIVIDE = Pattern.compile("([0-9]+)\\/([0-9]+)");
private static final Pattern ADD = Pattern.compile("([0-9]+)\\+([0-9]+)");
private static final Pattern SUBTRACT = Pattern.compile("([0-9]+)\\-([0-9]+)");
public static int calculate(String text) {
StringBuffer buf = new StringBuffer(text);
replace(buf, MULTIPLY, (a, b) -> a * b);
replace(buf, DIVIDE, (a, b) -> a / b);
replace(buf, ADD, (a, b) -> a + b);
replace(buf, SUBTRACT, (a, b) -> a - b);
return Integer.parseInt(buf.toString());
}
private static void replace(StringBuffer buf, Pattern pattern, BiFunction<Integer, Integer, Integer> doit) {
while (true) {
Matcher matcher = pattern.matcher(buf.toString());
if (!matcher.find())
break;
buf.replace(matcher.start(), matcher.end(), Integer
.toString(doit.apply(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)))));
}
}
}

84
src/calculator/Gui.java Normal file
View file

@ -0,0 +1,84 @@
package calculator;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Gui {
private JFrame frame;
private JPanel panel;
private JTextField tf;
public Gui() throws Exception {
frame = new JFrame();
frame.setMinimumSize(new Dimension(200, 400));
frame.setTitle("Calculator");
frame.setLayout(new BorderLayout(10, 10));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
tf = new JTextField();
tf.setFocusable(false);
frame.add(tf, BorderLayout.NORTH);
panel = new JPanel();
frame.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(5, 3));
String str = addNumbers();
str += "+0-*/";
addAppendButtons(str);
addButton('=').addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tf.setText(Integer.toString(Calc.calculate(tf.getText())));
}
});
}
public void show() {
frame.setVisible(true);
}
public void close() {
frame.dispose();
}
private String addNumbers() {
String str = "";
for (int i = 1; i < 10; i++) {
str += i;
}
return str;
}
private void addAppendButtons(String btns) {
for (int i = 0; i < btns.length(); i++) {
Character c = btns.charAt(i);
JButton num = addButton(c);
final Character number = c;
num.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tf.setText(tf.getText() + number);
}
});
}
}
private JButton addButton(Character c) {
JButton num = new JButton();
num.setText(Character.toString(c));
num.setFocusable(false);
panel.add(num);
return num;
}
}

8
src/calculator/Main.java Normal file
View file

@ -0,0 +1,8 @@
package calculator;
public class Main {
public static void main(String[] args) throws Exception {
Gui gui = new Gui();
gui.show();
}
}