Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Account implements AccountInterface {
- private long accountNumber;
- private double balance;
- public Account(long accountNumber) {
- this.accountNumber = accountNumber;
- this.balance = 0;
- }
- @Override
- public long getNumber() {
- return this.accountNumber;
- }
- @Override
- public double getBalance() {
- return this.balance;
- }
- protected void updateBalance(double amount) {
- this.balance+=amount;
- }
- @Override
- public void deposit(double amount) {
- if(amount <= 0) {
- throw new NegativeAmountException("The amount to deposit should be positive!");
- }
- this.balance+=amount;
- }
- @Override
- public void withdraw(double amount) {
- if(amount <= 0) {
- throw new NegativeAmountException("The amount to withdraw should be positive!");
- }
- if (this.balance >= amount) {
- this.balance-=amount;
- }
- else {
- throw new NegativeBalanceException("You don't have that much balance to withdraw!");
- }
- }
- }
- public class OverdraftAccount extends Account {
- private double overdraftAmount;
- public OverdraftAccount(long accountNumber, double overdraftAmount) {
- super(accountNumber);
- this.overdraftAmount=overdraftAmount;
- }
- @Override
- public void withdraw(double amount) {
- if(amount < 0) {
- throw new NegativeAmountException("The amount to withdraw should be positive!");
- }
- double currentBalance = this.getBalance();
- double minBalanceAllowed = currentBalance + overdraftAmount;
- if(minBalanceAllowed >= amount) {
- this.updateBalance(-amount);
- }
- else {
- throw new ExceededOverdraftException("Not enough overdraft for this withdraw!");
- }
- }
- }
- public class RestrictedWithdrawAccount extends Account {
- private double withdrawRestriction;
- public RestrictedWithdrawAccount(long accountNumber,double withdrawRestriction) {
- super(accountNumber);
- this.withdrawRestriction=withdrawRestriction;
- }
- @Override
- public void withdraw(double amount) {
- if(amount > this.withdrawRestriction) {
- throw new ExceedesWithdrawRestrictionException("You have exceeded your withdraw restriction!");
- }
- super.withdraw(amount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement