In: Computer Science
The Account class
Create a class named Account, which has the following private properties:
number: long
balance: double
Create a no-argument constructor that sets the number and balance to zero.
Create a two-parameter constructor that takes an account number and balance.
First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change.
Now implement deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero, the account balance remains untouched. If the amount is greater than the balance, withdraw() leaves the balance untouched.
Then, implement a toString method that returns a string with the account number and balance, properly labeled.
The SavingsAccount class
This class inherits from Account and adds an apr property, which is the annual percentage rate for interest. Write a no-argument constructor that sets the account number, balance, and APR to zero. Write a three-argument constructor that takes an account number, balance, and interest rate as a decimal.
Add a getter and setter method for apr. The setter should leave the APR untouched if given a negative value. Modify toString() to include the interest rate. Write a calculateInterest() method that returns the current balance times the annual interest rate.
The CreditCardAccount class
This class inherits from Account and adds an apr property, which is the annual interest rate charged on the balance. It also has a creditLimit property (double) which gives the credit limit for the card. Write a no-argument constructor that sets all the properties to zero. Write a four-argument constructor that takes an account number, balance, interest rate as a decimal, and credit limit.
Override the withdraw() function so that you can have a negative balance. If a withdrawal would push you over the credit limit, leave the balance untouched.
Add getters and setters for apr and creditLimit, leaving them untouched if the setter is given a negative value. Modify toString to include the interest rate and credit limit.
Add a calculatePayment() method that returns zero if the balance is positive and the minimum of 20 and ((apr/12)∗(-balance)) if the balance is negative.
The Test Program
Write a program named TestAccounts that creates an array of five Account objects:
An Account number 1066 with a balance of $7,500.
A SavingsAccount number 30507 with a balance of $4,500 and an APR of 1.5%
A CreditCardAccount number 51782737 with a balance of $7,000.00, APR of 8%, and a credit limit of $1000.00
A CreditCardAccount number 629553328 with a balance of $1,500.00, an APR of 7.5%, and a credit limit of $5,000
A CreditCardAccount number 4977201043 with a balance of zero, an APR of 7%, and a credit limit of $3,000
Your program will use a loop to do the following for each account:
Deposit $2,134.00
Withdraw $4,782.00
Print the account status using toString()
The program will then print the interest to be paid for the savings account and the monthly payment due for the credit card accounts.
Code Image:
Sample Output:
Code to Copy:
Account.java:
//Account.java
public class Account {
//Declare number as long type
private long number;
//Declare balance as double type
private double balance;
//Implementation of Default constructor
public Account() {
//Initialize number with 0
number = 0;
//Initialize balance with 0.0
balance = 0.0;
}
// Implementation of parameterized constructor
public Account(long number, double balance) {
this.number = number;
this.balance = balance;
}
// Implementation of getter function for number
public long getNumber() {
return number;
}
//Implementation of getBalance function
public double getBalance() {
return balance;
}
//Implementation of setBalance function
public void setBalance(double balance) {
this.balance = balance;
}
// Implementation of deposit function
public void deposit(double amount) {
balance = balance + amount;
}
// Implementation of withdraw function
public boolean withdraw(double amount) {
//check balance is greater than amount or not
if (balance >= amount)
{
balance = balance - amount;
//return true
return true;
}
else
{
//return false
return false;
}
}
@Override
//Implementation of toString function
public String toString() {
//return Account number and balance
return "Account Number= " + number + "\nBalance= " + balance;
}
}
SavingAccount.java:
//SavingAccount.java
//Implementation of SavingAccount class
public class SavingAccount extends Account {
// Declare apr as type of double
private double apr;
// Implementation of defualt constructor
public SavingAccount()
{
super();
//assign 0 to apr variable
apr = 0;
}
// Implementation of paramterized constructor
public SavingAccount(long number, double balance, double apr)
{
super(number, balance);
this.apr = apr;
}
//Implementation of getter method for apr
public double getApr()
{
//return apr
return apr;
}
//Implementation of setApr function with paramerter
//double type apr
public void setApr(double apr)
{
//check apr is greater than 0.0 or not
if (apr > 0.0)
{
this.apr = apr;
}
}
@Override
//Implementation of toString function
public String toString()
{
//return account number and balance,interest rate
return "Account Number= " + getNumber() + "\nBalance= " + getBalance() + "\nInterest rate= " + apr;
}
//Implementation of calculateInterest function
public double calculateInterest()
{
//return getBalance() * apr / 100 value
return getBalance() * apr / 100;
}
}
CreditCardAccount.java:
//CreditCardAccount.java
//Implementation of CreditCardAccount class which extends
//Account class
public class CreditCardAccount extends Account {
// Declare apr as type of double
private double apr;
// Declare creditLimit as type of double
private double creditLimit;
// Implementation of default constructor
public CreditCardAccount() {
super();
apr = 0;
creditLimit = 0;
}
// Implementation of paramterized constructor
public CreditCardAccount(long number, double balance, double apr, double creditLimit) {
super(number, balance);
this.apr = apr;
this.creditLimit = creditLimit;
}
// Implementation of setters function for apr
public double getApr() {
return apr;
}
//Implementation of setApr function with parameter type
//double
public void setApr(double apr) {
//check apr is greater than 0
if (apr > 0) {
this.apr = apr;
}
}
//Implementation of getCreditLimit function
public double getCreditLimit() {
return creditLimit;
}
//Implementation of setCreditLimit function with parameter
//type of creditLimit
public void setCreditLimit(double creditLimit) {
if (creditLimit > 0) {
this.creditLimit = creditLimit;
}
}
// Implementation of withdraw function function
public boolean withdraw(double amount) {
//check getBalance() + creditLimit is greater than equal to
//amount or not
if (getBalance() + creditLimit >= amount)
{
//call setBalance function
setBalance(getBalance() - amount);
//return true
return true;
}
else
{
//return false
return false;
}
}
@Override
//Implementation of toString function
public String toString()
{
return "Account Number= " + getNumber() + "\nBalance= " + getBalance() + "\nInterest rate= " + apr + "\nCredit limit= " + creditLimit;
}
// Implementation of caclulatePayment function
public double calculatePayment()
{
//check getBalance() is greater than 0 or not
if (getBalance() > 0)
{
//return 0
return 0;
}
else
{
//return (apr / 12) * (-getBalance()) value
return ((apr / 12) * (-getBalance()));
}
}
}
TestAccounts.java:
//TestAccounts.java
//Implementation of TestAccounts class
public class TestAccounts {
public static void main(String[] args) {
// Declare an Account class objects
Account accObject[] = new Account[5];
// creating an object and assign to Account class refernece
accObject[0] = new Account(1066, 7500);
// creating object and assign to SavingAccount class refernece
accObject[1] = new SavingAccount(30507, 4500, 1.5);
// creating object and assign to CreditCardAccount class refernece
accObject[2] = new CreditCardAccount(51782737, 7000.00, 8, 1000);
// creating object and assign to CreditCardAccount class refernece
accObject[3] = new CreditCardAccount(629553328, 1500.00, 7.5, 5000);
// creating object and assign to CreditCardAccount class refernece
accObject[4] = new CreditCardAccount(497720104, 0, 7, 3000);
//Iterate the loop
int k = 0;
//checj whether k is less than acc.length or not
while(k<accObject.length)
{
//call deposit function with value
accObject[k].deposit(2134);
//call withdraw function with value
accObject[k].withdraw(4782);
//Display statement for account details
System.out.println(accObject[k] + "\n");
//increment the k value
k++;
}
}
}