From 55f860689cd4c02ae983478effa697048ee0c16a Mon Sep 17 00:00:00 2001 From: eduardD Date: Tue, 4 Feb 2020 08:34:41 +0100 Subject: [PATCH] Kapselung in neue Klasse --- .../src/MeinBudgetView/Calculations.java | 36 +++++++++++++++++++ .../src/MeinBudgetView/MeinGui.java | 15 ++++---- Budget Projekt/src/MeinBudgetView/MyTest.java | 34 ++++++++++-------- 3 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 Budget Projekt/src/MeinBudgetView/Calculations.java diff --git a/Budget Projekt/src/MeinBudgetView/Calculations.java b/Budget Projekt/src/MeinBudgetView/Calculations.java new file mode 100644 index 0000000..d5253b0 --- /dev/null +++ b/Budget Projekt/src/MeinBudgetView/Calculations.java @@ -0,0 +1,36 @@ +package MeinBudgetView; + +import java.util.*; + +public class Calculations { + + private static int budget; + ArrayList all_expenses = new ArrayList<>(); + private static int expense; + private static int balance; + + public static int getBudget() { + return budget; + } + + public static void setBudget(int budgetEingabe) { + budget = budgetEingabe; + } + + public static int getExpense() { + return expense; + } + + public static void setExpense(int expenseEingabe) { + expense = expenseEingabe; + } + + public static int getBalance() { + return balance; + } + + public static void setBalance(int balanceEingabe) { + balance = balanceEingabe; + } + +} diff --git a/Budget Projekt/src/MeinBudgetView/MeinGui.java b/Budget Projekt/src/MeinBudgetView/MeinGui.java index 564b0f8..2d9e6bd 100644 --- a/Budget Projekt/src/MeinBudgetView/MeinGui.java +++ b/Budget Projekt/src/MeinBudgetView/MeinGui.java @@ -38,9 +38,6 @@ public class MeinGui extends JFrame implements ActionListener { public static JLabel budgetDispaly; public static JLabel expensesDisplay; public static JLabel balanceDisplay; - public static int budget; - public static int expenses; - public static int balance; private JTextField textExpensesName; /** @@ -199,15 +196,15 @@ public class MeinGui extends JFrame implements ActionListener { leerButton(); } - budget = Integer.parseInt(text1.getText()); + Calculations.setBudget(Integer.parseInt(text1.getText())); if (ae.getSource() == this.senden && (text1 != null && text2 != null)) { budgetDispaly.setText(setBudgetView()); } if (ae.getSource() == this.berechne && (text1 != null && text2 != null)) { - expenses = budget - Integer.parseInt(text2.getText()); + Calculations.setExpense(Calculations.getBudget() - Integer.parseInt(text2.getText())); expensesDisplay.setText(setExpensesView()); - balance = Integer.parseInt(text1.getText()) - Integer.parseInt(text2.getText()); + Calculations.setBalance(Integer.parseInt(text1.getText()) - Integer.parseInt(text2.getText())) ; balanceDisplay.setText(setBalanceView()); } @@ -227,17 +224,17 @@ public class MeinGui extends JFrame implements ActionListener { public static String setBudgetView() { - String result = budget + " €"; + String result = Calculations.getBudget() + " �"; return result; } public static String setExpensesView() { - String result = expenses - budget + " €"; + String result = Calculations.getExpense() - Calculations.getBudget() + " �"; return result; } public static String setBalanceView() { - String result = balance + " €"; + String result = Calculations.getBalance() + " �"; return result; } } diff --git a/Budget Projekt/src/MeinBudgetView/MyTest.java b/Budget Projekt/src/MeinBudgetView/MyTest.java index 1235593..5064580 100644 --- a/Budget Projekt/src/MeinBudgetView/MyTest.java +++ b/Budget Projekt/src/MeinBudgetView/MyTest.java @@ -8,31 +8,35 @@ class MyTest { @Test public void testSetBudgetView() { - assertEquals(MeinGui.budget + " €", MeinGui.setBudgetView()); - assertNotEquals(MeinGui.budget + " ", MeinGui.setBudgetView()); - assertNotEquals(MeinGui.budget + "€ ", MeinGui.setBudgetView()); - assertNotEquals(MeinGui.budget + " € ", MeinGui.setBudgetView()); - assertNotEquals(MeinGui.budget + " ", MeinGui.setBudgetView()); + assertEquals(Calculations.getBudget() + " �", MeinGui.setBudgetView()); + assertNotEquals(Calculations.getBudget() + " ", MeinGui.setBudgetView()); + assertNotEquals(Calculations.getBudget() + "� ", MeinGui.setBudgetView()); + assertNotEquals(Calculations.getBudget() + " � ", MeinGui.setBudgetView()); + assertNotEquals(Calculations.getBudget() + " ", MeinGui.setBudgetView()); } @Test public void testSetExpensesView() { - assertEquals(MeinGui.expenses - MeinGui.budget + " €", MeinGui.setExpensesView()); - assertNotEquals(MeinGui.expenses - MeinGui.budget, MeinGui.setExpensesView()); - assertNotEquals(MeinGui.expenses - MeinGui.budget + "€ " , MeinGui.setExpensesView()); - assertNotEquals(MeinGui.expenses - MeinGui.budget + " € " , MeinGui.setExpensesView()); - assertNotEquals(MeinGui.expenses - MeinGui.budget + "", MeinGui.setExpensesView()); + assertEquals(Calculations.getExpense() - Calculations.getBudget() + " �", MeinGui.setExpensesView()); + assertNotEquals(Calculations.getExpense() - Calculations.getBudget(), MeinGui.setExpensesView()); + assertNotEquals(Calculations.getExpense() - Calculations.getBudget() + "� " , MeinGui.setExpensesView()); + assertNotEquals(Calculations.getExpense() - Calculations.getBudget() + " � " , MeinGui.setExpensesView()); + assertNotEquals(Calculations.getExpense() - Calculations.getBudget() + "", MeinGui.setExpensesView()); } @Test public void testSetBalanceView() { - assertEquals(MeinGui.balance + " €", MeinGui.setExpensesView()); - assertNotEquals(MeinGui.balance, MeinGui.setExpensesView()); - assertNotEquals(MeinGui.balance + "€ " , MeinGui.setExpensesView()); - assertNotEquals(MeinGui.balance+ " € " , MeinGui.setExpensesView()); - assertNotEquals(MeinGui.balance*2 + " € ", MeinGui.setExpensesView()); + assertEquals(Calculations.getBalance() + " �", MeinGui.setExpensesView()); + assertNotEquals(Calculations.getBalance(), MeinGui.setExpensesView()); + assertNotEquals(Calculations.getBalance() + "� " , MeinGui.setExpensesView()); + assertNotEquals(Calculations.getBalance()+ " � " , MeinGui.setExpensesView()); + assertNotEquals(Calculations.getBalance()*2 + " � ", MeinGui.setExpensesView()); + + } + @Test + public void add_expanse() { }