In my code I am trying to create a scenario for a bank account. In the method processWithdrawal() I have doubles "amt" and "balance". Balance is an attribute of my super class "Bank Account" while "amt" is an attribute of my method processWithdrawal() which is in my subclass "CheckingAccount".
In this scenario the customer is withdrawal more than what they have in their account which throws an insufficient funds and then calls on processWithdrawal(). Everything works great except when I force it negative the output returns null.
This is my subclass where I believe the error is occurring. It is also where my static void main executes the code:
public class CheckingAccount extends BankAccount {
double interest;
int overdraft;
String ckext;
public CheckingAccount() {
interest = .09;
overdraft = 30;
ckext = "02";
}
Here is the processWithdrawal method. All other methods work and return correctly with the exception of this one which returns null after I enter the amount to withdrawal causing balance to go negative:
public void processWithdrawal() {
Scanner scan = new Scanner(System.in);
System.out.println("Warning. You are about to overdraft your account. Do you wish to proceed? y/n");
String ans = scan.next();
if("y".equals(ans)) {
System.out.println("How much would you like to withdrawal?");
double amt = scan.nextDouble();
balance = balance - amt - overdraft;
}else {
System.out.println("Returning to main menu");
}
scan.close();
}
Just in case, here is my superclass where attribute "balance" is found. Not shown is the constructor method where I set balance to 0.
public class BankAccount {
Scanner scan = new Scanner(System.in);
String firstName;
String lastName;
int accountID;
double balance;