added lab13 ex2
This commit is contained in:
parent
51f5d352c6
commit
64f7f56ffe
36
src/lab13_proxy/ex2/Account.java
Normal file
36
src/lab13_proxy/ex2/Account.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
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());
|
||||||
|
}
|
||||||
|
}
|
18
src/lab13_proxy/ex2/Main.java
Normal file
18
src/lab13_proxy/ex2/Main.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user