logo Practice-It logo

BankAccount

Related Links:
Author: Roy McElmurry

Suppose you have a pre-existing class BankAccount2 that represents users' accounts and money deposited at a given bank. The class has the following data and behavior:

Field/Constructor/Method Description
private String name name of the person using the account
private int id ID number of this account
private double balance amount of money currently in the account
public BankAccount2(String name, int id) makes account with given name/ID and $0.00
public void deposit(double amount) adds given amount to account's balance
public int getID() returns the account's ID number
public String getName() returns the account's name
public double getBalance() returns the account's balance
public String toString() returns String such as "Ed Smith: $12.56"
public void withdraw(double amount) subtracts given amount from account's balance

Your task is to modify the class to be Comparable by adding an appropriate compareTo method. Add any necessary code below, and/or make any changes to the existing code headings shown. The bank wants to sort by who has the most money, so accounts are compared by balance. One with a lower balance is considered to be "less than" one with a higher balance. If two accounts have the same balance, they are compared by ID. The one with the lower ID is considered to be "less than" the one with the higher ID. If two accounts have the same balance and ID, they are considered to be "equal." Your method should not modify any account's state. You may assume the parameter passed is not null.

public class BankAccount2 implements Comparable<BankAccount2> { 
    ... 
 
    // write any added code here
}
Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.