JAVA Programming....An account has the propteries account number, balance, annua
ID: 658035 • Letter: J
Question
JAVA Programming....An account has the propteries account number, balance, annual interest rate, date created, and methods to deposit and withdraw funds. Create two subclassess, for checkings and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.
Here is the code I have for the original account class...I only need the two subclasses now created from this code..nothing fancy
//Ghost_Account.java
//Header file section
import java.util.Date;
//Create a a class named Ghost_Account
public class Ghost_Account
{
//Declare variables
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
// Constructor
Ghost_Account()
{
//A private int data field named id
//for the account (default 0)
id = 0;
//A private double data field named balance
//for the account(default 0)
balance = 0;
//A private double data field named annualInterestRate
//that stores the current interest rate (default 0).
annualInterestRate = 0;
// A private Date data field named dateCreated
//that stores the date when the account was created
dateCreated = new Date();
}
// Parameterized Constructor
//A constructor that creates an account
//with the specified id and initial balance.
Ghost_Account(int ID, double BALANCE)
{
id = ID;
balance = BALANCE;
dateCreated = new Date();
}
//The accessor and mutator methods for id,
//balance, and annualInterestRate
// Getter method for Id
public int getId()
{
return id;
}
// Getter method for Balance
public double getBalance()
{
return balance;
}
// Getter method for Annual Interest Rate
public double getAnnualInterestRate()
{
return annualInterestRate;
}
// Setter method for Id.
public void setId(int id)
{
this.id = id;
}
// Setter method for Balance
public void setBalance(double balance)
{
this.balance = balance;
}
// Setter method for Annual Interest Rate
public void setAnnualInterestRate(double annualInterestRate)
{
this.annualInterestRate = annualInterestRate;
}
// The accessor method for dateCreated.
public Date dateCreated()
{
return dateCreated;
}
// A method named getMonthlyInterestRate()
//that returns the monthly interest rate
public double getMonthlyInterestRate()
{
return annualInterestRate / 12.0;
}
// A method named withdraw that withdraws
//a specified amount from the account.
public void withdraw(double amount)
{
balance = balance - amount;
}
// A method named deposit that deposits
//a specified amount to the account.
public void deposit(double amount)
{
balance = balance + amount;
}
}
//Headers file section
import java.sql.Date;
import java.text.DecimalFormat;
//main class
public class Ghost_AccountDemo
{
// main method.
public static void main(String[] args)
{
//Formating the data using the DecimalFormat object.
DecimalFormat df = new DecimalFormat("##.##");
//create an Account object with an account ID of 1122,
//a balance of $20,000,
Ghost_Account account = new Ghost_Account(1122,20000);
//Set an annual interest rate of 4.5%
account.setAnnualInterestRate(4.5);
//The withdraw method $2,500 is withdrawn,
//and the deposit method is used to deposit $3,000
account.withdraw(2500);
account.deposit(3000);
//Display output
System.out.println("Final Balance : " + df.format(account.getBalance()));
System.out.println("Monthly interest : " +df.format(account.getMonthlyInterestRate()));
System.out.println("Account created date : " +account.dateCreated().toString());
}
}
Explanation / Answer
//Ghost_Account.java
import java.util.Date;
//Create a a class named Ghost_Account
public class Ghost_Account
{
//Declare variables
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
// Constructor
Ghost_Account()
{
//A private int data field named id
//for the account (default 0)
id = 0;
//A private double data field named balance
//for the account(default 0)
balance = 0;
//A private double data field named annualInterestRate
//that stores the current interest rate (default 0).
annualInterestRate = 0;
// A private Date data field named dateCreated
//that stores the date when the account was created
dateCreated = new Date();
}
// Parameterized Constructor
//A constructor that creates an account
//with the specified id and initial balance.
Ghost_Account(int ID, double BALANCE)
{
id = ID;
balance = BALANCE;
dateCreated = new Date();
}
//The accessor and mutator methods for id,
//balance, and annualInterestRate
// Getter method for Id
public int getId()
{
return id;
}
// Getter method for Balance
public double getBalance()
{
return balance;
}
// Getter method for Annual Interest Rate
public double getAnnualInterestRate()
{
return annualInterestRate;
}
// Setter method for Id.
public void setId(int id)
{
this.id = id;
}
// Setter method for Balance
public void setBalance(double balance)
{
this.balance = balance;
}
// Setter method for Annual Interest Rate
public void setAnnualInterestRate(double annualInterestRate)
{
this.annualInterestRate = annualInterestRate;
}
// The accessor method for dateCreated.
public Date dateCreated()
{
return dateCreated;
}
// A method named getMonthlyInterestRate()
//that returns the monthly interest rate
public double getMonthlyInterestRate()
{
return annualInterestRate / 12.0;
}
// A method named withdraw that withdraws
//a specified amount from the account.
public void withdraw(double amount)
{
balance = balance - amount;
}
// A method named deposit that deposits
//a specified amount to the account.
public void deposit(double amount)
{
balance = balance + amount;
}
}
----------------------------------------------------------------------------------------------------------------
//CheckingsAccount.java
//This calss extends the Ghost_Account class
public class CheckingsAccount extends Ghost_Account
{
//Set the overdraft limit as 50000
private int OVERDRAFT_LIMIT=50000;
/*Parameterized constructor that calls the super
class constructor that sets the id and balance
as members variables values for id and balance */
public CheckingsAccount(int ID, double BALANCE)
{
super(ID, BALANCE);
}
// A method named withdraw that withdraws
//a specified amount from the account.
public void withdraw(double amount)
{
//Add overdraft amount and balance
double totalAmount=OVERDRAFT_LIMIT+super.getBalance();
/*check if amount is less or equal to totalAmount
then get the balance amount from Ghost_Account
and call Ghost_Account withdraw method for balanace
*/
if(amount<totalAmount)
super.withdraw(amount);
else if(amount<=totalAmount)
{
//Call getBalance of Ghost_Account
double availablaBal=super.getBalance();
//call withdraw avaliable balance
super.withdraw(availablaBal);
//Then call the overdraft amount from withdraw method
double overdraftBal=amount-availablaBal;
super.withdraw(overdraftBal);
}
//Chekcing if balance is negative
else if(super.getBalance()<=(-1)*OVERDRAFT_LIMIT)
{
//print a warning message
System.out.println("OVERDRAFT LIMIT IS REACHED.");
}
else
System.out.println("No sufficient funds to withdraw");
}
}
----------------------------------------------------------------------------------------------------------------
//SavingsAccount.java
//This calss extends the Ghost_Account class
public class SavingsAcccount extends Ghost_Account
{
//Parameterized constructor that calls the super
//class constructor that sets the id and balance
//as members variables values for id and balance
public SavingsAcccount(int ID, double BALANCE)
{
super(ID, BALANCE);
}
//The method withdraw that accepts the amount
//as input argument and checks that balance amount
//is greater than or equal then calls the parent
//class withdraw method to withdraw amount
//otherwise prints a warning message.
public void withdraw(double amount)
{
if(getBalance()>=amount)
//calling Ghost_Account class withdraw method
super.withdraw(amount);
else
System.out.println("No sufficienet funds to withdraw");
}
}
----------------------------------------------------------------------------------------------------------------
/*
* The java program that tests the classes Ghost_Account as parent class
* for CheckingAccount and SavingsAccount.
* Calls the methods withdraw methods to check overdraft limit on
* checkings account object and calls savings account objects
* withdraw method and print the balance.
* */
//Ghost_AccountDemo.java
//Headers file section
import java.text.DecimalFormat;
//main class
public class Ghost_AccountDemo
{
// main method.
public static void main(String[] args)
{
//Formating the data using the DecimalFormat object.
DecimalFormat df = new DecimalFormat("##.##");
//create an Account object with an account ID of 1122,
//a balance of $20,000,
Ghost_Account account = new Ghost_Account(1122,20000);
//Set an annual interest rate of 4.5%
account.setAnnualInterestRate(4.5);
//The withdraw method $2,500 is withdrawn,
//and the deposit method is used to deposit $3,000
account.withdraw(2500);
account.deposit(3000);
//Display output
System.out.println("Ghost_Account Details");
System.out.println("Final Balance : " + df.format(account.getBalance()));
System.out.println("Monthly interest : " +df.format(account.getMonthlyInterestRate()));
System.out.println("Account created date : " +account.dateCreated().toString());
//Create an instance of CheckingsAccount with ID and balance
//This class has overdraft limit is set to 50000
CheckingsAccount checkings=new CheckingsAccount(1123, 5000);
//call withdraw of 55000
//Since overdraft limit is 50000 is availbal this amount is withdrawn
checkings.withdraw(55000);
//Display balance in negative since overdraft limit is negative balnace
//indicates that the overdraft amount
System.out.println("CheckingsAccount Details");
System.out.println("Final Balance : " + df.format(checkings.getBalance()));
//call withdraw method with some amount
//This method will give a warning message that overdraft limit is reached.
checkings.withdraw(45000);
//Create an intance of SavingsAccount with id and balance
SavingsAcccount savings=new SavingsAcccount(1001, 15000);
//call the methods deposit
savings.deposit(2000);
System.out.println("SavingsAccount Details");
System.out.println("Final Balance : " + df.format(savings.getBalance()));
//call withdraw method
savings.withdraw(25000);
}
}
----------------------------------------------------------------------------------------------------------------
Sample output:
Ghost_Account Details
Final Balance : 20500
Monthly interest : 0.38
Account created date : Wed May 06 07:17:50 IST 2015
CheckingsAccount Details
Final Balance : -50000
OVERDRAFT LIMIT IS REACHED.
SavingsAccount Details
Final Balance : 17000
No sufficienet funds to withdraw
Hope this helps you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.