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;
public class AccountCard extends MensaCard {
private final SecuredContent account;
private final SecuredContent<String> account;
public AccountCard(String key, String account, int password) {
super(key, Color.white);
this.account = new SecuredContent(password, account);
this.account = new SecuredContent<>(password, account);
}
public String getAccount() {
return (String) account.getContent();
return account.getContent();
}
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;
public class CashCard extends MensaCard {
private final SecuredContent balance;
private final SecuredContent<Integer> balance;
public CashCard(String key, Color color, int password) {
super(key, color);
balance = new SecuredContent(password, 0);
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();
return balance.getContent();
}
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
* only authorized clients are allowed to modify the contents.
*
* @param <E> the type of the actual secured content
* @author Mark Minas
*/
public class SecuredContent {
public class SecuredContent<E> {
/**
* The actual, secured content
*/
private Object content;
private E content;
/**
* A random number generator used for generating challenges.
*/
@ -30,7 +31,7 @@ public class SecuredContent {
* challenge.
* @param content the contained content
*/
public SecuredContent(int password, Object content) {
public SecuredContent(int password, E content) {
this.password = password;
this.content = content;
}
@ -39,7 +40,7 @@ public class SecuredContent {
* Returns the contained contents. There is no authorization necessary for
* reading the contents.
*/
public Object getContent() {
public E getContent() {
return content;
}
@ -52,7 +53,7 @@ public class SecuredContent {
* @param content The new contained contents
* @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 {
final int challenge = nextChallenge();
if (client.challengeResponse(challenge) != requiredResponse(challenge))