// CS U213 - Spring 2008 // Lab 5 -- Assignment 5 // --------------------- /* Design Recipe Solution: +-------------------------+ | +---------------------+ | | | | | v v | | +--------+ | | | ILoS | | | +--------+ | | | | | / \ | | +---+ | | | | | +--------+ | | | ALoS |------------------+ | +--------+ | | | / \ | +---+ | | | +------+---------------+ | | | | +-----+-----+ | | | MtLoS | +--------+-----+ | +-----------+ | ConsLoS | | +--------------+ | +--------| Song first | | | | ILoS rest |--+ v +--------------+ +----------------+ | Song | +----------------+ | String title | | String artist | | int size | | double price | +----------------+ */ // Represents a Song for 'iTunes' class Song{ String title; String artist; int size; double price; Song(String title, String artist, int size, double price){ this.title = title; this.artist = artist; this.size = size; this.price = price; } } // Represents a List of Songs... interface ILoS{ // Count the number of songs in *this* ILoS int count(); // Count the number of songs in *this* ILoS // by adding to the current count int countAcc(int acc); // Find the total size of all the songs in *this* ILoS int totalSize(); // Find the total size of all the songs in *this* ILoS // by updating the given size int totalSizeAcc(int acc); // Tell if there is a song by the given artist in *this* ILoS boolean contains(String artist); // Tell if there is a song by the given artist in *this* ILoS // knowing whether it was found already // NOTE: here the result can be reported immediately without the accumulator boolean containsAcc(String artist, boolean acc); // Create a list of all songs by the given artist in *this* ILoS ILoS allBy(String artist); // Create a list of all songs by the given artist in *this* ILoS // adding to the already collected list ILoS allByAcc(String artist, ILoS acc); } // Represents a List of Songs... abstract class ALoS implements ILoS{ // Count the number of songs in *this* ILoS int count(){ return this.countAcc(0); } // Count the number of songs in *this* ILoS // by adding to the current count abstract int countAcc(int acc); // Find the total size of all the songs in *this* ILoS int totalSize(){ return this.totalSizeAcc(0); } // Find the total size of all the songs in *this* ILoS // by updating the given size abstract int totalSizeAcc(int acc); // Tell if there is a song by the given artist in *this* ILoS boolean contains(String artist){ return this.containsAcc(artist, false); } // Tell if there is a song by the given artist in *this* ILoS // knowing whether it was found already // NOTE: here the result can be reported immediately without the accumulator abstract boolean containsAcc(String artist, boolean acc); // Create a list of all songs by the given artist in *this* ILoS ILoS allBy(String artist){ return this.allByAcc(artist, new MtLoS()); } // Create a list of all songs by the given artist in *this* ILoS // adding to the already collected list abstract ILoS allByAcc(String artist, ILoS acc); } // Represents an empty List of Songs... class MtLoS extends ALoS{ // Nothing to be done for MtLoS MtLoS(){} // Count the number of songs in *this* ILoS // by adding to the current count int countAcc(int acc){ return acc; } // Find the total size of all the songs in *this* ILoS // by updating the given size int totalSizeAcc(int acc){ return acc; } // Tell if there is a song by the given artist in *this* ILoS // knowing whether it was found already // NOTE: here the result can be reported immediately without the accumulator boolean containsAcc(String artist, boolean acc){ return acc; } // Create a list of all songs by the given artist in *this* ILoS // adding to the already collected list ILoS allByAcc(String artist, ILoS acc){ return acc; } } // Represents a non-empty List of Songs... class ConsLoS extends ALoS{ Song first; ILoS rest; ConsLoS(Song first, ILoS rest){ this.first = first; this.rest = rest; } /* TEMPLATE ... this.first ... --- Song ... this.first.title ... --- String ... this.first.artist ... --- String ... this.first.size ... --- int ... this.first.price ... --- double ... this.rest ... --- ILoS ... this.rest.count() ... --- int ... this.rest.totalSize() ... --- int ... this.rest.contains(String) ... --- boolean ... this.rest.allBy(String) ... --- ILoS ... this.rest.countAcc(int acc) ... --- int ... this.rest.totalSizeAcc(int acc) ... --- int ... this.rest.containsAcc(String, boolean acc) ... --- boolean ... this.rest.allByAcc(String, ILoS acc) ... --- ILoS */ // Count the number of songs in *this* ILoS // by adding to the current count int countAcc(int acc){ return this.rest.countAcc(1 + acc); } // Find the total size of all the songs in *this* ILoS // by updating the given size int totalSizeAcc(int acc){ return this.rest.totalSizeAcc(this.first.size + acc); } // Tell if there is a song by the given artist in *this* ILoS // knowing whether it was found already // NOTE: here the result can be reported immediately without the accumulator boolean containsAcc(String artist, boolean acc){ return this.rest.containsAcc(artist, this.first.artist.equals(artist) || acc); } // Create a list of all songs by the given artist in *this* ILoS // adding to the already collected list ILoS allByAcc(String artist, ILoS acc){ if(this.first.artist.equals(artist)) return this.rest.allByAcc(artist, new ConsLoS(this.first, acc)); else return this.rest.allByAcc(artist, acc); } } // All Examples will be put in here... class Examples{ Examples(){} // * Examples, Songs and ILoSs * Song help = new Song("Help", "Beatles", 50, 0.99); Song dsotm = new Song("Dsotm", "Pink Floyd", 100, 0.25); Song hts = new Song("Hts", "Muse", 10, 0.90); ILoS mtlos = new MtLoS(); ILoS list1 = new ConsLoS(this.help, this.mtlos); ILoS list2 = new ConsLoS(this.dsotm, new ConsLoS( this.hts, this.list1)); // * Count Examples boolean countTests = ((check this.mtlos.count() expect 0) && (check this.list1.count() expect 1) && (check this.list2.count() expect 3)); // * Total Size Examples boolean totalSizeTests = ((check this.mtlos.totalSize() expect 0) && (check this.list1.totalSize() expect 50) && (check this.list2.totalSize() expect 160)); // * Contains Examples boolean containsTests = ((check this.mtlos.contains("Beatles") expect false) && (check this.list1.contains("Beatles") expect true) && (check this.list1.contains("Muse") expect false) && (check this.list2.contains("Muse") expect true) && (check this.list2.contains("ABC") expect false)); // * AllBy Examples boolean allByTests = ((check this.mtlos.allBy("Pink Floyd") expect this.mtlos) && (check this.list1.allBy("Pink Floyd") expect this.mtlos) && (check this.list1.allBy("Beatles") expect this.list1) && (check this.list2.allBy("Pink Floyd") expect new ConsLoS(this.dsotm, this.mtlos)) && (check this.list2.allBy("Beatles") expect this.list1)); }