added lab13 ex2
This commit is contained in:
52
src/lab13_proxy/ex2/Bank.java
Normal file
52
src/lab13_proxy/ex2/Bank.java
Normal file
@ -0,0 +1,52 @@
|
||||
package lab13_proxy.ex2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Bank {
|
||||
private final ArrayList<String> blacklistedClients = new ArrayList<>();
|
||||
|
||||
public void blacklist(String client) {
|
||||
blacklistedClients.add(client);
|
||||
}
|
||||
public boolean isBlacklisted(String client) {
|
||||
return blacklistedClients.contains(client);
|
||||
}
|
||||
|
||||
public void deposit(String client, Account account, int amount) {
|
||||
if (amount < 0) {
|
||||
System.out.println("Cannot deposit a negative amount");
|
||||
return;
|
||||
}
|
||||
if (isBlacklisted(client)) {
|
||||
System.out.println(client + " is on a blacklist and does not have the right to DEPOSIT money into " + account);
|
||||
return;
|
||||
}
|
||||
|
||||
account.deposit(amount);
|
||||
System.out.println(client + " has deposited " + amount + " on " + account + ". New balance is " + account.getBalance());
|
||||
}
|
||||
|
||||
public void withdraw(String client, Account account, int amount) {
|
||||
if (amount < 0) {
|
||||
System.out.println("Cannot withdraw a negative amount");
|
||||
return;
|
||||
}
|
||||
if (isBlacklisted(client)) {
|
||||
System.out.println(client + " is on a blacklist and does not have the right to WITHDRAW money from " + account);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!account.getOwner().equals(client)) {
|
||||
System.out.println(client + " cannot WITHDRAW money from " + account + " because they are not the owner");
|
||||
return;
|
||||
}
|
||||
|
||||
if (account.getBalance() < amount) {
|
||||
System.out.println(client + " cannot WITHDRAW money from " + account + " because there is not enough money on the account.");
|
||||
return;
|
||||
}
|
||||
|
||||
account.withdraw(amount);
|
||||
System.out.println(client + " has withdrawn " + amount + " from " + account + ". New balance is " + account.getBalance());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user