/* * Bank accounts list: original implementation */ import tester.Tester; /* +---------+ | ILoA |<------+ +---------+ | +---------+ | | | | | /_\ | | | +--------------+ | v v | +-------+ +------------+ | | MTLoA | | ConsLoA | | +-------+ +------------+ | +-| Acct first | | | | ILoA 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 books interface ILoA{ // compute the total size of this list int size(); // does this list of accounts contain the given account? boolean contains(Acct a); } // to represent an empty list of accounts class MtLoA implements ILoA{ MtLoA(){} // compute the total size of this list public int size(){ return 0; } // does this list of accounts contain the given account? public boolean contains(Acct a){ return false; } } // to represent a nonempty list of accounts class ConsLoA implements ILoA{ Acct first; ILoA rest; ConsLoA(Acct first, ILoA 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(Acct 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); ILoA mtlob = new MtLoA(); ILoA alist2 = new ConsLoA(this.chk1, new ConsLoA(this.chk2, this.mtlob)); ILoA alist3 = new ConsLoA(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);} }