Account class Methods: processDeposit ( ) accepts a single double parameter cont
ID: 662266 • Letter: A
Question
Account class
Methods:
processDeposit ( ) accepts a single double parameter containing the deposit amount. Updates the balance by adding the deposit amount to the balance.
processWithdrawal ( ) accepts a single double parameter containing the withdrawal amount. Updates the balance by subtracting the withdrawal amount for the balance. Note: For now do not worry about negative balances.
Sets and gets for name, account id
getBalance to return the balance
Attributes: String Account Name
Integer Account ID
Double balance
Constructor
Initialize balance to zero
displayAccount this method prints all account information, here is an example:
Account: 2345
Name: Brandon Miller
Balance: $120.55
Test and make sure account class works before continuing.
The CheckingAcct class which is a derived class of the Account class.
It has no additional attributes. Overdraft withdrawls are allowed but the user is charged a $10 overdraft fee. No interest is earned.
Attributes:
Inherits all the attributes of the Account class.
Methods:
processWithdrawl() if the withdrawl causes insufficient funds, subtract $10 and display a message overdraft fee charged
displayAccount this method prints all account information, here is an example:
Account type: Checking
Account: 2345
Name: Brandon Miller
Balance: $120.55
The SavingsAcct class which is a derived class of the Account class.
Attributes:
Inherits all the attributes of the Account class.
There are no other attributes.
Methods:
calcInterest ( ) If the account balance is greater than or equal to 5000.00 an interest rate of 4% or .04 is applied to increase the balance. If the balance is greater than or equal to 3000.00 an interest rate of 3% or .03 is applied to increase the balance. Otherwise use an interest rate of 2%. This method should be called by getBalance so that interest is added when the balance is returned.
Constructor:
Inherited from the Account class. (no code needed)
displayAccount this method prints all account information, here is an example:
Account type: Savings
Account: 2355
Name: Brandon Miller
Interest earned: $5.02
Balance: $250.75
calcInterest should be called by getBalance so that interest is added when the balance is displayed here.
Main:
Create an arrayList of Account type that can handle Checking and Savings accounts (this is the bank).
Prompt for name, account number (or assign account number in main) and add at least 1 checking object and 1 savings object to the bank array.
Now use a loop to process all accounts:
Make an initial deposit in each account (prompt for amount).
Display balance
Make a withdrawl with sufficient funds (prompt for amount)
Display balance
Make a withdrawl with insufficient funds (prompt for amount)
Display the account information using displayAccount.
See example output below:
Creating a checking account, enter name: Brandon Miller
Your checking account number is 1000
Creating a savings account, enter name: Brittany Jones
Your savings account number is 1001
Processing account: 1000
Amount to deposit: 50
Balance is 50.00
Amount to withdraw: 25
Balance is 25.00
Amount to withdraw: 30
$10 Overdraft fee charged!
Balance is -15.00
Account type: Checking
Account: 1000
Name: Brandon Miller
Balance: $-15.00
Processing account: 1001
Amount to deposit: 500
Balance is 500.00
Amount to withdraw: 150
Balance is 350.00
Amount to withdraw: 500
Insufficient funds!
Balance is 350.00
Account type: Savings
Account: 1001
Name: Brittany Jones
Interest earned: $7.00
Balance: $357.00
Explanation / Answer
Program:
Import java.io.*;
Import java.util.*;
//class Account
class Account
{
public string AccountName;
public int AccountID;
public double balance;
//constructor
public Account()
{
balance=0.0;
}
public void processDeposit(Scanner scan)
{
double amount;
Sytem.out.println(" Enter the deposit amount:");
amount=scan.nextDouble();
balance=balance+amount;
}
public void processWithdrawal(Scanner scan)
{
double amount;
Sytem.out.println("Amount to withdraw");
amount=scan.nextDouble();
balance=balance-amount;
System.out.println("balance is"+balance);
}
public void setAccountname(string name)
{
Accountname=name;
}
public void setAccountID(int id)
{
AccountID=id;
}
public string getName()
{
return AccountName;
}
public int getId()
{
return AccountID;
}
public double getBalance()
{
return balance;
}
public void displayAccount()
{
Sytem.out.println("Account: "+getId());
Sytem.out.println("Name: "+getName());
Sytem.out.println("Balance: "+getBalance());
}
}
//class Checking Account
class CheckingAcct extends Account
{
Public CheckingAcct()
{ }
public void processWithdrawal(Scanner scan)
{
double amount;
System.out.println("Amount to withdraw");
amount=scan.nextDouble();
if(amount>getBalance())
{
balance=balance-amount-10;
System.out.println("Overdraft fee charged");
System.out.println("balance is"+balance);
}
else
{
balance=balance-amount;
System.out.println("balance is"+balance);
}
}
public void displayAccount()
{
Sytem.out.println("Account Type:Checking");
Sytem.out.println("Account: "+getId());
Sytem.out.println("Name: "+getName());
Sytem.out.println("Balance: "+getBalance());
}
}
class SavingsAcct extends Account
{
Public SavingsAcct()
{ }
public void processWithdrawal(Scanner scan)
{
double amount;
System.out.println("Amount to withdraw");
amount=scan.nextDouble();
if(amount>getBalance())
{
System.out.println("Insufficient Funds");
System.out.println("balance is"+balance);
}
else
{
balance=balance-amount;
System.out.println("balance is"+balance);
}
}
//calculating Interest
public double calcInterest()
{
double rate;
if(balance>=5000.00)
rate=balance*0.04;
else if(balance>=3000.00)
rate=balance*0.03;
else
rate=balance*0.02;
return rate;
}
public double getBalance()
{
return balance+calcInterest();
}
//display the account information
public void displayAccount()
{
Sytem.out.println("Account Type:Saving");
Sytem.out.println("Account: "+getId());
Sytem.out.println("Name: "+getName());
Sytem.out.println("Interest Earned:"+calcInterest());
Sytem.out.println("Balance: "+getBalance());
}
}
//test class
public class BankImplementation
{
//main method
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in));
//creating ArrayList of Account type objects
ArrayList<Account> acct=new ArrayList();
//creating checking account
CheckingAcct ca=new CheckingAcct();
System.out.println("Creating checking account, Enter name:");
String name=scan.nextLine();
ca.setAccountName(name);
ca.setAccountID(1000);
acct.add(ca);
//creating Savings Account
SavingsAcct sa=new SavingsAcct();
System.out.println("Creating savings Account, enter name");
String name1=scan.nextLine();
sa.setAccountName(name1);
sa.setAccountID(1001);
acct.add(sa);
//processing both checking account and savings Account
for(int i=0;i<acct.size();i++)
{
System.out.println("Processing account:"+acct.get(i).getAccountID());
acct.get(i).processDeposit(scan);
acct.get(i).processWithdrawal(scan);
acct.get(i).displayAccount();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.