using System; using System.Collections.Generic; namespace Joppes_djurfamilj { class Program { static void Main(string[] args) { Petowner Joppe = new Petowner(25); Joppe.Menu(); } } class Petowner { private int age { get; set; } private List<Animal> animal_List = new List<Animal>(); public Petowner(int age) { animal_List.Add(new Cat("Oscar", 15, "fish")); animal_List.Add(new Dog("Fido", 10, "meat")); animal_List.Add(new Puppy("Wilbur", 6, "dogtreats")); animal_List.Add(new Animal("Sven", 40, "pizza")); } public void List_Animals() { Console.WriteLine("These are Joppe's animals:\n"); foreach (var animal in animal_List) { Console.WriteLine(animal); } Console.WriteLine("\nPress any button to go back to the menu."); Console.ReadKey(); Console.Clear(); } public void Play() { Console.WriteLine("Which animal do you want to play with?"); int i = 1; foreach (var animal in animal_List) { Console.WriteLine(i + " - " + animal); i++; } Console.Write("Your choice: "); int Answer = int.Parse(Console.ReadLine()); if (Answer == 1) { Console.WriteLine("Joppe is playing with {0}", animal_List[0]); animal_List[0].Interact(); } if (Answer == 2) { Console.WriteLine("Joppe is playing with {0}", animal_List[1]); animal_List[1].Interact(); } if (Answer == 3) { Console.WriteLine("Joppe is playing with {0}", animal_List[2]); animal_List[2].Interact(); } if (Answer == 4) { Console.WriteLine("Joppe is playing chess with {0}", animal_List[3]); animal_List[3].Interact(); } Console.WriteLine("\nPress any button to return to the menu."); Console.ReadKey(); Menu(); } public void Feed() { Console.WriteLine("Which animal do you want to feed?"); int i = 1; foreach (var animal in animal_List) { Console.WriteLine(i + " - " + animal); i++; } int answer = 0; try { answer = int.Parse(Console.ReadLine()); } catch (Exception) { Console.WriteLine("You must choose a pet from one of the numbers."); } Food_Menu(answer - 1); void Food_Menu(int animal) { Console.WriteLine("What do you want to feed {0}?", animal_List[animal]); Console.WriteLine("1 - Fish"); Console.WriteLine("2 - Meat"); Console.WriteLine("3 - Dogtreats"); Console.WriteLine("4 - Pizza"); try { answer = int.Parse(Console.ReadLine()); } catch (Exception) { Console.WriteLine("You must choose a pet from one of the numbers."); } if ((answer == 1 && animal == 0) || (answer == 2 && animal == 1) || (answer == 3 && animal == 2) || (answer == 4 && animal == 3)) { Console.WriteLine(animal_List[animal] + " eats the food and get full."); animal_List[animal].Eat(); } else { Console.WriteLine(animal_List[animal] + " doesn't eat much."); animal_List[animal].Hungry_Animal(); } Console.WriteLine("\nPress any button to return to the menu."); Console.ReadKey(); Menu(); } } public void Menu() { Console.Clear (); int menu = 0; do { Console.WriteLine("Welcome to Joppe's friendly animal family.\n" + "Please choose an option from the menu."); Console.WriteLine("1 - Play with animals"); Console.WriteLine("2 - Feed animals"); Console.WriteLine("3 - Show a list of the animals"); Console.WriteLine("4 - Exit program"); menu = int.Parse(Console.ReadLine()); switch (menu) { case 1: Play(); break; case 2: Feed(); break; case 3: List_Animals(); break; case 4: Console.Clear(); Console.WriteLine("Press any button to exit the program..."); Console.ReadKey(); break; default: break; } } while (menu != 4); } public override string ToString() { return String.Format("Joppe"); } } class Animal { protected int age { get; set; } protected string name { get; set; } protected string fav_food { get; set; } protected string breed { get; set; } protected bool hungry { get; set; } = true; public Animal(string name, int age, string fav_food) { } public virtual void Interact() { if (hungry == true) { Console.WriteLine("Sven can't focus because of hunger, and loses the game.\n" + "He proceeds to remind you that starving pets is illegal."); } if (hungry == false) { Console.WriteLine("Sven wins the game, since he is by nature a logical thinker."); } } public void Eat() { hungry = false; } public virtual void Hungry_Animal() { Console.WriteLine("Sven reminds you that it is customary to feed your pets."); } public override string ToString() { return String.Format("Sven"); } } class Cat : Animal { public Cat(string name, int age, string fav_food) : base(name, age, fav_food) { this.name = name; this.age = age; this.fav_food = fav_food; } public override void Interact() { if (hungry == true) { Console.WriteLine(name + " is too hungry to play."); } if (hungry == false) { Console.WriteLine(name + " zooms back and forth, chasing a laser dot."); } } public override void Hungry_Animal() { Random random = new Random(); int hunt = random.Next(0, 2); if (hunt == 0) { Console.Write("{0} found a mouse and is now full!", name); hungry = false; } if (hunt == 1) { Console.Write("{0} did not find any mouse, and is still hungry.", name); } } public override string ToString() { return String.Format("Oscar"); } } class Dog : Animal { public Dog(string name, int age, string fav_food) : base(name, age, fav_food) { this.name = name; this.age = age; this.fav_food = fav_food; } public override void Hungry_Animal() { Console.WriteLine(name + " whimpers because he is hungry."); } public override void Interact() { if (hungry == true) { Console.WriteLine(name + " is too hungry to play."); } if (hungry == false) { Console.WriteLine(name + " chases the ball you threw, and return with it."); } } public override string ToString() { return String.Format("Fido"); } } class Puppy : Dog { public Puppy(string name, int age, string fav_food) : base(name, age, fav_food) { this.name = name; this.age = age; this.fav_food = fav_food; } public override void Hungry_Animal() { Console.WriteLine(name + " whimpers because he is hungry."); } public override void Interact() { if (hungry == true) { Console.WriteLine(name + " is too hungry to play."); } if (hungry == false) { Console.WriteLine(name + " runs around so fast he stumbles on his big ears."); } } public override string ToString() { return String.Format("Wilbur"); } } }
if (foodItems[answer] == animal.fav_food)
Console.WriteLine("What do you want to feed {0}?", animal_List[animal]);
Console.WriteLine("1 - Fish");
Console.WriteLine("2 - Meat");
Console.WriteLine("3 - Dogtreats");
Console.WriteLine("4 - Pizza");
try
{
answer = int.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("You must choose a pet from one of the numbers.");
}
if ((answer == 1 && animal == 0) ||
(answer == 2 && animal == 1) ||
(answer == 3 && animal == 2) ||
(answer == 4 && animal == 3))
{
Console.WriteLine("What do you want to feed {0}?", animal_List[animal]);
Console.WriteLine("1 - Fish");
Console.WriteLine("2 - Meat");
Console.WriteLine("3 - Dogtreats");
Console.WriteLine("4 - Pizza");
try
{
answer = int.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("You must choose a pet from one of the numbers.");
}
if ((answer == 1 && animal == 0) ||
(answer == 2 && animal == 1) ||
(answer == 3 && animal == 2) ||
(answer == 4 && animal == 3))
{
if (myBoolean)
if (myBoolean == true)
Du måste vara medlem för att kunna kommentera
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106