From 4b73c1b5e325166113fdecb3c3afdb9751fb242c Mon Sep 17 00:00:00 2001 From: Peet Date: Fri, 6 Jun 2025 14:21:53 +0200 Subject: [PATCH] correction ue7 --- uebung07/src/oop/ch05/mensa/AccountCard.java | 42 +++---- .../src/oop/ch05/mensa/AccountManagement.java | 51 +++++++++ uebung07/src/oop/ch05/mensa/CashPoint.java | 103 +++++++++--------- uebung07/src/oop/ch05/mensa/MensaExample.java | 3 +- .../oop/ch05/generic/mensa/MensaTest.java | 4 +- 5 files changed, 126 insertions(+), 77 deletions(-) create mode 100644 uebung07/src/oop/ch05/mensa/AccountManagement.java diff --git a/uebung07/src/oop/ch05/mensa/AccountCard.java b/uebung07/src/oop/ch05/mensa/AccountCard.java index 6b98523..9ff1912 100644 --- a/uebung07/src/oop/ch05/mensa/AccountCard.java +++ b/uebung07/src/oop/ch05/mensa/AccountCard.java @@ -5,30 +5,30 @@ import oop.ch05.secured.SecuredContent; import oop.ch05.secured.SecurityClient; public class AccountCard extends MensaCard { - private final SecuredContent account; + private final SecuredContent account; - public AccountCard(String key, String account, int password) { - super(key, Color.white); - this.account = new SecuredContent<>(password, account); - } + public AccountCard(String key, String account, int password) { + super(key, Color.white); + this.account = new SecuredContent<>(password, account); + } - public String getAccount() { - return account.getContent(); - } + public String getAccount() { + return account.getContent(); + } - public void setAccount(SecurityClient client, String account) throws AuthorizationException { - if (account == null || account.trim().length() == 0) - throw new IllegalArgumentException("Invalid account " + account); - this.account.setContent(client, account); - } + public void setAccount(SecurityClient client, String account) throws AuthorizationException { + if (account == null || account.trim().isEmpty()) + throw new IllegalArgumentException("Invalid account " + account); + this.account.setContent(client, account); + } - @Override - public String toString() { - return super.toString() + " for account " + account.getContent(); - } + @Override + public String toString() { + return super.toString() + " for account " + account.getContent(); + } - @Override - public void pass(CashPoint cashPoint) { - cashPoint.charge(this); - } + @Override + public void pass(CashPoint cashPoint) throws RejectedException, AuthorizationException { + cashPoint.charge(this); + } } diff --git a/uebung07/src/oop/ch05/mensa/AccountManagement.java b/uebung07/src/oop/ch05/mensa/AccountManagement.java new file mode 100644 index 0000000..fef1fba --- /dev/null +++ b/uebung07/src/oop/ch05/mensa/AccountManagement.java @@ -0,0 +1,51 @@ +package oop.ch05.mensa; + +import oop.ch05.secured.AuthorizationException; + +import java.util.Map; +import java.util.Random; +import java.util.TreeMap; + +public class AccountManagement { + private final int password; + public final String name; + private final Random random = new Random(); + private final Map accounts = new TreeMap<>(); + + public AccountManagement(int password, String name) { + this.password = password; + this.name = name; + } + + public void deposit(String account, int euros) { + final int amount = accounts.getOrDefault(account, 0); + accounts.put(account, amount + 100 * euros); + } + + public void pay(AccountCard card, int price, CashPoint cashPoint) throws RejectedException, AuthorizationException { + final int amount = accounts.getOrDefault(card.getAccount(), 0); + final int challenge = nextChallenge(); + if (cashPoint.challengeResponse(challenge) != requiredResponse(challenge)) + throw new AuthorizationException(cashPoint + " is not authorized to access accounts on " + name); + if (amount < price) + throw new RejectedException(card + " bounced"); + accounts.put(card.getAccount(), amount - price); + } + + public int getAmount(String account) { + return accounts.getOrDefault(account, 0); + } + + private int nextChallenge() { + return random.nextInt(); + } + + private int requiredResponse(int challenge) { + return challenge ^ password; + } + + @Override + public String toString() { + return "Account Management " + name + " " + accounts; + } +} diff --git a/uebung07/src/oop/ch05/mensa/CashPoint.java b/uebung07/src/oop/ch05/mensa/CashPoint.java index afa1ec2..ea0d583 100644 --- a/uebung07/src/oop/ch05/mensa/CashPoint.java +++ b/uebung07/src/oop/ch05/mensa/CashPoint.java @@ -4,67 +4,62 @@ import oop.ch05.secured.AuthorizationException; import oop.ch05.secured.SecurityClient; public class CashPoint implements SecurityClient { - private final int password; - public final String name; - private int counter; - private int cents; + private final int password; + public final String name; + private final AccountManagement accountMgmt; + private int counter; + private int cents; - public CashPoint(String name, int password) { - this.name = name; - this.password = password; - this.counter = 0; - this.cents = 0; - } + public CashPoint(String name, int password, AccountManagement accountMgmt) { + this.name = name; + this.password = password; + this.accountMgmt = accountMgmt; + } - @Override - public int challengeResponse(int challenge) { - return challenge ^ password; - } + public int challengeResponse(int challenge) { + return challenge ^ password; + } - @Override - public String toString() { - return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)"; - } + @Override + public String toString() { + return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)"; + } - public int getCounter() { - return counter; - } + public int getCounter() { + return counter; + } - public int getCents() { - return cents; - } + public int getCents() { + return cents; + } - private int getPrice(Color color) { - return switch (color) { - case green -> 267; - case blue -> 357; - case white -> 495; - default -> 0; - }; - } + private int getPrice(Color color) { + return switch (color) { + case green -> 267; + case blue -> 357; + case white -> 495; + default -> 0; + }; + } - void count(MensaCard card) { - counter++; - } + void count(MensaCard card) { + counter++; + } - void charge(CashCard cashCard) throws AuthorizationException, RejectedException { - final int price = getPrice(cashCard.color); - if (cashCard.getBalance() < price) - throw new RejectedException("insufficient payment"); - cashCard.charge(this, price); - count(cashCard); - cents += price; - } + void charge(CashCard cashCard) throws AuthorizationException, RejectedException { + final int price = getPrice(cashCard.color); + if (cashCard.getBalance() < price) + throw new RejectedException("insufficient payment"); + cashCard.charge(this, price); + count(cashCard); + cents += price; + } - void charge(AccountCard accountCard) { - final int price = getPrice(accountCard.color); - // zahlen methode - /* - - zahlen bein acmgmt - - */ - System.out.println("Charging " + price + " cents on account " + accountCard.getAccount()); - cents += price; - } + void charge(AccountCard accountCard) throws RejectedException, AuthorizationException { + final int price = getPrice(accountCard.color); + System.out.println("Charging " + price + " cents on account " + accountCard.getAccount()); + accountMgmt.pay(accountCard, price, this); + count(accountCard); + cents += price; + } } diff --git a/uebung07/src/oop/ch05/mensa/MensaExample.java b/uebung07/src/oop/ch05/mensa/MensaExample.java index 478c549..4d90a22 100644 --- a/uebung07/src/oop/ch05/mensa/MensaExample.java +++ b/uebung07/src/oop/ch05/mensa/MensaExample.java @@ -4,10 +4,11 @@ import oop.ch05.secured.AuthorizationException; public class MensaExample { public static void main(String[] args) { + AccountManagement mgmt = new AccountManagement(4711, "Uni"); VendingMachine vm1 = new VendingMachine("left", 4711); VendingMachine vm2 = new VendingMachine("right", 4711); VendingMachine tumVM = new VendingMachine("TUM Mensa", 3141); - CashPoint unibwMensa = new CashPoint("UniBw Mensa", 4711); + CashPoint unibwMensa = new CashPoint("UniBw Mensa", 4711, mgmt); AccountCard conf = new AccountCard("conference", "33-1298", 42); MensaCard frankSmith = new CountCard("Frank Smith", Color.gray); diff --git a/uebung07/test/oop/ch05/generic/mensa/MensaTest.java b/uebung07/test/oop/ch05/generic/mensa/MensaTest.java index b3a3e06..f193a19 100644 --- a/uebung07/test/oop/ch05/generic/mensa/MensaTest.java +++ b/uebung07/test/oop/ch05/generic/mensa/MensaTest.java @@ -1,6 +1,8 @@ package oop.ch05.generic.mensa; -import oop.ch05.generic.secured.AuthorizationException; +import oop.ch05.secured.AuthorizationException; +import oop.ch05.mensa.*; + import org.junit.Before; import org.junit.Test;