Sort Account-class Use class \"Account\" (Shown below) and implement Comparable
ID: 3699262 • Letter: S
Question
Sort Account-class
Use class "Account" (Shown below) and implement Comparable interface and use it to sort accounts.
The Program should:
- Implement Comparable interface, and be sorted by ; Account balance or datecreated if balance is same.
- To test functionality you have to create a constructor who recieves a GregorianCalendar object to specify when this account was created.
- In the test-class you must create at least 4 accounts where two have the same balance, but different date created.
- Add all objects to an ArrayList and sort in rising order.(Highest balance first)
- Print all accounts in ArrayList, should implement toString.
Account-class:
class Account {
private java.util.ArrayList<Transaction> transactions = new java.util.ArrayList<>();
private static double annualInterestRate;
private java.util.Date dateCreated;
private double balance;
private String name;
private int id;
public Account() {
this ("Undefined", -9999, 0);
}
public Account(String name, int id, double balance) {
dateCreated = new java.util.Date();
this.balance = balance;
this.name = name;
this.id = id;
}
public java.util.ArrayList<Transaction> getTransactions() {
return transactions;
}
public static void setAnnualInterestRate(double annualInterestRate) {
Account.annualInterestRate = annualInterestRate;
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public void withdraw(double amount, String description) {
balance -= amount;
transactions.add(new Transaction('W', amount, balance, description));
}
public void deposit(double amount, String description) {
balance += amount;
transactions.add(new Transaction('D', amount, balance, description));
}
}
Transaction-Class:
class Transaction {
private double amount, balance;
private String description;
private java.util.Date date;
private char type;
public Transaction(char type, double amount, double balance, String description) {
date = new java.util.Date();
this.type = type;
this.amount = amount;
this.balance = balance;
this.description = description;
}
public String getDescription() {
return description;
}
public java.util.Date getDate() {
return date;
}
public double getBalance() {
return balance;
}
public double getAmount() {
return amount;
}
public char getType() {
return type;
}
@Override
public String toString(){
return String.format("%tF %1$tT %-15.2s%-15s%-15s%s", getDate(), getType(), getAmount(),
getBalance(), getDescription());
}
}
Explanation / Answer
class Account implements Comparable{
private java.util.ArrayList<Transaction> transactions = new java.util.ArrayList<>();
private static double annualInterestRate;
private java.util.Date dateCreated;
private double balance;
private String name;
private int id;
public Account() {
this ("Undefined", -9999, 0);
}
public Account(String name, int id, double balance) {
dateCreated = new java.util.Date();
this.balance = balance;
this.name = name;
this.id = id;
}
if(this.getBalance() > acc.getBalance()){
return 1;
}
else if(this.getBalance() < acc.getBalance()){
return -1;
}
else {
return this.getDateCreated().compareTo(acc.getDateCreated());
}
public java.util.ArrayList<Transaction> getTransactions() {
return transactions;
}
public static void setAnnualInterestRate(double annualInterestRate) {
Account.annualInterestRate = annualInterestRate;
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public void withdraw(double amount, String description) {
balance -= amount;
transactions.add(new Transaction('W', amount, balance, description));
}
public void deposit(double amount, String description) {
balance += amount;
transactions.add(new Transaction('D', amount, balance, description));
}
@Override
public String toString(){
return "Name : " +this.getName()+" ID:"+this.getId()+" Balance : "+this.getBalance()+" Date Created: "+this.getDateCreated();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.