Question:
BankAccount and SavingsAccount Classes?
Pinkstars
2013-03-06 11:47:18 UTC
BankAccount and SavingsAccount Classes

Design an abstract class named BankAccount to hold the following data for a bank account:

-Account Name & Number
-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 bal- ance, 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 can 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. The 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 ser- vice 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.)

Write a driver program that creates an object or two of the SavingsAccount class and show sample runs.

Well I did the program and I am getting 46 errors and I tried to figure it out for 5 days and still no progress. I would greatly appreciate for someone to help me out. Please this is 50% of my grade. :(
Four answers:
MarkF120
2013-03-06 11:52:52 UTC
The problem is how your creating a new instance of SavingsAccount, the syntax is:



ClassName objName = new ClassName();



Then objName will be a new instance of whatever ClassName is, also your passing in 2 strings as parameters for your constructor, if you look at the SavingsAccount class you'll see the constructor expects a String and an int i.e



SavingsAccount objName = new SavingsAccount("string", 2343);



Also, the methods your calling in main, numDeposit() for example, should be getNumDeposit()... and the last method your calling returns void...so it can't be used in a System.out.println() like that. I think it needs to be called outside of a print statement.



This works:



public static void main(String[] args) {

SavingsAccount account = new SavingsAccount("James Howard", 1289789);



System.out.println("Enter your account balance $:");

System.out.println("Enter the annual interest rate:");

System.out.println("Enter the number of withdrawals:");

System.out.println("Enter the number of deposits:");



System.out.println(account);

System.out.println("Account Balance $:" + account.getBalance());

System.out.println("Withdraw:" + account.getNumWithdrawals());

System.out.println("Deposit:" + account.getNumDeposits());

account.monthlyProcess();

}



http://ideone.com/8kEPOv
Justin
2013-11-03 22:56:27 UTC
Here you go. The completed project:





import java.awt.HeadlessException;

import java.text.DecimalFormat;



import javax.swing.JOptionPane;





public class BankAccountAndSavingsAccountClasses {



/**

* Design an abstract class named BankAccount 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:

// See Book

*

* 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:

// See Book

*

*

*/

public static class SavingsAccount extends BankAccount {

protected boolean isActive = (super.balance > 25);



// Constructors

public SavingsAccount() {

super();

}

public SavingsAccount(double balance, double annualInterest) {

super(balance, annualInterest);

}



public double getMonthlyCharge() {

this.monthlyProcess();

return monthlyCharge;

}



// Monthly charge process.

public void monthlyProcess() {

if (super.numberOfWithdrawals > 4) {

super.monthlyCharge += super.numberOfWithdrawals - 4;

if (super.balance < 25) {

this.isActive = false;

}

}

}





// Withdraw.

public boolean withdraw(double amount) {

if (isActive) {

return super.withdraw(amount);

} else {

return false;

}

}



// Deposit

public boolean deposit(double amount) {

super.deposit(amount);

if ((!this.isActive) && super.balance > 25) {

this.isActive = true;

return true;

} else {

return false;

}



}

public boolean getIsActive() {

return isActive;

}

public void setActive(boolean isActive) {

this.isActive = isActive;

}



}



public static abstract class BankAccount {

protected double balance;

protected int numberOfDeposits;

protected int numberOfWithdrawals;

protected double annualInterest;

protected double monthlyCharge;



// Constructor accepts annual interest and

// the balance.

public BankAccount(double balance, double annualInterest) {

this.setAnnualInterest(annualInterest);

this.setBalance(balance);

}

// Default constructor.

public BankAccount() {}



// Monthly charge process.

public void monthlyProcess() {

this.balance -= this.monthlyCharge;

this.calcInterest();

this.monthlyCharge = 0;

this.numberOfDeposits = 0;

this.numberOfWithdrawals = 0;

}



// Calculate the interest.

protected void calcInterest() {

double monthlyInterestRate;

double monthlyInterest;



monthlyInterestRate = this.annualInterest / 12;

monthlyInterest = this.balance * monthlyInterestRate;

this.balance = this.balance + monthlyInterest;

}



// Withdraw.

public boolean withdraw(double amount) {

if ((this.balance > amount) && (amount > 0)) {

this.balance -= amount;

this.numberOfWithdrawals++;

return true;

} else {

return false;

}

}



// Deposit

public boolean deposit(double amount) {

if (amount > 0) {

this.balance += amount;

this.numberOfDeposits++;

return true;

} else {

return false;

}

}



// Accessors and mutators.

public double getBalance() {

return balance;

}



public void setBalance(double balance) {

this.balance = balance;

}



public int getNumDepositsThisMonth() {

return numberOfDeposits;

}



public void setNumDepositsThisMonth(int numDepositsThisMonth) {

this.numberOfDeposits = numDepositsThisMonth;

}



public double getAnnualInterest() {

return annualInterest;

}



public void setAnnualInterest(double annualInterest) {

this.annualInterest = annualInterest;

}



public double getMonthlyCharge() {

return monthlyCharge;

}



public void setMonthlyCharge(double monthlyCharge) {

this.monthlyCharge = monthlyCharge;

}

public boolean getIsActive() {

// TODO Auto-generated method stub

return (Boolean) null;

}

}



public static void main(String[] args) {

double balance = 0;

double annualInterest = 0;

String output;

String input;

DecimalFormat df = new DecimalFormat("#0.00");



do {

try {

output = "Enter your account balance $:";

input = JOptionPane.showInputDialog(output);



balance = Double.parseDouble(input);

if (balance < 0) {

throw new NumberFormatException();

}

break;

} catch (HeadlessException e) {

errorMsg();

} catch (NumberFormatException e) {

errorMsg();

}

} while (true);



do {

try {

output = "Please enter your annual interest rate: \n"

+ "EXAMPLE: A 4% interst rate is 0.04";

input = JOptionPane.showInputDialog(output);



annualInterest = Double.parseDouble(input);

if (annualInterest < 0) {

throw new NumberFormatException();

}

break;

} catch (HeadlessException e) {

errorMsg();

} catch (NumberFormatException e) {

errorMsg();

}

} while (true);



// Instantiate your object.

BankAccount savings = new SavingsAccount(balance, annualInterest);



do {

try {

output = "Enter the number of withdrawals:";

input = JOptionPane.showInputDialog(output);



savings.numberOfWithdrawals = Integer.parseInt(input);

if (savings.numberOfWithdrawals < 0) {

throw new NumberFormatException();

}

break;

} catch (HeadlessException e) {

errorMsg();

} catch (NumberFormatException e) {

errorMsg();

}

} while (true);



do {

try {

output = "Enter the number of deposits:";

input = JOptionPane.showInputDialog(output);



savings.numberOfDeposits = Integer.parseInt(input);

if (savings.numberOfDeposits < 0) {

throw new NumberFormatException();

}

break;

} catch (HeadlessException e) {

errorMsg();

} catch (NumberFormatException e) {

errorMsg();

}

} while (true);



// Add the interest to the balance:

savings.calcInterest();



output = "Account Balance with Interest: $" + df.format(savings.getBalance());

output += "\nMonthly Charge that will be deducted: $" + df.format(savings.getMonthlyCharge());

output += "\nAccount Status: ";

output += savings.getIsActive() ? "Is Active" : "Not Active";



JOptionPane.showMessageDialog(null, output);



}



private static void errorMsg() {

String output;

output = "Error: There was an error with your entry";

JOptionPane.showMessageDialog(null, output);

}



}



?
2016-12-14 09:19:57 UTC
Bank Account Class
CramShark.com
2014-02-12 20:08:28 UTC
The answer is available here including source and comments



http://www.cramshark.com/assignment/746/Java-BankAccount-and-SavingsAccount


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...