added uebung07 angabe

This commit is contained in:
2025-05-23 20:54:12 +02:00
parent 6efe637f23
commit 8942229d87
19 changed files with 733 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecuredContent;
import oop.ch05.secured.SecurityClient;
public class AccountCard extends MensaCard {
private final SecuredContent account;
public AccountCard(String key, String account, int password) {
super(key, Color.white);
this.account = new SecuredContent(password, account);
}
public String getAccount() {
return (String) 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);
}
@Override
public String toString() {
return super.toString() + " for account " + account.getContent();
}
@Override
public void pass(CashPoint cashPoint) {
cashPoint.charge(this);
}
}

View File

@@ -0,0 +1,43 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecuredContent;
public class CashCard extends MensaCard {
private final SecuredContent balance;
public CashCard(String key, Color color, int password) {
super(key, color);
balance = new SecuredContent(password, 0);
if (color != Color.blue && color != Color.green)
throw new IllegalArgumentException("Invalid CashCard color " + color);
}
public int getBalance() {
return (Integer) balance.getContent();
}
void deposit(VendingMachine client, int cents)
throws AuthorizationException {
if (cents <= 0)
throw new IllegalArgumentException("Non-positive deposit");
final int newBalance = getBalance() + cents;
balance.setContent(client, newBalance);
}
void charge(CashPoint client, int cents) throws AuthorizationException {
if (cents < 0)
throw new IllegalArgumentException("Negative charge");
balance.setContent(client, getBalance() - cents);
}
@Override
public String toString() {
return super.toString() + " with " + balance.getContent() + " cents";
}
@Override
public void pass(CashPoint cashPoint) throws AuthorizationException, RejectedException {
cashPoint.charge(this);
}
}

View File

@@ -0,0 +1,64 @@
package oop.ch05.mensa;
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;
public CashPoint(String name, int password) {
this.name = name;
this.password = password;
this.counter = 0;
this.cents = 0;
}
@Override
public int challengeResponse(int challenge) {
return challenge ^ password;
}
@Override
public String toString() {
return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)";
}
public int getCounter() {
return counter;
}
public int getCents() {
return cents;
}
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 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);
System.out.println("Charging " + price + " cents on account " + accountCard.getAccount());
cents += price;
}
}

View File

@@ -0,0 +1,5 @@
package oop.ch05.mensa;
public enum Color {
green, red, blue, white, gray
}

View File

@@ -0,0 +1,15 @@
package oop.ch05.mensa;
public class CountCard extends MensaCard {
public CountCard(String key, Color color) {
super(key, color);
if (color != Color.red && color != Color.gray)
throw new IllegalArgumentException("Invalid CountCard color " + color);
}
@Override
public void pass(CashPoint cashPoint) {
cashPoint.count(this);
}
}

View File

@@ -0,0 +1,20 @@
package oop.ch05.mensa;
import oop.ch02.cards.Card;
import oop.ch05.secured.AuthorizationException;
public abstract class MensaCard extends Card {
public final Color color;
protected MensaCard(String name, Color color) {
super(name);
this.color = color;
}
@Override
public String toString() {
return color + " card " + getNumber() + " (" + getName() + ")";
}
public abstract void pass(CashPoint cashPoint) throws RejectedException, AuthorizationException;
}

View File

@@ -0,0 +1,68 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
public class MensaExample {
public static void main(String[] args) {
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);
AccountCard conf = new AccountCard("conference", "33-1298", 42);
MensaCard frankSmith = new CountCard("Frank Smith", Color.gray);
CashCard hansMueller = new CashCard("Hans Müller", Color.green, 4711);
CashCard peterSchmidt = new CashCard("Peter Schmidt", Color.green, 4711);
CashCard thomasMayer = new CashCard("Thomas Mayer", Color.blue, 4711);
deposit(vm1, hansMueller, 10);
deposit(vm1, peterSchmidt, 5);
deposit(vm2, thomasMayer, 2);
deposit(tumVM, hansMueller, 10);
System.out.println(vm1);
System.out.println(vm2);
System.out.println(tumVM);
System.out.println(hansMueller);
System.out.println(peterSchmidt);
System.out.println(thomasMayer);
System.out.println();
pass(hansMueller, unibwMensa);
System.out.println(hansMueller);
System.out.println(unibwMensa);
pass(frankSmith, unibwMensa);
pass(conf, unibwMensa);
pass(thomasMayer, unibwMensa);
pass(hansMueller, unibwMensa);
pass(hansMueller, unibwMensa);
pass(hansMueller, unibwMensa);
System.out.println(unibwMensa);
System.out.println(hansMueller);
System.out.println(peterSchmidt);
System.out.println(thomasMayer);
}
private static void pass(MensaCard mensaCard, CashPoint cashPoint) {
try {
mensaCard.pass(cashPoint);
}
catch (RejectedException e) {
System.out.println("rejected: " + e.getMessage());
}
catch (AuthorizationException e) {
System.out.println("authrozation failed: " + e.getMessage());
}
}
private static void deposit(VendingMachine vm, CashCard cashCard, int euros) {
try {
vm.deposit(cashCard, euros);
}
catch (AuthorizationException e) {
System.out.println("authorization failed: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,7 @@
package oop.ch05.mensa;
public class RejectedException extends Exception {
public RejectedException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,37 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecurityClient;
public class VendingMachine implements SecurityClient {
private final int password;
public final String name;
private int euros;
public VendingMachine(String name, int password) {
this.name = name;
this.password = password;
this.euros = 0;
}
@Override
public int challengeResponse(int challenge) {
return challenge ^ password;
}
@Override
public String toString() {
return "Vending machine " + name + " (contains EUR " + euros + ")";
}
public int getEuros() {
return euros;
}
public void deposit(CashCard card, int euros) throws AuthorizationException {
if (euros <= 0)
throw new IllegalArgumentException("Non-positive deposit");
card.deposit(this, euros * 100);
this.euros += euros;
}
}