In: Computer Science
In-class exercise 2
Objective and Overview:
The exercises in this document is on Lecture 3
Exercise 1:
(The Account class) Design a class named Account that contains:
1. A private int data field named id for the account (default 0).
2. A private double data field named balance for the account (default 0).
3. A private static double data field named annualInterestRate that stores the current interest rate (default 0). Assume that all accounts have the same interest rate.
4. A private static int data field named transactions, that stores the number of transactions for all accounts.
5. A no-arg (default) constructor that creates a default account.
6. An initialization constructor that creates an account with the specified id and initial balance.
7. A copy constructor that creates an Account and initializes its attribute to the values of another account.
8. The accessor and mutator methods for id, balance, annualInterestRate, and transactions. 9. A method named getMonthlyInterestRate() that returns the monthly interest rate.
10. A method named getMonthlyInterest() that returns the monthly interest.
11. A method named withdraw that withdraws a specified amount from the account.
12. A method named deposit that deposits a specified amount to the account.
13. a toString() method to display all account details and the number of transactions.
Draw the UML diagram for the class 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 annualInterestRate is a percentage, for example 4.5%. You need to divide it by 100.)
Account.java
public class Account {
private int id;
private double balance;
private static double annualInterestRate;
private static int nTransactions;
public Account()
{
this.id = 0;
this.balance = 0;
Account.annualInterestRate = 0;
}
public Account(int id, double balance, double
annualInterestRate)
{
this.id = id;
this.balance = balance;
Account.annualInterestRate = annualInterestRate;
}
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)
{
Account.annualInterestRate = annualInterestRate;
}
public static int getnTransactions() {
return nTransactions;
}
public static void setnTransactions(int nTransactions) {
Account.nTransactions = nTransactions;
}
public Account(Account account)
{
this.id = account.getId();
this.balance = account.getBalance();
Account.annualInterestRate = account.getAnnualInterestRate();
}
public double getMonthlyInterest()
{
return((getAnnualInterestRate() / 1200) * this.balance);
}
public void deposit(double amount)
{
if(amount > 0)
{
setBalance(getBalance() + amount);
nTransactions++;
}
}
public void withdraw(double amount)
{
if(amount > getBalance() || (getBalance() - amount) <
0)
System.out.println("Transaction failed due to insuffiecient
funds!");
else
{
setBalance(getBalance() - amount);
nTransactions++;
}
}
@Override
public String toString()
{
return("ID: " + getId() + "\n"
+ "Interest Rate: " + getAnnualInterestRate() + "%\n"
+ "Balance: $" + String.format("%.2f", getBalance()) + "\n"
+ "Monthly Interest: $" + String.format("%.2f",
getMonthlyInterest()) + "\n"
+ "Number of transactions so far: " + nTransactions);
}
}
AccountTest.java (Driver class)
public class AccountTest {
public static void main(String[] args)
{
Account acc = new Account(389, 5000, 4.5);
System.out.println("A new account created with Id: " + acc.getId()
+ "...\n" + acc.toString());
System.out.println("\nDeposited $100...");
acc.deposit(100);
System.out.println(acc.toString());
System.out.println("\nWithdraw $6100...");
acc.withdraw(6100);
System.out.println(acc.toString());
System.out.println("\nWithdraw $4100...");
acc.withdraw(4100);
System.out.println(acc.toString());
System.out.println("\nDeposited $1500...");
acc.deposit(1500);
System.out.println(acc.toString());
}
}
********************************************************************* SCREENSHOT ******************************************************
UML Diagram: