/* +----------+ | IGrocery | +----------+ +----------+ | / \ --- | ---------------------------------------------- | | | +--------------------+ +---------------+ +---------------+ | Coffee | | Juice | | IceCream | +--------------------+ +---------------+ +---------------+ | String brand | | String brand | | String brand | | int weight | | int weight | | int weight | | price price | | int price | | int price | | boolean caffinated | | String flavor | | String flavor | +--------------------+ | String pack | +---------------+ +---------------+ */ // to represent a shopping list interface IGrocery { // extracts the brand name from the item String extractBrand(); // extracts the price from the item int extractPrice(); } // to represent coffee class Coffee implements IGrocery { String brand; int weight; int price; boolean caffinated; Coffee(String brand, int weight, int price, boolean caffinated) { this.brand = brand; this.weight = weight; this.price = price; this.caffinated = caffinated; } // extracts the brand name from String extractBrand(){ return this.brand; } // extracts the price from the item int extractPrice(){ return this.price; } } // to represent juice class Juice implements IGrocery { String brand; int weight; int price; String flavor; String pack; Juice(String brand, int weight, int price, String flavor, String pack) { this.brand = brand; this.weight = weight; this.price = price; this.flavor = flavor; this.pack = pack; } // extracts the brand name from String extractBrand(){ return this.brand; } // extracts the price from the item int extractPrice(){ return this.price; } } // to represent ice cream class IceCream implements IGrocery { String brand; int weight; int price; String flavor; IceCream(String brand, int weight, int price, String flavor) { this.brand = brand; this.weight = weight; this.price = price; this.flavor = flavor; } // extracts the brand name from String extractBrand(){ return this.brand; } // extracts the price from the item int extractPrice(){ return this.price; } } // to represent a list of grocery items interface ILoG {//produces a list sorted by the brand name ILoG sortshoplist(); // inserts the given grocery item into this list ILoG insert(IGrocery item); //determines the number of items in the list int howMany(); //produces a list of brand names ILoS brandList(); // determines the highest price in the list int highestPrice(); } // to represent an empty list class MTLoG implements ILoG { MTLoG() { } //produces a list sorted by the brand name ILoG sortshoplist(){ return this; } // inserts the given grocery item into this list ILoG insert(IGrocery item){ return new ConsLoG(item, this); } //determines the number of items in the list int howMany(){ return 0; } //produces a list of brand names ILoS brandList(){ return new MTLoS(); } // determines the highest price in the list int highestPrice(){ return 0; } } // to represent a non-empty list of grocery items class ConsLoG implements ILoG { IGrocery first; ILoG rest; ConsLoG(IGrocery first, ILoG rest) { this.first = first; this.rest = rest; } //produces a list sorted by the brand name ILoG sortshoplist(){ return this.rest.sortshoplist().insert(this.first); } // inserts the given grocery item into this list ILoG insert(IGrocery item){ if (item.extractBrand().compareTo(this.first.extractBrand()) < 0) return new ConsLoG(item, this); else return new ConsLoG(this.first, this.rest.insert(item)); } //determines the number of items in the list int howMany(){ return (this.rest.howMany() + 1); } //produces a list of brand names ILoS brandList(){ return new ConsLoS(this.first.extractBrand(), this.rest.brandList()); } // determines the highest price in the list int highestPrice(){ if (this.first.extractPrice() >= this.rest.highestPrice()) return this.first.extractPrice(); else return this.rest.highestPrice(); } } // to represents a list of strings interface ILoS { } // represents a empty list of strings class MTLoS implements ILoS { MTLoS() { } // determines the highest price in the list int highestPrice(){ return 0; } } // represents a non-empty list of strings class ConsLoS implements ILoS { String first; ILoS rest; ConsLoS(String first, ILoS rest) { this.first = first; this.rest = rest; } } class Examples{ Examples(){} Coffee c1 = new Coffee("Chockfullonuts", 12, 5, true); Juice j1 = new Juice("Tropicana", 12, 4, "Orange", "bottle"); IceCream i1 = new IceCream("Bryers", 24, 5, "Tin roof"); ILoG list1 = new ConsLoG(this.c1, new ConsLoG(this.j1, new ConsLoG(this.i1, new MTLoG()))); //howmany test boolean test1 = (check this.list1.howMany() expect 3); //brandlist test boolean test2 = (check this.list1.brandList() expect new ConsLoS("Chockfullonuts", new ConsLoS("Tropicana", new ConsLoS("Bryers", new MTLoS())))); //highestPrice test boolean test3 = (check this.list1.highestPrice() expect 5); //shopsortlist test boolean test4 = (check this.list1.sortshoplist() expect new ConsLoG(this.i1, new ConsLoG(this.c1, new ConsLoG(this.j1, new MTLoG())))); }