Develop a banking program using an Account class Create Savi
Develop a banking program using an Account class. Create SavingsAccount and CheckingAccount objects. For each account, the user to use an amount of money to withdraw from the account using member function, debit, and an amount of money to deposit into the account using member function, credit. As you process each account, determine its type. If an Account is a SavingsAccount, calculate the amount of interest owed to the account using member function, calculateInterest, then add the interest to the account balance using member function, credit. After processing an account, print the updated account balance obtained by invoking base-class member function, getBalance. If an Account is a CheckingAccount, calculate the fees (2%) charged per transaction.
The Account Inheritance Hierarchy is as follows:
Create a base class Account and derived classes, SavingsAccount and CheckingAccount that inherit from class Account. Base class Account should include one data member of type double to represent the account balance. The class should provide at least three member functions:
Member function, credit, should add an amount to the current balance.
Member function, debit, should withdraw money from the account and ensure the debit amount does not exceed the account’s balance. If it does, the balance should be left unchanged and the function should print the message “Debit amount exceeded account balance.”
Member function, getBalance, should return the current balance.
The SavingsAccount should have a data member of type double indicating the interest rate assigned to the account. It also includes a member function, calculateInterest, that returns a double indicating the amount of interest earned by an account, by multiplying the interest rate by the account balance.
The CheckingAccount should have a data member of type constant double indicating the fee charged per transaction. Every time there is a debit or a credit, it should subtract the fee from the account balance. CheckingAccount’s debit function should charge a fee only if money is actually withdrawn (i.e., the debit amount does not exceed the account balance.)
Use constructors to initialize the data members and make sure the initial balance is greater than or equal to zero.
Write a program to test your classes designed above by using the following numbers and transactions:
-500.00
(Yes, the last deposit into savings is a negative amount).
Make sure you display the balance after each transaction.
If there is an error, make sure you state the error and what caused it.
Turn in your code and your output file with the transactions above showing deposits and withdraws including all error messages.
| Checking Starting Balance | 500.00 |
| Savings Starting Balance | 1000.00 |
| Withdraw from Checking | 230.00 |
| Withdraw from Savings | 100.85 |
| Deposit into Checking | 400.25 |
| Deposit into Checking | 250.00 |
| Deposit into Savings | 500.00 |
| Withdraw from Checking | 2000.00 |
| Withdraw from Savings | 1000.00 |
| Withdraw from Savings | 1500.00 |
| Deposit into Savings | -500.00 |
Solution
Hi,
Please see below the JAVA classes. Please comment for any queries/feedbacks.
Thanks,
Anita
Account.java
public class Account {
private double accountBalance;
public Account(){
this.accountBalance =0;
}
// to add an amount to the current balance
public void credit(double amount){
//debit amount does not exceed the account’s balance
if(amount>0){
this.accountBalance = this.accountBalance +amount;
}
else{
System.out.println(\"Amount is negative\");
}
}
//to withdraw money from the account
public void debit(double amount){
if(amount<=getAccountBalance()){
this.accountBalance = this.accountBalance -amount;
}
else{
System.out.println(\"Debit amount exceeded account balance.\");
}
}
//return the current balance
public double getBalance(){
return getAccountBalance();
}
public double getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(double accountBalance) {
this.accountBalance = accountBalance;
}
}
SavingsAccount.java
public class SavingsAccount extends Account {
private double interestRate;
public SavingsAccount(){
super();
this.interestRate = 0.1;
}
// to add an amount to the current balance
public void credit(double amount){
//debit amount does not exceed the account’s balance
if(amount>0){
//Adding the amount+interest for savings account
double bal = amount+this.getAccountBalance();
bal = bal +calculateInterest(amount);
this.setAccountBalance(bal);
}
else{
System.out.println(\"Amount is negative\");
}
}
// to return the amount of interest earned by an account
public double calculateInterest(double amount){
double amt = this.interestRate * amount;
return amt;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
}
CheckingAccount.java
public class CheckingAccount extends Account {
//service fee
private final double FEE = .02;
public CheckingAccount(){
super();
}
//to withdraw money from the account . This also deducts FEE from the balance
public void debit(double amount){
//debit amount does not exceed the account’s balance
if(amount<=getAccountBalance()){
//Deducting the fee
double bal = getAccountBalance()- amount;
bal = bal- (FEE*bal);
setAccountBalance(bal);
}
else{
System.out.println(\"Debit amount exceeded account balance.\");
}
}
}
AccountTester.java
public class AccountTester {
public static void main(String [] args){
Account checkingAccount = new CheckingAccount();
//Setting Initial Balance as 500
checkingAccount.setAccountBalance(500.00);
Account savingsAccount = new SavingsAccount();
//Setting Initial Balance as 1000.00
savingsAccount.setAccountBalance(1000.00);
//Testing transactions
System.out.println(\"Checking Starting Balance : \"+checkingAccount.getBalance());
System.out.println(\"Savings Starting Balance :\"+savingsAccount.getBalance());
System.out.println(\"Withdraw from Checking : 230.00 \");
checkingAccount.debit(230.00);
System.out.println(\"Checking Account Balance : \"+ checkingAccount.getBalance());
System.out.println(\"Withdraw from Savings : 100.85\");
savingsAccount.debit(100.85);
System.out.println(\"Savings Account Balance : \"+ savingsAccount.getBalance());
System.out.println(\"Deposit into Checking : 400.25 \");
checkingAccount.credit(400.25);
System.out.println(\"Checking Account Balance : \"+ checkingAccount.getBalance());
System.out.println(\"Deposit into Checking : 250.00\");
checkingAccount.credit(250.00);
System.out.println(\"Checking Account Balance : \"+ checkingAccount.getBalance());
System.out.println(\"Deposit into Savings : 500.00\");
savingsAccount.credit(500.00);
System.out.println(\"Savings Account Balance : \"+ savingsAccount.getBalance());
System.out.println(\"Withdraw from Checking : 2000.00\");
checkingAccount.debit(2000.00);
System.out.println(\"Checking Account Balance : \"+checkingAccount.getBalance());
System.out.println(\"Withdraw from Savings : 1000.00\");
savingsAccount.debit(1000.00);
System.out.println(\"Savings Account Balance : \"+savingsAccount.getBalance());
System.out.println(\"Withdraw from Savings : 1500.00\");
savingsAccount.debit(1500.00);
System.out.println(\"Savings Account Balance : \"+savingsAccount.getBalance());
System.out.println(\"Deposit into Savings : -500.00 \");
savingsAccount.credit(-500.00);
System.out.println(\"Savings Account Balance : \"+ savingsAccount.getBalance());
}
}
Sample output:
Checking Starting Balance : 500.0
Savings Starting Balance :1000.0
Withdraw from Checking : 230.00
Checking Account Balance : 264.6
Withdraw from Savings : 100.85
Savings Account Balance : 899.15
Deposit into Checking : 400.25
Checking Account Balance : 664.85
Deposit into Checking : 250.00
Checking Account Balance : 914.85
Deposit into Savings : 500.00
Savings Account Balance : 1449.15
Withdraw from Checking : 2000.00
Debit amount exceeded account balance.
Checking Account Balance : 914.85
Withdraw from Savings : 1000.00
Savings Account Balance : 449.1500000000001
Withdraw from Savings : 1500.00
Debit amount exceeded account balance.
Savings Account Balance : 449.1500000000001
Deposit into Savings : -500.00
Amount is negative
Savings Account Balance : 449.1500000000001




