In: Computer Science
In java, I have problem how to declare BankAccount Account = new BasicAccount(100.00);
Given the class BankAccount , implement a subclass of BankAccount called BasicAccount whose withdraw method will only withdraw money if there is enough money currently in the account (no overdrafts allowed). Assume all inputs to withdraw and the constructor will be valid non-negative doubles.
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0;
}
public BankAccount(double initialBalance)
{
balance =
initialBalance;
}
public void deposit(double amount) {
double newBalance =
balance + amount;
balance =
newBalance;
}
public void withdraw(double amount) {
double newBalance =
balance - amount;
balance =
newBalance;
}
public double getBalance() {
return
balance;
}
}
public class Main { public static void main(String []args) {
BankAccount account = new BasicAccount(100.00); // creates a BasicAccount with an initial balance of 100.00 account.withdraw(80.00); account.getBalance(); // returns 20.0 account.withdraw(50.00); // amount won't be withdrawn since it's greater than balance account.getBalance(); // returns 20.0
}
}
The class BasicAccount will extend the class BankAccount.
Since, balance is a private member in class BankAccount, a setter method is also required in the BankAccount class.
Therefore, the complete definition of class BankAccount is as follows:
BankAccount.java
public class BankAccount {
private double balance;
//default constructor
public BankAccount() {
balance = 0;
}
//for initializing the balance
public BankAccount(double initialBalance) {
balance = initialBalance;
}
//for depositing in the account
public void deposit(double amount) {
double newBalance = balance + amount;
balance = newBalance;
}
//for withdrawing amount from the account
public void withdraw(double amount) {
double newBalance = balance - amount;
balance = newBalance;
}
//for getting the value of balance
public double getBalance() {
return balance;
}
//setter method for setting value of balance
public void setBalance(double b){
balance = b;
}
}
The subclass BasicAccount will define the method withdraw() such that the amount gets withdrawn only when there is sufficient balance.
The constructor calls the super constructor for initialization.
The defintion of class BasicAccount is as follows:
BasicAccount.java
//subclass BasicAccount extends BankAccount class
//the constructor calls the super constructor
class BasicAccount extends BankAccount{
//constructor calls the super constructor
public BasicAccount(double initialBalance){
super(initialBalance);
}
//withdraw method
//withdraw only when there is sufficient balance
public void withdraw(double amount) {
double newBalance = getBalance() - amount;
if (newBalance >= 0)
setBalance(newBalance);
}
}
The main class for showing the object initialzation and result after calling the withdraw() method is as follows:
Main.java
public class Main {
public static void main(String []args) {
BankAccount account = new BasicAccount(100.00); // creates a BasicAccount with an initial balance of 100.00
account.withdraw(80.00);
System.out.println(account.getBalance()); // returns 20.0
account.withdraw(50.00); // amount won't be withdrawn since it's greater than balance
System.out.println(account.getBalance()); // returns 20.0
}
}
The output after the main class is executed is as follows: