/*********************************************** * CS2510 Fall 2011 * Lecture #14 * State Change: mutate a person ***********************************************/ /** Goal: change the phone number for a given person */ import tester.Tester; /* +-----------+ | ILoPerson |<------------+ +-----------+ | +-----------+ | / \ | --- | | | ------------------- | | | | +------------+ +----------------+ | | MTLoPerson | | ConsLoPerson | | +------------+ +----------------+ | +------------+ +-| Person first | | | | ILoPerson rest |-+ | +----------------+ v +-------------+ | IPerson |<----------------------+ +-------------+ | +-------------+ | / \ | --- | | | +-------------+ | | APerson | | +-------------+ | | String name | | | int phone | | +-------------+ | / \ | --- | | | ----------------------- | | | | +------------+ +---------------------+ | | Student | | Superhero | | +------------+ +---------------------+ | | double gpa | | String power | | +------------+ | boolean invincibleP | | | IPerson secretID |-+ +---------------------+ */ // IPerson: A union for types of people interface IPerson { // revealSecretID: Returns the secret identity person for this person IPerson revealSecretID(); // revealSecretName: Returns the string name for the secret id String revealSecretName(); // getName: Returns the string representation of the name String getName(); // getPhone: Returns the int representation of the phone int getPhone(); // newPhone: Mutates phone number for this Person void newPhone(int newphone); // samePerson: Returns true if this person is the same as the given person boolean samePerson(IPerson person); } // APerson: Holds common fields and methods for all people abstract class APerson implements IPerson{ String name; int phone; APerson(String name, int phone) { this.name = name; this.phone = phone; } // getName: Returns the name of the person as a String public String getName() { return this.name; } // getPhone: Returns the phone of the person as an int public int getPhone() { return this.phone; } // Mutates the phone number for this Person public void newPhone(int newphone) { this.phone = newphone; } // samePerson: Returns true if the people have the same name and phone number public boolean samePerson(IPerson person) { return (this.name == person.getName() && this.phone == person.getPhone()); } } // Student: Information and methods for Students class Student extends APerson { double gpa; Student(String name, int phone, double gpa) { super(name, phone); this.gpa = gpa; } // revealSecretID: Error, students have no secret ID public IPerson revealSecretID() { throw new RuntimeException("No one loves you."); } // revealSecretName: Error, students have no secret Name public String revealSecretName() { throw new RuntimeException("No one loves you still."); } } // Superhero: Holds information and the secret identitiy for the superhero class Superhero extends APerson { String power; boolean invincibleP; IPerson secretID; Superhero(String name, int phone, String power, boolean invincibleP, IPerson secretID) { super(name,phone); this.power = power; this.invincibleP = invincibleP; this.secretID = secretID; } // revealSecretID: Returns the IPerson for th secret identity public IPerson revealSecretID() { return this.secretID; } // revealSecretName: Returns the string for the name of the secret identity public String revealSecretName() { return this.secretID.getName(); } } // ILoP: Contains a list union for people interface ILoP { // Returns a list of all people's secret identities public ILoP revealAll(); // Changes all phone numbers in the list public void setAllPhone(int newphone); // contains : Returns true if list contains the given person public boolean contains(IPerson person); } // MtLoP: The empty list element for people class MtLoP implements ILoP { MtLoP() { } // revealAll: Returns am empty list as there is nothing to reveal public ILoP revealAll() { return new MtLoP(); } // setAllPhone: Does nothing in empty list public void setAllPhone(int newphone) { } // contains : Returns false, no people in an empty list public boolean contains(IPerson person) { return false; } } // ConsLoP: The cons list element containing one person class ConsLoP implements ILoP { IPerson first; ILoP rest; ConsLoP(IPerson first, ILoP rest) { this.first = first; this.rest = rest; } // revealAll: Returns the secret ID for the first elements with the secret IDs for the rest public ILoP revealAll() { return new ConsLoP(first.revealSecretID(),rest.revealAll()); } // setAllPhone: Creates sequential phone numbers for everyone in the list // starting at newphone public void setAllPhone(int newphone) { this.first.newPhone(newphone); this.rest.setAllPhone(newphone+1); } // contains : Returns true if this person is the same or if // contained in the rest of the list public boolean contains(IPerson person) { return first.samePerson(person) || rest.contains(person); } } // Examples of people class PersonExamples { IPerson john; IPerson stevie; IPerson peterp; IPerson spiderman; IPerson captnfalcon; ILoP mtl; ILoP students; ILoP sheros; ILoP mixlist; ILoP sherossecret; // Sets the objects initially for our Tester PersonExamples() { reset(); } // Resets the state for our test objects void reset() { john = new Student("John",7,6.3); stevie = new Student("Stevie Ray", 5551212, 3.2); peterp = new Student("Peter Parker", 1234, 2.8); spiderman = new Superhero("Spiderman",3,"Spidey power",false,peterp); captnfalcon = new Superhero("Captain Falcon",99999999,"Falcon Punch",true,spiderman); mtl = new MtLoP(); students = new ConsLoP(john,new ConsLoP(stevie,new ConsLoP(peterp,mtl))); sheros = new ConsLoP(spiderman,new ConsLoP(captnfalcon,mtl)); mixlist = new ConsLoP(spiderman,students); sherossecret = new ConsLoP(peterp,new ConsLoP(spiderman,mtl)); } // Tests for revealSecretID boolean testRevealSecretID(Tester t) { reset(); return t.checkExpect(spiderman.revealSecretID(), peterp) && t.checkExpect(captnfalcon.revealSecretID(), spiderman) && t.checkException(new RuntimeException("No one loves you."), john, "revealSecretID") && t.checkException(new RuntimeException("No one loves you."), peterp, "revealSecretID"); } // Tests for revealSecretName boolean testRevealSecretName(Tester t) { reset(); return t.checkExpect(spiderman.revealSecretName(), peterp.getName()) && t.checkExpect(captnfalcon.revealSecretName(), spiderman.getName()) && t.checkException(new RuntimeException("No one loves you still."), john, "revealSecretName") && t.checkException(new RuntimeException("No one loves you still."), peterp, "revealSecretName"); } // Tests for getName boolean testGetName(Tester t) { reset(); return t.checkExpect(john.getName(),"John") && t.checkExpect(stevie.getName(),"Stevie Ray") && t.checkExpect(spiderman.getName(), "Spiderman"); } // Tests for revealAll boolean testRevealAll(Tester t) { reset(); return t.checkExpect(sheros.revealAll(),sherossecret) && t.checkExpect(mtl.revealAll(),mtl) && t.checkException(new RuntimeException("No one loves you."),students,"revealAll"); } boolean testNewPhone(Tester t) { reset(); spiderman.newPhone(61755512); t.checkExpect(spiderman, new Superhero("Spiderman",61755512,"Spidey power",false,peterp)); return t.checkExpect(sheros,new ConsLoP( new Superhero("Spiderman",61755512,"Spidey power",false,peterp), new ConsLoP(captnfalcon,mtl))); } boolean testSetAllPhone(Tester t) { reset(); Superhero spiderman2 = new Superhero("Spiderman",123456,"Spidey power",false,peterp); Superhero captnfalcon2 = new Superhero("Captain Falcon",123457,"Falcon Punch",true,spiderman2); sheros.setAllPhone(123456); return t.checkExpect(sheros,new ConsLoP(spiderman2, new ConsLoP(captnfalcon2, mtl))); } }