Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

** THE ACCOUNT CLASS CODE // the Account class from excercise 9.7 public class A

ID: 3565994 • Letter: #

Question

** THE ACCOUNT CLASS CODE

// the Account class from excercise 9.7
public class Account {
//encapsulated data fields
private int id;
private double balance;
private double interest_Rate;
private Date dateCreated;
// no arg constructor that creates default account
public Account(){
id = 0;
balance = 0;
interest_Rate = 0;
dateCreated = new Date();
}
//constructor that creates account with specified id and balance
public Account(int id, double balance){
this.id = id;
this.balance = balance;
dateCreated = new Date();
  
}
  
//accessor and mutator methods
public double getinterestRate(){
return interest_Rate;   
}
public void setinterestRate(double interestRate){
this.interest_Rate = interestRate;
}

public double getBalance(){
return balance;
}
public void setBalance(double balance){
this.balance = balance;
}
public int getid(){
return id;
}
public void setid(int id){
this.id = id;
}
public Date getDateCreated(){
return dateCreated;
}
public double getMonthlyinterestRate(){
return (interest_Rate/100)/12;
}
public double getMonthlyInterest(){
return balance * getMonthlyinterestRate();
  
}
public double withdraw(double amount){
if (amount > balance)
System.out.println("Not enough funds");
else
balance -=amount;
return balance;
}
public void deposit(double amount){
balance += amount;
}

Explanation / Answer

Account.java

import java.text.DecimalFormat;
import java.util.ArrayList;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
private java.util.Date dateCreated;
private String name;
private ArrayList<Transaction> transactions = new ArrayList<Transaction>();
/* Default no-arg constructor */
public Account() {
this(0, 0.0, "Unknown name"); // Invole the 2-param constructor
}
/* Constructor */
public Account(int id, double balance, String name) {
setID(id);
setBalance(balance);
setName(name);
annualInterestRate = 0;
dateCreated = new java.util.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 rate) {
annualInterestRate = rate / 100.0; // e.g. 5% = 5 / 100 = 0.05
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
public double getMonthlyInterest() {
return balance * getMonthlyInterestRate();
}
public void withdraw(double amount) {
balance -= amount;
transactions.add(new Transaction('W', amount, balance, "Withdraw"));
}
public void deposit(double amount) {
balance += amount;
transactions.add(new Transaction('D', amount, balance, "Deposit"));
}
@Override public String toString() {
StringBuilder output = new StringBuilder();
output.append(this.getClass().getName() + " Object { ");
output.append(" ID: " + id + " ");
DecimalFormat formatter = new DecimalFormat("$###,###.###");
output.append(" Balance: " + formatter.format(balance) + " ");
formatter.applyPattern("###,###.###%");
output.append(" APR: " + formatter.format(annualInterestRate) + " ");
output.append(" Created: " + dateCreated + " ");
output.append(" Transactions: ");
for (int i = 0; i < transactions.size(); i++) {
output.append(" #" + i + ": " + transactions.get(i).toString());
}
output.append("}");
return output.toString();
}
}

Employee.java

Faculty.java


}

Person.java


}

Student.java


}

TestAccount.java

}

}

TestPersonSubs.java


}

TestTransaction.java

}

Transaction.java

}

}

public class Employee extends Person { private String office; private double salary; private java.util.Date dateHired; public Employee() { this(new Person("","","",""), "", 0, new java.util.Date()); } public Employee(Person p, String office, double salary, java.util.Date dateHired) { this(p.getName(), p.getAddress(), p.getPhone(), p.getEmail(), office, salary, dateHired); } public Employee(String name, String address, String phone, String email) { this(name, address, phone, email, "", 0, new java.util.Date()); } public Employee(String name, String address, String phone, String email, String office, double salary, java.util.Date dateHired) { super(name, address, phone, email); setOffice(office); setSalary(salary); this.dateHired = dateHired; } public String getOffice() { return office; } public void setOffice(String office) { this.office = office; } public double getSalary() { return salary; } public void setSalary(double salary) { if (salary >= 0) { this.salary = salary; } else { System.out.println("ERROR: Salary is < 0. Defaulting to 0."); this.salary = 0; } } public java.util.Date getDateHired() { return dateHired; } public void setDateHired(java.util.Date dateHired) { this.dateHired = dateHired; } @Override public String toString() { super.toString(); StringBuilder output = new StringBuilder(); java.text.DecimalFormat moneyOut = new java.text.DecimalFormat("$###,##0.00"); output.append(this.getClass().getName() + "Object "); output.append(" Office: " + office + " "); output.append(" Salary: " + moneyOut.format(salary) + " "); output.append(" Hire Date: " + dateHired + " "); return output.toString(); }
}