Tentei aplicar de uma maneira diferente, só uma dúvida que fiquei, seria necessário a ação deposit e withdraw, ter uma classe para si mesmas ? OU poderia deixar apenas o método na classe BankAccount mesmo ?
Main :
public static void main(String[] args) {
try{
Person person = new Person("Igor",26,"99999999999");
BankAccount bankAccount = new BankAccount("25",12356,person);
BankOperation deposit = new Deposit(1000);
BankOperation withdraw = new Withdraw(800);
deposit.execute(bankAccount);
withdraw.execute(bankAccount);
System.out.println(bankAccount.getBalance());
}catch (NoBalanceException | InvalidCpf e){
System.err.println(e.getMessage());
}
}
Person:
String name;
int age;
String cpf;
BankAccount bankAccount;
public Person(String name, int age , String cpf) {
this.name = name;
this.age = age;
checkCpf(cpf);
this.cpf = cpf;
}
public void checkCpf(String cpf){
if(cpf.length() == 11){
Pattern pattern = Pattern.compile("^\\d+$");
Matcher matcher = pattern.matcher(cpf);
if(!matcher.matches()){
throw new InvalidCpf("Cpf only accept numbers");
}
}
else{
throw new InvalidCpf("Invalid CPF");
}
}
BankAccount :
private double balance;
private final String agencia;
private final long number;
private Person owner;
public BankAccount(String agencia, long number, Person person) {
this.agencia = agencia;
this.number = number;
this.owner = person;
}
public void deposit(double value){
this.balance += value;
}
public void withdraw(double value){
this.balance -= value;
}
@Override
public String toString() {
return "BankAccount{" +
"balance=" + balance +
", agencia='" + agencia + '\'' +
", number=" + number +
", owner=" + owner +
'}';
}
public double getBalance() {
return balance;
}
public String getAgencia() {
return agencia;
}
public long getNumber() {
return number;
}
public Person getOwner() {
return owner;
}
BankOperation :
public abstract class BankOperation implements BankAction{
private double amount;
public BankOperation(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
Deposit :
public class Deposit extends BankOperation{
public Deposit(double amount) {
super(amount);
}
@Override
public void execute(BankAccount bankAccount){
bankAccount.deposit(super.getAmount());
System.out.println("Deposit of " + super.getAmount() + " successful");
}
}
Withdraw :
public class Withdraw extends BankOperation{
public Withdraw( double amount){
super(amount);
}
@Override
public void execute(BankAccount bankAccount) {
if(bankAccount.getBalance() < super.getAmount()){
throw new NoBalanceException("Balance insufficient");
}
bankAccount.withdraw(super.getAmount());
System.out.println("Withdraw of " + super.getAmount() + " successful");
}
}
Exceptions :
public class InvalidCpf extends RuntimeException {
public InvalidCpf(String message) {
super(message);
}
}
public class NoBalanceException extends RuntimeException {
public NoBalanceException(String message) {
super(message);
}
}