From 99f7589b06a34277c59b13145303cd9507576f57 Mon Sep 17 00:00:00 2001 From: 3009594 Date: Sun, 8 Sep 2024 18:10:29 +0200 Subject: [PATCH] Taschenrechner --- .classpath | 2 +- Programmierung2/src/module-info.java | 1 + .../Übungen/TaschenrechnerGUI/Controller.java | 156 ++++++++++++++++++ .../Übungen/TaschenrechnerGUI/JunitTest.java | 39 +++++ .../src/Übungen/TaschenrechnerGUI/Modell.java | 58 +++++++ .../src/Übungen/TaschenrechnerGUI/View.java | 107 ++++++++++++ 6 files changed, 362 insertions(+), 1 deletion(-) create mode 100644 Programmierung2/src/Übungen/TaschenrechnerGUI/Controller.java create mode 100644 Programmierung2/src/Übungen/TaschenrechnerGUI/JunitTest.java create mode 100644 Programmierung2/src/Übungen/TaschenrechnerGUI/Modell.java create mode 100644 Programmierung2/src/Übungen/TaschenrechnerGUI/View.java diff --git a/.classpath b/.classpath index 403a6f4..1b83d66 100644 --- a/.classpath +++ b/.classpath @@ -6,7 +6,7 @@ - + diff --git a/Programmierung2/src/module-info.java b/Programmierung2/src/module-info.java index 6b36951..0abbc23 100644 --- a/Programmierung2/src/module-info.java +++ b/Programmierung2/src/module-info.java @@ -8,4 +8,5 @@ module Programmierung2 { requires java.desktop; requires org.junit.jupiter.api; requires junit; + exports Übungen.TaschenrechnerGUI to junit; // Exportiere das Paket für das JUnit-Modul } \ No newline at end of file diff --git a/Programmierung2/src/Übungen/TaschenrechnerGUI/Controller.java b/Programmierung2/src/Übungen/TaschenrechnerGUI/Controller.java new file mode 100644 index 0000000..ba37064 --- /dev/null +++ b/Programmierung2/src/Übungen/TaschenrechnerGUI/Controller.java @@ -0,0 +1,156 @@ +package Übungen.TaschenrechnerGUI; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class Controller implements ActionListener { + + Modell m1; + View v1; + String letzterText = ""; + char operationen[] = {'+', '-','*','÷'}; + char merker = ' '; + String erst = ""; + String last = ""; + Integer zahl1; + Integer zahl2; + + + public Controller(Modell 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 m = new Modell<>(); + View v = new View(); + Controller n1 = new Controller(m, v); + } +} \ No newline at end of file diff --git a/Programmierung2/src/Übungen/TaschenrechnerGUI/JunitTest.java b/Programmierung2/src/Übungen/TaschenrechnerGUI/JunitTest.java new file mode 100644 index 0000000..72e4c72 --- /dev/null +++ b/Programmierung2/src/Übungen/TaschenrechnerGUI/JunitTest.java @@ -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 m1; + + @BeforeEach + public void initi() { + m1 = new Modell<>(); + } + + @Disabled + public void testAdd() { + Modell m1 = new Modell<>(); + assertEquals(15 ,m1.add(10, 5).intValue()); + + } + + @Test + public void testSub() { + Modell m1 = new Modell<>(); + assertEquals(5 ,m1.sub(10, 5).intValue()); + + } + + @Test + public void testdivid() { + Modell m1 = new Modell<>(); + assertEquals(5 ,m1.divid(5, 0).intValue()); + + } + +} diff --git a/Programmierung2/src/Übungen/TaschenrechnerGUI/Modell.java b/Programmierung2/src/Übungen/TaschenrechnerGUI/Modell.java new file mode 100644 index 0000000..5d6a34c --- /dev/null +++ b/Programmierung2/src/Übungen/TaschenrechnerGUI/Modell.java @@ -0,0 +1,58 @@ +package Übungen.TaschenrechnerGUI; + +public class Modell { + + + 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(); + + } + +} diff --git a/Programmierung2/src/Übungen/TaschenrechnerGUI/View.java b/Programmierung2/src/Übungen/TaschenrechnerGUI/View.java new file mode 100644 index 0000000..805c3de --- /dev/null +++ b/Programmierung2/src/Übungen/TaschenrechnerGUI/View.java @@ -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 + ""); + } + + +}