Advertisement
Enkrona

Joppes djurfamilj

Nov 19th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp12
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Petowner joppe = new Petowner();
  11.  
  12.             Console.WriteLine("Välkommen!");
  13.            joppeAge: Console.Write("Fyll i din ålder innan vi går vidare: ");
  14.  
  15.             int joppeAge;
  16.             try
  17.             {
  18.                 joppeAge = int.Parse(Console.ReadLine());
  19.             }
  20.             catch
  21.             {
  22.                 Console.WriteLine("Felaktig inmatning, försök igen!");
  23.                 goto joppeAge;
  24.             }
  25.  
  26.             Console.WriteLine($"Hej Joppe! Programmet har nu registrerat att du är {joppeAge} år gammal.");
  27.             Console.WriteLine("--------------------------------------------------------------------");
  28.  
  29.             joppe.allPets();
  30.             joppe.Menu();
  31.  
  32.             Console.ReadLine();
  33.         }
  34.     }
  35.  
  36.     class Petowner
  37.     {
  38.         private List<Animal> pet = new List<Animal>();
  39.         private string favfood;
  40.  
  41.         public void allPets()
  42.         {
  43.             pet.Add(new Dog(4, "Sally", "Korv", "Schapendoes"));
  44.             pet.Add(new Cat(14, "Banjo", "Ost", "Skogskatt"));
  45.             pet.Add(new Puppy(7, "Samira", "Pankaka", "Schapendoes"));
  46.         }
  47.  
  48.         public void petList()
  49.         {
  50.             foreach (var name in pet)
  51.             {
  52.                 Console.WriteLine(name);
  53.             }
  54.         }
  55.  
  56.         public void interact()
  57.         {
  58.             Console.WriteLine("\nFör att leka med " + pet[0].PetName + " tryck 1");
  59.             Console.WriteLine("\nFör att leka med " + pet[1].PetName + " tryck 2");
  60.             Console.WriteLine("\nFör att leka med " + pet[2].PetName + " tryck 3");
  61.  
  62.  
  63.             int petPlay = int.Parse(Console.ReadLine());
  64.  
  65.             if (petPlay == 1)
  66.             {
  67.                 Console.WriteLine("du leker nu med {0}...", pet[0].PetName);
  68.                 System.Threading.Thread.Sleep(2500);
  69.                 Console.Clear();
  70.             }
  71.             else if (petPlay == 2)
  72.             {
  73.                 Console.WriteLine("du leker nu med {0}...", pet[1].PetName);
  74.                 System.Threading.Thread.Sleep(2500);
  75.                 Console.Clear();
  76.             }
  77.             else if (petPlay == 3)
  78.             {
  79.                 Console.WriteLine("du leker nu med {0}...", pet[2].PetName);
  80.                 System.Threading.Thread.Sleep(2500);
  81.                 Console.Clear();
  82.             }
  83.         }
  84.  
  85.         public void Feed()
  86.         {
  87.             Console.WriteLine("\nVem vill du mata?");
  88.             Console.WriteLine("\nFör att mata " + pet[0].PetName + " tryck 1");
  89.             Console.WriteLine("\nFör att mata " + pet[1].PetName + " tryck 2");
  90.             Console.WriteLine("\nFör att mata " + pet[2].PetName + " tryck 3");
  91.  
  92.             int petFeed = int.Parse(Console.ReadLine());
  93.  
  94.             if (petFeed == 1)
  95.             {
  96.                 favfood = "Korv";
  97.                 pet[0].eat(favfood);
  98.             }
  99.             else if (petFeed == 2)
  100.             {
  101.                 favfood = "Ost";
  102.                 pet[1].eat(favfood);
  103.             }
  104.             else if (petFeed == 3)
  105.             {
  106.                 favfood = "Pankaka";
  107.                 pet[2].eat(favfood);
  108.             }
  109.         }
  110.  
  111.         public void Menu()
  112.         {
  113.             int userInput = 0;
  114.             while (userInput != 4)
  115.             {
  116.                 Console.WriteLine("\nMENY");
  117.                 Console.WriteLine("Vad vill du göra?");
  118.  
  119.                 Console.WriteLine("\n1. Leka med ett av dina husdjur");
  120.                 Console.WriteLine("2. Mata ett av dina husdjur");
  121.                 Console.WriteLine("3. Se en lista över dina husdjur");
  122.                 Console.WriteLine("4. Avsluta programmet");
  123.                 Console.WriteLine("--------------------------------------------------------------------");
  124.  
  125.                 userInput = int.Parse(Console.ReadLine());
  126.  
  127.                 switch (userInput)
  128.                 {
  129.                     case 1:
  130.                         interact();
  131.                         break;
  132.  
  133.                     case 2:
  134.                         Feed();
  135.                         break;
  136.  
  137.                     case 3:
  138.                         petList();
  139.                         break;
  140.  
  141.                     case 4:
  142.                         Console.WriteLine("Goodbye.");
  143.                         break;
  144.  
  145.                     default:
  146.                         Console.WriteLine("Felaktig inmatning, försök igen.");
  147.                         break;
  148.                 }
  149.  
  150.             }
  151.         }
  152.  
  153.         abstract class Animal
  154.         {
  155.             protected int petAge;
  156.             protected string petName;
  157.             protected string favfood;
  158.             protected string breed;
  159.             protected bool hungry;
  160.  
  161.             public Animal(int _petAge, string _petName, string _favfood, string _breed)
  162.             {
  163.                 this.petAge = _petAge;
  164.                 this.petName = _petName;
  165.                 this.favfood = _favfood;
  166.                 this.breed = _breed;
  167.             }
  168.  
  169.             public int PetAge
  170.             {
  171.                 get { return petAge; }
  172.                 set { petAge = value; }
  173.             }
  174.  
  175.             public string PetName
  176.             {
  177.                 get { return petName; }
  178.                 set { petName = value; }
  179.             }
  180.  
  181.             public string FavFood
  182.             {
  183.                 get { return favfood; }
  184.                 set { favfood = value; }
  185.             }
  186.  
  187.             public string Breed
  188.             {
  189.                 get { return breed; }
  190.                 set { breed = value; }
  191.             }
  192.  
  193.             public virtual void interact()
  194.             {
  195.                 if (hungry == true)
  196.                 {
  197.                     hungryAnimal();
  198.                 }
  199.                 else
  200.                 {
  201.                     Console.WriteLine("Tryck på valfri tangent för att återgå till menyn...");
  202.                     Console.ReadKey();
  203.                 }
  204.             }
  205.  
  206.             public virtual void eat(string favfood)
  207.             {
  208.                 Console.WriteLine($"\nSkriv nedan vad du vill mata {PetName} med. \nGå till djurlistan om du är osäker på vad {petName} tycker om för mat.");
  209.  
  210.                 string foodChoice = Console.ReadLine();
  211.                 if (foodChoice == favfood)
  212.                 {
  213.                     Console.WriteLine($"{petName} fick sin favoritmat och äter nu...");
  214.                     Console.WriteLine($"{petName} är nu mätt och belåten.");
  215.                     Console.WriteLine("Tryck på valfri tangent för att återgå till menyn...");
  216.                     Console.ReadKey();
  217.                     Console.Clear();
  218.                     hungry = false;
  219.                 }
  220.                 else
  221.                 {
  222.                     hungryAnimal();
  223.                 }
  224.             }
  225.  
  226.             public virtual void hungryAnimal()
  227.             {
  228.                 Console.WriteLine($"\n{petName} har inte fått sin favoritmat och är därför hungrig & gnäller!");
  229.                 Console.WriteLine("Tryck på valfri tangent för att återgå till menyn...");
  230.                 Console.ReadKey();
  231.                 hungry = true;
  232.             }
  233.  
  234.             public override string ToString()
  235.             {
  236.                 return string.Format($"{petName} är {petAge} år gammal och är en {breed}. {petName}s favoritmat är {favfood}");
  237.             }
  238.         }
  239.  
  240.         class Dog : Animal
  241.         {
  242.             public Dog(int petAge, string petName, string favfood, string breed) : base(petAge, petName, favfood, breed)
  243.             {
  244.  
  245.             }
  246.         }
  247.  
  248.         class Puppy : Dog
  249.         {
  250.             public Puppy(int petAge, string petName, string favfood, string breed) : base(petAge, petName, favfood, breed)
  251.             {
  252.  
  253.             }
  254.             public override string ToString()
  255.             {
  256.                 return string.Format($"{petName} är en hundvalp som är på {petAge} månader. {petName}s favoritmat är {favfood}");
  257.             }
  258.         }
  259.  
  260.             class Cat : Animal
  261.             {
  262.                 Random random = new Random();
  263.  
  264.                 public Cat(int petAge, string petName, string favfood, string breed) : base(petAge, petName, favfood, breed)
  265.                 {
  266.  
  267.                 }
  268.  
  269.                 public override void hungryAnimal()
  270.                 {
  271.                 Console.WriteLine($"{petName} tycker inte om maten, istället går han ut och jagar möss..");
  272.                 System.Threading.Thread.Sleep(2500);
  273.                 int mouse = random.Next() % 100;
  274.  
  275.                     if (mouse >= 50)
  276.                     {
  277.                         hungry = false;
  278.                         Console.WriteLine($"{petName} lyckades fånga en mus och är nu mätt och belåten.");
  279.                         Console.WriteLine("--------------------------------------------------------------------");
  280.                     System.Threading.Thread.Sleep(2500);
  281.                     Console.Clear();
  282.                     }
  283.                     else
  284.                     {
  285.                         hungry = true;
  286.                         Console.WriteLine($"{petName} lyckades inte fånga någon mus och är fortfarande hungrig!");
  287.                         Console.WriteLine("--------------------------------------------------------------------");
  288.  
  289.                     System.Threading.Thread.Sleep(3500);
  290.                     Console.Clear();
  291.                     }
  292.                 }
  293.             }
  294.         }
  295.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement