Taschenrechner

master
obai 2024-09-08 21:09:56 +02:00
parent 99f7589b06
commit d4fa8a35ba
4 changed files with 183 additions and 131 deletions

View File

@ -3,24 +3,25 @@ package Übungen.TaschenrechnerGUI;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Controller implements ActionListener { public class Controller implements ActionListener {
Modell<Integer,Integer> m1; Modell<Number, Number> m1;
View v1; View v1;
String letzterText = ""; String letzterText = "";
char operationen[] = {'+', '-','*','÷'}; char operationen[] = { '+', '-', 'x', '÷' };
char merker = ' '; char merker = ' ';
String erst = ""; String erst = "";
String last = ""; String last = "";
Integer zahl1; Number zahl1;
Integer zahl2; Number zahl2;
public Controller(Modell<Number, Number> m1, View v1) {
public Controller(Modell<Integer, Integer> m1, View v1) {
this.m1 = m1; this.m1 = m1;
this.v1 = v1; this.v1 = v1;
getButtons(); getButtons();
} }
public void getButtons() { public void getButtons() {
@ -28,101 +29,76 @@ public class Controller implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
letzterText = v1.eingabe.getText(); letzterText = v1.eingabe.getText();
String speicher = "";
JButton clickedButton;
if (e.getSource() == v1.button1) if (e.getSource() == v1.button1 || e.getSource() == v1.button2 || e.getSource() == v1.button3
v1.eingabe.setText(letzterText + v1.button1.getText()); || e.getSource() == v1.button4 || e.getSource() == v1.button5 || e.getSource() == v1.button6
|| e.getSource() == v1.button7 || e.getSource() == v1.button8 || e.getSource() == v1.button9
|| e.getSource() == v1.button0 || e.getSource() == v1.plus || e.getSource() == v1.minus
|| e.getSource() == v1.multi || e.getSource() == v1.divid || e.getSource() == v1.punkt) {
if (e.getSource() == v1.button2) clickedButton = (JButton) e.getSource();
v1.eingabe.setText(letzterText + v1.button2.getText()); speicher = clickedButton.getText();
v1.setEingabe(letzterText + speicher);
if (e.getSource() == v1.button3) }
v1.eingabe.setText(letzterText + v1.button3.getText()); letzterText = v1.eingabe.getText();
if (e.getSource() == v1.button4)
v1.eingabe.setText(letzterText + v1.button4.getText());
if (e.getSource() == v1.button5)
v1.eingabe.setText(letzterText + v1.button5.getText());
if (e.getSource() == v1.button1)
v1.eingabe.setText(letzterText + v1.button1.getText());
if (e.getSource() == v1.button6)
v1.eingabe.setText(letzterText + v1.button6.getText());
if (e.getSource() == v1.button7)
v1.eingabe.setText(letzterText + v1.button7.getText());
if (e.getSource() == v1.button8)
v1.eingabe.setText(letzterText + v1.button8.getText());
if (e.getSource() == v1.button9)
v1.eingabe.setText(letzterText + v1.button9.getText());
if (e.getSource() == v1.button0)
v1.eingabe.setText(letzterText + v1.button0.getText());
if (e.getSource() == v1.punkt)
v1.eingabe.setText(letzterText + v1.punkt.getText());
if (e.getSource() == v1.plus)
v1.eingabe.setText(letzterText + v1.plus.getText());
if (e.getSource() == v1.minus)
v1.eingabe.setText(letzterText + v1.minus.getText());
if (e.getSource() == v1.multi)
v1.eingabe.setText(letzterText + v1.multi.getText());
if (e.getSource() == v1.divid)
v1.eingabe.setText(letzterText + v1.divid.getText());
if (e.getSource() == v1.berechnen) { if (e.getSource() == v1.berechnen) {
checkeOperator(); checkeOperator();
textverteilen(); textverteilen();
textToNumber(); textToNumber();
if (merker != ' ') String err = "";
if (merker == '+') { double ergebnis = 0;
int ergebnisse = (int) m1.add(zahl1, zahl2);
v1.updateErgebnisse(ergebnisse); switch (merker) {
case '+':
ergebnis = m1.add(zahl1, zahl2).doubleValue();
break;
case '-':
ergebnis = m1.sub(zahl1, zahl2).doubleValue();
break;
case 'x':
ergebnis = m1.multi(zahl1, zahl2).doubleValue();
break;
case '÷':
ergebnis = m1.divid(zahl1, zahl2).doubleValue();
break;
default:
err = "Bitte geben Sie einen gültigen Operator ein.";
} }
else if (merker == '-') { if (!err.isEmpty())
int ergebnisse = (int) m1.sub(zahl1, zahl2); System.out.println(err);
v1.updateErgebnisse(ergebnisse); // v1.showError(err);
else
v1.updateErgebnisse(ergebnis);
} }
else if (merker == '*') { if (e.getSource() == v1.clear) {
int ergebnisse = (int) m1.multi(zahl1, zahl2); v1.eingabe.setText("");
v1.updateErgebnisse(ergebnisse);
} }
else if (merker == '÷') { if (e.getSource() == v1.back) {
int ergebnisse = (int) m1.divid(zahl1, zahl2); if (letzterText.length() == 0)
v1.updateErgebnisse(ergebnisse); v1.eingabe.setText(letzterText);
else {
letzterText = letzterText.substring(0, letzterText.length() - 1);
v1.eingabe.setText(letzterText);
} }
} }
letzterText = v1.eingabe.getText(); letzterText = v1.eingabe.getText();
} }
}; };
v1.button1.addActionListener(actionListener); v1.setAction(actionListener);
v1.button2.addActionListener(actionListener);
v1.button3.addActionListener(actionListener);
v1.button4.addActionListener(actionListener);
v1.button5.addActionListener(actionListener);
v1.button6.addActionListener(actionListener);
v1.button7.addActionListener(actionListener);
v1.button8.addActionListener(actionListener);
v1.button9.addActionListener(actionListener);
v1.button0.addActionListener(actionListener);
v1.punkt.addActionListener(actionListener);
v1.plus.addActionListener(actionListener);
v1.minus.addActionListener(actionListener);
v1.divid.addActionListener(actionListener);
v1.multi.addActionListener(actionListener);
v1.eingabe.addActionListener(actionListener);
v1.berechnen.addActionListener(actionListener);
} }
private void checkeOperator() { private void checkeOperator() {
@ -137,19 +113,39 @@ public class Controller implements ActionListener {
if (merker != ' ') { if (merker != ' ') {
int i = letzterText.indexOf(merker); int i = letzterText.indexOf(merker);
erst = letzterText.substring(0, i); erst = letzterText.substring(0, i);
last = letzterText.substring(i + 1,letzterText.length()); last = letzterText.substring(i + 1, letzterText.length());
} }
} }
public void textToNumber() { public void textToNumber() {
if (erst.contains(".") || last.contains(".")) {
zahl1 = Double.parseDouble(erst);
zahl2 = Double.parseDouble(last);
} else {
zahl1 = Integer.parseInt(erst);
zahl2 = Integer.parseInt(last); zahl2 = Integer.parseInt(last);
} }
}
public void clearTextfield() {
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == v1.clear) {
v1.eingabe.setText("");
}
}
};
}
@Override @Override
public void actionPerformed(ActionEvent e) {} public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) { public static void main(String[] args) {
Modell<Integer, Integer> m = new Modell<>(); Modell<Number, Number> m = new Modell<>();
View v = new View(); View v = new View();
Controller n1 = new Controller(m, v); Controller n1 = new Controller(m, v);
} }

View File

@ -22,14 +22,20 @@ public class JunitTest {
} }
@Test @Disabled
public void testSub() { public void testSub() {
Modell<Integer, Integer> m1 = new Modell<>(); Modell<Integer, Integer> m1 = new Modell<>();
assertEquals(5 ,m1.sub(10, 5).intValue());
} }
@Test @Test
public void testmulti() {
Modell<Double, Double> m1 = new Modell<>();
assertEquals(1.44 ,m1.multi(1.2, 1.2).doubleValue());
}
@Disabled
public void testdivid() { public void testdivid() {
Modell<Integer, Integer> m1 = new Modell<>(); Modell<Integer, Integer> m1 = new Modell<>();
assertEquals(5 ,m1.divid(5, 0).intValue()); assertEquals(5 ,m1.divid(5, 0).intValue());

View File

@ -15,6 +15,7 @@ public class View extends JFrame {
// Fenster Elemente: // Fenster Elemente:
JTextField eingabe = new JTextField(); JTextField eingabe = new JTextField();
JLabel ergebnisse = new JLabel(); JLabel ergebnisse = new JLabel();
JLabel error = new JLabel();
JButton button1 = new JButton("1"); JButton button1 = new JButton("1");
JButton button2 = new JButton("2"); JButton button2 = new JButton("2");
@ -31,7 +32,9 @@ public class View extends JFrame {
JButton multi = new JButton("x"); JButton multi = new JButton("x");
JButton divid = new JButton("÷"); JButton divid = new JButton("÷");
JButton punkt = new JButton("."); JButton punkt = new JButton(".");
JButton berechnen = new JButton("OK"); JButton berechnen = new JButton("=");
JButton clear = new JButton("C");
JButton back = new JButton("←");
int buttonWidth = 60; int buttonWidth = 60;
int buttonheight = 30; int buttonheight = 30;
@ -47,7 +50,8 @@ public class View extends JFrame {
this.setLayout(null); this.setLayout(null);
ergebnisse.setBounds(40, 60, 100, 30); ergebnisse.setBounds(40, 60, 100, 30);
eingabe.setBounds(40, 100, buttonWidth * 5, 40); error.setBounds(40, 60, 100, 30);
eingabe.setBounds(40, 100, buttonWidth * 6 + 20, 40);
button1.setBounds(40,150,buttonWidth,buttonheight); button1.setBounds(40,150,buttonWidth,buttonheight);
button2.setBounds(120,150,buttonWidth,buttonheight); button2.setBounds(120,150,buttonWidth,buttonheight);
button3.setBounds(200,150,buttonWidth,buttonheight); button3.setBounds(200,150,buttonWidth,buttonheight);
@ -73,10 +77,22 @@ public class View extends JFrame {
divid.setBackground(Color.GRAY); divid.setBackground(Color.GRAY);
divid.setForeground(Color.WHITE); divid.setForeground(Color.WHITE);
berechnen.setBackground(Color.RED);
berechnen.setForeground(Color.WHITE);
berechnen.setBounds(280, 310, buttonWidth, buttonheight);
clear.setBackground(Color.RED);
clear.setForeground(Color.white);
clear.setBounds(360,150 , buttonWidth, buttonheight);
back.setBackground(Color.ORANGE);
back.setForeground(Color.white);
back.setBounds(360,190, buttonWidth, buttonheight);
berechnen.setBackground(Color.GRAY);
berechnen.setForeground(Color.WHITE);
berechnen.setBounds(360, 230, buttonWidth, buttonheight* 2 + 10);
this.add(clear);
this.add(error);
this.add(ergebnisse); this.add(ergebnisse);
this.add(eingabe); this.add(eingabe);
this.add(button1); this.add(button1);
@ -95,13 +111,46 @@ public class View extends JFrame {
this.add(multi); this.add(multi);
this.add(divid); this.add(divid);
this.add(berechnen); this.add(berechnen);
this.add(back);
} }
public void updateErgebnisse(int erg) { public void updateErgebnisse(double erg) {
ergebnisse.setText(erg + ""); ergebnisse.setText(erg + "");
} }
public void showError(String err) {
error.setText(err);
}
public void setEingabe(String auswahl) {
eingabe.setText(auswahl);
}
public void setAction(ActionListener actionListener) {
button1.addActionListener(actionListener);
button2.addActionListener(actionListener);
button3.addActionListener(actionListener);
button4.addActionListener(actionListener);
button5.addActionListener(actionListener);
button6.addActionListener(actionListener);
button7.addActionListener(actionListener);
button8.addActionListener(actionListener);
button9.addActionListener(actionListener);
button0.addActionListener(actionListener);
punkt.addActionListener(actionListener);
plus.addActionListener(actionListener);
minus.addActionListener(actionListener);
divid.addActionListener(actionListener);
multi.addActionListener(actionListener);
eingabe.addActionListener(actionListener);
berechnen.addActionListener(actionListener);
clear.addActionListener(actionListener);
back.addActionListener(actionListener);
}
} }

View File

@ -16,6 +16,7 @@ public class Controller {
} }
public static boolean checkSet(String X_O, int x, int y) { public static boolean checkSet(String X_O, int x, int y) {
if (x < 0 || x > 2 || y < 0 || y > 2) if (x < 0 || x > 2 || y < 0 || y > 2)
return false; return false;