Guest User

Untitled

a guest
Jun 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. // Account.java
  2.  
  3.     public class Account implements Comparable<Account> {
  4.         private String name;
  5.         private Integer amount;
  6.         public Account(String nm, Integer amt) {
  7.             name = nm;
  8.             amount = amt; }
  9.         public static Account account(String nm, Integer amt) {
  10.             return new Account(nm, amt); }
  11.         public String name() { return name; }
  12.         public Integer amount() { return amount; }
  13.         public boolean equals(Object x) {
  14.             if ( x == null ) return false;
  15.             else if ( getClass() != x.getClass() ) return false;
  16.             else return name.equals( ((Account)x).name); }
  17.  
  18.         // return -1 to sort this account before x, else 1
  19.         public int compareTo(Account x) {
  20.           //First, name is same
  21.           if(this.name().equals(x.name()))
  22.           {
  23.              if(this.amount >= 0 && x.amount >= 0)
  24.              {
  25.                return -1;
  26.              }
  27.              //all these have at least one negative amount
  28.              //this has a larger amount than x
  29.              else if(this.amount >= x.amount)
  30.              {
  31.                return 1;
  32.              }
  33.              //this has a smaller amount than x
  34.              else
  35.              {
  36.                return -1;
  37.              }
  38.           }
  39.           else
  40.           {
  41.             return this.name().compareTo(x.name());
  42.           }
  43.         }
  44.        
  45.        
  46.         public String toString() {
  47.             return ( "(" + this.name + " " + this.amount + ")"); }
  48.     }
Add Comment
Please, Sign In to add comment