C#-Assignment5

Jisu Lee·2023년 2월 3일
0

C# 코드 모음집

목록 보기
5/5

다섯 번째 작고 소듕한 과제(Petting Zoo)입니다.

첫 번째로는 main.cs 코드입니다.

using System;

class Program {
  public static void Main (string[] args) {
    Zoo BtownZoo = new Zoo("Happy Pets", "Bloomingon", 8);

    Animal[] arr = new Animal[] {
      new Goat("Goatee", 3, false, false, false),
      new Goat("Jeni", 4, true, true, true),
      new Goat("Jumper", 2, true, true, false),
      new Goat("Kid", 1, true, true, true),
      new Goat("Pete", 2, false, false, true),
      new Pony("Bolt", 3, false, 50, 5, true, false),
      new Pony("Colt", 4, true, 30, 10, true, true)
    };

    foreach (Animal a in arr) BtownZoo.AddAnimal(a);
   
    ZooOperations ZooInc = new ZooOperations(BtownZoo);
    ZooInc.Run();
   
  }
}

다음으로는 zoo.cs 코드입니다.

using System;
using System.Collections.Generic;

public class Zoo
{
  public string Name { get; set; }
  public string City { get; set; }
  public int Capacity { get; set; }
  public List<Animal> Animals {get; set; }

  public Zoo (string name, string city, int capacity)
  {
    Name = name;
    City = city;
    Capacity = capacity;
    Animals = new List<Animal>();
  }

  public void AddAnimal(Animal animal)
  {
    // ***** [CODE] *****
    // Only if there is capacity add the given animal.
    // Otherwise print a warning that the given animal is not added.
    // Hint: Use the ToString of the animal object.
    if (Animals.Count < 8) Animals.Add(animal);
    else Console.WriteLine("Zoo is at capacity. Cannot add animal" + " " + animal.GetType().Name + " " + animal.Name + " (" + animal.Age + "/" + animal.Female + ")");
  }

  public void RemoveAnimal(string name) 
  {
    // ***** [CODE] *****
    // Look for the name of the animal and if found, 
    // remove it from the Animals list.
    var item = Animals.Find(x=>x.Name == name);
    Animals.Remove(item);
  }

  public void ListAnimals()
  {
    string heading = String.Format("{0} in {1} (Capacity {2}/{3})", 
      Name, City, Animals.Count, Capacity);
    Console.WriteLine(heading);
    Console.WriteLine("".PadRight(heading.Length, '-'));

    // ***** [CODE] *****
    // Print the Animals list.
    foreach(Animal animal in Animals)
    {
    Console.WriteLine(animal);
    }
  }

  public void FeedAnimals()
  {
    // ***** [CODE] *****
    // Feed all the animals.
    foreach(Animal animal in Animals)
    {
      Console.WriteLine(animal.GetType().Name + " " + animal.Name + " is fed");
    }
    Console.WriteLine("All animals are fed.");
  }

  public void ClearAnimals()
  {
    // ***** [CODE] *****
    // Clear the animals.
    ClearAnimals();
  } 
}

다음은 animal.cs입니다.

using System;

public abstract class Animal
{
  public string Name { get; set; }
  public int Age { get; set; }
  public bool Female { get; set; }
  public Animal(string name, int age, bool female)
  {
    Name = name;
    Age = age;
    Female = female;    
  }

  public override string ToString()
  {
    return String.Format("{0} {1} ({2}/{3})", this.GetType(), Name, Age, 
                         Female ? "F" : "M"); 
  }  
  
  public abstract void Eat();
}

interface Pet
{
  bool Playful { get; set; }
  bool Adoptable { get; set; }
}

interface Riding
{
  int MaxRiderWeight { get; set; }
  int RestBetweenRides { get; set; }
}

interface Cost
{
  double FeedCost { get; set; }
}

class Goat : Animal, Pet
{
  public bool Playful { get; set; }
  public bool Adoptable { get; set; }

  public override string ToString()
  {
    return String.Format("{0} {1}{2}",
                         base.ToString(), 
                         Playful ? " Playful" : "", 
                         Adoptable ? " Adoptable" : "");
  }

  public Goat(string name, int age, bool female) : base(name, age, female)
  {
  }
  
  public Goat(string name, int age, bool female, bool playful, bool adoptable) :
    base(name, age, female)
  {
    // ***** [CODE] *****
    // Set properties that are specific to this class.
    Playful = playful;
    Adoptable = adoptable;
  }

  public override void Eat()
  {
    Console.WriteLine("{0} {1} is fed.", this.GetType(), Name);
  }
}

class Pony : Animal, Pet, Riding
{
  public bool Playful { get; set; }
  public bool Adoptable { get; set; }

  private int maxWeight; 
  public int MaxRiderWeight { 
    get { return maxWeight; }
    set { 
      // ***** [CODE] *****
      // Do not allow more than 100 pounds or negative values. 
      // Default max is 100.
      maxWeight = 100;
      if (value>100 || value<0) Console.WriteLine("Not allowed value.");
      else maxWeight = value;
    }
  }

  private int restMinutes;
  public int RestBetweenRides { 
    get { return restMinutes; }
    set { 
      // ***** [CODE] *****
      // Do not less than 5 minutes rest
      // Default minimum is 5.
      restMinutes = 5;
      if (value<5) Console.WriteLine("Not allowed value.");
      else restMinutes = value; 
    }
  }

  public override string ToString()
  {
    return String.Format("{0} {1}{2}{3}{4}",
                         base.ToString(), 
                         "[max "+ MaxRiderWeight + " lbs] ",
                         "[rest "+ RestBetweenRides + " mins]",
                         Playful ? " Playful" : "", 
                         Adoptable ? " Adoptable" : "");
  }

  public Pony(string name, int age, bool female) : base(name, age, female)
  {
  }
  
  public Pony(string name, int age, bool female, int maxriderweight, int restbetweenrides, bool playful, bool adoptable) :
    base(name, age, female)
  {
    // ***** [CODE] *****
    // Set properties that are specific to this class.
    MaxRiderWeight = maxriderweight;
    RestBetweenRides = restbetweenrides;
    Playful = playful;
    Adoptable = adoptable;
  }

  public override void Eat()
  {
    Console.WriteLine("{0} {1} is fed.", this.GetType(), Name);
  }
}

// ***** [CODE] *****
// Code all there additional animal classes. They should implement the 
// required interfaces and provide ToString overrides as in Goat and Pony.
//
// class Peacock : Animal
class Peacock : Animal
{
  public Peacock(string name, int age, bool female) :
    base(name, age, female)
  {
  }
  public override void Eat()
  {
    Console.WriteLine("{0} {1} is fed.", this.GetType(), Name);
  }
}
// 
// class Pig : Animal, Pet
// Hint: Copy from Goat
class Pig : Animal, Pet
{
  public bool Playful { get; set; }
  public bool Adoptable { get; set; }

  public override string ToString()
  {
    return String.Format("{0} {1}{2}",
                         base.ToString(),
                         Playful ? " Playful" : "", 
                         Adoptable ? " Adoptable" : "");
  }

  public Pig(string name, int age, bool female) : base(name, age, female)
  {
  }
  
  public Pig(string name, int age, bool female, bool playful, bool adoptable) :
    base(name, age, female)
  {
    // ***** [CODE] *****
    // Set properties that are specific to this class.
    Playful = playful;
    Adoptable = adoptable;
  }

  public override void Eat()
  {
    Console.WriteLine("{0} {1} is fed.", this.GetType(), Name);
  }
}
                        
// 
// class Donkey : Animal, Pet, Riding
// Hint: Copy from Pony
class Donkey : Animal, Pet, Riding
{
  public bool Playful { get; set; }
  public bool Adoptable { get; set; }

  private int maxWeight; 
  public int MaxRiderWeight { 
    get { return maxWeight; }
    set { 
      // ***** [CODE] *****
      // Do not allow more than 100 pounds or negative values. 
      // Default max is 100.
      maxWeight = 100;
      if (value>100 || value<0) Console.WriteLine("Not allowed value.");
      else maxWeight = value;
    }
  }

  private int restMinutes;
  public int RestBetweenRides { 
    get { return restMinutes; }
    set { 
      // ***** [CODE] *****
      // Do not less than 5 minutes rest
      // Default minimum is 5.
      restMinutes = 5;
      if (value<5) Console.WriteLine("Not allowed value.");
      else restMinutes = value; 
    }
  }

  public override string ToString()
  {
    return String.Format("{0} {1}{2}{3}{4}",
                         base.ToString(), 
                         "[max "+ MaxRiderWeight + " lbs] ",
                         "[rest "+ RestBetweenRides + " mins]",
                         Playful ? " Playful" : "", 
                         Adoptable ? " Adoptable" : "");
  }

  public Donkey(string name, int age, bool female) : base(name, age, female)
  {
  }
  
  public Donkey(string name, int age, bool female, int maxriderweight, int restbetweenrides, bool playful, bool adoptable) :
    base(name, age, female)
  {
    // ***** [CODE] *****
    // Set properties that are specific to this class.
    MaxRiderWeight = maxriderweight;
    RestBetweenRides = restbetweenrides;
    Playful = playful;
    Adoptable = adoptable;
  }

  public override void Eat()
  {
    Console.WriteLine("{0} {1} is fed.", this.GetType(), Name);
  }
}

마지막으로 ZooOperations.cs 코드입니다.

using System;
using System.IO;
using System.Collections.Generic;

public class ZooOperations
{
  private Zoo zoo { get; set; }
  
  public ZooOperations(Zoo z)
  {
    zoo = z;
  }

  public void Run()
  {
    string[] line;
    string input, command;
    do {
      Console.Write("Zoo Op > ");
      input = Console.ReadLine();
      line = input.Split(',');
      if (line.Length == 0) continue;
      command = line[0].ToLower();
      switch (command) {
        case "help":
          Console.WriteLine("Commands: add clear feed help list remove quit");
          Console.WriteLine("Command line arguments are comma separated.");
          break;
        case "exit":
        case "quit":
        case "q": 
          return;
        case "add":
          AddAnimal(line);
          break;
        case "clear":
          zoo.ClearAnimals();
          break;
        case "feed":
          zoo.FeedAnimals();
          break;
        case "list":
          zoo.ListAnimals();
          break;
        case "remove":
          RemoveAnimal(line);
          break;
        default:
          Console.WriteLine("Error in Command");
          break;
      }
    } while(true);
  }

  private void AddAnimal(string[] cmd)
  {
    if (cmd.Length < 4) { 
      Console.WriteLine("Error: Missing required arguments");
      return;
    }
    string kind = cmd[1].ToLower();
    string name = cmd[2];
    int age;
    int.TryParse(cmd[3], out age);
    bool female = Array.IndexOf(cmd, "female") > 0;
    bool playful = Array.IndexOf(cmd, "playful") > 0;
    bool adoptable = Array.IndexOf(cmd, "adoptable") > 0;
    switch (kind) {
      case "peacock":
        // ***** [CODE] *****
        // Add code to create a new peacock and add it to the zoo.
        Animal peacock = new Peacock(name, age, female);
        zoo.AddAnimal(peacock);
        break;
      case "goat":
        Animal goat = new Goat(name, age, female, playful, adoptable);
        zoo.AddAnimal(goat);
        break;
      case "pig":
        // ***** [CODE] *****
        // Add code to create a new pig and add it to the zoo.
        // Hint: See how "goat" is done above.
        Animal pig = new Pig(name, age, female);
        zoo.AddAnimal(pig);
        break;
      case "pony":
        if (cmd.Length < 7) Console.WriteLine("Error: Missing arguments");
        else {
          int load, rest;
          int.TryParse(cmd[5], out load);
          int.TryParse(cmd[6], out rest);
          Animal pony = new Pony(name, age, female, load, rest, playful, adoptable);
          zoo.AddAnimal(pony);
        }
        break;
      case "donkey":
        // ***** [CODE] *****
        // Add code to create a new donkey and add it to the zoo.
        // Hint: See how "donkey" is done above.
        Animal donkey = new Donkey(name, age, female);
        zoo.AddAnimal(donkey);
        break;        
      default:
        Console.WriteLine("Error in command");
        break;
    }
  }

  private void RemoveAnimal(string[] cmd)
  {
    if (cmd.Length < 2) { 
      Console.WriteLine("Error: Remove needs an argument");
      return;
    }
    string name = cmd[1];
    zoo.RemoveAnimal(name);
  }

}

최종 결과입니다.

0개의 댓글