Design and implement a BankAccount program, to find the account balance for a mo
ID: 3841648 • Letter: D
Question
Design and implement a BankAccount program, to find the account balance for a month. Each bank account has a number and holds the initial balance, amount of deposits, amount of withdrawals, annual interest rate, and a monthly service charges. The final balance for an account consists of whatever the original balance is, plus the interest earned by the account. The final balance can be found using the following equations:
Monthly Interest Rate = (Annual Interest Rate / 12)
Monthly Interest = (Initial Balance + Deposits – Withdrawals) * Monthly Interest
Final Balance = Initial Balance + Deposits - Withdrawals + Monthly Interest - Monthly Services Charges
Write a complete program that reads the account number, the initial balance, the monthly service charges, and the monthly transactions for an account from a file and calculates the following:
The total number of deposit transactions The total amount of deposits
The total number of withdrawal transactions The total amount of withdrawals
The ending balance.
The data file format is as follows:
# amount //account number
b amount //the initial balance
i amount //annual interest rate
c amount //monthly service charge d amount //deposit operation
w amount //withdraw operation ...
When the program runs, it should prompt the user for a data filename, and display all the information of an account on the screen.
Create a BankAccount class, and use an object of that class to read the data file, and find all the required information about that account. In this class create a constructor, destructor, getters and setters to all variables.
Keep your driver class (where the main() is) “clean”, this means your main() function should only have function calls through the BankAccount object, and no data file processing.
Explanation / Answer
public class SavingsDemo
{
public static void main(String[] args)
{
// Create a Decimalformat object for formatting output.
DecimalFormat dollar = new DecimalFormat("#,##0.00");
// Create a SavingsAccount object with a $100 balance,
// 3% interest rate, and a monthly service charge
// of $2.50.
SavingsAccount savings =
new SavingsAccount(100.0, 0.03, 2.50);
// Display what we've got.
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
// Make some deposits.
savings.deposit(25.00);
savings.deposit(10.00);
savings.deposit(35.00);
// Display what we've done so far.
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
// Make some withdrawals.
savings.withdraw(100.00);
savings.withdraw(50.00);
savings.withdraw(10.00);
// Display what we've done so far.
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
// Do the monthly processing.
savings.monthlyProcess();
// Display what we've done so far.
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
}
}
Balance: $100.00
Number of deposits: 0
Number of withdrawals: 0
Balance: $170.00
Number of deposits: 3
Number of withdrawals: 0
Balance: $20.00
Number of deposits: 3
Number of withdrawals: 2
Balance: $17.54
Number of deposits: 0
Number of withdrawals: 0
Process completed.
HERE ARE MY CODES:
public class SavingsAccount extends BankAccount
{
public SavingsAccount(double bal, double intRate, double mon)
{
super(bal, intRate, mon);
}
public boolean isActive()
{
return balance >= 50;
}
public void withdraw(double amount)
{
if (isActive() == false)
{
throw new IllegalStateException("This account is inactive.");
}
super.withdraw(amount);
}
public void monthlyProcess()
{
int costlyWithdrawals = numWithdrawals - 5;
if (costlyWithdrawals > 0)
{
monthlyServiceCharge += costlyWithdrawals * 0.5;
}
super.monthlyProcess();
}
}
public abstract class BankAccount
{
protected double balance;
protected int numDeposits;
protected int numWithdrawals;
protected double interestRate;
protected double monthlyServiceCharge;
public BankAccount(double bal, double intRate, double mon)
{
balance = bal;
interestRate = intRate;
monthlyServiceCharge = mon;
}
public void deposit(double amount)
{
balance += amount;
numDeposits++;
}
public void withdraw(double amount) {
balance -= amount;
numWithdrawals++;
}
protected void calcInterest()
{
double monthlyInterestRate = interestRate / 12;
double monthlyInterest = balance * monthlyInterestRate;
balance += monthlyInterest;
}
public void monthlyProcess()
{
balance -= monthlyServiceCharge;
calcInterest();
numDeposits = 0;
numWithdrawals = 0;
}
public void setMonthlyServiceCharges(double amount)
{
this.monthlyServiceCharge += amount;
}
public double getBalance()
{
return balance;
}
public int getNumDeposits()
{
return numDeposits;
}
public int getNumWithdrawals()
{
return numWithdrawals;
}
public double getInterestRate()
{
return interestRate;
}
public double getMonthlyServiceCharges()
{
return monthlyServiceCharge;
}
}
import java.text.DecimalFormat;
public class SavingsDemo
{
public static void main(String[] args)
{
// Create a Decimalformat object for formatting output.
DecimalFormat dollar = new DecimalFormat("#,##0.00");
// Create a SavingsAccount object with a $100 balance,
// 3% interest rate, and a monthly service charge
// of $2.50.
SavingsAccount savings =
new SavingsAccount(100.0, 0.03, 2.50);
// Display what we've got.
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
// Make some deposits.
savings.deposit(25.00);
savings.deposit(10.00);
savings.deposit(35.00);
// Display what we've done so far.
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
// Make some withdrawals.
savings.withdraw(100.00);
savings.withdraw(50.00);
savings.withdraw(10.00);
// Display what we've done so far.
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
// Do the monthly processing.
savings.monthlyProcess();
// Display what we've done so far.
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
}
}
DESIRED OUT:
Balance: $100.00
Number of deposits: 0
Number of withdrawals: 0
Balance: $170.00
Number of deposits: 3
Number of withdrawals: 0
Balance: $20.00
Number of deposits: 3
Number of withdrawals: 2
Balance: $17.54
Number of deposits: 0
Number of withdrawals: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.