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

@ -5,30 +5,30 @@ import oop.ch05.secured.SecuredContent;
import oop.ch05.secured.SecurityClient; import oop.ch05.secured.SecurityClient;
public class AccountCard extends MensaCard { public class AccountCard extends MensaCard {
private final SecuredContent<String> account; private final SecuredContent<String> account;
public AccountCard(String key, String account, int password) { public AccountCard(String key, String account, int password) {
super(key, Color.white); super(key, Color.white);
this.account = new SecuredContent<>(password, account); this.account = new SecuredContent<>(password, account);
} }
public String getAccount() { public String getAccount() {
return account.getContent(); return account.getContent();
} }
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);
} }
@Override @Override
public String toString() { public String toString() {
return super.toString() + " for account " + account.getContent(); return super.toString() + " for account " + account.getContent();
} }
@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

@ -4,67 +4,62 @@ import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecurityClient; 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 int counter; private final AccountManagement accountMgmt;
private int cents; private int counter;
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; }
}
@Override @Override
public String toString() { public String toString() {
return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)"; return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)";
} }
public int getCounter() { public int getCounter() {
return counter; return counter;
} }
public int getCents() { public int getCents() {
return cents; return cents;
} }
private int getPrice(Color color) { private int getPrice(Color color) {
return switch (color) { return switch (color) {
case green -> 267; case green -> 267;
case blue -> 357; case blue -> 357;
case white -> 495; case white -> 495;
default -> 0; default -> 0;
}; };
} }
void count(MensaCard card) { void count(MensaCard card) {
counter++; counter++;
} }
void charge(CashCard cashCard) throws AuthorizationException, RejectedException { void charge(CashCard cashCard) throws AuthorizationException, RejectedException {
final int price = getPrice(cashCard.color); final int price = getPrice(cashCard.color);
if (cashCard.getBalance() < price) if (cashCard.getBalance() < price)
throw new RejectedException("insufficient payment"); throw new RejectedException("insufficient payment");
cashCard.charge(this, price); cashCard.charge(this, price);
count(cashCard); count(cashCard);
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 System.out.println("Charging " + price + " cents on account " + accountCard.getAccount());
/* accountMgmt.pay(accountCard, price, this);
count(accountCard);
zahlen bein acmgmt cents += price;
}
*/
System.out.println("Charging " + price + " cents on account " + accountCard.getAccount());
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;