Skip to content

Instantly share code, notes, and snippets.

@bpmct
Created February 27, 2020 00:26
Show Gist options
  • Save bpmct/fd3f630f86943e56542ff0a780d8b6f0 to your computer and use it in GitHub Desktop.
Save bpmct/fd3f630f86943e56542ff0a780d8b6f0 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PetApp
{
public class Pets
{
public List<Pet> petList = new List<Pet>();
//1. In the Pets class, "this:int petEl" is an indexer property based on the int "petEl".
public Pet this[int nPetEl]
{
get
{
Pet returnVal;
try
{
returnVal = (Pet)petList[nPetEl];
}
catch
{
returnVal = null;
}
return (returnVal);
}
set
{
// if the index is less than the number of list elements
if (nPetEl < petList.Count)
{
// update the existing value at that index
petList[nPetEl] = value;
}
else
{
// add the value to the list
petList.Add(value);
}
}
}
//2. In Pets, "Count:int:r" is a read-only property that returns (get's) petList.Count.
public int Count
{
get
{
return petList.Count;
}
}
//3. Pets.Add(pet:Pet) should call petList.Add(pet)
public void Add(Pet thePet)
{
petList.Add(thePet);
}
//4. Pets.Remove(pet:Pet) should call petList.Remove(pet)
public void Remove(Pet thePet)
{
petList.Remove(thePet);
}
//5. Pets.RemoveAt(petEl:int) should call petList.RemoveAt(petEl)
public void RemoveAt(int petEl)
{
petList.RemoveAt(petEl);
}
}
public abstract class Pet
{
private string name;
public int Age;
//6. Pet.Name is the readwrite property for the private name field. You will need to use that in your output to indicate which pet is responding.
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public abstract void Eat();
public abstract void Play();
public abstract void GotoVet();
//7. If there is a 4th box for a class, then that is the list of required constructors.
public Pet() { }
public Pet(string theName, int theAge)
{
name = theName;
Age = theAge;
}
}
public interface ICat
{
void Eat();
void Play();
void Scratch();
void Purr();
}
public interface IDog
{
void Eat();
void Play();
void Bark();
void NeedWalk();
void GotoVet();
}
public class Cat : Pet, ICat
{
//8. The member methods in Dog and Cat should call Console.WriteLine() with the pet's name and a phrase that suggests the activity. (Be creative!).
public override void Eat()
{
Console.WriteLine($"{Name} is eating fish.");
}
public override void Play()
{
Console.WriteLine($"{Name} is playing with yarn!");
}
public void Purr()
{
Console.WriteLine($"{Name} goes purrr...");
}
public void Scratch()
{
Console.WriteLine($"{Name} is itching... scratch scratch.");
}
public override void GotoVet()
{
Console.WriteLine($"{Name} went to the vet... Poor cat");
}
//I decided to call the Pet constructor to give it a name and age
public Cat() { }
public Cat(string theName, int theAge) : base(theName, theAge) { }
}
public class Dog : Pet, IDog
{
public string license;
//8. The member methods in Dog and Cat should call Console.WriteLine() with the pet's name and a phrase that suggests the activity. (Be creative!).
public override void Eat()
{
Console.WriteLine($"{Name} is eating a bone.");
}
public override void Play()
{
Console.WriteLine($"{Name} is playing fetch!");
}
public void Bark()
{
Console.WriteLine($"{Name} goes BARK!!");
}
public void NeedWalk()
{
Console.WriteLine($"{Name} needs a walk.");
}
public override void GotoVet()
{
Console.WriteLine($"{Name} went to the vet... Poor dog :( ");
}
//7. If there is a 4th box for a class, then that is the list of required constructors.
public Dog() { }
public Dog(string szLicense, string szName, int nAge) : base(szName, nAge)
{
license = szLicense;
}
}
class Program
{
static void Main(string[] args)
{
//1. Create reference variables for the pets and interfaces:
Pet thisPet = null;
Dog dog = null;
Cat cat = null;
IDog iDog = null;
ICat iCat = null;
//2. Create the list of pets:
Pets pets = new Pets();
//3. Create the random number generator:
Random rand = new Random();
//List of cat names to randomly chose from
string[] catNames = new string[] { "Jake", "TC", "Filo", "Kono", "Casper" };
//List of dog names to randomly chose from
string[] dogNames = new string[] { "Dino", "Spark", "Buddy", "Buster", "Susan" };
//4. Implement a for() loop that iterates 50 times, and within the for() loop:
for (int i = 0; i < 50; i++)
{
//a. 1 in 10 chance of adding an animal
if (rand.Next(1, 11) == 1)
{
// 50% chance of adding a dog
if (rand.Next(0, 2) == 0)
{
//Note that the Dog() object must be passed the 3 fields in the constructor.
dog = new Dog(
"DL" + rand.Next(4544, 9020), ////random license ID
dogNames[rand.Next(0, dogNames.Length)], //random name
rand.Next(4, 13)
);
pets.Add(dog); //random age
Console.WriteLine($"Adding a new dog named {dog.Name}. They are {dog.Age} years old with ID: {dog.license}.");
}
else
{
// else add a cat
cat = new Cat(
catNames[rand.Next(0, catNames.Length)], //random name
rand.Next(3, 9) //random age
);
pets.Add(cat);
Console.WriteLine($"Adding a new cat named {cat.Name}. They are {cat.Age} years old.");
}
}
//b. Else if it did not generate a 1 then set thisPet to a random pet from the pets List<> based on a random number between 0 and pets.Count.
//Note that if there were no pets added yet, this will return null.
//If null was returned, then continue to the top of the for() loop.
else
{
thisPet = pets[rand.Next(0, pets.Count)];
if (thisPet != null)
{
//c. If a valid pet was returned, set your interface variable (iDog or iCat) to thisPet (based on thisPet.GetType())
if (thisPet.GetType() == typeof(Cat))
{
iCat = (ICat)thisPet;
//Randomly call one of the member methods of the interface.
switch (rand.Next(0, 4))
{
case 0:
iCat.Eat();
break;
case 1:
iCat.Play();
break;
case 2:
iCat.Scratch();
break;
case 3:
iCat.Purr();
break;
default:
Console.WriteLine("Ben messed up with the cat switch...");
break;
}
}
else if (thisPet.GetType() == typeof(Dog))
{
iDog = (IDog)thisPet;
//Randomly call one of the member methods of the interface.
switch (rand.Next(0, 5))
{
case 0:
iDog.Eat();
break;
case 1:
iDog.Play();
break;
case 2:
iDog.Bark();
break;
case 3:
iDog.NeedWalk();
break;
case 4:
iDog.GotoVet();
break;
default:
Console.WriteLine("Ben messed up with the dog switch...");
break;
}
}
else
{
Console.WriteLine("Ben, you messed up somehow.");
}
}
}
}
//ReadLine to keep the window open
Console.Write("\n\nPress ENTER to exit: ");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment