You are on page 1of 7

package bank;

import java.util.*;
// ...

public class Bank {

private Map<String, BankAccount> accounts;

public Bank() {
this.accounts = new HashMap<String,BankAccount>();
}

public void addAccount(BankAccount account) {


this.accounts.put(account.getNumber(), account);
}

public void displayBalance(String accountNumber) {


float value = this.accounts.get(accountNumber).getBalance();
System.out.println("balance of account n°"+accountNumber+" : "+value);
}

public void creditAccount(String accountNumber, float amount) {


this.accounts.get(accountNumber).credit(amount);
}

public void withdraw(String accountNumber, float amount) {


this.accounts.get(accountNumber).withdraw(amount);
}

// and other methods handling BankAccount...

public static void main(String[] args) {

Bank bank = new Bank();

bank.addAccount(new ProtectedBankAccount(new Person(),"number1",0));


bank.addAccount(new BonusBankAccount(new Person(),"number2",0.01f));

bank.creditAccount("number1", 1000);
bank.creditAccount("number2", 500);
bank.displayBalance("number2");
bank.withdraw("number1",444);

}
package interfaces;

import java.util.Date;
import java.util.List;

public interface BankAccount {

public Person getOwner();


public String getNumber();
public float getBalance();
public void credit(float amount);
public void withdraw(float amount);
public List<Operation> getOperationsAfter(Date time);

}
package interfaces;

import java.util.*;

public class StandardBankAccount implements BankAccount {

private Person owner;


private String number;
private float balance;
private List<Operation> theOperations;

public StandardBankAccount(Person owner, String number) {


this.owner = owner;
this.number = number;
this.balance = 0;
this.theOperations = new ArrayList<Operation>();

public Person getOwner() {


return this.owner;
}

public String getNumber() {


return this.number;
}

public float getBalance() {


return this.balance;
}

public void credit(float amount) {


this.balance = this.balance + amount;
Date date = Calendar.getInstance().getTime();
this.theOperations.add(new Operation(amount,date));
}

public void withdraw(float amount) {


this.balance = this.balance + amount;
Date date = Calendar.getInstance().getTime();
this.theOperations.add(new Operation(amount,date));

public List<Operation> getOperationsAfter(Date time) {


List<Operation> result = new ArrayList<Operation>();
for(Operation op : this.theOperations) {
if (op.getDate().compareTo(time) >= 0) {
result.add(op);
}
}
return result;
}

}
package interfaces;

import java.util.*;

public class ProtectedBankAccount implements BankAccount {

private float lowerBound;

private Person owner;


private String number;
private float balance;
private List<Operation> theOperations;

public ProtectedBankAccount(Person owner, String number, float lowerBound){


this.owner = owner;
this.number = number;
this.balance = 0;
this.theOperations = new ArrayList<Operation>();
this.lowerBound = lowerBound;
}

public Person getOwner() {


return this.owner;
}

public String getNumber() {


return this.number;
}

public float getBalance() {


return this.balance;
}

public void credit(float amount) {


this.balance = this.balance + amount;
Date date = Calendar.getInstance().getTime();
this.theOperations.add(new Operation(amount,date));
}

public void withdraw(float amount) {


if (this.balance-amount >= this.lowerBound) {
this.balance = this.balance + amount;
Date date = Calendar.getInstance().getTime();
this.theOperations.add(new Operation(amount,date));
}
// else nothing... or an exception to be thrown
}

public List<Operation> getOperationsAfter(Date time) {


List<Operation> result = new ArrayList<Operation>();
for(Operation op : this.theOperations) {
if (op.getDate().compareTo(time) >= 0) {
result.add(op);
}
}
return result;
}

public float getLowerBound() {


return this.lowerBound;
}
}
package interfaces;

import java.util.*;

public class BonusBankAccount implements BankAccount {

private float bonusRate;

private Person owner;


private String number;
private float balance;
private List<Operation> theOperations;

public BonusBankAccount(Person owner, String number, float bonusRate) {


this.owner = owner;
this.number = number;
this.balance = 0;
this.theOperations = new ArrayList<Operation>();
this.bonusRate = bonusRate;
}

public Person getOwner() {


return this.owner;
}

public String getNumber() {


return this.number;
}

public float getBalance() {


return this.balance;
}

public void credit(float amount) {


this.balance = this.balance + (amount*this.bonusRate);
Date date = Calendar.getInstance().getTime();
this.theOperations.add(new Operation(amount,date));
}

public void withdraw(float amount) {


this.balance = this.balance + amount;
Date date = Calendar.getInstance().getTime();
this.theOperations.add(new Operation(amount,date));

public List<Operation> getOperationsAfter(Date time) {


List<Operation> result = new ArrayList<Operation>();
for(Operation op : this.theOperations) {
if (op.getDate().compareTo(time) >= 0) {
result.add(op);
}
}
return result;
}

public float getBonusRate() {


return this.bonusRate;
}
}
package interfaces;

import java.util.*;

public class *****BankAccount implements BankAccount {

*************************

private Person owner;


private String number;
private float balance;
private List<Operation> theOperations;

public *****BankAccount(Person owner, String number *****) {


this.owner = owner;
this.number = number;
this.balance = 0;
this.theOperations = new ArrayList<Operation>();

public Person getOwner() {


return this.owner;
}

public String getNumber() {


return this.number;
}

public float getBalance() {


return this.balance;
}

public void credit(float amount) {


*************************
*************************
*************************
}

public void withdraw(float amount) {


*************************
*************************
*************************
*************************
*************************

public List<Operation> getOperationsAfter(Date time) {


List<Operation> result = new ArrayList<Operation>();
for(Operation op : this.theOperations) {
if (op.getDate().compareTo(time) >= 0) {
result.add(op);
}
}
return result;
}

*************************
*************************
*************************
}

You might also like