Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Account.java
- public class Account implements Comparable<Account> {
- private String name;
- private Integer amount;
- public Account(String nm, Integer amt) {
- name = nm;
- amount = amt; }
- public static Account account(String nm, Integer amt) {
- return new Account(nm, amt); }
- public String name() { return name; }
- public Integer amount() { return amount; }
- public boolean equals(Object x) {
- if ( x == null ) return false;
- else if ( getClass() != x.getClass() ) return false;
- else return name.equals( ((Account)x).name); }
- // return -1 to sort this account before x, else 1
- public int compareTo(Account x) {
- //First, name is same
- if(this.name().equals(x.name()))
- {
- if(this.amount >= 0 && x.amount >= 0)
- {
- return -1;
- }
- //all these have at least one negative amount
- //this has a larger amount than x
- else if(this.amount >= x.amount)
- {
- return 1;
- }
- //this has a smaller amount than x
- else
- {
- return -1;
- }
- }
- else
- {
- return this.name().compareTo(x.name());
- }
- }
- public String toString() {
- return ( "(" + this.name + " " + this.amount + ")"); }
- }
Add Comment
Please, Sign In to add comment