/* * Bank account list: abstracting over Object */ import tester.Tester; /* +---------+ | ILoObj |<-------+ +---------+ | +---------+ | | | | | /_\ | | | +--------------+ | v v | +---------+ +--------------+ | | MTLoObj | | ConsLoObj | | +---------+ +--------------+ | +--| Object first | | | | ILoObj rest |-+ | +--------------+ v +---------------+ | Acct | +---------------+ | String owner | | String kind | | int balance | +---------------+ */ // to represent an account in a bank class Acct{ 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? boolean same(Acct that){ return this.owner.equals(that.owner) && this.kind.equals(that.kind) && this.balance == that.balance; } } // to represent a list of accounts interface ILoObj{ // compute the total size of this list int size(); // does this list of accounts contain the given account? boolean contains(Object a); } // to represent an empty list of accounts class MtLoObj implements ILoObj{ MtLoObj(){} // compute the total size of this list public int size(){ return 0; } // does this list of accounts contain the given account? public boolean contains(Object b){ return false; } } // to represent a nonempty list of accounts class ConsLoObj implements ILoObj{ Object first; ILoObj rest; ConsLoObj(Object first, ILoObj rest){ this.first = first; this.rest = rest; } /* TEMPLATE: FIELDS: ... this.first ... -- Object ... this.rest ... -- ILoObj METHODS FOR FIELDS: ... this.first.same(Object) ... -- boolean ... this.rest.size() ... -- int ... this.rest.contains(Object) ... -- 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(Object a){ return 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); ILoObj mtlob = new MtLoObj(); ILoObj alist2 = new ConsLoObj(this.chk1, new ConsLoObj(this.chk2, this.mtlob)); ILoObj alist3 = new ConsLoObj(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);} }