0% found this document useful (0 votes)
16 views5 pages

Week 7

The document defines classes for animals (Animal), dogs (Dog), cats (Cat) and bats (Bat) with properties and methods. The main method displays a menu to create an animal by species or list existing animals. Based on the selection, it either prompts for animal details and adds it to a static list, or iterates through the lists to display animals.

Uploaded by

Kyla Barribal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Week 7

The document defines classes for animals (Animal), dogs (Dog), cats (Cat) and bats (Bat) with properties and methods. The main method displays a menu to create an animal by species or list existing animals. Based on the selection, it either prompts for animal details and adds it to a static list, or iterates through the lists to display animals.

Uploaded by

Kyla Barribal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//PascalCase for public properties, Class Names, Method names

//camelCase for private properties, arguements, private fields, parameters


public class Animal {
private static int id = 1;
protected int nextId { get; set; }
public void setNextId()
{
this.nextId = id++;
}
}
public class Dog : Animal
{
public static List<Dog> Dogs { get; } = new List<Dog>();
public Dog() {
this.legCount = 0;
this.color = "None";
}
public int Id { get; }
private int legCount { get; }
private string color { get; }
public Dog(string? color, int legCount)
{
this.color = color == null ? "No Color" : color;
this.legCount = legCount;
this.setNextId();
this.Id = this.nextId;
Dogs.Add(this);
}

public bool getDog(int id)


{
var found = Dogs.Find(it => it.Id == id);
if (found != null)
{
Console.WriteLine("ID: " + found.Id);
Console.WriteLine("Color: " + found.color);
Console.WriteLine("Leg Count: " + found.legCount);
return true;
}
return false;
}
}
public class Cat : Animal
{
public static List<Cat> Cats { get; } = new List<Cat>();
public Cat()
{
this.eyeColor = "None";
this.height = 0;
}
public int Id { get; }
private string eyeColor { get; }
private double height { get; }
public Cat(string? eyeColor, double height)
{
this.eyeColor = eyeColor == null ? "No eye color" : eyeColor;
this.height = height;
this.setNextId();
this.Id = this.nextId;
Cats.Add(this);
}
public bool getCat(int id)
{
var found = Cats.Find(it => it.Id == id);
if (found != null)
{
Console.WriteLine("ID: " + found.Id);
Console.WriteLine("Eye Color: " + found.eyeColor);
Console.WriteLine("Height: " + found.height);
return true;
}
return false;
}
}
public class Bat : Animal
{
public static List<Bat> Bats { get; } = new List<Bat>();
public Bat()
{
this.wingspan = 0;
this.hairColor = "Default";
}
public int Id { get; }
private double wingspan { get; }
private string hairColor { get; }
public Bat(string? hairColor, double wingspan)
{
this.setNextId();
this.wingspan = wingspan;
this.hairColor = hairColor == null ? "No haircolor" : hairColor;
this.Id = this.nextId;
Bats.Add(this);
}
public bool getBat(int id)
{
var found = Bats.Find(it => it.Id == id);
if (found != null)
{
Console.WriteLine("ID: " + found.Id);
Console.WriteLine("Wingspan: " + found.wingspan);
Console.WriteLine("Height: " + found.hairColor);
return true;
}
return false;
}
}

class MainClass
{
static void Main(string[] args)
{
choices();
}
static void choices()
{
Console.WriteLine("What do you want?\nA.Create\nB.List all");
string? choice = Console.ReadLine();
switch (choice?.ToLower())
{
case "a":
Console.WriteLine("A.Dog\nB.Cat\nC.Bat\nD.Go Back");
string? choice2 = Console.ReadLine();
executeChoice2(choice2);
break;
case "b":
int iterator = 1;
executeChoiceB(iterator);
break;
default:
break;

}
}
static void executeChoiceB(int iterator)
{
//Console.WriteLine(Dogs[0].Id);
Dog dogs = new Dog();
Cat cats = new Cat();
Bat bats = new Bat();
bool found = dogs.getDog(iterator);
Console.WriteLine(iterator);
switch(found)
{
case true:
executeChoiceB(iterator + 1);
break;
case false:
bool foundCat = cats.getCat(iterator);
switch (foundCat)
{
case true:
executeChoiceB(iterator + 1);
break;
case false:
bool foundBat = bats.getBat(iterator);
switch (foundBat)
{
case true:
executeChoiceB(iterator + 1);
break;
case false:
choices();
break;
}
break;
}
break;
}
}
static void executeChoice2(string? choice)
{
switch (choice?.ToLower())
{
case "a":
Console.WriteLine("Input legcount");
string? legCountString = Console.ReadLine();
int legCount = 0;
if (int.TryParse(legCountString, out legCount))
{
Console.WriteLine("Input color");
string? color = Console.ReadLine();
Dog dog = new Dog(color, legCount);
Console.WriteLine("Dog successfully created with id of: " +
dog.Id);
choices();

}
else
{
Console.WriteLine("Sorry, a non numeric value was input.");
executeChoice2(choice);
}
break;
case "b":
Console.WriteLine("Input Eye color");
string? eyeColor = Console.ReadLine();
double height = 0;
Console.WriteLine("Input height");
string? heightString = Console.ReadLine();
if (double.TryParse(heightString, out height))
{
Cat cat = new Cat(eyeColor, height);
Console.WriteLine("Cat successfully created with id of: " +
cat.Id);
choices();
}
else
{
Console.WriteLine("Sorry, a non numeric value was input.");
executeChoice2(choice);
}
break;
case "c":
Console.WriteLine("Input Hair Color");
string? hairColor = Console.ReadLine();
double wingspan = 0;
Console.WriteLine("Input wingspan");
string? wingspanString = Console.ReadLine();
if (double.TryParse(wingspanString, out wingspan))
{
Bat bat = new Bat(hairColor, wingspan);
Console.WriteLine("Bat successfully created with id of: " +
bat.Id);
choices();
}
else
{
Console.WriteLine("Sorry, a non numeric value was input.");
executeChoice2(choice);
}
break;
default:
break;

}
}
}

You might also like