diff --git a/src/lab13_proxy/ex2/Account.java b/src/lab13_proxy/ex2/Account.java new file mode 100644 index 0000000..d85982f --- /dev/null +++ b/src/lab13_proxy/ex2/Account.java @@ -0,0 +1,36 @@ +package lab13_proxy.ex2; + +import java.util.Random; + +public class Account { + private int balance; + private int accountNumber; + private String owner; + + public Account(String owner, int startBalance) { + this.owner = owner; + this.balance = startBalance; + this.accountNumber = new Random().hashCode(); + } + + public void deposit(int amount) { + balance += amount; + } + + public void withdraw(int amount) { + balance -= amount; + } + + public String getOwner() { + return owner; + } + + public int getBalance() { + return balance; + } + + @Override + public String toString() { + return "account " + this.accountNumber + " belonging to " + this.owner; + } +} diff --git a/src/lab13_proxy/ex2/Bank.java b/src/lab13_proxy/ex2/Bank.java new file mode 100644 index 0000000..a925a99 --- /dev/null +++ b/src/lab13_proxy/ex2/Bank.java @@ -0,0 +1,52 @@ +package lab13_proxy.ex2; + +import java.util.ArrayList; + +public class Bank { + private final ArrayList 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()); + } +} diff --git a/src/lab13_proxy/ex2/Main.java b/src/lab13_proxy/ex2/Main.java new file mode 100644 index 0000000..bcaca28 --- /dev/null +++ b/src/lab13_proxy/ex2/Main.java @@ -0,0 +1,18 @@ +package lab13_proxy.ex2; + +public class Main { + public static void main(String[] args) { + Account account = new Account("Pascale", 16000); + + Bank bank = new Bank(); + bank.blacklist("Jean"); + bank.blacklist("Pierre"); + + bank.deposit("Marcel", account, 1000); + bank.deposit("Jean", account, 1000); + bank.withdraw("Marcel", account, 1000); + bank.withdraw("Pascale", account, 1000000); + bank.withdraw("Pascale", account, 1000); + bank.withdraw("Pierre", account, 1000); + } +}