In: Computer Science
Account
class
Create a class named
Account
, which has the following private properties:
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 these methods:
void deposit(double amount)
and
void withdraw(double amount)
. For both these methods, if the
amount is less than zero, the account balance remains untouched.
For the
withdraw()
method, if the amount is greater than the
balance, it remains untouched.
Then, implement a
toString()
method that returns a string with the account
number and balance, properly labeled.
SavingsAccount
class
This class inherits from
Account
and adds a private
apr
property, which is the annual percentage rate for
interest. Write a no-argument constructor that sets the account
number, balanace, and APR to zero. Write a three-argument
constructor that takes an account number, balance, and interest
rate as a decimal (thus, a 3.5% interest rate is given as
0.035).
Modify
toString()
to include the interest rate.
Add a getter and setter method for
apr
. The setter should leave the APR untouched if given a
negative value.
Write a
calculateInterest()
method that returns the annual interest,
calculated as the current balance times the annual interest
rate.
CreditCardAccount
class
This class inherits from
Account
and adds a private
apr
property, which is the annual interest rate charged on
the balance. It also has a private
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 (thus, a 3.5%
interest rate is given as 0.035), and credit limit. Write getters
and setters for the
apr
and
creditLimit
.
Modify
toString()
to include the interest rate 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. Examples:
In short, the maximum amount you can withdraw is your current balance plus the credit limit.
Add a
calculatePayment()
method that works as follows:
Write a program named
TestAccounts
that creates an array of five
Account
objects:
Account
number 1066 with a balance of $7,500.
SavingsAccount
number 30507 with a balance of $4,500 and an
APR of 1.5%
CreditCardAccount
number 51782737 with a balance of
$7,000.00, APR of 8%, and credit limit of $1000.00
CreditCardAccount
number 629553328 with a balance of
$1,500.00, an APR of 7.5%, and a credit limit of $5,000
CreditCardAccount
number 4977201043 with a balance of
-$5,000.00, an APR of 7%, and a credit limit of $10,000Your program will use a loop to do the following for each account:
toString()
.
Here is some sample output:
Account: 1066 Balance: $4852.00 Account: 30507 Balance: $1852.00 Interest Rate: 1.50% Annual Interest: $27.78 Account: 51782737 Balance: $4352.00 Interest Rate: 8.00% Credit Limit: $1000.00 Monthly Payment: $0.00 Account: 629553328 Balance: $-1148.00 Interest Rate: 7.50% Credit Limit: $5000.00 Monthly Payment: $7.18 Account: 4977201043 Balance: $-7648.00 Interest Rate: 7.00% Credit Limit: $10000.00 Monthly Payment: $20.00
Put the
Account
,
SavingsAccount
, and
CreditCardAccount
classes in the
TestAccounts.java
file rather than creating separate files.
Please submit only one file. Upload the
TestAccounts.java
file here.
public class TestAccounts {
public static void main(String[] args) {
// creating an array of five
Account objects
Account accounts[] = {new
Account(1066,7500),
new SavingsAccount(30507,4500,0.015),
new
CreditCardAccount(51782737,7000,0.08,1000),
new
CreditCardAccount(629553328,1500,0.075,5000),
new
CreditCardAccount(4977201043L,-5000,0.07,10000)};
//using a loop to deposit, withdraw
and print for each account
for(int i=0; i<accounts.length;
i++) {
accounts[i].deposit(2134);
accounts[i].withdraw(4782);
System.out.println("\n" + accounts[i].toString());
}
}
// --------------------------Account
class--------------------------------
public static class Account {
private long number;
private double balance;
public Account() {
number =
0;
balance =
0;
}
public Account(long number, double
balance) {
this.number =
number;
this.balance =
balance ;
}
public long getNumber() {
return
number;
}
public double getBalance() {
return
balance;
}
public void setBalance(double
newBalance) {
balance =
newBalance;
}
public void deposit(double amount)
{
if(amount >=
0)
// else leave account intact
balance += amount;
}
public void withdraw(double amount)
{
if(amount >=
0 && amount <= balance) // else leave
account intact
balance -= amount;
}
public String toString() {
return "Account:
" + number + "\nBalance: $" + String.format("%.2f",balance);
}
}
// --------------------------SavingsAccount
class--------------------------------
public static class SavingsAccount extends
Account{
private double apr;
public SavingsAccount() {
super(0,0);
// Parent
constructor
apr = 0;
}
public SavingsAccount(long number,
double balance, double apr) {
super(number,balance);
// Parent constructor
this.apr =
apr;
}
public String toString() {
return
super.toString() + "\nInterest Rate: " +
String.format("%.2f",apr*100) + "%";
}
public double getApr() {
return
apr;
}
public void setApr(double apr)
{
if(apr>=0)
// else leave account
intact
this.apr = apr;
}
public double calculateInterest()
{
return
getBalance() * apr;
}
}
// --------------------------CreditCardAccount
class--------------------------------
public static class CreditCardAccount extends
Account{
private double apr;
private double creditLimit;
public CreditCardAccount() {
super(0,0);
// Parent constructor
apr = 0;
creditLimit =
0;
}
public CreditCardAccount(long
number, double balance, double apr, double creditLimit) {
super(number,balance);
this.apr =
apr;
this.creditLimit
= creditLimit;
}
public double getApr() {
return
apr;
}
public void setApr(double apr)
{
this.apr =
apr;
}
public double getCreditLimit()
{
return
creditLimit;
}
public void setCreditLimit(double
creditLimit) {
this.creditLimit = creditLimit;
}
public String toString() {
return
super.toString() +
"\nInterest Rate: " +
String.format("%.2f",apr*100) + "%" +
"\nCredit Limit: $" +
creditLimit +
"\nMonthly Payment: $" +
String.format("%.2f",calculatePayment());
}
@Override
public void withdraw(double amount)
{
if(amount >=0
) {
// else leave account
intact
double withdrawLimit = getBalance() +
creditLimit;
if(amount <= withdrawLimit)
// else leave account intact
setBalance( getBalance() -
amount );
}
}
public double calculatePayment()
{
if(getBalance()
> 0)
return 0;
else
return Math.min( 20, (apr/12) * -getBalance() );
// choose the smallest of the two
}
}
} // TestAccounts class
------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,
IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~