Quote
Balance: 2578.9246
Charges: 0.0
Number of Transactions: 1.0
Balance: 2481.115
Charges: 0.0
Number of Transactions: 2.0
Balance: 2574.152
Charges: 0.0
Number of Transactions: 3.0
Balance: 2545.7878
Charges: 0.0
Number of Transactions: 4.0
Balance: 2576.6392
Charges: 0.0
Number of Transactions: 5.0
Balance: 2490.1394
Charges: 0.0
Number of Transactions: 6.0
Balance: 2528.9172
Charges: 0.0
Number of Transactions: 7.0
Balance: 2439.5935
Charges: 0.0
Number of Transactions: 8.0
Balance: 2497.9705
Charges: 0.0
Number of Transactions: 9.0
Balance: 2435.6626
Charges: 0.0
Number of Transactions: 10.0
Charges: 0.0
Number of Transactions: 1.0
Balance: 2481.115
Charges: 0.0
Number of Transactions: 2.0
Balance: 2574.152
Charges: 0.0
Number of Transactions: 3.0
Balance: 2545.7878
Charges: 0.0
Number of Transactions: 4.0
Balance: 2576.6392
Charges: 0.0
Number of Transactions: 5.0
Balance: 2490.1394
Charges: 0.0
Number of Transactions: 6.0
Balance: 2528.9172
Charges: 0.0
Number of Transactions: 7.0
Balance: 2439.5935
Charges: 0.0
Number of Transactions: 8.0
Balance: 2497.9705
Charges: 0.0
Number of Transactions: 9.0
Balance: 2435.6626
Charges: 0.0
Number of Transactions: 10.0
Design a class named Bank Account to hold the following data for a bank account:
- Balance
- Number of deposits this month
- Number of withdrawals
- Annual interest rate
- Monthly service charges
The class should have the following methods:
- Constructor: The constructor should accept arguments for the balance and annual interest rate
- deposit: A method that accepts an argument for the amount of the deposit. The method should add the argument to the account balance. It should also increment the variable holding the number of deposits.
- withdraw: A method that accepts an argument for the amount of the withdrawal. The method should subtract the argument from the balance. It should also increment the variable holding the number of withdrawals.
- calcInterest: A method that updates the balance by calculating the monthly interest earned by the account, and adding this interest to the balance. This is performed by the following formulas:
Monthly Interest Rate = (Annual Interest Rate / 12)
Monthly Interest = Balance * Monthly Interest Rate
Balance = Balance + Monthly Interest - monthlyProcess: A method that subtracts the monthly service charges from the balance, calls the calcInterest method, and then sets the variables that hold the number of withdrawals, number of deposits, and monthly service charges to zero.
Next, design a SavingsAccount class that extends the BankAccount class. The SavingsAccount class should have a status field to represent an active or inactive account. If the balance of a savings account falls below $25, it becomes inactive. (The status field could be a boolean variable.) No more withdrawals may be made until the balance is raised above $25, at which time the account becomes active again. The savings account class should have the following methods:
- withdraw: A method that determines whether the account is inactive before a withdrawal is made. (No withdrawal will be allowed if the account is not active.) A withdrawal is then made by calling the superclass version of the method.
- deposit: A method that determines whether the account is inactive before a deposit is made. If the account is inactive and the deposit brings the balance above $25, the account becomes active again. A deposit is then made by calling the superclass version of the method.
- monthlyProcess: Before the superclass method is called, this method checks the number of withdrawals. If the number of withdrawals for the month is more than 4, a service charge of $1 for each withdrawal above 4 is added to the superclass field that holds the monthly service charges. (Don't forget to check the account balance after the service charge is taken. If the balance falls below $25, the account becomes inactive.
In your main, create a SavingsAccount object by making up a starting balance and rate and write a loop that iterates 5 times and does a random deposit and withdrawal (values 1 - 100) in each iteration on the account using your methods. Print out the status of the account(balance, charges, number of transactions) after each transaction.
BankAccount Class
public class BankAccount
{
protected float balance;
protected float numDeposits;
protected float numWithdrawals;
protected float annualRate;
protected float monthlyServCharg;
public BankAccount()
{
balance = 0;
numDeposits = 0;
numWithdrawals = 0;
annualRate = 0;
monthlyServCharg = 0;
}
public BankAccount(float bal, float rate)
{
balance = bal;
annualRate = rate;
}
public void deposit(float amount)
{
balance += amount;
numDeposits++;
}
public void withdraw(float amount)
{
balance -= amount;
numDeposits++;
}
public void calcInterest()
{
float monRate = annualRate / 12;
float monInt = balance * monRate;
balance += monInt;
}
public void monthlyProcess()
{
balance -= monthlyServCharg;
calcInterest();
numWithdrawals = 0;
numDeposits = 0;
monthlyServCharg = 0;
}
}
SavingsAccount Class
public class SavingsAccount extends BankAccount
{
private boolean active;
public SavingsAccount(float bal, float rate)
{
super(bal, rate);
if(bal < 25)
active = false;
else
active = true;
}
public void withdraw(float amount)
{
if(active)
super.withdraw(amount);
}
public void deposit(float amount)
{
if(!active)
{
if(amount + balance < 25)
return;
}
super.deposit(amount);
}
public void monthlyProcess()
{
if(numWithdrawals > 4)
{
monthlyServCharg += numWithdrawals - 4;
}
super.monthlyProcess();
if(balance < 25)
active = false;
}
public void printStatus()
{
System.out.println("Balance: " + balance);
System.out.println("Charges: " + monthlyServCharg);
System.out.println("Number of Transactions: " + (numDeposits + numWithdrawals));
System.out.println();
}
}
Demo Class
public class BankDemo
{
public static void main(String[] args)
{
float bal = (float)(Math.random() * 5000 + 1000);
float rate = 12;
SavingsAccount sv = new SavingsAccount(bal, rate);
for(int i = 0; i < 5; i++)
{
float dep = (float)(Math.random() * 100);
sv.deposit(dep);
sv.printStatus();
float with = (float)(Math.random() * 100);
sv.withdraw(with);
sv.printStatus();
}
}
}

New Topic/Question
Reply


MultiQuote



|