Created
April 6, 2020 13:29
-
-
Save ythirion/75281486c37a9dbccc31815126b462bf to your computer and use it in GitHub Desktop.
PBT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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