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: getNunber(), getBalance(), setBalance (double
newBalance) . There is no setNunber() - once an account is created,
its account number cannot change.
Now implement these methods: void deposit (double
anount) and void withdraw(double anount). 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.
The 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)
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. Modify toString() to include the interest rate and annual interest.
The 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 apr and creditLimit
Override the withdrar() function so that you can have a negative balance. If a withdrawal would push you over the credit limit, leave the balance untouched. Examples:
• If your balance is $300 with a credit limit of $700, you can withdraw $900 (leaving a balance of $-600).
• If your balance is $-300 with a credit limit of $700, you can withdraw $350 (leaving a balance of $-650).
. If your balance is $-300 with a credit limit of $700, you can not withdraw $500, because that would then owe $800, which is more than your $700 limit.
In short, the maximum amount you can withdraw is your current balance plus the credit limit.
Add a calculatePaynent () method that works as follows: • If the balance is positive, the minimum amount you have to pay
on your card per month is zero.
• Otherwise, your monthly payment is the minimum of 20 and (apr/12) - (-balance)
Modify tostring() to include the interest rate, credit limit, and monthly payment.
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 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-
$5,000.00, an APR of 7%, and a credit limit of $10,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()
Here is some sample output:
Account: 1866
Balance: $4852.0e
Account: 30507
Balance: $1852.0e
Interest Rate: 1.50%
Annual Interest: $27.78
Account: 51782737
Balance: $4352.0e
Interest Rate: 8.00%
Credit Linit: $1eee.a
Monthly Payment: se.e
Account: 629553328
Balance: $-1148.0e
Interest Rate: 7.50%
Credit Linit: sseee.a
Monthly Payment: $7.18
Account: 4977201043
Balance: $-7648.ee
Interest Rate: 7.08%
Credit Linit: s1eeee.oe
Monthly Payment: $20.08
//Account.java
public class Account {
private long number;
private double balance;
public Account() {
number=0;
balance=0.0;
}
public Account(long number, double balance) {
this.number = number;
this.balance = balance;
}
// getters and setters
public long getNumber() {
return number;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
// method to deposit amount
public void deposit(double amount) {
balance=balance+amount;
}
// method to withdraw amount
public boolean withdraw(double amount) {
if(balance>=amount) {
balance=balance-amount;
return true;
}else
return false;
}
@Override
public String toString() {
return "Account Number= " + number + "\nBalance= " + balance;
}
}
//Account.java ends here
//SavingAccount.java
public class SavingAccount extends Account {
private double apr;
public SavingAccount() {
super();
apr=0;
}
public SavingAccount(long number, double balance, double apr) {
super(number, balance); // call super class constructor
this.apr = apr;
}
public double getApr() {
return apr;
}
public void setApr(double apr) {
if(apr>0.0)
this.apr = apr;
}
public double calculateInterest(){
return getBalance()*apr/100;
}
public String toString() {
return "Account Number= " + getNumber() + "\nBalance= " + getBalance()+"\nInterest rate= "+apr+"\nAnnualInterest= "+calculateInterest();
}
}
//SavingAccount.java ends here
//CreditCardAccount.java
public class CreditCardAccount extends Account {
// instance variables
private double apr;
private double creditLimit;
public CreditCardAccount() {
super();
apr=0;
creditLimit=0;
}
public CreditCardAccount(long number, double balance, double apr, double creditLimit) {
super(number, balance); // calls super class constructor
this.apr = apr;
this.creditLimit = creditLimit;
}
public double getApr() {
return apr;
}
public void setApr(double apr) {
if(apr>0)
this.apr = apr;
}
public double getCreditLimit() {
return creditLimit;
}
public void setCreditLimit(double creditLimit) {
if(creditLimit>0)
this.creditLimit = creditLimit;
}
public boolean withdraw(double amount) {
if(getBalance()+creditLimit>=amount) {
setBalance(getBalance()-amount);
return true;
}else {
return false;
}
}
public double calculatePayment() {
if(getBalance()>0)
return 0;
else if((apr/1200)*(-getBalance()) > 20) {
return 20;
}else
return ((apr/1200)*(-getBalance()));
}
public String toString() {
return "Account Number= " + getNumber() + "\nBalance= " + getBalance()+"\nInterest rate= "+apr+"\nCredit limit= "+creditLimit+"\nMonthly Payment: "+calculatePayment();
}
}
//CreditCardAccount.java ends here
//Tester.java
public class Tester {
public static void main(String[] args) {
// TODO Auto-generated method stub
Account acc[]=new Account[5];
// creating objects and assign to Account class refernece
acc[0]=new Account(1066, 7500);
acc[1]=new SavingAccount(30507, 4500, 1.5);
acc[2]=new CreditCardAccount(51782737, 7000.00, 8, 1000);
acc[3]=new CreditCardAccount(629553328, 1500.00, 7.5, 5000);
acc[4]=new CreditCardAccount(497720104, -5000, 7, 10000);
for(int i=0;i
acc[i].deposit(2134); // deposit amount
acc[i].withdraw(4782); // withdraw amount
System.out.println(acc[i].toString()+"\n");
// print account details
} } } //Tester.java ends here Output: