ASSIGNMENT #9
DEANNA'S PET SHOP
Create a class pet. This class should include variables for:DEANNA'S PET SHOP
type (such as dog, cat), name (fido), breed (german shepard), color (yellow). Include methods: talk (bark), move (run, walk), also include set and get methods
Make an instance of the class
call the set methods for each variable (type, name, breed, color) and get methods to change and display the values. Also call the talk and move methods.
Adjust Assignment 9 to include the following:
Class Animal Kingdom
Include two properties (such as body style and size). You must also include a way to change these properties, either by a constructor, or by set methods.
Class Canine or Feline (this class will inherit (extends) the Animal Kingdom class)
includes two properties (such as breed and color). You must also include a way to change these properties, either by a constructor, or by set methods.
Adjust your Pet class to extend the Canine or Feline class.
Add a way (either through the constructors or set methods) to change the values in the four properties in the Animal Kingdom and Canine (or Feline) classes.
Print or echo the values in the four new properties, in addition to what was already printed out in assignment 9.
------------------------------------------------------------------------------------------------------------
The code above is for Assignment 10 the completed adjustment of Assignment 9
If you look at the two you will notice in Assignment 10 there are additional traits for the pet. This is achieved with the extension of the class.
The first part of the code I will address is the HTML for the project.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Assignment 10</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.5.3/modernizr.min.js"></script>
<script type="text/javascript" src="Assignment9.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" media="screen" />
<style type="text/css">
body {
background-color: pink ;
font-family: Verdana, Tahoma, Arial;
font-size: 11pt;
}
textarea {
resize: none;
}
#petViewer {
font-family: Courier, Courier New, "OCR A Extended";
font-size: 12pt;
}
#petSetupDiv {
overflow-x: hidden;
}
#container {
width: 800px;
margin-left: auto;
margin-right: auto;
}
.petFormField {
display: block;
width: 400px;
}
.petFormLabel {
display: inline-block;
width: 100px;
font-weight: bold;
}
.petFormLabelWide {
display: inline-block;
width: 200px;
font-weight: bold;
margin-bottom: 2px;
text-decoration: underline;
}
.petFormInput {
display: inline;
width: 130px;
}
h1 {
text-align:center;
}
#footer {font-size: small;
text-align:center;
clear:right;
padding-bottom:20px;
}
</style>
</head>
<body>
<div id="petDetailOuterDiv" name="petDetailOuterDiv" style="display: none;">
<div id="petDetailDiv" name="petDetailDiv"></div>
</div>
<div id="petSetupOuterDiv" name="petSetupOuterDiv" style="display: none;">
<div id="petSetupDiv" name="petSetupDiv">
<form name="petSetupForm" id="petSetupForm" action="AnimalFarm.php" method="post">
<input type="hidden" id="formAction" name="formAction" value="configurePet" />
<div style="float: left; width: 45%;">
<span class="petFormField"> <label class="petFormLabelWide">Required Fields</label></span>
<span class="petFormField"> <label class="petFormLabel">Species:</label>
<select class="petFormInput" id="speciesSelect" name="speciesSelect">
<option selected="selected" value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="bird">Bird</option>
<option value="dinosaur">Dinosaur</option>
<option value="alien">Alien</option>
</select> </span>
<span class="petFormField"><label class="petFormLabel">Breed:</label>
<input class="petFormInput" type="text" required="required" maxlength="15" id="breedTextBox" name="breedTextBox" />
</span>
<span class="petFormField"><label class="petFormLabel">Color:</label>
<select class="petFormInput" id="colorSelect" name="colorSelect">
<option selected="selected" value="black">Black</option>
<option value="pink">Pink</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select></span>
<span class="petFormField"><label class="petFormLabel">Name:</label>
<input class="petFormInput" type="text" required="required" maxlength="15" id="nameTextBox" name="nameTextBox" />
</span>
</div>
<div style="float: left; padding-left: 10px; width: 45%;">
<span class="petFormField"> <label class="petFormLabelWide">Optional Fields</label></span>
<span class="petFormField"> <label class="petFormLabel">Height:</label>
<input class="petFormInput" type="text" maxlength="10" id="heightTextBox" name="heightTextBox" />
</span>
<span class="petFormField"> <label class="petFormLabel">Weight:</label>
<input class="petFormInput" type="text" maxlength="10" id="weightTextBox" name="weightTextBox" />
</span>
<span class="petFormField"> <label class="petFormLabel">Age:</label>
<input class="petFormInput" type="text" maxlength="10" id="ageTextBox" name="ageTextBox" />
</span>
</div>
<input type="hidden" id="petDataHiddenField" name="petDataHiddenField"/>
</form>
</div>
</div>
<div id="container">
<h1>DEANNA'S PET SHOP </h1>
<h1>Create New Pet to Play</h1>
<div style="text-align: center;"><IMG HEIGHT="200" WIDTH="300" SRC="pet.jpg" ALT="image"></div>
<div style="text-align: center;">
<p id="petLabel" name="petLabel">
</p>
<textarea readonly="readonly" id="petViewer" name="petViewer" rows="25" cols="70">
</textarea>
<p id="petSpeak" name="petSpeak">
</p>
<p>
<input type="button" title="Tell me all about my pet." id="dataPetButton" value="Info" />
<input type="button" title="Makes your pet move." id="movePetButton" value="Move" />
<input type="button" title="Makes your pet go back." id="sitPetButton" value="Sit" />
<input type="button" title="Makes your pet speak." id="speakPetButton" value="Speak" />
<input type="button" title="Makes you a new pet." id="configurePetButton" value="New Pet" />
<input type="button" title="Your pet runs away." id="runawayPetButton" value="Escape" />
</p>
</div>
<p id="footer">Copyright © 2013 DeAnna Riddlespur</p>
</div>
</body>
</html>
This is how this file works………
This is what the java script file looks like
$(document).ready(function() {
$("#configurePetButton").button().click(function(event) {
ConfigurePet();
})
$("#movePetButton").button().click(function(event) {
MovePet();
})
$("#dataPetButton").button().click(function(event) {
DataOnPet();
})
$("#runawayPetButton").button().click(function(event) {
RunawayPet();
})
$("#speakPetButton").button().click(function(event) {
SpeakPet();
})
$("#sitPetButton").button().click(function(event) {
SitPet();
})
});
function DataOnPet() {
if ($("#petDataHiddenField").val() == "") {
alert("Maybe you should adopt a pet before you ask all kinds of questions.");
} else {
var theData = $("#petDataHiddenField").val();
$("#petDetailDiv").load("AnimalFarm.php", {
"petDataHiddenField" : theData,
"returnType" : "data"
}).dialog({
modal : true,
title : "All About My Pet",
resizable : false,
draggable : false,
width : 500,
height : 200
});
}
}
function RunawayPet() {
if ($("#petDataHiddenField").val() == "") {
alert("Maybe you should adopt a pet before you let it escape.");
} else {
if (confirm("You heartless bastard! Really?")) {
$("#petViewer").val('');
$("#petViewer").removeAttr("title");
var theData = $("#petDataHiddenField").val();
$("#petLabel").load("AnimalFarm.php", {
"petDataHiddenField" : theData,
"returnType" : "escaped"
}).fadeOut(1600, "swing", function() {
ResetLabel();
});
$("#petDataHiddenField").val('');
}
}
}
function SitPet() {
if ($("#petDataHiddenField").val() == "") {
if (confirm("No pet is configured. Create a new one now?")) {
ConfigurePet();
} else {
return;
}
} else {
var theData = $("#petDataHiddenField").val();
$.ajax({
url : "AnimalFarm.php",
data : {
"petDataHiddenField" : theData,
"returnType" : "sit"
},
dataType : 'text',
type : "POST",
async : false,
cache : false,
success : function(resp) {
$("#petViewer").val(resp);
}
});
}
}
function MovePet() {
if ($("#petDataHiddenField").val() == "") {
if (confirm("No pet is configured. Create a new one now?")) {
ConfigurePet();
} else {
return;
}
} else {
var theData = $("#petDataHiddenField").val();
$.ajax({
url : "AnimalFarm.php",
data : {
"petDataHiddenField" : theData,
"returnType" : "move"
},
dataType : 'text',
type : "POST",
async : false,
cache : false,
success : function(resp) {
$("#petViewer").val(resp);
}
});
}
}
function SpeakPet() {
if ($("#petDataHiddenField").val() == "") {
if (confirm("No pet is configured. Create a new one now?")) {
ConfigurePet();
} else {
return;
}
} else {
var theData = $("#petDataHiddenField").val();
$("#petSpeak").load("AnimalFarm.php", {
"petDataHiddenField" : theData,
"returnType" : "speak"
}).fadeOut(1600, "swing", function() {
ResetSpeak();
});
}
}
function ResetLabel() {
$("#petLabel").html(" ").show();
}
function ResetSpeak() {
$("#petSpeak").html(" ").show();
}
function ConfigurePet() {
if ($("#petDataHiddenField").val() != "") {
if (confirm("You already have a pet. Do you want to create a new one?")) {
// don't clear this field here -- if blank here, will cause errors if the user cancels on the dialog box.
// $("#petDataHiddenField").val("");
} else {
return;
}
}
$("#petSetupDiv").dialog({
modal : true,
title : "New Pet",
resizable : false,
draggable : false,
width : 600,
buttons : {
"Adopt" : function() {
if ($("#speciesSelect").val() == "" || $("#breedTextBox").val() == "" || $("#colorSelect").val() == "" || $("#nameTextBox").val() == "") {
alert("All required fields must be completed.");
if ($("#breedTextBox").val() == "") {
$("#breedTextBox").css({
"background-color" : "khaki"
});
} else {
$("#breedTextBox").css({
"background-color" : ""
});
}
if ($("#nameTextBox").val() == "") {
$("#nameTextBox").css({
"background-color" : "khaki"
});
} else {
$("#nameTextBox").css({
"background-color" : ""
});
}
return false;
}
// clear the form HERE so things still work after a cancel
$("#petDataHiddenField").val("");
var theData = $("#petSetupForm").serialize();
$.ajax({
url : "AnimalFarm.php",
data : theData,
dataType : 'json',
type : "POST",
async : false,
cache : false,
success : function(resp) {
$("#petDataHiddenField").val(resp.PetData)
$("#petLabel").html("My " + resp.PetSpecies.toLowerCase() + " is a " + resp.PetColor + " " + resp.PetBreed + " named " + resp.PetName + "." );
$("#petViewer").css("color", resp.PetColor);
$("#petViewer").val(resp.PetPicture);
$("#petViewer").attr("title", "A picture of " + resp.PetName);
},
error : function(jqXHR, textStatus, errorThrown) {
alert("ERROR OCCURRED: " + textStatus + ": " + jqXHR + " : " + errorThrown);
},
complete : function() {
// loadingIndicator.fadeOut();
$("#petSetupDiv").dialog("close");
}
});
},
"Cancel" : function() {
$("#petSetupDiv").dialog("close");
}
}
});
}
This is how the java script file works…………
The beginning of the php files
animalfarm.php
<?php
include_once "AnimalKingdomClass.php";
include_once "AvianGroupClass.php";
include_once "MammalGroupClass.php";
include_once "SaurischianGroupClass.php";
include_once "XenomorphGroupClass.php";
include_once "AlienClass.php";
include_once "BirdClass.php";
include_once "CatClass.php";
include_once "DinosaurClass.php";
include_once "DogClass.php";
function GetObject($species, $name) {
switch (strtolower($species)) {
case "bird" :
return new Bird($name);
case "cat" :
return new Cat($name);
case "dinosaur" :
return new Dinosaur($name);
case "dog" :
return new Dog($name);
default :
return new Alien($name);
}
}
if (empty($_POST["petDataHiddenField"])) {
$thePet = GetObject($_POST["speciesSelect"], $_POST["nameTextBox"]);
$thePet -> setBreed($_POST["breedTextBox"]);
$thePet -> setColor($_POST["colorSelect"]);
// set optional properties if included in post request, otherwise, ignore them
if (!empty($_POST["weightTextBox"])) {
$thePet -> setWeight($_POST["weightTextBox"]);
}
if (!empty($_POST["heightTextBox"])) {
$thePet -> setHeight($_POST["heightTextBox"]);
}
if (!empty($_POST["ageTextBox"])) {
$thePet -> setAge($_POST["ageTextBox"]);
}
// populate json response from AnimalClass object
$result -> PetSpecies = $thePet -> Species();
$result -> PetBreed = $thePet -> getBreed();
$result -> PetColor = $thePet -> getColor();
$result -> PetName = $thePet -> getName();
$result -> PetBreed = $thePet -> getBreed();
$result -> PetPicture = $thePet -> getPicture();
$result -> PetKingdom = $thePet -> Kingdom();
$result -> PetGroup = $thePet -> Group();
$result -> PetAge = $thePet -> getAge();
$result -> PetHeight = $thePet -> getHeight();
$result -> PetWeight = $thePet -> getWeight();
// $reply -> PetSpecies = $thePet->Kingdom() . " " . $_POST["breedTextBox"] . " " . date("Y/m/d h:i:s");
// echo json_encode($reply);
// exit ;
$result -> PetData = urlencode(serialize($thePet));
echo json_encode($result);
} else {
$thePet = unserialize(urldecode($_POST["petDataHiddenField"]));
switch (strtolower($_POST["returnType"])) {
case "speak" :
echo($thePet -> Speak());
break;
case "move" :
echo($thePet -> Move());
break;
case "sit" :
echo($thePet -> getPicture());
break;
case "data" :
$html = "<strong>Name: " . $thePet -> getName() . "</strong><br/>";
$html .= "<div style='margin-top: 10px; float: left; width: 45%;'>";
$html .= "Species: " . $thePet -> Species() . "<br/>";
$html .= "Breed: " . $thePet -> getBreed() . "<br/>";
$html .= "Color: " . $thePet -> getColor() . "<br/>";
$html .= "</div>";
$html .= "<div style='margin-top: 10px; float: left; width: 45%;'>";
if ($thePet -> getHeight() != "") {
$html .= "Height: " . $thePet -> getHeight() . "<br/>";
}
if ($thePet -> getWeight() != "") {
$html .= "Weight: " . $thePet -> getWeight() . "<br/>";
}
if ($thePet -> getAge() != "") {
$html .= "Age: " . $thePet -> getAge() . "<br/>";
}
$html .= "</div>";
echo $html;
break;
case "escaped" :
echo($thePet -> getName() . " has run away!");
break;
}
}
?>The code responsible for the images in the project
<?php
class AnimalArt {
private $animal;
public function AnimalArt($animal) {
$this -> animal = $animal;
}
public function Move() {
$movement = (rand(1, 100) % 4);
$picture = $this -> Draw();
for ($x = 0; $x <= $movement; $x++) {
$picture = "\n" . $picture;
}
return $picture;
}
public function Draw() {
switch (strtolower($this->animal)) {
case "dog" :
return $this -> Dog();
case "cat" :
return $this -> Cat();
case "dinosaur" :
return $this -> Dinosaur();
case "bird" :
return $this -> Bird();
default :
return $this -> Alien();
}
}
private function Dog() {
$picture = "";
$picture .= " " . "\n";
$picture .= " ;\ " . "\n";
$picture .= " |' \ " . "\n";
$picture .= " _ ; : ; " . "\n";
$picture .= " / `-. /: : | " . "\n";
$picture .= " | ,-.`-. ,': : | " . "\n";
$picture .= " \ : `. `. ,'-. : | " . "\n";
$picture .= " \ ; ; `-.__,' `-.| " . "\n";
$picture .= " \ ; ; ::: ,::'`:. `. " . "\n";
$picture .= " \ `-. : ` :. `. \ " . "\n";
$picture .= " \ \ , ; ,: (\ " . "\n";
$picture .= " \ :., :. ,'o)): ` `-. " . "\n";
$picture .= " ,/,' ;' ,::~'`.`---' `. `-._ " . "\n";
$picture .= " ,/ : ; '~ `;' ,--`. " . "\n";
$picture .= " ;/ :; ; ,:' ( ,:) " . "\n";
$picture .= " ,.,:. ; ,:., ,-._ `. \~~'/ " . "\n";
$picture .= " '::' `:'` ,'( \`._____.-'~' " . "\n";
$picture .= " ;, ; `. `. `._`-. \\ " . "\n";
$picture .= " ;:. ;: `-._`-.\ \`. " . "\n";
$picture .= " '`:. : |' `. `\ ) \ " . "\n";
$picture .= " ` ;: | `--\__,' " . "\n";
$picture .= " '` ,' " . "\n";
$picture .= " ,-' " . "\n";
return $picture;
}
private function Cat() {
$picture = "";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " _ " . "\n";
$picture .= " \`*-. " . "\n";
$picture .= " ) _`-. " . "\n";
$picture .= " . : `. . " . "\n";
$picture .= " : _ ' \ " . "\n";
$picture .= " ; *` _. `*-._ " . "\n";
$picture .= " `-.-' `-. " . "\n";
$picture .= " ; ` `. " . "\n";
$picture .= " :. . \ " . "\n";
$picture .= " . \ . : .-' . " . "\n";
$picture .= " ' `+.; ; ' : " . "\n";
$picture .= " : ' | ; ;-. " . "\n";
$picture .= " ; ' : :`-: _.`* ; " . "\n";
$picture .= " .*' / .*' ; .*`- +' `*' " . "\n";
$picture .= " `*-* `*-* `*-*' " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
return $picture;
}
private function Dinosaur() {
$picture = "";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " __.--'~~~~~`--. " . "\n";
$picture .= " .. __. .-~ ~-. " . "\n";
$picture .= " ((\ / `}.~ `. " . "\n";
$picture .= " \\\ .{ } / \ \ " . "\n";
$picture .= "(\ \\~~ } | } \ " . "\n";
$picture .= " \`.-~ -@~ } ,-,. | ) \ " . "\n";
$picture .= " (___ ) _} ( : | / / `._ " . "\n";
$picture .= " `----._-~. _\ \ |_ \ / /-.__ `._ " . "\n";
$picture .= " ~~----~~ \ \| ~~--~~~( + / ~-._ ~-._ " . "\n";
$picture .= " / / \ \ ~--.,___~_-_. " . "\n";
$picture .= " __/ / _\ ) " . "\n";
$picture .= " .<___.' .<___/ " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
return $picture;
}
private function Bird() {
$picture = "";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " /\\ " . "\n";
$picture .= " .\\\.. " . "\n";
$picture .= " \\ \\ " . "\n";
$picture .= " \ (o) / " . "\n";
$picture .= " (/ \ " . "\n";
$picture .= " /\ \ " . "\n";
$picture .= " /// \ " . "\n";
$picture .= " ///| | " . "\n";
$picture .= " ////| | " . "\n";
$picture .= " ////// / " . "\n";
$picture .= " |//// / " . "\n";
$picture .= " /|////--V/ " . "\n";
$picture .= " //\//| | " . "\n";
$picture .= " ___////__\___\__________ " . "\n";
$picture .= " ()_________'___'_________) " . "\n";
$picture .= " \\\\ " . "\n";
$picture .= " \\\ " . "\n";
$picture .= " \\ " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
return $picture;
}
private function Alien() {
$picture = "";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " (_\ /_) " . "\n";
$picture .= " )) (( " . "\n";
$picture .= " .-~~~~~~~-. " . "\n";
$picture .= " /^\/ _. _. \/^\ " . "\n";
$picture .= " \( /__\ /__\ )/ " . "\n";
$picture .= " \, \o_/_\o_/ ,/ " . "\n";
$picture .= " \ (_) / " . "\n";
$picture .= " `-.'==='.-' " . "\n";
$picture .= " __) - (__ " . "\n";
$picture .= " / `~~~` \ " . "\n";
$picture .= " / / \ \ " . "\n";
$picture .= " \ : ; / " . "\n";
$picture .= " \|==(*)==|/ " . "\n";
$picture .= " : : " . "\n";
$picture .= " \ | / " . "\n";
$picture .= " ___)=|=(___ " . "\n";
$picture .= " {____/ \____} " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
$picture .= " " . "\n";
return $picture;
?>
}
} private $animal
{PRIVATE}You will see this alot throughout the code
because this is declared as private it may only be accessed by the class that defines the member (animal)
return $picture
{RETURN} IF CALLED FROM WITHIN A FUNCTION, THE RETURN STATEMENT IMMEDIATELY ENDS EXECUTION OF THE CURRENT FUNCTION, AND RETURNS ITS ARGUMENT AS THE VALUE OF THE FUNCTION CALL{RETURN} WILL ALSO END THE EXECUTION OF AN EVAL() STATEMENT OR SCRIPT FILE.
No comments:
Post a Comment