correction ue7

This commit is contained in:
Johannes Schmelz 2025-06-06 14:21:53 +02:00
parent 1b07a3b6ab
commit 4b73c1b5e3
5 changed files with 126 additions and 77 deletions

View File

@ -17,7 +17,7 @@ public class AccountCard extends MensaCard {
} }
public void setAccount(SecurityClient client, String account) throws AuthorizationException { public void setAccount(SecurityClient client, String account) throws AuthorizationException {
if (account == null || account.trim().length() == 0) if (account == null || account.trim().isEmpty())
throw new IllegalArgumentException("Invalid account " + account); throw new IllegalArgumentException("Invalid account " + account);
this.account.setContent(client, account); this.account.setContent(client, account);
} }
@ -28,7 +28,7 @@ public class AccountCard extends MensaCard {
} }
@Override @Override
public void pass(CashPoint cashPoint) { public void pass(CashPoint cashPoint) throws RejectedException, AuthorizationException {
cashPoint.charge(this); cashPoint.charge(this);
} }
} }

View File

@ -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<String, Integer> 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;
}
}

View File

@ -6,17 +6,16 @@ import oop.ch05.secured.SecurityClient;
public class CashPoint implements SecurityClient { public class CashPoint implements SecurityClient {
private final int password; private final int password;
public final String name; public final String name;
private final AccountManagement accountMgmt;
private int counter; private int counter;
private int cents; private int cents;
public CashPoint(String name, int password) { public CashPoint(String name, int password, AccountManagement accountMgmt) {
this.name = name; this.name = name;
this.password = password; this.password = password;
this.counter = 0; this.accountMgmt = accountMgmt;
this.cents = 0;
} }
@Override
public int challengeResponse(int challenge) { public int challengeResponse(int challenge) {
return challenge ^ password; return challenge ^ password;
} }
@ -56,15 +55,11 @@ public class CashPoint implements SecurityClient {
cents += price; cents += price;
} }
void charge(AccountCard accountCard) { void charge(AccountCard accountCard) throws RejectedException, AuthorizationException {
final int price = getPrice(accountCard.color); final int price = getPrice(accountCard.color);
// zahlen methode
/*
zahlen bein acmgmt
*/
System.out.println("Charging " + price + " cents on account " + accountCard.getAccount()); System.out.println("Charging " + price + " cents on account " + accountCard.getAccount());
accountMgmt.pay(accountCard, price, this);
count(accountCard);
cents += price; cents += price;
} }
} }

View File

@ -4,10 +4,11 @@ import oop.ch05.secured.AuthorizationException;
public class MensaExample { public class MensaExample {
public static void main(String[] args) { public static void main(String[] args) {
AccountManagement mgmt = new AccountManagement(4711, "Uni");
VendingMachine vm1 = new VendingMachine("left", 4711); VendingMachine vm1 = new VendingMachine("left", 4711);
VendingMachine vm2 = new VendingMachine("right", 4711); VendingMachine vm2 = new VendingMachine("right", 4711);
VendingMachine tumVM = new VendingMachine("TUM Mensa", 3141); 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); AccountCard conf = new AccountCard("conference", "33-1298", 42);
MensaCard frankSmith = new CountCard("Frank Smith", Color.gray); MensaCard frankSmith = new CountCard("Frank Smith", Color.gray);

View File

@ -1,6 +1,8 @@
package oop.ch05.generic.mensa; 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.Before;
import org.junit.Test; import org.junit.Test;