Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!------------------------------
- ---------- Person.cfc ----------
- -------------------------------->
- component hint="Person" output="false"{
- function init(firstName, lastName, gender, hyphenateKidNames=false){
- variables.firstName = firstName;
- variables.lastName = lastName;
- variables.gender = gender;
- variables.spouse = "";
- variables.hyphenateKidNames = hyphenateKidNames;
- variables.children = [];
- }
- function marry(spouse){
- variables.spouse = spouse;
- }
- function isMarried(){
- if(isObject(variables.spouse))
- return true;
- return false;
- }
- function addAnotherMouthToFeed(child){
- var numOfKids = numberOfKids() + 1;
- variables.children[numOfKids] = child;
- }
- function isAMan(){
- return (variables.gender == "m");
- }
- function hyphenateKidNames(value){
- return variables.hyphenateKidNames;
- }
- function numberOfKids(){
- return arrayLen(variables.children);
- }
- function getKid(favorite){
- return variables.children[favorite];
- }
- function getYoungestKid(){
- return variables.children[arrayLen(variables.children)];
- }
- function getKids(){
- return variables.children;
- }
- function getSpouse(){
- return variables.spouse;
- }
- function getLastName(){
- return variables.lastName;
- }
- function getFullName(){
- return variables.firstName & " " & variables.lastName;
- }
- }
- <!------------------------------
- ---------- eHarmony.cfc --------
- -------------------------------->
- component hint="Spouse Factory" output="false"{
- 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";
- 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";
- 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";
- variables.numOfMaleNames = listLen(variables.maleNames);
- variables.numOfFemaleNames = listLen(variables.femaleNames);
- variables.numOfLastNames = listLen(variables.lastNames);
- function meetThatSpecialSomeone(lonelyPerson){
- if(deservesAChanceAtHappiness(lonelyPerson))
- lonelyPerson.marry(findMatch(lonelyPerson));
- }
- function findMatch(lonelyPerson){
- if(lonelyPerson.isAMan())
- return woman();
- return man();
- }
- function deservesAChanceAtHappiness(lonely){
- // 33% chance this person is a loser.
- return (randRange(1,3) > 1);
- }
- function man(){
- var fnamePos = randRange(1, variables.numOfMaleNames);
- var lnamePos = randRange(1, variables.numOfLastNames);
- var info = {
- firstName = listGetAt(variables.maleNames, fnamePos),
- lastName = listGetAt(variables.lastNames, lnamePos),
- gender = "m"
- };
- return new Person(argumentCollection=info);
- }
- function woman(){
- var fnamePos = randRange(1, variables.numOfFemaleNames);
- var lnamePos = randRange(1, variables.numOfLastNames);
- var info = {
- firstName = listGetAt(variables.femaleNames, fnamePos),
- lastName = listGetAt(variables.lastNames, lnamePos),
- gender = "f",
- hyphenateKidsName = (randRange(1,10) <= 2)
- };
- return new Person(argumentCollection=info);
- }
- }
- <!------------------------------
- ---------- Bedroom.cfc ---------
- -------------------------------->
- component hint="Baby Factory" output="false"{
- 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";
- 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";
- variables.numOfBoyNames = listLen(variables.boyNames);
- variables.numOfGirlNames = listLen(variables.girlNames);
- function letsMakeBabies(father, mother){
- while(haveAKid()){
- var kid = {
- gender = boyOrGirl()
- };
- kid.firstName = chooseFirstName(kid.gender);
- kid.lastName = chooseLastName(father, mother);
- father.addAnotherMouthToFeed(new Person(argumentCollection=kid));
- mother.addAnotherMouthToFeed(father.getYoungestKid());
- }
- }
- function boyOrGirl(){
- // 50% chance of having a boy or girl
- return (randRange(1,2) == 1) ? "m" : "f";
- }
- function chooseLastName(father, mother){
- var lastName = father.getLastName();
- // Does mom want to hyphenate the kid's name?
- if(mother.hyphenateKidNames())
- lastName = mother.getLastName() & "-" & father.getLastName();
- return lastName;
- }
- function chooseFirstName(gender){
- if(gender == "m"){
- var namePos = randRange(1, variables.numOfBoyNames);
- var chosenName = listGetAt(variables.boyNames, namePos);
- }else{
- var namePos = randRange(1, variables.numOfGirlNames);
- var chosenName = listGetAt(variables.girlNames, namePos);
- }
- return chosenName;
- }
- function haveAKid(){
- // 50% chance of having a kid
- return (randRange(1,2) == 1);
- }
- }
- <!------------------------------
- ---------- familyTree.cfm ------
- -------------------------------->
- <cfscript>
- // Our first man & woman.
- man = new Person('Adam', 'West', "m");
- woman = new Person('Eve', 'East', "f");
- man.marry(woman);
- // Baby factory
- bedroom = new Bedroom();
- // Spouse factory. Where our kids will go to meet people
- eHarmony = new EHarmony();
- // Limit the # of descendants so we don't kill our server
- descendants = 20;
- // Here we go...
- spawn(man, woman, eHarmony, bedroom, descendants);
- function spawn(man, woman, matchMakingService, bedroom, descendants){
- // Bom chicka wah wah!
- bedroom.letsMakeBabies(man, woman);
- // Kids grow up and start families of thier own!
- for(var i=1; i <= man.numberOfKids(); i++){
- var person = man.getKid(i);
- matchMakingService.meetThatSpecialSomeone(person);
- if(person.isMarried() && descendants > 0){
- if(person.isAMan())
- spawn(person, person.getSpouse(), matchMakingService, bedroom, descendants-1);
- else
- spawn(person.getSpouse(), person, matchMakingService, bedroom, descendants-1);
- }
- }
- }
- </cfscript>
- <cffunction name="displayTree" output="false">
- <cfargument name="person" type="any" required="true">
- <cfset var tree = "">
- <cfset var kids = person.getKids()>
- <cfset var kid = "">
- <cfsavecontent variable="tree">
- <cfoutput>
- <ul>
- <li>
- #person.getFullName()#
- <cfif person.isMarried()>
- married #person.getSpouse().getFullName()#
- </cfif>
- <cfloop array="#kids#" index="kid">
- #displayTree(kid)#
- </cfloop>
- </li>
- </ul>
- </cfoutput>
- </cfsavecontent>
- <cfreturn tree>
- </cffunction>
- <html>
- <head>
- <title>Family Tree</title>
- <style type="text/css">
- li{cursor:pointer;}
- </style>
- </head>
- <body>
- <cfoutput>#displayTree(man)#</cfoutput>
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
- <script type="text/javascript">
- $("li").click(function(event){
- $(this).children("ul").toggle();
- event.stopPropagation();
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment