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

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());
}
}

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

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();

}
}

@Override
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote