Skip to content

Instantly share code, notes, and snippets.

@ythirion
Created April 6, 2020 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ythirion/75281486c37a9dbccc31815126b462bf to your computer and use it in GitHub Desktop.
Save ythirion/75281486c37a9dbccc31815126b462bf to your computer and use it in GitHub Desktop.
PBT
import io.vavr.control.Try;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@Getter
public class Account {
private final double balance;
private final boolean isOverdraftAuthorized;
private final double maxWithdrawal;
public Try<Account> withdraw(Withdraw command) {
return Try.of(() -> {
checkMaxWithdrawal(command);
checkBalance(command);
return new Account(balance - command.getAmount(),
this.isOverdraftAuthorized,
this.maxWithdrawal);
});
}
private void checkMaxWithdrawal(Withdraw command) {
if(command.getAmount() > maxWithdrawal) {
throw new IllegalArgumentException("Amount exceeding your limit of " + maxWithdrawal);
}
}
private void checkBalance(Withdraw command) {
if(command.getAmount() > balance && !isOverdraftAuthorized()) {
throw new IllegalArgumentException("Insufficient balance to withdraw : " + command.getAmount());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment