Taschenrechner

master
3009594 2024-09-08 18:10:29 +02:00
parent d7a8a4fc20
commit 99f7589b06
6 changed files with 362 additions and 1 deletions

View File

@ -6,7 +6,7 @@
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="Programmierung2/src"/> <classpathentry kind="src" path="Programmierung2/src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"> <classpathentry exported="true" kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5">
<attributes> <attributes>
<attribute name="module" value="true"/> <attribute name="module" value="true"/>
</attributes> </attributes>

View File

@ -8,4 +8,5 @@ module Programmierung2 {
requires java.desktop; requires java.desktop;
requires org.junit.jupiter.api; requires org.junit.jupiter.api;
requires junit; requires junit;
exports Übungen.TaschenrechnerGUI to junit; // Exportiere das Paket für das JUnit-Modul
} }

View File

@ -0,0 +1,156 @@
package Übungen.TaschenrechnerGUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Controller implements ActionListener {
Modell<Integer,Integer> m1;
View v1;
String letzterText = "";
char operationen[] = {'+', '-','*','÷'};
char merker = ' ';
String erst = "";
String last = "";
Integer zahl1;
Integer zahl2;
public Controller(Modell<Integer, Integer> m1, View v1) {
this.m1 = m1;
this.v1 = v1;
getButtons();
}
public void getButtons() {
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
letzterText = v1.eingabe.getText();
if (e.getSource() == v1.button1)
v1.eingabe.setText(letzterText + v1.button1.getText());
if (e.getSource() == v1.button2)
v1.eingabe.setText(letzterText + v1.button2.getText());
if (e.getSource() == v1.button3)
v1.eingabe.setText(letzterText + v1.button3.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) {
checkeOperator();
textverteilen();
textToNumber();
if (merker != ' ')
if (merker == '+') {
int ergebnisse = (int) m1.add(zahl1, zahl2);
v1.updateErgebnisse(ergebnisse);
}
else if (merker == '-') {
int ergebnisse = (int) m1.sub(zahl1, zahl2);
v1.updateErgebnisse(ergebnisse);
}
else if (merker == '*') {
int ergebnisse = (int) m1.multi(zahl1, zahl2);
v1.updateErgebnisse(ergebnisse);
}
else if (merker == '÷') {
int ergebnisse = (int) m1.divid(zahl1, zahl2);
v1.updateErgebnisse(ergebnisse);
}
}
letzterText = v1.eingabe.getText();
}
};
v1.button1.addActionListener(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() {
merker = ' ';
for (int i = 0; i < letzterText.length(); i++)
for (int j = 0; j < operationen.length; j++)
if (letzterText.charAt(i) == operationen[j])
merker = operationen[j];
}
private void textverteilen() {
if (merker != ' ') {
int i = letzterText.indexOf(merker);
erst = letzterText.substring(0, i);
last = letzterText.substring(i + 1,letzterText.length());
}
}
public void textToNumber() {
zahl2 = Integer.parseInt(last);
}
@Override
public void actionPerformed(ActionEvent e) {}
public static void main(String[] args) {
Modell<Integer, Integer> m = new Modell<>();
View v = new View();
Controller n1 = new Controller(m, v);
}
}

View File

@ -0,0 +1,39 @@
package Übungen.TaschenrechnerGUI;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
public class JunitTest {
Modell<Double, Double> m1;
@BeforeEach
public void initi() {
m1 = new Modell<>();
}
@Disabled
public void testAdd() {
Modell<Integer, Integer> m1 = new Modell<>();
assertEquals(15 ,m1.add(10, 5).intValue());
}
@Test
public void testSub() {
Modell<Integer, Integer> m1 = new Modell<>();
assertEquals(5 ,m1.sub(10, 5).intValue());
}
@Test
public void testdivid() {
Modell<Integer, Integer> m1 = new Modell<>();
assertEquals(5 ,m1.divid(5, 0).intValue());
}
}

View File

@ -0,0 +1,58 @@
package Übungen.TaschenrechnerGUI;
public class Modell <T extends Number,T2 extends Number> {
public Number add(T z1, T2 z2) {
if (z1 instanceof Integer || z2 instanceof Integer )
return z1.intValue() + z2.intValue();
else if (z1 instanceof Double || z2 instanceof Double )
return z1.doubleValue() + z2.doubleValue();
else
return z1.floatValue() + z2.floatValue();
}
public Number sub(T z1, T2 z2) {
if (z1 instanceof Integer || z2 instanceof Integer)
return z1.intValue() - z2.intValue();
else if (z1 instanceof Double || z2 instanceof Double)
return z1.doubleValue() - z2.doubleValue();
else
return z1.floatValue() - z2.floatValue();
}
public Number multi(T z1, T2 z2) {
if (z1 instanceof Integer || z2 instanceof Integer)
return z1.intValue() * z2.intValue();
else if (z1 instanceof Double || z2 instanceof Double)
return z1.doubleValue() * z2.doubleValue();
else
return z1.floatValue() * z2.floatValue();
}
public Number divid(T z1, T2 z2) throws ArithmeticException {
if (z2.intValue() == 0)
throw new ArithmeticException ("Ungültige division Operation!");
if (z1 instanceof Integer || z2 instanceof Integer)
return z1.intValue() / z2.intValue();
else if (z1 instanceof Double || z2 instanceof Double)
return z1.doubleValue() / z2.doubleValue();
else
return z1.floatValue() / z2.floatValue();
}
}

View File

@ -0,0 +1,107 @@
package Übungen.TaschenrechnerGUI;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class View extends JFrame {
// Fenster Eigenschaften:
final int WIDTH = 500;
final int HEIGHT = 500;
// Fenster Elemente:
JTextField eingabe = new JTextField();
JLabel ergebnisse = new JLabel();
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton button0 = new JButton("0");
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton multi = new JButton("x");
JButton divid = new JButton("÷");
JButton punkt = new JButton(".");
JButton berechnen = new JButton("OK");
int buttonWidth = 60;
int buttonheight = 30;
public View() {
this.setTitle("Taschenrechner");
this.setSize(WIDTH, HEIGHT);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setLayout(null);
ergebnisse.setBounds(40, 60, 100, 30);
eingabe.setBounds(40, 100, buttonWidth * 5, 40);
button1.setBounds(40,150,buttonWidth,buttonheight);
button2.setBounds(120,150,buttonWidth,buttonheight);
button3.setBounds(200,150,buttonWidth,buttonheight);
button4.setBounds(40,190,buttonWidth,buttonheight);
button5.setBounds(120,190,buttonWidth,buttonheight);
button6.setBounds(200,190,buttonWidth,buttonheight);
button7.setBounds(40,230,buttonWidth,buttonheight);
button8.setBounds(120,230,buttonWidth,buttonheight);
button9.setBounds(200,230,buttonWidth,buttonheight);
button0.setBounds(40,270,buttonWidth*2 + 20,buttonheight);
punkt.setBounds(200, 270, buttonWidth, buttonheight);
plus.setBounds(280, 150, buttonWidth, buttonheight);
plus.setBackground(Color.GRAY);
plus.setForeground(Color.WHITE);
minus.setBounds(280, 190, buttonWidth, buttonheight);
minus.setBackground(Color.GRAY);
minus.setForeground(Color.WHITE);
multi.setBounds(280, 230, buttonWidth, buttonheight);
multi.setBackground(Color.GRAY);
multi.setForeground(Color.WHITE);
divid.setBounds(280, 270, buttonWidth, buttonheight);
divid.setBackground(Color.GRAY);
divid.setForeground(Color.WHITE);
berechnen.setBackground(Color.RED);
berechnen.setForeground(Color.WHITE);
berechnen.setBounds(280, 310, buttonWidth, buttonheight);
this.add(ergebnisse);
this.add(eingabe);
this.add(button1);
this.add(button2);
this.add(button3);
this.add(button4);
this.add(button5);
this.add(button6);
this.add(button7);
this.add(button8);
this.add(button9);
this.add(button0);
this.add(punkt);
this.add(plus);
this.add(minus);
this.add(multi);
this.add(divid);
this.add(berechnen);
}
public void updateErgebnisse(int erg) {
ergebnisse.setText(erg + "");
}
}