As a student of C# right now, I am trying my best to get a grasp on many of the systems being taught. I just completed Week 1 of a C# 102 class and the class was assigned a bit of homework. I am curious, from a beginners point of view, how you might restructure this code to make it more readable and understandable.
For context, here is a word for word description of what the homework was supposed to be.
Create an abstract Pet class that has abstract functionality (at least 3 methods). It must have a "string Name" read-only (he didnt mean readonly keyword) that is filled by a parameter to the constructor. At least one of the methods must take in parameters, and at least one of the methods must return a value instead of printing things to the console.
Create four implementations of the Pet class that all appropriately implement the different methods.
Allow the user to enter 0 to many pets (can be any type) and store them into a List object
After the user is done entering in pets allow the user to:
A: select a pet
B: perform an operation
C: allow the user to repeat everything
Thanks for your time. And if you do find yourself answering, please keep in mind, we have not gone over Interfaces yet. That is the next class. The things we learned in this class had mostly to do with virtual, override, abstract members and classes.
using System;
using System.Collections.Generic;
namespace Pet_Application
{
public enum PetMood
{
Furious,
Upset,
Bored,
Content,
Happy
};
public enum HungerLevel
{
Starving,
Hungry,
Content,
Full
}
public abstract class Pet
{
public string Name { get; private set; }
public string Breed { get; private set; }
//Happiness relates to playing with the pet
public PetMood Mood { get; private set; }
//Pet hunger level
public HungerLevel Hunger { get; private set; }
//Has the pet been vaccinated
public bool IsVaccinated { get; private set; }
//The pet class constructor
public Pet(string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated)
{
Name = name;
Breed = breed;
Mood = mood;
Hunger = hunger;
IsVaccinated = isVaccinated;
}
public void GivePetShot()
{
IsVaccinated = true;
}
public abstract PetMood PlayWithPet();
public abstract PetMood PunishPet();
public abstract HungerLevel FeedPet();
public abstract HungerLevel StarvePet();
public void UpdatePet(HungerLevel hunger)
{
if (hunger != Hunger)
{
Hunger = hunger;
}
}
public void UpdatePet(PetMood mood)
{
if (mood != Mood)
{
Mood = mood;
}
}
public void DisplayPetInformation()
{
Console.WriteLine("\n\nName: {0}\n" +
"Breed: {1}\n" +
"Mood: {2}\n" +
"Hunger Status: {3}\n" +
"Is Pet Vaccinated: {4}\n",
Name, Breed, Mood, Hunger, IsVaccinated);
}
}
public class Cat : Pet
{
private string _name;
private string _breed;
private PetMood _mood;
private HungerLevel _hunger;
private bool _isVaccinated;
public Cat(string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated) :
base(name, breed, mood, hunger, isVaccinated)
{
_name = name;
_breed = breed;
_mood = mood;
_hunger = hunger;
_isVaccinated = isVaccinated;
}
public override PetMood PlayWithPet()
{
if ((int)_mood < 4)
{
Console.WriteLine("You gave the cat a ball!");
return _mood += 1;
}
Console.WriteLine("You gave the cat a ball!");
return _mood;
}
public override PetMood PunishPet()
{
if ((int)_mood > 0)
{
Console.WriteLine("You slapped that kitty!");
return _mood -= 1;
}
Console.WriteLine("You slapped that kitty!");
return _mood;
}
public override HungerLevel FeedPet()
{
if ((int)_hunger < 3)
{
Console.WriteLine("You fed the cat!");
_hunger += 1;
return _hunger;
}
Console.WriteLine("You failed to feed the cat!");
return _hunger;
}
public override HungerLevel StarvePet()
{
if ((int)_hunger > 0)
{
Console.WriteLine("You starved the cat!");
_hunger -= 1;
return _hunger;
}
Console.WriteLine("You starved the cat!");
return _hunger;
}
}
public class Dog : Pet
{
private string _name;
private string _breed;
private PetMood _mood;
private HungerLevel _hunger;
private bool _isVaccinated;
public Dog(string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated) :
base(name, breed, mood, hunger, isVaccinated)
{
_name = name;
_breed = breed;
_mood = mood;
_hunger = hunger;
_isVaccinated = isVaccinated;
}
public override PetMood PlayWithPet()
{
if ((int)_mood < 4)
{
Console.WriteLine("You threw a frisby!");
return _mood += 1;
}
return _mood;
}
public override PetMood PunishPet()
{
if ((int)_mood > 0)
{
Console.WriteLine("You scolded to dog!");
return _mood -= 1;
}
return _mood;
}
public override HungerLevel FeedPet()
{
if ((int)_hunger < 3)
{
Console.WriteLine("You fed the dog!");
_hunger += 1;
return _hunger;
}
Console.WriteLine("You failed to feed the dog!");
return _hunger;
}
public override HungerLevel StarvePet()
{
if ((int)_hunger > 0)
{
Console.WriteLine("You starved the dog!");
_hunger -= 1;
return _hunger;
}
Console.WriteLine("You starved the dog!");
return _hunger;
}
}
public class Shark : Pet
{
private string _name;
private string _breed;
private PetMood _mood;
private HungerLevel _hunger;
private bool _isVaccinated;
public Shark(string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated) :
base(name, breed, mood, hunger, isVaccinated)
{
_name = name;
_breed = breed;
_mood = mood;
_hunger = hunger;
_isVaccinated = isVaccinated;
}
public override PetMood PlayWithPet()
{
if ((int)_mood < 4)
{
Console.WriteLine("You swam with! {0}", _name);
return _mood += 1;
}
return _mood;
}
public override PetMood PunishPet()
{
if ((int)_mood > 0)
{
Console.WriteLine("You hit your shark on the nose!");
return _mood -= 1;
}
return _mood;
}
public override HungerLevel FeedPet()
{
if ((int)_hunger < 3)
{
Console.WriteLine("You fed the shark!");
_hunger += 1;
return _hunger;
}
Console.WriteLine("You failed to feed the shark!");
return _hunger;
}
public override HungerLevel StarvePet()
{
if ((int)_hunger > 0)
{
Console.WriteLine("You starved the shark!");
_hunger -= 1;
return _hunger;
}
Console.WriteLine("You starved the shark!");
return _hunger;
}
}
public class Bird : Pet
{
private string _name;
private string _breed;
private PetMood _mood;
private HungerLevel _hunger;
private bool _isVaccinated;
public Bird(string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated) :
base(name, breed, mood, hunger, isVaccinated)
{
_name = name;
_breed = breed;
_mood = mood;
_hunger = hunger;
_isVaccinated = isVaccinated;
}
public override PetMood PlayWithPet()
{
if ((int)_mood < 4)
{
Console.WriteLine("You petted the bird!");
return _mood += 1;
}
return _mood;
}
public override PetMood PunishPet()
{
if ((int)_mood > 0)
{
Console.WriteLine("You clipped the birds wings!");
return _mood -= 1;
}
return _mood;
}
public override HungerLevel FeedPet()
{
if ((int)_hunger < 3)
{
Console.WriteLine("You fed the bird!");
_hunger += 1;
return _hunger;
}
Console.WriteLine("You failed to feed the bird!");
return _hunger;
}
public override HungerLevel StarvePet()
{
if ((int)_hunger > 0)
{
Console.WriteLine("You starved the bird!");
_hunger -= 1;
return _hunger;
}
Console.WriteLine("You starved the bird!");
return _hunger;
}
}
public class PetDataHandler
{
private List<Pet> pets = new List<Pet>();
public void AddPet(Pet pet)
{
pets.Add(pet);
}
public void RemovePet(int i)
{
pets.RemoveAt(i);
}
public int PetCount()
{
return pets.Count;
}
public void Feed(int index)
{
pets[index].UpdatePet(pets[index].FeedPet());
}
public void Starve(int index)
{
pets[index].UpdatePet(pets[index].StarvePet());
}
public void PlayWith(int index)
{
pets[index].UpdatePet(pets[index].PlayWithPet());
}
public void Punish(int index)
{
pets[index].UpdatePet(pets[index].PunishPet());
}
public void GiveShot(int index)
{
pets[index].GivePetShot();
}
public void ShowData(int index)
{
pets[index].DisplayPetInformation();
}
//Creates an indexer for the pets list
public string this[int i]
{
get { return pets[i].Name; }
}
}
public class Menu
{
Input input = new Input();
PetDataHandler petList = new PetDataHandler();
public void DisplayMainMenu()
{
DisplayTitle();
Console.WriteLine("[1] Add a pet\n" +
"[2] Remove a pet\n" +
"[3] Inspect a pet\n" +
"[4] Quit application\n");
MenuChoice(input.GetChoice("Enter choice"));
}
public void DisplayPetTypeMenu()
{
DisplayTitle();
Console.WriteLine("[1] Cat\n" +
"[2] Dog\n" +
"[3] Shark\n" +
"[4] Bird\n" +
"[5] Exit\n");
PetTypeChoice(input.GetChoice("Enter choice"));
}
public void DisplayPetActionMenu(int petIndex)
{
Console.WriteLine("[1] Feed Pet\n" +
"[2] Starve Pet\n" +
"[3] Play With Pet\n" +
"[4] Punish Pet\n" +
"[5] Vaccinate Pet\n" +
"[6] Main Menu");
PetActionChoice(input.GetChoice("Enter choice"), petIndex);
}
public void DisplayPetRemovalMenu()
{
DisplayTitle();
for (int i = 0; i < petList.PetCount(); i++)
{
Console.WriteLine("[{0}]{1}", (i + 1), petList[i]);
}
int choice = input.GetChoice("Enter choice");
if (choice > 0 && choice <= petList.PetCount())
{
petList.RemovePet(choice - 1);
}
DisplayMainMenu();
}
public void DisplayPetInspectionMenu()
{
DisplayTitle();
for (int i = 0; i < petList.PetCount(); i++)
{
Console.WriteLine("[{0}]{1}", (i + 1), petList[i]);
}
int choice = input.GetChoice("Enter number for pet");
if (choice > 0 && choice <= petList.PetCount())
{
petList.ShowData(choice - 1);
}
DisplayPetActionMenu(choice - 1);
Console.ReadKey(true);
DisplayMainMenu();
}
private void DisplayTitle()
{
Console.Clear();
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n" +
" Pet Application 1.0\n" +
"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
}
public void MenuChoice(int choice)
{
switch (choice)
{
case 1:
DisplayPetTypeMenu();
PetTypeChoice(input.GetChoice("Enter choice"));
break;
case 2:
DisplayPetRemovalMenu();
break;
case 3:
DisplayPetInspectionMenu();
break;
case 4:
Environment.Exit(0);
break;
default:
Console.Write("\n\nNot a valid selection. Press any key to return");
Console.ReadKey(true);
DisplayMainMenu();
break;
}
}
public void PetTypeChoice(int choice)
{
switch (choice)
{
case 1:
petList.AddPet(new Cat(input.GetString("\n\nName"), input.GetString("Breed"), PetMood.Content, HungerLevel.Content, false));
DisplayMainMenu();
break;
case 2:
petList.AddPet(new Dog(input.GetString("\n\nName"), input.GetString("Breed"), PetMood.Content, HungerLevel.Content, false));
DisplayMainMenu();
break;
case 3:
petList.AddPet(new Shark(input.GetString("\n\nName"), input.GetString("Breed"), PetMood.Content, HungerLevel.Content, false));
DisplayMainMenu();
break;
case 4:
petList.AddPet(new Bird(input.GetString("\n\nName"), input.GetString("Breed"), PetMood.Content, HungerLevel.Content, false));
DisplayMainMenu();
break;
case 5:
DisplayMainMenu();
break;
default:
Console.Write("\n\nNot a valid selection. Press any key to return>");
Console.ReadKey(true);
DisplayPetTypeMenu();
break;
}
}
public void PetActionChoice(int choice, int petIndex)
{
switch (choice)
{
case 1:
petList.Feed(petIndex);
DisplayPetInspectionMenu();
break;
case 2:
petList.Starve(petIndex);
DisplayPetInspectionMenu();
break;
case 3:
petList.PlayWith(petIndex);
DisplayPetInspectionMenu();
break;
case 4:
petList.Punish(petIndex);
DisplayPetInspectionMenu();
break;
case 5:
petList.GiveShot(petIndex);
DisplayPetInspectionMenu();
break;
case 6:
DisplayMainMenu();
break;
default:
Console.Write("\n\nNot a valid selection. Press any key to return>");
Console.ReadKey(true);
DisplayPetTypeMenu();
break;
}
}
}
public class Input
{
public string GetString(string prompt)
{
Console.Write("{0}> ", prompt);
return Console.ReadLine();
}
public int GetInt(string prompt)
{
Console.Write("{0}> ", prompt);
return int.Parse(Console.ReadLine());
}
public int GetChoice(string prompt)
{
Console.Write("{0}> ", prompt);
return int.Parse(Console.ReadKey().KeyChar.ToString());
}
}
class Program
{
static void Main(string[] args)
{
Menu menu = new Menu();
menu.DisplayMainMenu();
}
}
}