/* * Bank accounts list: using generics; * list parametrized over data that implements IsSame */ import tester.Tester; /* +---------+ | ILo |<------+ +---------+ | +---------+ | | | | | /_\ | | | +--------------+ | v v | +---------+ +-------------+ | | MTLo | | ConsLo | | +---------+ +-------------+ | +--| T first | | | | ILo rest |-+ | +-------------+ v +---------------+ | Acct | +---------------+ | String owner | | String kind | | int balance | +---------------+ */ // to represent an account in a bank class Acct implements IsSame{ String owner; String kind; int balance; Acct(String owner, String kind, int balance){ this.owner = owner; this.kind = kind; this.balance = balance; } /* TEMPLATE: FIELDS: ... this.owner ... -- String ... this.kind ... -- String ... this.balance ... -- int METHODS FOR FIELDS: ... this.owner.equals(String) ... -- boolean ... this.kind.equals(String) ... -- boolean METHODS FOR THIS CLASS: ... this.same(Acct) ... -- boolean */ // is this account the same as the given one? public boolean same(Acct that){ return this.owner.equals(that.owner) && this.kind.equals(that.kind) && this.balance == that.balance; } } //to represent a method to verify equality of this and that object interface IsSame{ boolean same(T that); } // to represent a list of accounts interface ILo>{ // compute the total size of this list int size(); // does this list of accounts contain the given account? boolean contains(T a); } // to represent an empty list of accounts class MtLo> implements ILo{ MtLo(){} // compute the total size of this list public int size(){ return 0; } // does this list of accounts contain the given account? public boolean contains(T b){ return false; } } // to represent a nonempty list of accounts class ConsLo> implements ILo{ T first; ILo rest; ConsLo(T first, ILo rest){ this.first = first; this.rest = rest; } /* TEMPLATE: FIELDS: ... this.first ... -- Acct ... this.rest ... -- ILoB METHODS FOR FIELDS: ... this.first.same(Acct) ... -- boolean ... this.rest.size() ... -- int ... this.rest.contains(Acct) ... -- boolean */ // compute the total size of this list public int size(){ return 1 + this.rest.size(); } // does this list of accounts contain the given account? public boolean contains(T a){ return ((IsSame)this.first).same(a) || this.rest.contains(a); } /* // produce a list of all accounts with balance below the given limit public ILoA allLow(int limit){ if (this.first.balance < limit){ return new ConsLoA(this.first, this.rest.allLow(limit)); } else{ return this.rest.allLow(limit); } } */ } // Examples and tests for books and lists of books class Examples{ Examples(){} Acct chk1 = new Acct("JFK", "checking", 4000); Acct chk2 = new Acct("LBJ", "checking", 1000); Acct sav = new Acct("DDE", "savings", 2000); Acct credit = new Acct("RMN", "credit card", 3000); ILo mtlob = new MtLo(); ILo alist2 = new ConsLo(this.chk1, new ConsLo(this.chk2, this.mtlob)); ILo alist3 = new ConsLo(this.sav, this.alist2); // test the method same in the classes that implement ILoB boolean testSame(Tester t){ return t.checkExpect( this.chk1.same(new Acct("JFK", "checking", 4000)), true) && t.checkExpect(this.chk1.same(this.chk2), false) && t.checkExpect(this.chk1.same(this.sav), false) ;} // test the method size in the classes that implement ILoB boolean testSize(Tester t){ return t.checkExpect(this.mtlob.size(), 0) && t.checkExpect(this.alist2.size(), 2) && t.checkExpect(this.alist3.size(), 3);} // test the method contains in the classes that implement ILoB boolean testContains(Tester t){ return t.checkExpect(this.mtlob.contains(this.chk1), false) && t.checkExpect(this.alist2.contains(this.sav), false) && t.checkExpect(this.alist3.contains(this.sav), true);} }