Sei sulla pagina 1di 7

CS480 (L)_HW#5_13306_AJAY KUMAR ATHICOM

Description:
(The Account class)
Design a class named Account that contains:
A private int data field named id for the account (default 0).
A private double data field named balance for the account (default 0).
A private double data field named annualInterestRate that stores the current
interest rate (default 0). Assume all accounts have the same interest rate.
A private Date data field named dateCreated that stores the date when the
account was created.
A no-arg constructor that creates a default account.
A constructor that creates an account with the specified id and initial balance.
The accessor and mutator methods for id, balance, and annualInterestRate.
The accessor method for dateCreated.
A method named getMonthlyInterestRate() that returns the monthly interest
rate.
A method named getMonthlyInterest() that returns the monthly interest.
A method named withdraw that withdraws a specified amount from the
account.
A method named deposit that deposits a specified amount to the account.
Draw the UML diagram for the class and then implement the class. (Hint: The
method getMonthlyInterest() is to return monthly interest, not the interest rate.
Monthly interest is balance * monthlyInterestRate. monthlyInterestRate is
annualInterestRate / 12. Note that annualInterestRate is a percentage, e.g.,like
3%. You need to divide it by 100.)
Write a test program that creates an Account object array of size 5. The account
IDs are 1000 to 5000, balance of $1,000 to 5000, and an annual interest rate of
3%. Use the withdraw method to withdraw $500, use the deposit method to
deposit $1000, and print the balance, the monthly interest, and the date when
this account was created for each account.
Submit your source code,UML diagram and screenshots in one WORD document
or zip file (if more files included) at Week 4 Assignment from course portal by
11:30pm the next class day.

CS480 (L)_HW#5_13306_AJAY KUMAR ATHICOM

UML Diagram:

package HW5;
import java.util.*;
public class Account
{
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;

CS480 (L)_HW#5_13306_AJAY KUMAR ATHICOM

public Account()
{
id=123131;
}
public Account(int id, double balance)
{
this.id=id;
this.balance=balance;
dateCreated=new Date();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}

CS480 (L)_HW#5_13306_AJAY KUMAR ATHICOM


public Date getDateCreated() {
return dateCreated;
}

public double getMonthlyInterestRate()


{
return (annualInterestRate/(12*100));
}
public double getMonthlyInterest()
{
return (getMonthlyInterestRate()*balance);
}
public void withdraw(double amount) {
balance= balance- amount;
}
public void deposit(double amount) {
balance= balance+amount;
}
}

package HW5;
public class TestAccount {
public static void main(String[] args) {
System.out
.println("Account Number\t Balance\t Monthly Interest\t Created Date ");
Account[] account = new Account[5];
for (int i = 0; i < account.length; i++) {
account[i] = new Account((i + 1) * 1000, (i + 1) * 1000);

CS480 (L)_HW#5_13306_AJAY KUMAR ATHICOM


account[i].setAnnualInterestRate(3);
System.out.println(account[i].getId() + "\t"
+ account[i].getBalance() + "\t"
+ account[i].getMonthlyInterest() + "\t"
+ account[i].getDateCreated());
}
for (int i = 0; i < account.length; i++) {
System.out.println("After withdrawing 500 from Account:"
+ account[i].getId());
account[i].withdraw(500);
System.out.println(account[i].getId() + "\t"
+ account[i].getBalance() + "\t"
+ account[i].getMonthlyInterest() + "\t"
+ account[i].getDateCreated());
}
for (int i = 0; i < account.length; i++) {
System.out.println("After depositing 1000 to Account:"
+ account[i].getId());
account[i].deposit(1000);
System.out.println(account[i].getId() + "\t"
+ account[i].getBalance() + "\t"
+ account[i].getMonthlyInterest() + "\t"
+ account[i].getDateCreated());
}
}
}

Output:
Account Number

Balance

Monthly Interest

Created Date

CS480 (L)_HW#5_13306_AJAY KUMAR ATHICOM


1000

1000.0

2.5

Mon Jun 15 21:09:16 PDT 2015

2000

2000.0

5.0

Mon Jun 15 21:09:16 PDT 2015

3000

3000.0

7.5

Mon Jun 15 21:09:16 PDT 2015

4000

4000.0

10.0

Mon Jun 15 21:09:16 PDT 2015

5000

5000.0

12.5

Mon Jun 15 21:09:16 PDT 2015

After withdrawing 500 from Account:1000


1000

500.0 1.25

Mon Jun 15 21:09:16 PDT 2015

After withdrawing 500 from Account:2000


2000

1500.0 3.75

Mon Jun 15 21:09:16 PDT 2015

After withdrawing 500 from Account:3000


3000

2500.0 6.25

Mon Jun 15 21:09:16 PDT 2015

After withdrawing 500 from Account:4000


4000

3500.0 8.75

Mon Jun 15 21:09:16 PDT 2015

After withdrawing 500 from Account:5000


5000

4500.0 11.25 Mon Jun 15 21:09:16 PDT 2015

After depositing 1000 to Account:1000


1000

1500.0 3.75

Mon Jun 15 21:09:16 PDT 2015

After depositing 1000 to Account:2000


2000

2500.0 6.25

Mon Jun 15 21:09:16 PDT 2015

After depositing 1000 to Account:3000


3000

3500.0 8.75

Mon Jun 15 21:09:16 PDT 2015

After depositing 1000 to Account:4000


4000

4500.0 11.25 Mon Jun 15 21:09:16 PDT 2015

After depositing 1000 to Account:5000


5000

5500.0 13.75 Mon Jun 15 21:09:16 PDT 2015

CS480 (L)_HW#5_13306_AJAY KUMAR ATHICOM

Potrebbero piacerti anche