updated to generic

This commit is contained in:
Johannes Schmelz 2025-05-26 14:39:37 +02:00
parent 8942229d87
commit ac9d7fdd39
3 changed files with 13 additions and 12 deletions

View File

@ -5,15 +5,15 @@ 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 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 (String) account.getContent(); return account.getContent();
} }
public void setAccount(SecurityClient client, String account) throws AuthorizationException { public void setAccount(SecurityClient client, String account) throws AuthorizationException {

View File

@ -4,17 +4,17 @@ import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecuredContent; import oop.ch05.secured.SecuredContent;
public class CashCard extends MensaCard { public class CashCard extends MensaCard {
private final SecuredContent balance; private final SecuredContent<Integer> balance;
public CashCard(String key, Color color, int password) { public CashCard(String key, Color color, int password) {
super(key, color); super(key, color);
balance = new SecuredContent(password, 0); balance = new SecuredContent<>(password, 0);
if (color != Color.blue && color != Color.green) if (color != Color.blue && color != Color.green)
throw new IllegalArgumentException("Invalid CashCard color " + color); throw new IllegalArgumentException("Invalid CashCard color " + color);
} }
public int getBalance() { public int getBalance() {
return (Integer) balance.getContent(); return balance.getContent();
} }
void deposit(VendingMachine client, int cents) void deposit(VendingMachine client, int cents)

View File

@ -7,13 +7,14 @@ import java.util.Random;
* by a challenge-response approach. The content may be read at any time, but * by a challenge-response approach. The content may be read at any time, but
* only authorized clients are allowed to modify the contents. * only authorized clients are allowed to modify the contents.
* *
* @param <E> the type of the actual secured content
* @author Mark Minas * @author Mark Minas
*/ */
public class SecuredContent { public class SecuredContent<E> {
/** /**
* The actual, secured content * The actual, secured content
*/ */
private Object content; private E content;
/** /**
* A random number generator used for generating challenges. * A random number generator used for generating challenges.
*/ */
@ -30,7 +31,7 @@ public class SecuredContent {
* challenge. * challenge.
* @param content the contained content * @param content the contained content
*/ */
public SecuredContent(int password, Object content) { public SecuredContent(int password, E content) {
this.password = password; this.password = password;
this.content = content; this.content = content;
} }
@ -39,7 +40,7 @@ public class SecuredContent {
* Returns the contained contents. There is no authorization necessary for * Returns the contained contents. There is no authorization necessary for
* reading the contents. * reading the contents.
*/ */
public Object getContent() { public E getContent() {
return content; return content;
} }
@ -52,12 +53,12 @@ public class SecuredContent {
* @param content The new contained contents * @param content The new contained contents
* @throws AuthorizationException if the specified client cannot authorize himself * @throws AuthorizationException if the specified client cannot authorize himself
*/ */
public void setContent(SecurityClient client, Object content) public void setContent(SecurityClient client, E content)
throws AuthorizationException { throws AuthorizationException {
final int challenge = nextChallenge(); final int challenge = nextChallenge();
if (client.challengeResponse(challenge) != requiredResponse(challenge)) if (client.challengeResponse(challenge) != requiredResponse(challenge))
throw new AuthorizationException(client throw new AuthorizationException(client
+ " is not authorized to access contents."); + " is not authorized to access contents.");
this.content = content; this.content = content;
} }