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
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.
Output example:
Account-class:
class Account { private java.util.ArrayList
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
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()); } }
Name: Alexander Erlingsen, Id: 4321, Balance: 10000.00, Date created: 2018-01-28 18:26:53 Date created: 2018-e Name: Jakob Overrein, Id: 1234, Balance: 25000.00, Date created: 2018-01-28 21:13:33 Name: Lasse Heia, Id: 1425, Balance: 1028340.00, Date created: 2018-01-28 21:13:3:3
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started