I have got a bank account class which has all of your usual attributes and methods e.g deposit, withdraw etc. The balance attribute should work as expected "Hold a value for the amount of a particular currency" but I want to include the option of having a fixed overdraft of £500. The user cannot withdraw more than this amount. When I have wrote the if statement, non of the parameters are being followed so I am not able to have a fixed overdraft of £500. I know this as I used the withdraw method to withdraw more than should be allowed.
This is my code;
package javaCode.AdvancedBankAccount;
public class StudentAccount {
private String accountNumber;
private String accountHolder;
private double balance;
private boolean hasOverdraft;
private final int OVERDRAFTLIMIT = -500;
public StudentAccount (String accountNumber, String accountHolder, boolean hasOverdraft) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.balance = 0.0;
this.hasOverdraft = hasOverdraft;
}
public String getAccountNumber () {
return accountNumber;
}
public void setAccountNumber (String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountHolder () {
return accountHolder;
}
public void setAccountHolder (String accountHolder) {
this.accountHolder = accountHolder;
}
public double getBalance () {
return balance;
}
public void setBalance (double balance) {
this.balance = balance;
}
public boolean isHasOverdraft () {
return hasOverdraft;
}
public void setHasOverdraft (boolean hasOverdraft) {
this.hasOverdraft = hasOverdraft;
}
public boolean deposit (double amount) {
if (amount>0){
this.balance+=amount;
return true;
}
else {
return false;
}
}
public boolean withdraw (double amount, double balance, double overdraftLimit) {
overdraftLimit = OVERDRAFTLIMIT;
if (amount > 0 && balance + overdraftLimit - amount > overdraftLimit) {
this.balance -= amount;
return true;
} else {
return false;
}
}
@Override
public String toString() {
return "Account Number: " + accountNumber +
" | Account Holder: " + accountHolder +
" | Balance: " + balance +
" | Account has overdraft?: " + hasOverdraft;
}
public static void main(String[] args) {
CurrentAccount jake = new CurrentAccount("10801", "Jake Mellor", true);
jake.withdraw(300);
System.out.println(jake);
jake.deposit(-300);
System.out.println(jake);
jake.withdraw(1000);
System.out.println(jake);
}
}
And these are the results;
Account Number: 10801 | Account Holder: Jake Mellor | Balance: -300.0 | Account has overdraft?: true
Account Number: 10801 | Account Holder: Jake Mellor | Balance: -300.0 | Account has overdraft?: true
Account Number: 10801 | Account Holder: Jake Mellor | Balance: -1300.0 | Account has overdraft?: true
Process finished with exit code 0
Aucun commentaire:
Enregistrer un commentaire