JAVA Account class Methods: processDeposit ( ) – accepts a single double paramet
ID: 662267 • Letter: J
Question
JAVA
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
Explanation / Answer
/*The account clas that contains methods to set name,id and
*having methods to processdeposit and process withdraw amount from
*the account
**/
//Account.java
public class Account
{
//private members of Account class
private String name;
private int id;
private double balance;
//constructor that sets balance to zero
public Account()
{
balance=0;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setID(int id)
{
this.id=id;
}
public int getID()
{
return id;
}
public double getBalance()
{
return balance;
}
//processDeposit method
public void processDeposit(double balance)
{
this.balance+=balance;
}
//processWithdrawl method
public void processWithdrawl(double balance)
{
this.balance-=balance;
}
//print account details
public void displayAccount()
{
System.out.println("Account: "+id);
System.out.println("Name :"+name);
System.out.println("Balance:$"+balance);
}
}
----------------------------------------------------------------
//CheckingAcc.java
//The class CheckingAcc inherits the public methods of class Account
public class CheckingAcc extends Account
{
//10 $ fee for overdraft
private final int OVERDRAFT_FEE=10;
public CheckingAcc()
{
super();
}
/*The method processWithdrawl that takes the balance
and checks if the balance is less than getBalance method
and calls the parent class processWithdrawl with balance
add overdraft amount to deposit
and print the overdraft amount to user*/
public void processWithdrawl(double balance)
{
if(getBalance()<balance)
{
super.processWithdrawl(balance);
super.processDeposit(-OVERDRAFT_FEE);
System.out.println("$"+OVERDRAFT_FEE+" Overdraft fee charged!");
}
else
super.processWithdrawl(balance);
}
//display the CheckingAcc class object details
public void displayAccount()
{
System.out.println("Checking");
super.displayAccount();
}
}
----------------------------------------------------------------
//SavingsAcc.java
//The class SavingsAcc inherits the public methods of class Account
public class SavingsAcc extends Account
{
public SavingsAcc()
{
super();
}
//The method calcInterest calculates the interest earned on balance amount
public double calcInterest()
{
double interest=0;
if(getBalance()>=5000)
{
//calculating 4 percent interest on balance
interest=getBalance()*0.04;
//add interest amount to the balance
processDeposit(interest);
}
else if(getBalance()>=3000)
{
//calculating 3 percent interest on balance
interest=getBalance()*0.03;
//add interest amount to the balance
processDeposit(interest);
}
else
{
//calculating 2 percent interest on balance
interest=getBalance()*0.02;
//add interest amount to the balance
processDeposit(interest);
}
return interest;
}
//Override thee method processWithdrawl method for insufficient funds before
//withdrawls
public void processWithdrawl(double balance)
{
if(getBalance()<balance)
{
System.out.println("Insufficients funds");
}
else
super.processWithdrawl(balance);
}
//print saving account deatils
public void displayAccount()
{
System.out.println("Account type:"+"Savings");
super.displayAccount();
System.out.println("Interest earned:$"+calcInterest());
}
}
----------------------------------------------------------------
/*The java program tests the classes Acount ,CheckingAcc and SavingsAcc
* Create an ArrayList and add checking and saving account to the array list object
* and process the both account from the arraylist
* */
//Main.java
import java.util.ArrayList;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
String name;
//create an ArrayList object
ArrayList<Account>accountList=new ArrayList<Account>();
//create an instance of CheckingAcc and set account id =1000
CheckingAcc checking=new CheckingAcc();
checking.setID(1000);
//create an instance of SavingsAcc and set account id=1001
SavingsAcc saving=new SavingsAcc();
saving.setID(1001);
accountList.add(checking);
accountList.add(saving);
System.out.print("Creating a checking account, enter name:");
name=scanner.nextLine();
checking.setName(name);
System.out.println("Your checking account number is "+accountList.get(0).getID());
System.out.print("Creating a savings account, enter name:");
name=scanner.nextLine();
saving.setName(name);
System.out.println("Your savings account number is "+accountList.get(1).getID());
System.out.println("Processing account :1000");
System.out.print("Amount to deposit:");
double deposit=scanner.nextDouble();
accountList.get(0).processDeposit(deposit);
System.out.println("Balance is "+accountList.get(0).getBalance());;
System.out.print("Amount to withdraw :");
double withdraw=scanner.nextDouble();
accountList.get(0).processWithdrawl(withdraw);
System.out.println("Balance is "+accountList.get(0).getBalance());;
System.out.print("Amount to withdraw :");
withdraw=scanner.nextDouble();
accountList.get(0).processWithdrawl(withdraw);
System.out.println("Balance is "+accountList.get(0).getBalance());;
accountList.get(0).displayAccount();
System.out.println("Processing account:1001");
System.out.print("Amoun to deposit");
deposit=scanner.nextDouble();
accountList.get(1).processDeposit(deposit);
System.out.println("Balance is "+accountList.get(1).getBalance());;
System.out.print("Amount to withdraw :");
withdraw=scanner.nextDouble();
accountList.get(1).processWithdrawl(withdraw);
System.out.println("Balance is "+accountList.get(1).getBalance());;
System.out.print("Amount to withdraw :");
withdraw=scanner.nextDouble();
accountList.get(1).processWithdrawl(withdraw);
System.out.println("Balance is "+accountList.get(1).getBalance());;
accountList.get(1).displayAccount();
}
}
----------------------------------------------------------------
sample output:
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.0
Amount to withdraw :25
Balance is 25.0
Amount to withdraw :30
$10 Overdraft fee charged!
Balance is -15.0
Checking
Account: 1000
Name :Brandon Miller
Balance:$-15.0
Processing account:1001
Amoun to deposit500
Balance is 500.0
Amount to withdraw :150
Balance is 350.0
Amount to withdraw :500
Insufficients funds
Balance is 350.0
Account type:Savings
Account:1001
Name:Brittany Jones
Interest earned:$7.0
Hope this helps you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.