correction ue7
This commit is contained in:
parent
1b07a3b6ab
commit
4b73c1b5e3
@ -5,30 +5,30 @@ import oop.ch05.secured.SecuredContent;
|
||||
import oop.ch05.secured.SecurityClient;
|
||||
|
||||
public class AccountCard extends MensaCard {
|
||||
private final SecuredContent<String> account;
|
||||
private final SecuredContent<String> 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);
|
||||
}
|
||||
}
|
||||
|
51
uebung07/src/oop/ch05/mensa/AccountManagement.java
Normal file
51
uebung07/src/oop/ch05/mensa/AccountManagement.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user