correction ue7
This commit is contained in:
parent
1b07a3b6ab
commit
4b73c1b5e3
@ -17,7 +17,7 @@ public class AccountCard extends MensaCard {
|
||||
}
|
||||
|
||||
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);
|
||||
this.account.setContent(client, account);
|
||||
}
|
||||
@ -28,7 +28,7 @@ public class AccountCard extends MensaCard {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pass(CashPoint cashPoint) {
|
||||
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;
|
||||
}
|
||||
}
|
@ -6,17 +6,16 @@ import oop.ch05.secured.SecurityClient;
|
||||
public class CashPoint implements SecurityClient {
|
||||
private final int password;
|
||||
public final String name;
|
||||
private final AccountManagement accountMgmt;
|
||||
private int counter;
|
||||
private int cents;
|
||||
|
||||
public CashPoint(String name, int password) {
|
||||
public CashPoint(String name, int password, AccountManagement accountMgmt) {
|
||||
this.name = name;
|
||||
this.password = password;
|
||||
this.counter = 0;
|
||||
this.cents = 0;
|
||||
this.accountMgmt = accountMgmt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int challengeResponse(int challenge) {
|
||||
return challenge ^ password;
|
||||
}
|
||||
@ -56,15 +55,11 @@ public class CashPoint implements SecurityClient {
|
||||
cents += price;
|
||||
}
|
||||
|
||||
void charge(AccountCard accountCard) {
|
||||
void charge(AccountCard accountCard) throws RejectedException, AuthorizationException {
|
||||
final int price = getPrice(accountCard.color);
|
||||
// zahlen methode
|
||||
/*
|
||||
|
||||
zahlen bein acmgmt
|
||||
|
||||
*/
|
||||
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