using System;
using System.Collections.Generic;
namespace Pet_Application
{
public enum PetMood
{
Furious,
Upset,
Bored,
Content,
Happy
};
public enum HungerLevel
{
Starvingpublic enum PetMood
{
Furious,
Hungry Upset,
Bored,
Content,
Full Happy
};
public abstract class Pet
{
public Pet( string name, string breed, PetMood mood,enum HungerLevel hunger, bool isVaccinated )
{
Name = name;
Breed = breed;Starving,
Mood = mood;Hungry,
Hunger = hunger;Content,
IsVaccinated = isVaccinated;Full
}
public stringabstract Nameclass {Pet
get; private set; }{
public Pet( string Breedname, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated )
{
get; private set; Name = name;
Breed = breed;
Mood = mood;
Hunger = hunger;
IsVaccinated = isVaccinated;
}
//Happiness relates to playing withpublic thestring petName { get; private set; }
public PetMoodstring MoodBreed { get; protectedprivate set; }
//PetHappiness hungerrelates levelto playing with the pet
public HungerLevelPetMood HungerMood { get; protected set; }
//HasPet thehunger petlevel
been vaccinated
public boolHungerLevel IsVaccinatedHunger { get; privateprotected set; }
//TheHas the pet classbeen constructorvaccinated
public bool IsVaccinated { get; private set; }
public void GivePetShot()
{
IsVaccinated = true;
//The pet class }constructor
public virtual PetMood PlayWithPet public void GivePetShot()
{
var message IsVaccinated = GetPlayWithPetMessage();true;
if}
( (int)this.Mood < 4 public virtual PetMood PlayWithPet()
{
var message = GetPlayWithPetMessage();
if ( (int)this.Mood < 4 )
{
Console.WriteLine( message );
return this.Mood += 1;
}
Console.WriteLine( message );
return this.MoodMood; += 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPlayWithPetMessage();
public virtual PetMood PunishPet()
{
string message = GetPunishPetMessage();
public ifvirtual (PetMood PunishPet(int) this.Mood > 0 )
{
string message = GetPunishPetMessage();
if ( (int) this.Mood > 0 )
{
Console.WriteLine( message );
return this.Mood -= 1;
}
Console.WriteLine( message );
return this.MoodMood; -= 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPunishPetMessage();
public virtual HungerLevel FeedPet()
{
public ifvirtual (HungerLevel FeedPet(int)this.Hunger < 3 )
{
if ( (int)this.Hunger < 3 )
{
Console.WriteLine( GetFeedPetSuccessMessage() );
this.Hunger += 1;
return this.Hunger;
}
Console.WriteLine( GetFeedPetFailedMessage() );
return this.Hunger;
}
public abstract string GetFeedPetSuccessMessage();
public abstract string GetFeedPetFailedMessage();
public HungerLevel StarvePet()
{
string message = GetStarvePetMessage();
ifpublic (HungerLevel StarvePet(int)this.Hunger > 0 )
{
string message = GetStarvePetMessage();
if ( (int)this.Hunger > 0 )
{
Console.WriteLine( message );
this.Hunger -= 1;
return this.Hunger;
}
Console.WriteLine( message );
return this.Hunger -= 1;
Hunger; return this.Hunger;
}
public abstract string GetStarvePetMessage();
Console.WriteLinepublic void UpdatePet( messageHungerLevel hunger );
return{
this.Hunger; if ( hunger != Hunger )
{
Hunger = hunger;
}
public abstract string GetStarvePetMessage(); }
public void UpdatePet( HungerLevel hunger )
{
public ifvoid UpdatePet( hunger !=PetMood Hungermood )
{
Hungerif ( mood != hunger;Mood )
{
Mood = mood;
}
}
}
public void UpdatePet( PetMood mood )
{
public ifvoid DisplayPetInformation( mood != Mood )
{
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, mood;IsVaccinated );
}
}
public voidclass DisplayPetInformation()Cat : Pet
{
Console.WriteLine( "\n\nName: {0}\n" +
"Breed: {1}\n" +
public Cat( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) "Mood: {2}\n" +
base( name, breed, "Hungermood, Status:hunger, {3}\n"isVaccinated +)
"Is Pet Vaccinated: {4}\n",
Name, Breed, Mood, Hunger, IsVaccinated );
}
}
public class Cat : Pet
{
public Cat( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
const string result = @"You gave the cat a ball!";
return result;
}
public override string GetPlayWithPetMessageGetPunishPetMessage()
{
const string result = @"You gavereturn the"You catslapped athat ballkitty!";
return result;
}
public override string GetPunishPetMessageGetFeedPetSuccessMessage()
{
return "You slappedfed thatthe kittycat!";
}
public override string GetFeedPetSuccessMessageGetFeedPetFailedMessage()
{
return "You fedfailed to feed the cat!";
}
public override string GetFeedPetFailedMessageGetStarvePetMessage()
{
return "You failed to feedreturn "You starved the cat!";
}
}
public overrideclass stringDog GetStarvePetMessage(): Pet
{
returnpublic "YouDog( starvedstring thename, cat!";string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
} base( name, breed, mood, hunger, isVaccinated )
{
}
public class Dog : Pet
{
public Dog(override string name,GetPlayWithPetMessage()
string breed, PetMood mood, HungerLevel hunger, bool isVaccinated{
) :
base( name, breed,return mood,@"You hunger,threw isVaccinateda )frisby!";
{
}
public override string GetPlayWithPetMessageGetPunishPetMessage()
{
return @"You"You threwscolded ato frisbydog!";
}
public override string GetPunishPetMessageGetFeedPetSuccessMessage()
{
return "You scoldedfed tothe dog!";
}
public override string GetFeedPetSuccessMessageGetFeedPetFailedMessage()
{
return "You fedfailed to feed the dog!";
}
public override string GetFeedPetFailedMessageGetStarvePetMessage()
{
return "You failed to feedreturn "You starved the dog!";
}
}
public overrideclass stringShark GetStarvePetMessage(): Pet
{
returnpublic "YouShark( starvedstring thename, dog!";string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
} base( name, breed, mood, hunger, isVaccinated )
{
}
public class Shark : Pet
{
public Shark(override string name,GetPlayWithPetMessage()
string breed, PetMood mood, HungerLevel hunger, bool isVaccinated{
) :
base( name, breed,return mood,"You hunger,swam isVaccinatedwith! )" + this.Name;
{
}
public override string GetPlayWithPetMessageGetPunishPetMessage()
{
return "You swamhit with!your "shark +on this.Name;the nose!";
}
public override string GetPunishPetMessageGetFeedPetSuccessMessage()
{
return "You hit your sharkreturn on"You fed the noseshark!";
}
public override string GetFeedPetSuccessMessageGetFeedPetFailedMessage()
{
return "You fedfailed to feed the shark!";
}
public override string GetFeedPetFailedMessageGetStarvePetMessage()
{
return "You failed to feedreturn "You starved the shark!";
}
}
public overrideclass stringBird GetStarvePetMessage(): Pet
{
returnpublic "YouBird( starvedstring thename, shark!";string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
} base( name, breed, mood, hunger, isVaccinated )
{
}
public class Bird : Pet
{
public Bird(override string name,GetPlayWithPetMessage()
string breed, PetMood mood, HungerLevel hunger, bool isVaccinated{
) :
base( name, breed,return mood,"You hunger,petted isVaccinatedthe )bird!";
{
}
public override string GetPlayWithPetMessageGetPunishPetMessage()
{
return "You pettedclipped the birdbirds wings!";
}
public override string GetPunishPetMessageGetFeedPetSuccessMessage()
{
return "You clippedfed the birds wingsbird!";
}
public override string GetFeedPetSuccessMessageGetFeedPetFailedMessage()
{
return "You fedfailed to feed the bird!";
}
public override string GetFeedPetFailedMessageGetStarvePetMessage()
{
return "You failed to feedreturn "You starved the bird!";
}
}
public override stringclass GetStarvePetMessage()PetDataHandler
{
return "You starvedprivate thereadonly bird!";
List<Pet> pets = new }
}List<Pet>();
public classstring PetDataHandlerthis[ int i ]
{
private readonly List<Pet> pets = new List<Pet>(); get { return pets[i].Name; }
}
public stringvoid this[AddPet( intPet ipet ])
{
get { return pets[i] pets.Name;Add( }pet );
}
public void AddPetRemovePet( Petint peti )
{
pets.AddRemoveAt( peti );
}
public void RemovePet( int ipublic int PetCount()
{
return pets.RemoveAt(Count;
i );
}
public intvoid PetCountFeed( int index )
{
return pets pets[index].Count;UpdatePet( pets[index].FeedPet() );
}
public void FeedStarve( int index )
{
pets[index].UpdatePet( pets[index].FeedPetStarvePet() );
}
public void StarvePlayWith( int index )
{
pets[index].UpdatePet( pets[index].StarvePetPlayWithPet() );
}
public void PlayWithPunish( int index )
{
pets[index].UpdatePet( pets[index].PlayWithPetPunishPet() );
}
public void PunishGiveShot( int index )
{
pets[index].UpdatePet( pets[index].PunishPetGivePetShot() );
}
public void GiveShotShowData( int index )
{
pets[index].GivePetShotDisplayPetInformation();
}
public void ShowData( int index )
{
//Creates an indexer for the pets pets[index].DisplayPetInformation();list
}
//Creates an indexer for the pets list
}
public class Menu
{
readonly Input input = new Input();
readonly PetDataHandler petList = new PetDataHandler();
public voidclass DisplayMainMenu()Menu
{
DisplayTitle();
readonly Input input = new Console.WriteLineInput( "[1] Add a pet\n" +
"[2] Remove a pet\n" +
"[3] Inspect a pet\n" +
"[4] Quit application\n" );
MenuChoice(readonly input.GetChoice(PetDataHandler "EnterpetList choice"= )new PetDataHandler();
}
public void DisplayPetTypeMenuDisplayMainMenu()
{
{
DisplayTitle();
Console.WriteLineDisplayTitle();
"[1] Cat\n" +
Console.WriteLine( "[1] Add a pet\n" +
"[2] Dog\n" +
"[2] Remove a pet\n" +
"[3] Shark\n" +
"[3] Inspect a pet\n" +
"[4] Bird\n" +
"[4] "[5]Quit Exit\n"application\n" );
PetTypeChoice MenuChoice( input.GetChoice( "Enter choice" ) );
}
public void DisplayPetActionMenu( int petIndexpublic void DisplayPetTypeMenu()
{
{
Console.WriteLine( "[1] Feed Pet\n" +
DisplayTitle();
Console.WriteLine( "[1] Cat\n" +
"[2] Starve Pet\n" +
"[2] Dog\n" +
"[3] Play With Pet\n" +
"[3] Shark\n" +
"[4] Punish Pet\n" +
"[4] Bird\n" "[5]+
Vaccinate Pet\n" +
"[6] Main"[5] Menu"Exit\n" );
PetActionChoice PetTypeChoice( input.GetChoice( "Enter choice" ), petIndex );
}
public void DisplayPetRemovalMenu()
{
DisplayTitle();
public forvoid DisplayPetActionMenu( int i = 0; i < petList.PetCount(); i++petIndex )
{
Console.WriteLine( "[{0}]{1}","[1] (Feed iPet\n" +
1 ), petList[i] ); "[2] Starve Pet\n" +
} "[3] Play With Pet\n" +
"[4] Punish Pet\n" +
"[5] Vaccinate Pet\n" +
"[6] Main Menu" );
int choice = PetActionChoice( input.GetChoice( "Enter choice" ), petIndex );
}
if ( choice > 0 && choicepublic <=void petList.PetCountDisplayPetRemovalMenu() )
{
DisplayTitle();
for ( int i = 0; i < petList.RemovePetPetCount(); choicei++ -)
{
Console.WriteLine( "[{0}]{1}", ( i + 1 ), petList[i] );
}
DisplayMainMenu();
}int choice = input.GetChoice( "Enter choice" );
public void DisplayPetInspectionMenu()
{
DisplayTitle();
forif ( intchoice i> =0 0;&& ichoice <<= petList.PetCount(); i++ )
{
Console.WriteLine( "[{0}]{1}", ( i +petList.RemovePet( 1choice ),- petList[i]1 );
}
int choice = input.GetChoice DisplayMainMenu();
"Enter number for pet" ); }
if ( choice > 0 && choicepublic <=void petList.PetCountDisplayPetInspectionMenu() )
{
DisplayTitle();
for ( int i = 0; i < petList.ShowDataPetCount(); choicei++ -)
{
Console.WriteLine( "[{0}]{1}", ( i + 1 ), petList[i] );
}
DisplayPetActionMenu( int choice -= 1input.GetChoice( "Enter number for pet" );
Console if ( choice > 0 && choice <= petList.ReadKeyPetCount() true)
{
petList.ShowData( choice - 1 );
}
DisplayMainMenu DisplayPetActionMenu();
choice - 1 });
private void DisplayTitle()
{
Console.Clear();
Console.WriteLineReadKey( "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n" +
" Pet Application 1.0\n" +
"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"true );
}
public void MenuChoice( int choice DisplayMainMenu();
{
}
switch ( choice private void DisplayTitle()
{
case 1: Console.Clear();
DisplayPetTypeMenuConsole.WriteLine(); "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n" +
PetTypeChoice( input.GetChoice( "Enter choice" ) ); " Pet Application 1.0\n" +
break; "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" );
}
public void MenuChoice( int choice )
{
switch ( choice )
{
case 21:
DisplayPetRemovalMenu DisplayPetTypeMenu();
PetTypeChoice( input.GetChoice( "Enter choice" ) );
break;
case 32:
DisplayPetInspectionMenu DisplayPetRemovalMenu();
break;
case 43:
Environment.Exit( 0 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" ),switch input.GetString( "Breed"choice ), PetMood.Content,
HungerLevel.Content, false ) );{
DisplayMainMenu();
case break;
1:
case 2:
petList.AddPet( new DogCat( input.GetString( "\n\nName" ), input.GetString( "Breed" ), PetMood.Content,
HungerLevel.Content, false ) );
DisplayMainMenu();
break;
case 32:
petList.AddPet( new SharkDog( input.GetString( "\n\nName" ), input.GetString( "Breed" ), PetMood.Content,
HungerLevel.Content, false ) );
DisplayMainMenu();
break;
case 43:
petList.AddPet( new BirdShark( input.GetString( "\n\nName" ), input.GetString( "Breed" ), PetMood.Content,
HungerLevel.Content, false ) );
DisplayMainMenu();
break;
case 54:
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 )
{
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 promptclass )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[] argsclass )Program
{
static void Main( string[] args )
{
var menu = new Menu();
menu.DisplayMainMenu();
}
}
}
}
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 Pet( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated )
{
Name = name;
Breed = breed;
Mood = mood;
Hunger = hunger;
IsVaccinated = isVaccinated;
}
public string Name { get; private set; }
public string Breed { get; private set; }
//Happiness relates to playing with the pet
public PetMood Mood { get; protected set; }
//Pet hunger level
public HungerLevel Hunger { get; protected set; }
//Has the pet been vaccinated
public bool IsVaccinated { get; private set; }
//The pet class constructor
public void GivePetShot()
{
IsVaccinated = true;
}
public virtual PetMood PlayWithPet()
{
var message = GetPlayWithPetMessage();
if ( (int)this.Mood < 4 )
{
Console.WriteLine( message );
return this.Mood += 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPlayWithPetMessage();
public virtual PetMood PunishPet()
{
string message = GetPunishPetMessage();
if ( (int) this.Mood > 0 )
{
Console.WriteLine( message );
return this.Mood -= 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPunishPetMessage();
public virtual HungerLevel FeedPet()
{
if ( (int)this.Hunger < 3 )
{
Console.WriteLine( GetFeedPetSuccessMessage() );
this.Hunger += 1;
return this.Hunger;
}
Console.WriteLine( GetFeedPetFailedMessage() );
return this.Hunger;
}
public abstract string GetFeedPetSuccessMessage();
public abstract string GetFeedPetFailedMessage();
public HungerLevel StarvePet()
{
string message = GetStarvePetMessage();
if ( (int)this.Hunger > 0 )
{
Console.WriteLine( message );
this.Hunger -= 1;
return this.Hunger;
}
Console.WriteLine( message );
return this.Hunger;
}
public abstract string GetStarvePetMessage();
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
{
public Cat( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
const string result = @"You gave the cat a ball!";
return result;
}
public override string GetPunishPetMessage()
{
return "You slapped that kitty!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the cat!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the cat!";
}
public override string GetStarvePetMessage()
{
return "You starved the cat!";
}
}
public class Dog : Pet
{
public Dog( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return @"You threw a frisby!";
}
public override string GetPunishPetMessage()
{
return "You scolded to dog!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the dog!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the dog!";
}
public override string GetStarvePetMessage()
{
return "You starved the dog!";
}
}
public class Shark : Pet
{
public Shark( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You swam with! " + this.Name;
}
public override string GetPunishPetMessage()
{
return "You hit your shark on the nose!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the shark!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the shark!";
}
public override string GetStarvePetMessage()
{
return "You starved the shark!";
}
}
public class Bird : Pet
{
public Bird( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You petted the bird!";
}
public override string GetPunishPetMessage()
{
return "You clipped the birds wings!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the bird!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the bird!";
}
public override string GetStarvePetMessage()
{
return "You starved the bird!";
}
}
public class PetDataHandler
{
private readonly List<Pet> pets = new List<Pet>();
public string this[ int i ]
{
get { return pets[i].Name; }
}
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 class Menu
{
readonly Input input = new Input();
readonly 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 )
{
var menu = new Menu();
menu.DisplayMainMenu();
}
}
}
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 Pet( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated )
{
Name = name;
Breed = breed;
Mood = mood;
Hunger = hunger;
IsVaccinated = isVaccinated;
}
public string Name { get; private set; }
public string Breed { get; private set; }
//Happiness relates to playing with the pet
public PetMood Mood { get; protected set; }
//Pet hunger level
public HungerLevel Hunger { get; protected set; }
//Has the pet been vaccinated
public bool IsVaccinated { get; private set; }
//The pet class constructor
public void GivePetShot()
{
IsVaccinated = true;
}
public virtual PetMood PlayWithPet()
{
var message = GetPlayWithPetMessage();
if ( (int)this.Mood < 4 )
{
Console.WriteLine( message );
return this.Mood += 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPlayWithPetMessage();
public virtual PetMood PunishPet()
{
string message = GetPunishPetMessage();
if ( (int) this.Mood > 0 )
{
Console.WriteLine( message );
return this.Mood -= 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPunishPetMessage();
public virtual HungerLevel FeedPet()
{
if ( (int)this.Hunger < 3 )
{
Console.WriteLine( GetFeedPetSuccessMessage() );
this.Hunger += 1;
return this.Hunger;
}
Console.WriteLine( GetFeedPetFailedMessage() );
return this.Hunger;
}
public abstract string GetFeedPetSuccessMessage();
public abstract string GetFeedPetFailedMessage();
public HungerLevel StarvePet()
{
string message = GetStarvePetMessage();
if ( (int)this.Hunger > 0 )
{
Console.WriteLine( message );
this.Hunger -= 1;
return this.Hunger;
}
Console.WriteLine( message );
return this.Hunger;
}
public abstract string GetStarvePetMessage();
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
{
public Cat( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
const string result = @"You gave the cat a ball!";
return result;
}
public override string GetPunishPetMessage()
{
return "You slapped that kitty!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the cat!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the cat!";
}
public override string GetStarvePetMessage()
{
return "You starved the cat!";
}
}
public class Dog : Pet
{
public Dog( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return @"You threw a frisby!";
}
public override string GetPunishPetMessage()
{
return "You scolded to dog!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the dog!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the dog!";
}
public override string GetStarvePetMessage()
{
return "You starved the dog!";
}
}
public class Shark : Pet
{
public Shark( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You swam with! " + this.Name;
}
public override string GetPunishPetMessage()
{
return "You hit your shark on the nose!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the shark!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the shark!";
}
public override string GetStarvePetMessage()
{
return "You starved the shark!";
}
}
public class Bird : Pet
{
public Bird( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You petted the bird!";
}
public override string GetPunishPetMessage()
{
return "You clipped the birds wings!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the bird!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the bird!";
}
public override string GetStarvePetMessage()
{
return "You starved the bird!";
}
}
public class PetDataHandler
{
private readonly List<Pet> pets = new List<Pet>();
public string this[ int i ]
{
get { return pets[i].Name; }
}
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 class Menu
{
readonly Input input = new Input();
readonly 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 )
{
var menu = new Menu();
menu.DisplayMainMenu();
}
}
}
LetsLet's start with PlayWithPetPlayWithPet
. This is doing the same thing in all subclasses except displaying a different message for each one of them.
So letsLet's move this code into Base and implement a method GetPlayWithPetMessageGetPlayWithPetMessage
in all child classes which will return that different message.
So Change PlayWithPetPlayWithPet
from abstract to virtual and add a new abstract method instead which is "GetPlayWithPetMessage"GetPlayWithPetMessage
and then PlayWithPetPlayWithPet
will call this new method.
Now do the same thing for PunishPetPunishPet
which is to change it to a virtual method and a new abstract method GetPunishPetMessageGetPunishPetMessage
, and so on for FeedPetFeedPet
and StarvePetStarvePet
.
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 Pet( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated )
{
Name = name;
Breed = breed;
Mood = mood;
Hunger = hunger;
IsVaccinated = isVaccinated;
}
public string Name { get; private set; }
public string Breed { get; private set; }
//Happiness relates to playing with the pet
public PetMood Mood { get; protected set; }
//Pet hunger level
public HungerLevel Hunger { get; protected set; }
//Has the pet been vaccinated
public bool IsVaccinated { get; private set; }
//The pet class constructor
public void GivePetShot()
{
IsVaccinated = true;
}
public virtual PetMood PlayWithPet()
{
var message = GetPlayWithPetMessage();
if ( (int)this.Mood < 4 )
{
Console.WriteLine( message );
return this.Mood += 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPlayWithPetMessage();
public virtual PetMood PunishPet()
{
string message = GetPunishPetMessage();
if ( (int) this.Mood > 0 )
{
Console.WriteLine( message );
return this.Mood -= 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPunishPetMessage();
public virtual HungerLevel FeedPet()
{
if ( (int)this.Hunger < 3 )
{
Console.WriteLine( GetFeedPetSuccessMessage() );
this.Hunger += 1;
return this.Hunger;
}
Console.WriteLine( GetFeedPetFailedMessage() );
return this.Hunger;
}
public abstract string GetFeedPetSuccessMessage();
public abstract string GetFeedPetFailedMessage();
public HungerLevel StarvePet()
{
string message = GetStarvePetMessage();
if ( (int)this.Hunger > 0 )
{
Console.WriteLine( message );
this.Hunger -= 1;
return this.Hunger;
}
Console.WriteLine( message );
return this.Hunger;
}
public abstract string GetStarvePetMessage();
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
{
public Cat( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
const string result = @"You gave the cat a ball!";
return result;
}
public override string GetPunishPetMessage()
{
return "You slapped that kitty!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the cat!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the cat!";
}
public override string GetStarvePetMessage()
{
return "You starved the cat!";
}
}
public class Dog : Pet
{
public Dog( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return @"You threw a frisby!";
}
public override string GetPunishPetMessage()
{
return "You scolded to dog!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the dog!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the dog!";
}
public override string GetStarvePetMessage()
{
return "You starved the dog!";
}
}
public class Shark : Pet
{
public Shark( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You swam with! " + this.Name;
}
public override string GetPunishPetMessage()
{
return "You hit your shark on the nose!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the shark!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the shark!";
}
public override string GetStarvePetMessage()
{
return "You starved the shark!";
}
}
public class Bird : Pet
{
public Bird( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You petted the bird!";
}
public override string GetPunishPetMessage()
{
return "You clipped the birds wings!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the bird!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the bird!";
}
public override string GetStarvePetMessage()
{
return "You starved the bird!";
}
}
public class PetDataHandler
{
private readonly List<Pet> pets = new List<Pet>();
public string this[ int i ]
{
get { return pets[i].Name; }
}
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 class Menu
{
readonly Input input = new Input();
readonly 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 )
{
var menu = new Menu();
menu.DisplayMainMenu();
}
}
}
}
Lets start with PlayWithPet. This is doing the same thing in all subclasses except displaying a different message for each one of them.
So lets move this code into Base and implement a method GetPlayWithPetMessage in all child classes which will return that different message.
So Change PlayWithPet from abstract to virtual and add a new abstract method instead which is "GetPlayWithPetMessage" and then PlayWithPet will call this new method.
Now do the same thing for PunishPet which is to change it to a virtual method and a new abstract method GetPunishPetMessage, and so on for FeedPet and StarvePet.
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 Pet( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated )
{
Name = name;
Breed = breed;
Mood = mood;
Hunger = hunger;
IsVaccinated = isVaccinated;
}
public string Name { get; private set; }
public string Breed { get; private set; }
//Happiness relates to playing with the pet
public PetMood Mood { get; protected set; }
//Pet hunger level
public HungerLevel Hunger { get; protected set; }
//Has the pet been vaccinated
public bool IsVaccinated { get; private set; }
//The pet class constructor
public void GivePetShot()
{
IsVaccinated = true;
}
public virtual PetMood PlayWithPet()
{
var message = GetPlayWithPetMessage();
if ( (int)this.Mood < 4 )
{
Console.WriteLine( message );
return this.Mood += 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPlayWithPetMessage();
public virtual PetMood PunishPet()
{
string message = GetPunishPetMessage();
if ( (int) this.Mood > 0 )
{
Console.WriteLine( message );
return this.Mood -= 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPunishPetMessage();
public virtual HungerLevel FeedPet()
{
if ( (int)this.Hunger < 3 )
{
Console.WriteLine( GetFeedPetSuccessMessage() );
this.Hunger += 1;
return this.Hunger;
}
Console.WriteLine( GetFeedPetFailedMessage() );
return this.Hunger;
}
public abstract string GetFeedPetSuccessMessage();
public abstract string GetFeedPetFailedMessage();
public HungerLevel StarvePet()
{
string message = GetStarvePetMessage();
if ( (int)this.Hunger > 0 )
{
Console.WriteLine( message );
this.Hunger -= 1;
return this.Hunger;
}
Console.WriteLine( message );
return this.Hunger;
}
public abstract string GetStarvePetMessage();
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
{
public Cat( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
const string result = @"You gave the cat a ball!";
return result;
}
public override string GetPunishPetMessage()
{
return "You slapped that kitty!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the cat!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the cat!";
}
public override string GetStarvePetMessage()
{
return "You starved the cat!";
}
}
public class Dog : Pet
{
public Dog( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return @"You threw a frisby!";
}
public override string GetPunishPetMessage()
{
return "You scolded to dog!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the dog!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the dog!";
}
public override string GetStarvePetMessage()
{
return "You starved the dog!";
}
}
public class Shark : Pet
{
public Shark( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You swam with! " + this.Name;
}
public override string GetPunishPetMessage()
{
return "You hit your shark on the nose!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the shark!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the shark!";
}
public override string GetStarvePetMessage()
{
return "You starved the shark!";
}
}
public class Bird : Pet
{
public Bird( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You petted the bird!";
}
public override string GetPunishPetMessage()
{
return "You clipped the birds wings!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the bird!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the bird!";
}
public override string GetStarvePetMessage()
{
return "You starved the bird!";
}
}
public class PetDataHandler
{
private readonly List<Pet> pets = new List<Pet>();
public string this[ int i ]
{
get { return pets[i].Name; }
}
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 class Menu
{
readonly Input input = new Input();
readonly 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 )
{
var menu = new Menu();
menu.DisplayMainMenu();
}
}
}
Let's start with PlayWithPet
. This is doing the same thing in all subclasses except displaying a different message for each one of them.
Let's move this code into Base and implement a method GetPlayWithPetMessage
in all child classes which will return that different message.
Change PlayWithPet
from abstract to virtual and add a new abstract method instead which is GetPlayWithPetMessage
and then PlayWithPet
will call this new method.
Now do the same thing for PunishPet
which is to change it to a virtual method and a new abstract method GetPunishPetMessage
, and so on for FeedPet
and StarvePet
.
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 Pet( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated )
{
Name = name;
Breed = breed;
Mood = mood;
Hunger = hunger;
IsVaccinated = isVaccinated;
}
public string Name { get; private set; }
public string Breed { get; private set; }
//Happiness relates to playing with the pet
public PetMood Mood { get; protected set; }
//Pet hunger level
public HungerLevel Hunger { get; protected set; }
//Has the pet been vaccinated
public bool IsVaccinated { get; private set; }
//The pet class constructor
public void GivePetShot()
{
IsVaccinated = true;
}
public virtual PetMood PlayWithPet()
{
var message = GetPlayWithPetMessage();
if ( (int)this.Mood < 4 )
{
Console.WriteLine( message );
return this.Mood += 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPlayWithPetMessage();
public virtual PetMood PunishPet()
{
string message = GetPunishPetMessage();
if ( (int) this.Mood > 0 )
{
Console.WriteLine( message );
return this.Mood -= 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPunishPetMessage();
public virtual HungerLevel FeedPet()
{
if ( (int)this.Hunger < 3 )
{
Console.WriteLine( GetFeedPetSuccessMessage() );
this.Hunger += 1;
return this.Hunger;
}
Console.WriteLine( GetFeedPetFailedMessage() );
return this.Hunger;
}
public abstract string GetFeedPetSuccessMessage();
public abstract string GetFeedPetFailedMessage();
public HungerLevel StarvePet()
{
string message = GetStarvePetMessage();
if ( (int)this.Hunger > 0 )
{
Console.WriteLine( message );
this.Hunger -= 1;
return this.Hunger;
}
Console.WriteLine( message );
return this.Hunger;
}
public abstract string GetStarvePetMessage();
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
{
public Cat( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
const string result = @"You gave the cat a ball!";
return result;
}
public override string GetPunishPetMessage()
{
return "You slapped that kitty!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the cat!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the cat!";
}
public override string GetStarvePetMessage()
{
return "You starved the cat!";
}
}
public class Dog : Pet
{
public Dog( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return @"You threw a frisby!";
}
public override string GetPunishPetMessage()
{
return "You scolded to dog!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the dog!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the dog!";
}
public override string GetStarvePetMessage()
{
return "You starved the dog!";
}
}
public class Shark : Pet
{
public Shark( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You swam with! " + this.Name;
}
public override string GetPunishPetMessage()
{
return "You hit your shark on the nose!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the shark!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the shark!";
}
public override string GetStarvePetMessage()
{
return "You starved the shark!";
}
}
public class Bird : Pet
{
public Bird( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You petted the bird!";
}
public override string GetPunishPetMessage()
{
return "You clipped the birds wings!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the bird!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the bird!";
}
public override string GetStarvePetMessage()
{
return "You starved the bird!";
}
}
public class PetDataHandler
{
private readonly List<Pet> pets = new List<Pet>();
public string this[ int i ]
{
get { return pets[i].Name; }
}
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 class Menu
{
readonly Input input = new Input();
readonly 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 )
{
var menu = new Menu();
menu.DisplayMainMenu();
}
}
}
- Remove fields which we already have as properties in base class.
- Change everything to use base properties
- Change setter scope of base properties to protected
Lets start with PlayWithPet. This is doing the same thing in all subclasses except displaying a different message for each one of them.
So lets move this code into Base and implement a method GetPlayWithPetMessage in all child classes which will return that different message.
So Change PlayWithPet from abstract to virtual and add a new abstract method instead which is "GetPlayWithPetMessage" and then PlayWithPet will call this new method.
Now do the same thing for PunishPet which is to change it to a virtual method and a new abstract method GetPunishPetMessage, and so on for FeedPet and StarvePet.
Have a look at the modified source code. For you info, this code still has some duplication which can be removed by using C# lambda Actions or interfaces but probably too early for you. After writing few lines of code, always try to refactory (basically you should refactor after passing your test but I guess its again too early for you). And the primary factor when doing refactoring is to look for duplication in your code and try to remove it. BTW I only looked into Pet and subclasses but I am pretty sure others classes functionality can be moved into base pet classes as well and then each sub class display its own information.
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 Pet( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated )
{
Name = name;
Breed = breed;
Mood = mood;
Hunger = hunger;
IsVaccinated = isVaccinated;
}
public string Name { get; private set; }
public string Breed { get; private set; }
//Happiness relates to playing with the pet
public PetMood Mood { get; protected set; }
//Pet hunger level
public HungerLevel Hunger { get; protected set; }
//Has the pet been vaccinated
public bool IsVaccinated { get; private set; }
//The pet class constructor
public void GivePetShot()
{
IsVaccinated = true;
}
public virtual PetMood PlayWithPet()
{
var message = GetPlayWithPetMessage();
if ( (int)this.Mood < 4 )
{
Console.WriteLine( message );
return this.Mood += 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPlayWithPetMessage();
public virtual PetMood PunishPet()
{
string message = GetPunishPetMessage();
if ( (int) this.Mood > 0 )
{
Console.WriteLine( message );
return this.Mood -= 1;
}
Console.WriteLine( message );
return this.Mood;
}
public abstract string GetPunishPetMessage();
public virtual HungerLevel FeedPet()
{
if ( (int)this.Hunger < 3 )
{
Console.WriteLine( GetFeedPetSuccessMessage() );
this.Hunger += 1;
return this.Hunger;
}
Console.WriteLine( GetFeedPetFailedMessage() );
return this.Hunger;
}
public abstract string GetFeedPetSuccessMessage();
public abstract string GetFeedPetFailedMessage();
public HungerLevel StarvePet()
{
string message = GetStarvePetMessage();
if ( (int)this.Hunger > 0 )
{
Console.WriteLine( message );
this.Hunger -= 1;
return this.Hunger;
}
Console.WriteLine( message );
return this.Hunger;
}
public abstract string GetStarvePetMessage();
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
{
public Cat( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
const string result = @"You gave the cat a ball!";
return result;
}
public override string GetPunishPetMessage()
{
return "You slapped that kitty!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the cat!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the cat!";
}
public override string GetStarvePetMessage()
{
return "You starved the cat!";
}
}
public class Dog : Pet
{
public Dog( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return @"You threw a frisby!";
}
public override string GetPunishPetMessage()
{
return "You scolded to dog!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the dog!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the dog!";
}
public override string GetStarvePetMessage()
{
return "You starved the dog!";
}
}
public class Shark : Pet
{
public Shark( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You swam with! " + this.Name;
}
public override string GetPunishPetMessage()
{
return "You hit your shark on the nose!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the shark!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the shark!";
}
public override string GetStarvePetMessage()
{
return "You starved the shark!";
}
}
public class Bird : Pet
{
public Bird( string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated ) :
base( name, breed, mood, hunger, isVaccinated )
{
}
public override string GetPlayWithPetMessage()
{
return "You petted the bird!";
}
public override string GetPunishPetMessage()
{
return "You clipped the birds wings!";
}
public override string GetFeedPetSuccessMessage()
{
return "You fed the bird!";
}
public override string GetFeedPetFailedMessage()
{
return "You failed to feed the bird!";
}
public override string GetStarvePetMessage()
{
return "You starved the bird!";
}
}
public class PetDataHandler
{
private readonly List<Pet> pets = new List<Pet>();
public string this[ int i ]
{
get { return pets[i].Name; }
}
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 class Menu
{
readonly Input input = new Input();
readonly 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 )
{
var menu = new Menu();
menu.DisplayMainMenu();
}
}
}