Guest User

Family Tree

a guest
Sep 23rd, 2011
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!------------------------------
  2. ---------- Person.cfc ----------
  3. -------------------------------->
  4. component hint="Person" output="false"{
  5.     function init(firstName, lastName, gender, hyphenateKidNames=false){
  6.         variables.firstName = firstName;
  7.         variables.lastName = lastName;
  8.         variables.gender = gender;
  9.         variables.spouse = "";
  10.         variables.hyphenateKidNames = hyphenateKidNames;
  11.         variables.children = [];
  12.     }
  13.    
  14.     function marry(spouse){
  15.         variables.spouse = spouse;
  16.     }
  17.    
  18.     function isMarried(){
  19.         if(isObject(variables.spouse))
  20.             return true;
  21.        
  22.         return false;
  23.     }
  24.    
  25.     function addAnotherMouthToFeed(child){
  26.         var numOfKids = numberOfKids() + 1;
  27.         variables.children[numOfKids] = child;
  28.     }
  29.    
  30.     function isAMan(){
  31.         return (variables.gender == "m");
  32.     }
  33.    
  34.     function hyphenateKidNames(value){
  35.         return variables.hyphenateKidNames;
  36.     }
  37.    
  38.     function numberOfKids(){
  39.         return arrayLen(variables.children);
  40.     }
  41.    
  42.     function getKid(favorite){
  43.         return variables.children[favorite];
  44.     }
  45.    
  46.     function getYoungestKid(){
  47.         return variables.children[arrayLen(variables.children)];
  48.     }
  49.    
  50.     function getKids(){
  51.         return variables.children;
  52.     }
  53.    
  54.     function getSpouse(){
  55.         return variables.spouse;
  56.     }
  57.    
  58.     function getLastName(){
  59.         return variables.lastName;
  60.     }
  61.    
  62.     function getFullName(){
  63.         return variables.firstName & " " & variables.lastName;
  64.     }
  65. }
  66.  
  67. <!------------------------------
  68. ---------- eHarmony.cfc --------
  69. -------------------------------->
  70. component hint="Spouse Factory" output="false"{
  71.     variables.maleNames = "Albus,Arnold,Arthur,Benjamin,Bert,Bill,Bruce,Caleb,Chance,Charles,Christopher,David,Dennis,Dewey,Donald,Elwood,Fonzie,Frank,George,Gilbert,Hank,Herbert,Huey,Isolder,Jacob,James,John,Joseph,Kevin,Kurt,Lamaar,Lucky,Mo,Mack,Ned,Paul,Peter,Richard,Raul,Reed,Robert,Sid,Steve,Taylor,Timothy,Ted,Tyrone,Travis,Vern,Warren,Willis,Wilford,Xavier,Xander";
  72.     variables.femaleNames = "Abigail,Annie,Beatrice,Bertha,Brittany,Courtney,Chelsie,Caitlin,Davina,Dora,Danita,Ellora,Elvira,Eleanor,Ellen,Francine,Farrah,Fantasia,Gaby,Gemma,Genevieve,Hannah,Hailey,Hillary,Idelle,Ingrid,Iris,Jada,Jade,Jamie,Katherine,Kimberly,Karen,Lacey,Mary,Martha,Maude,Nell,Nancy,Olga,Olivia,Penny,Peaches,Rachel,Rain,Sally,Sue,Sydney,Tabitha,Tara,Uma,Wilma,Vanessa,Xena";
  73.     variables.lastNames = "Dumbledore,Jeeves,Kelly,O'Kelley,O'Malley,Thurman,Foreman,Wayne,Potter,Snape,Kent,Rogers,Smith,Jackson,Hill,Jones,Jefferies,Hansen,Fields,Drew,Parker,Snyder,Riddle,Weasley,Octavius,Luther,Conners,Banner,Jennings,Bullock,Summers,Xavier,Pullman,Moore,Michaels,Petty,Kardashian,Obama,Clinton,Reagan,Bush,Carter,Lincoln,Ford,Washington,Franklin,Gates";
  74.     variables.numOfMaleNames = listLen(variables.maleNames);
  75.     variables.numOfFemaleNames = listLen(variables.femaleNames);
  76.     variables.numOfLastNames = listLen(variables.lastNames);
  77.    
  78.     function meetThatSpecialSomeone(lonelyPerson){
  79.         if(deservesAChanceAtHappiness(lonelyPerson))
  80.             lonelyPerson.marry(findMatch(lonelyPerson));
  81.     }
  82.  
  83.     function findMatch(lonelyPerson){
  84.         if(lonelyPerson.isAMan())
  85.             return woman();
  86.        
  87.         return man();
  88.     }
  89.  
  90.     function deservesAChanceAtHappiness(lonely){
  91.         // 33% chance this person is a loser.
  92.         return (randRange(1,3) > 1);
  93.     }
  94.    
  95.     function man(){
  96.         var fnamePos = randRange(1, variables.numOfMaleNames);
  97.         var lnamePos = randRange(1, variables.numOfLastNames);
  98.         var info = {
  99.             firstName = listGetAt(variables.maleNames, fnamePos),
  100.             lastName = listGetAt(variables.lastNames, lnamePos),
  101.             gender = "m"
  102.         };
  103.        
  104.         return new Person(argumentCollection=info);
  105.     }
  106.    
  107.     function woman(){
  108.         var fnamePos = randRange(1, variables.numOfFemaleNames);
  109.         var lnamePos = randRange(1, variables.numOfLastNames);
  110.         var info = {
  111.             firstName = listGetAt(variables.femaleNames, fnamePos),
  112.             lastName = listGetAt(variables.lastNames, lnamePos),
  113.             gender = "f",
  114.             hyphenateKidsName = (randRange(1,10) <= 2)
  115.         };
  116.        
  117.         return new Person(argumentCollection=info);
  118.     }
  119. }
  120.  
  121. <!------------------------------
  122. ---------- Bedroom.cfc ---------
  123. -------------------------------->
  124. component hint="Baby Factory" output="false"{
  125.     variables.boyNames = "Al;bus,Arnold,Arthur,Benjamin,Bert,Bill,Bruce,Caleb,Chance,Charles,Christopher,David,Dennis,Dewey,Donald,Elwood,Fonzie,Frank,George,Gilbert,Hank,Herbert,Huey,Isolder,Jacob,James,John,Joseph,Kevin,Kurt,Lamaar,Lucky,Mo,Mack,Ned,Paul,Peter,Richard,Raul,Reed,Robert,Sid,Steve,Taylor,Timothy,Ted,Tyrone,Travis,Vern,Warren,Willis,Wilford,Xavier,Xander";
  126.     variables.girlNames = "Abigail,Annie,Beatrice,Bertha,Brittany,Courtney,Chelsie,Caitlin,Davina,Dora,Danita,Ellora,Elvira,Eleanor,Ellen,Francine,Farrah,Fantasia,Gaby,Gemma,Genevieve,Hannah,Hailey,Hillary,Idelle,Ingrid,Iris,Jada,Jade,Jamie,Katherine,Kimberly,Karen,Lacey,Mary,Martha,Maude,Nell,Nancy,Olga,Olivia,Penny,Peaches,Rachel,Rain,Sally,Sue,Sydney,Tabitha,Tara,Uma,Wilma,Vanessa,Xena";
  127.     variables.numOfBoyNames = listLen(variables.boyNames);
  128.     variables.numOfGirlNames = listLen(variables.girlNames);
  129.    
  130.     function letsMakeBabies(father, mother){
  131.         while(haveAKid()){
  132.             var kid = {
  133.                 gender = boyOrGirl()
  134.             };
  135.            
  136.             kid.firstName = chooseFirstName(kid.gender);
  137.             kid.lastName = chooseLastName(father, mother);
  138.            
  139.             father.addAnotherMouthToFeed(new Person(argumentCollection=kid));
  140.             mother.addAnotherMouthToFeed(father.getYoungestKid());
  141.         }
  142.     }
  143.    
  144.     function boyOrGirl(){
  145.         // 50% chance of having a boy or girl
  146.         return (randRange(1,2) == 1) ? "m" : "f";
  147.     }
  148.  
  149.     function chooseLastName(father, mother){
  150.         var lastName = father.getLastName();
  151.        
  152.         // Does mom want to hyphenate the kid's name?
  153.         if(mother.hyphenateKidNames())
  154.             lastName = mother.getLastName() & "-" & father.getLastName();
  155.        
  156.         return lastName;
  157.     }
  158.  
  159.     function chooseFirstName(gender){
  160.         if(gender == "m"){
  161.             var namePos = randRange(1, variables.numOfBoyNames);
  162.             var chosenName = listGetAt(variables.boyNames, namePos);
  163.         }else{
  164.             var namePos = randRange(1, variables.numOfGirlNames);
  165.             var chosenName = listGetAt(variables.girlNames, namePos);
  166.         }
  167.        
  168.         return chosenName;
  169.     }
  170.  
  171.     function haveAKid(){
  172.         // 50% chance of having a kid
  173.         return (randRange(1,2) == 1);
  174.     }
  175. }
  176.  
  177. <!------------------------------
  178. ---------- familyTree.cfm ------
  179. -------------------------------->
  180. <cfscript>
  181.     // Our first man & woman.
  182.     man = new Person('Adam', 'West', "m");
  183.     woman = new Person('Eve', 'East', "f");
  184.    
  185.     man.marry(woman);
  186.    
  187.     // Baby factory
  188.     bedroom = new Bedroom();
  189.    
  190.     // Spouse factory.  Where our kids will go to meet people
  191.     eHarmony = new EHarmony();
  192.    
  193.     // Limit the # of descendants so we don't kill our server
  194.     descendants = 20;
  195.    
  196.     // Here we go...
  197.     spawn(man, woman, eHarmony, bedroom, descendants);
  198.    
  199.     function spawn(man, woman, matchMakingService, bedroom, descendants){
  200.         // Bom chicka wah wah!
  201.         bedroom.letsMakeBabies(man, woman);
  202.        
  203.         // Kids grow up and start families of thier own!
  204.         for(var i=1; i <= man.numberOfKids(); i++){
  205.             var person = man.getKid(i);
  206.             matchMakingService.meetThatSpecialSomeone(person);
  207.            
  208.             if(person.isMarried() && descendants > 0){
  209.                 if(person.isAMan())
  210.                     spawn(person, person.getSpouse(), matchMakingService, bedroom, descendants-1);
  211.                 else
  212.                     spawn(person.getSpouse(), person, matchMakingService, bedroom, descendants-1);
  213.             }
  214.         }
  215.        
  216.     }
  217. </cfscript>
  218. <cffunction name="displayTree" output="false">
  219.     <cfargument name="person" type="any" required="true">
  220.     <cfset var tree = "">
  221.     <cfset var kids = person.getKids()>
  222.     <cfset var kid = "">
  223.    
  224.     <cfsavecontent variable="tree">
  225.         <cfoutput>
  226.             <ul>
  227.                 <li>
  228.                     #person.getFullName()#
  229.                     <cfif person.isMarried()>
  230.                         married #person.getSpouse().getFullName()#
  231.                     </cfif>
  232.                     <cfloop array="#kids#" index="kid">
  233.                         #displayTree(kid)#
  234.                     </cfloop>
  235.                 </li>
  236.             </ul>
  237.         </cfoutput>
  238.     </cfsavecontent>
  239.  
  240.     <cfreturn tree>
  241. </cffunction>
  242. <html>
  243. <head>
  244.     <title>Family Tree</title>
  245.     <style type="text/css">
  246.         li{cursor:pointer;}
  247.     </style>
  248. </head>
  249. <body>
  250.     <cfoutput>#displayTree(man)#</cfoutput>
  251.    
  252.     <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
  253.     <script type="text/javascript">
  254.         $("li").click(function(event){
  255.             $(this).children("ul").toggle();
  256.             event.stopPropagation();
  257.         });
  258.     </script>
  259. </body>
  260. </html>
Advertisement
Add Comment
Please, Sign In to add comment