/*********************************************** * CS2510 Spring 2011 * Lecture #14.2 * Circular Data and Lists ***********************************************/ import tester.*; // Represents a Movie class Movie{ String title; ILoA stars; Movie(String title, ILoA stars){ this.title = title; this.stars = stars; this.stars.rolify(this); } // Add the given Actor to our Stars void addActor(Actor a){ this.stars = new ConsLoA(a, this.stars); } // Is this movie the same as the given movie? boolean sameMovie(Movie m){ // Don't check the stars... avoid infinite loop return (this.title.equals(m.title)); } } // Reps a(n) Actor class Actor{ String name; ILoM roles; Actor(String name){ this.name = name; this.roles = new MtLoM(); } // Set the Movie Role for this Actor void addRole(Movie m){ this.roles = new ConsLoM(m, this.roles); } // Is this actor the same as the given actor? boolean sameActor(Actor a){ // Should check the Movies... but we'd have to // implement it return (this.name.equals(a.name)); } } // Represents a Studio with Movies... makin' Cash class Studio{ ILoM movies; Studio(ILoM movies){ this.movies = movies; } } // Represents a list of Movies interface ILoM{} // Represents the empty list of Movies class MtLoM implements ILoM{ MtLoM(){} } // Represents a nonempty list of Movies class ConsLoM implements ILoM{ Movie first; ILoM rest; ConsLoM(Movie first, ILoM rest){ this.first = first; this.rest = rest; } } // Represents a list of Actors interface ILoA{ // Assign this List of actors to the given movie public void rolify(Movie m); } // Represents the empty list of Actors class MtLoA implements ILoA{ MtLoA(){} // Assign this List of actors to the given movie public void rolify(Movie m){ // Nothing to do... } } // Represents a nonempty list of Actors class ConsLoA implements ILoA{ Actor first; ILoA rest; ConsLoA(Actor first, ILoA rest){ this.first = first; this.rest = rest; } // Assign this List of actors to the given movie public void rolify(Movie m){ // Add to the first... this.first.addRole(m); // Add to the rest this.rest.rolify(m); } } // Examples... Tests class LectureExamples{ // Some actors Actor bryan; Actor spencer; // Some Movies Movie mine; Movie sd_spy; // Lists of ILoM mtM = new MtLoM(); ILoA mtA = new MtLoA(); ILoA all; LectureExamples(){ this.resetData(); } // Setup our testing data void resetData(){ this.bryan = new Actor("Bryan"); this.spencer = new Actor("Spencer"); this.all = new ConsLoA(this.bryan, new ConsLoA(this.spencer, this.mtA)); this.mine = new Movie("Minecraft: the Movie", this.all); this.sd_spy = new Movie("The Spy Who #*%@&$", this.all); } // Make sure our constructor fixes up the cicles boolean testCircular(Tester t){ this.resetData(); return (t.checkExpect(this.bryan.roles, this.spencer.roles) && t.checkExpect(this.mine.stars, this.all) && t.checkExpect(this.sd_spy.stars, this.all)); } }