Advertisement
Guest User

Zach Stevenson's Response to Ray Camden's Challenge

a guest
Sep 26th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Application.cfc
  2. component
  3. {
  4.     Application.name = "RayCamdenFamilyTreeTest";
  5.  
  6.  
  7.     public function onApplicationStart()
  8.     {
  9.         Application.personService = new PersonService();
  10.         Application.startingPerson = Application.personService.createPerson("Zach", "Stevenson", Application.personService.MALE);
  11.         Application.personService.lifecycle(Application.startingPerson);
  12.     }
  13.  
  14.     public function onRequest()
  15.     {
  16.         if(StructKeyExists(url, "restart") && url.restart == 1)
  17.         {
  18.             onApplicationStart();
  19.         }
  20.         writeDump(Application.startingPerson);
  21.         abort;
  22.     }
  23.  
  24. }
  25.  
  26.  
  27. //Person.cfc
  28. component accessors="true"
  29. {
  30.  
  31.     property name="firstName";
  32.     property name="lastName";
  33.     property name="gender";
  34.     property name="spouse";
  35.     property name="children";
  36.  
  37.     public function init(required firstName, required lastName, required gender)
  38.     {
  39.         StructAppend(variables, arguments);
  40.         setChildren([]);
  41.         return this;
  42.     }
  43.  
  44.     public function addChild(child)
  45.     {
  46.         ArrayAppend(variables.children, arguments.child);
  47.     }
  48.  
  49.     public function hasChildren()
  50.     {
  51.         return ArrayLen(variables.children);
  52.     }
  53.  
  54.     public function isMale()
  55.     {
  56.         return variables.gender == GetComponentMetadata("PersonService").MALE;
  57.     }
  58.  
  59. }
  60.  
  61.  
  62.  
  63. //PersonService.cfc
  64. component MALE="Male" FEMALE="Female"
  65. {
  66.  
  67.     this.MALE = GetComponentMetaData("PersonService").MALE;
  68.     this.FEMALE = GetComponentMetaData("PersonService").FEMALE;
  69.  
  70.     public function init()
  71.     {
  72.         initNames();
  73.     }
  74.  
  75.     public function lifecycle(person)
  76.     {
  77.         if(willPersonMarry())
  78.         {
  79.             marryPeople(person);
  80.  
  81.             for(local.i = 1; local.i <= getTotalChildrenBorn(); local.i++)
  82.             {
  83.                 local.child = haveChild(argumentCollection = getHaveChildrenArgumentsFromPerson(arguments.person));
  84.                 lifecycle(local.child);
  85.             }
  86.         }
  87.     }
  88.  
  89.     public function createPerson()
  90.     {
  91.         return new Person(ArgumentCollection = arguments);
  92.     }
  93.  
  94.     /**
  95.       * @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
  96.       **/
  97.     private function marryPeople(person)
  98.     {
  99.         local.spouse = createSpouse(getOppositeGender(person.getGender()));
  100.         arguments.person.setSpouse(local.spouse);
  101.         return local.spouse;
  102.     }
  103.  
  104.     private function getHaveChildrenArgumentsFromPerson(person)
  105.     {
  106.         local.value.father = arguments.person;
  107.         local.value.mother = arguments.person.getSpouse();
  108.         if(!arguments.person.isMale())
  109.         {
  110.             local.value.mother = arguments.person;
  111.             local.value.father = arguments.person.getSpouse();
  112.         }
  113.         return local.value;
  114.     }
  115.  
  116.     private function haveChild(father, mother)
  117.     {
  118.         local.gender = getRandomGender();
  119.         local.child = createPerson(
  120.             firstName = getRandomFirstName(local.gender),
  121.             lastName = arguments.father.getLastName(),
  122.             gender = local.gender
  123.         );
  124.         arguments.father.addChild(local.child);
  125.         arguments.mother.addChild(local.child);
  126.         return local.child;
  127.     }
  128.  
  129.     private function willPersonMarry()
  130.     {
  131.         return (RandRange(0,4))?true:false; //1 in 5 chance to not get married. 0 will return falsy value
  132.     }
  133.  
  134.     private function getTotalChildrenBorn()
  135.     {
  136.         return RandRange(0,2);
  137.     }
  138.  
  139.     private function getOppositeGender(gender)
  140.     {
  141.         return (arguments.gender == this.MALE)?this.FEMALE:this.MALE;
  142.     }
  143.  
  144.     private function createSpouse(gender)
  145.     {
  146.         arguments.firstName = getRandomFirstName(arguments.gender);
  147.         arguments.lastName = getRandomLastName();
  148.         return createPerson(argumentCollection = arguments);
  149.     }
  150.  
  151.     private function getRandomGender()
  152.     {
  153.         return (RandRange(1,2)-1)?this.MALE:this.FEMALE;
  154.     }
  155.  
  156.     private function getRandomFirstName(gender)
  157.     {
  158.         local.firstNames = randomizeArray(variables[arguments.gender].names);
  159.         return local.firstNames[1];
  160.     }
  161.  
  162.     private function getRandomLastName()
  163.     {
  164.         local.familyNames = randomizeArray(variables.family.names);
  165.         return local.familyNames[1];
  166.     }
  167.  
  168.     private function randomizeArray(value)
  169.     {
  170.         local.javaUtilsCollectionsClass = getJavaUtilsCollectionsClass();
  171.         local.javaUtilsCollectionsClass.shuffle(arguments.value);
  172.         return arguments.value;
  173.     }
  174.  
  175.     private function getJavaUtilsCollectionsClass()
  176.     {
  177.         if(!StructKeyExists(variables, "javaLangUtilsCollectionsClass"))
  178.         {
  179.             variables.javaLangUtilsCollectionsClass = createObject("java", "java.util.Collections");
  180.         }
  181.         return variables.javaLangUtilsCollectionsClass;
  182.     }
  183.  
  184.     private function initNames()
  185.     {
  186.         variables[this.MALE] = {};
  187.         variables[this.FEMALE] = {};
  188.  
  189.         variables[this.MALE].names = [
  190.             "Adam",
  191.             "Ben",
  192.             "Charlie",
  193.             "Darrel",
  194.             "Evan",
  195.             "Frank",
  196.             "George",
  197.             "Hank",
  198.             "Izzy",
  199.             "Joseph",
  200.             "Kyle",
  201.             "Lorenzo",
  202.             "Michael",
  203.             "Nathaniel",
  204.             "Otis",
  205.             "Phillip",
  206.             "Quinton",
  207.             "Raymond",
  208.             "Stephen",
  209.             "Tyler",
  210.             "Uther",
  211.             "Victor",
  212.             "William",
  213.             "Xavier",
  214.             "Yorick",
  215.             "Zach"
  216.         ];
  217.  
  218.         variables[this.FEMALE].names = [
  219.             "Annie",
  220.             "Bonnie",
  221.             "Charlise",
  222.             "Darla",
  223.             "Eve",
  224.             "Francine",
  225.             "Georgia",
  226.             "Helga",
  227.             "Izzabella",
  228.             "Jessica",
  229.             "Kayla",
  230.             "Lauren",
  231.             "Michelle",
  232.             "Natalie",
  233.             "Ophelia",
  234.             "Persephone",
  235.             "Queen",
  236.             "Regina",
  237.             "Shelley",
  238.             "Tiffany",
  239.             "Unique",
  240.             "Victoria",
  241.             "Wendy",
  242.             "Xena",
  243.             "Yvonne",
  244.             "Zelda"
  245.         ];
  246.  
  247.         variables.family.names = [
  248.             "Andress",
  249.             "Bonfils",
  250.             "Camden",
  251.             "Delks",
  252.             "Evans",
  253.             "Fields",
  254.             "Gregory",
  255.             "Hufflepuff",
  256.             "Iwans",
  257.             "Jones",
  258.             "Kearney",
  259.             "Lacan",
  260.             "Munz",
  261.             "Newberry",
  262.             "Ortiz",
  263.             "Peterman",
  264.             "Query", // Yes, I have a friend named Jake Query.  I call him jQuery, but he doesn't get it...
  265.             "Rudell",
  266.             "Snow",
  267.             "Tatti",
  268.             "Underwood",
  269.             "Vitolins",
  270.             "Ward",
  271.             "Xolichitoa",
  272.             "Young",
  273.             "Zanagro"
  274.         ];
  275.     }
  276. }
  277.  
  278.  
  279.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement