Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Application.cfc
- component
- {
- Application.name = "RayCamdenFamilyTreeTest";
- public function onApplicationStart()
- {
- Application.personService = new PersonService();
- Application.startingPerson = Application.personService.createPerson("Zach", "Stevenson", Application.personService.MALE);
- Application.personService.lifecycle(Application.startingPerson);
- }
- public function onRequest()
- {
- if(StructKeyExists(url, "restart") && url.restart == 1)
- {
- onApplicationStart();
- }
- writeDump(Application.startingPerson);
- abort;
- }
- }
- //Person.cfc
- component accessors="true"
- {
- property name="firstName";
- property name="lastName";
- property name="gender";
- property name="spouse";
- property name="children";
- public function init(required firstName, required lastName, required gender)
- {
- StructAppend(variables, arguments);
- setChildren([]);
- return this;
- }
- public function addChild(child)
- {
- ArrayAppend(variables.children, arguments.child);
- }
- public function hasChildren()
- {
- return ArrayLen(variables.children);
- }
- public function isMale()
- {
- return variables.gender == GetComponentMetadata("PersonService").MALE;
- }
- }
- //PersonService.cfc
- component MALE="Male" FEMALE="Female"
- {
- this.MALE = GetComponentMetaData("PersonService").MALE;
- this.FEMALE = GetComponentMetaData("PersonService").FEMALE;
- public function init()
- {
- initNames();
- }
- public function lifecycle(person)
- {
- if(willPersonMarry())
- {
- marryPeople(person);
- for(local.i = 1; local.i <= getTotalChildrenBorn(); local.i++)
- {
- local.child = haveChild(argumentCollection = getHaveChildrenArgumentsFromPerson(arguments.person));
- lifecycle(local.child);
- }
- }
- }
- public function createPerson()
- {
- return new Person(ArgumentCollection = arguments);
- }
- /**
- * @hint Because the application doesn't have two separate family tree's merging, every time you choose to marry someone it will create that person on the fly
- **/
- private function marryPeople(person)
- {
- local.spouse = createSpouse(getOppositeGender(person.getGender()));
- arguments.person.setSpouse(local.spouse);
- return local.spouse;
- }
- private function getHaveChildrenArgumentsFromPerson(person)
- {
- local.value.father = arguments.person;
- local.value.mother = arguments.person.getSpouse();
- if(!arguments.person.isMale())
- {
- local.value.mother = arguments.person;
- local.value.father = arguments.person.getSpouse();
- }
- return local.value;
- }
- private function haveChild(father, mother)
- {
- local.gender = getRandomGender();
- local.child = createPerson(
- firstName = getRandomFirstName(local.gender),
- lastName = arguments.father.getLastName(),
- gender = local.gender
- );
- arguments.father.addChild(local.child);
- arguments.mother.addChild(local.child);
- return local.child;
- }
- private function willPersonMarry()
- {
- return (RandRange(0,4))?true:false; //1 in 5 chance to not get married. 0 will return falsy value
- }
- private function getTotalChildrenBorn()
- {
- return RandRange(0,2);
- }
- private function getOppositeGender(gender)
- {
- return (arguments.gender == this.MALE)?this.FEMALE:this.MALE;
- }
- private function createSpouse(gender)
- {
- arguments.firstName = getRandomFirstName(arguments.gender);
- arguments.lastName = getRandomLastName();
- return createPerson(argumentCollection = arguments);
- }
- private function getRandomGender()
- {
- return (RandRange(1,2)-1)?this.MALE:this.FEMALE;
- }
- private function getRandomFirstName(gender)
- {
- local.firstNames = randomizeArray(variables[arguments.gender].names);
- return local.firstNames[1];
- }
- private function getRandomLastName()
- {
- local.familyNames = randomizeArray(variables.family.names);
- return local.familyNames[1];
- }
- private function randomizeArray(value)
- {
- local.javaUtilsCollectionsClass = getJavaUtilsCollectionsClass();
- local.javaUtilsCollectionsClass.shuffle(arguments.value);
- return arguments.value;
- }
- private function getJavaUtilsCollectionsClass()
- {
- if(!StructKeyExists(variables, "javaLangUtilsCollectionsClass"))
- {
- variables.javaLangUtilsCollectionsClass = createObject("java", "java.util.Collections");
- }
- return variables.javaLangUtilsCollectionsClass;
- }
- private function initNames()
- {
- variables[this.MALE] = {};
- variables[this.FEMALE] = {};
- variables[this.MALE].names = [
- "Adam",
- "Ben",
- "Charlie",
- "Darrel",
- "Evan",
- "Frank",
- "George",
- "Hank",
- "Izzy",
- "Joseph",
- "Kyle",
- "Lorenzo",
- "Michael",
- "Nathaniel",
- "Otis",
- "Phillip",
- "Quinton",
- "Raymond",
- "Stephen",
- "Tyler",
- "Uther",
- "Victor",
- "William",
- "Xavier",
- "Yorick",
- "Zach"
- ];
- variables[this.FEMALE].names = [
- "Annie",
- "Bonnie",
- "Charlise",
- "Darla",
- "Eve",
- "Francine",
- "Georgia",
- "Helga",
- "Izzabella",
- "Jessica",
- "Kayla",
- "Lauren",
- "Michelle",
- "Natalie",
- "Ophelia",
- "Persephone",
- "Queen",
- "Regina",
- "Shelley",
- "Tiffany",
- "Unique",
- "Victoria",
- "Wendy",
- "Xena",
- "Yvonne",
- "Zelda"
- ];
- variables.family.names = [
- "Andress",
- "Bonfils",
- "Camden",
- "Delks",
- "Evans",
- "Fields",
- "Gregory",
- "Hufflepuff",
- "Iwans",
- "Jones",
- "Kearney",
- "Lacan",
- "Munz",
- "Newberry",
- "Ortiz",
- "Peterman",
- "Query", // Yes, I have a friend named Jake Query. I call him jQuery, but he doesn't get it...
- "Rudell",
- "Snow",
- "Tatti",
- "Underwood",
- "Vitolins",
- "Ward",
- "Xolichitoa",
- "Young",
- "Zanagro"
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement