Професионална програма
Loading...
+ Нов въпрос
Elena123456 avatar Elena123456 224 Точки

04. Wild Farm

Здравейте, извинявам се много, но може ли някой да погледне решението ми на тази задача?

Получавам 20/100 и не мога да открия къде е проблема. С третия даден инпут виждам, че имам проблем при закръглянето на weight, но в условието не е опоменато какво закръгляне да се използва. Предполагам, че не би трябвало само заради това  Judge да ми дава толкова малко точки.

Благодаря предварително!

https://judge.softuni.bg/Contests/Practice/Index/1504#3 (условието е достъпно във файла)

https://github.com/elipopovadev/CSharp-OOP/tree/main/Polymorphism-Exercise/WildFarm

 

Тагове:
0
C# OOP Advanced 17/04/2021 00:08:52
Axiomatik avatar Axiomatik 1243 Точки
Best Answer

Hi Eli,

Failed tests can be caused either by how some of the classes are implemented or in the engine logic. Try first to rewrittes some of the bass classes and then the engine class to find the remaining issues.

Nice weekend!

Engine:

using System;
using System.Collections.Generic;
using WildFarm.Interfaces;
using WildFarm.IO.Interfaces;
using WildFarm.Models;

namespace WildFarm.Core.Engines
{
    internal enum FoodTypes
    {
        Vegetable = 1,
        Fruit = 2,
        Meat = 3,
        Seeds = 4,
    }

    public class Engine : IEngine
    {
        private IReader reader;
        private IWriter writer;

        private ICollection<IAnimal> animalList;

        private Engine()
        {
            this.animalList = new List<IAnimal>();
        }

        public Engine(IReader reader , IWriter writer)
            : this()
        {
            this.reader = reader;
            this.writer = writer;
        }

        public void Run()
        {
            string[] animalInfo = Console.ReadLine()
                .Split();

            while (animalInfo[0] != "End")
            {
                string[] foodInfo = Console.ReadLine()
                    .Split();

                string animalType = animalInfo[0];
                string animalName = animalInfo[1];
                double animalWeight = double.Parse(animalInfo[2]);

                string foodName = foodInfo[0];
                int quantity = int.Parse(foodInfo[1]);
                IFood food = null;
                IAnimal animal = null;


                try
                {
                    animal = AddAnimalToList(animalInfo, animalType, animalName, animalWeight, animal);
                    food = CreateFood(foodName, quantity, food);
                    Console.WriteLine(animal.AskForFood());
                    animal.FeedAnimal(food);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                animalInfo = Console.ReadLine()
                    .Split();
            }

            PrintOutput();

        }

        private void PrintOutput()
        {
            foreach (var animal in animalList)
            {
                Console.WriteLine(animal);
            }
        }

        private static IFood CreateFood(string foodName, int quantity, IFood food)
        {
            bool parse = Enum.TryParse(foodName, out FoodTypes foodtest);

            if (!parse)
            {
                throw new ArgumentException();
            }
            if (foodName == "Vegetable")
            {
                Vegetable vegetable = new Vegetable(quantity);
                food = vegetable;
            }
            else if (foodName == "Fruit")
            {
                Fruit fruit = new Fruit(quantity);
                food = fruit;
            }
            else if (foodName == "Meat")
            {
                Meat meat = new Meat(quantity);
                food = meat;
            }
            else if (foodName == "Seeds")
            {
                Seeds seeds = new Seeds(quantity);
                food = seeds;
            }

            return food;
        }

        private IAnimal AddAnimalToList(string[] animalInfo, string animalType, string animalName, double animalWeight, IAnimal animal)
        {
            if (animalType == "Hen")
            {
                double wingSize = double.Parse(animalInfo[3]);
                Hen hen = new Hen(animalName, animalWeight, wingSize);
                animal = hen;
            }
            else if (animalType == "Owl")
            {
                double wingSize = double.Parse(animalInfo[3]);
                Owl owl = new Owl(animalName, animalWeight, wingSize);
                animal = owl;
            }
            else if (animalType == "Mouse")
            {
                string livingRegion = animalInfo[3];
                Mouse mouse = new Mouse(animalName, animalWeight, livingRegion);
                animal = mouse;
            }
            else if (animalType == "Cat")
            {
                string livingRegion = animalInfo[3];
                string breed = animalInfo[4];
                Cat cat = new Cat(animalName, animalWeight, livingRegion, breed);
                animal = cat;
            }
            else if (animalType == "Dog")
            {
                string livingRegion = animalInfo[3];
                Dog dog = new Dog(animalName, animalWeight, livingRegion);
                animal = dog;
            }
            else if (animalType == "Tiger")
            {
                string livingRegion = animalInfo[3];
                string breed = animalInfo[4];
                Tiger tiger = new Tiger(animalName, animalWeight, livingRegion, breed);
                animal = tiger;
            }

            if (animal != null)
            {
                animalList.Add(animal);
            }

            return animal;
        }
    }
}

Animal:

using System;
using WildFarm.Interfaces;

namespace WildFarm.Models
{
    public abstract class Animal : IAnimal
    {
        private int foodEaten;
        private double weight;
        private string name;

        protected Animal(string name, double weight)
        {
            this.Name = name;
            this.Weight = weight;
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            private set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    throw new ArgumentException();
                }
                this.name = value;
            }
        }

        public double Weight
        {
            get
            {
                return this.weight;
            }
            protected set
            {
                if (value < 0)
                {
                    throw new ArgumentException();
                }
                this.weight = value;
            }
        }

        public int FoodEaten
        {
            get
            {
                return this.foodEaten;
            }
            protected set
            {
                if (value < 0)
                {
                    throw new ArgumentException();
                }
                this.foodEaten = value;
            }
        }

        public abstract string AskForFood();

        public abstract void FeedAnimal(IFood food);
    }
}

Mammal:

using System;
using WildFarm.Interfaces;

namespace WildFarm.Models
{
    public abstract class Mammal : Animal, IMammal
    {
        private string livingRegion;

        protected Mammal(string name, double weight, string livingRegion)
            : base(name, weight)
        {
            this.LivingRegion = livingRegion;
        }

        public string LivingRegion
        {
            get
            {
                return this.livingRegion;
            }
            private set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    throw new ArgumentException();
                }
                this.livingRegion = value;
            }
        }

        public override string ToString()
        {
            return $"{this.GetType().Name} [{this.Name}, {this.Weight}, {this.LivingRegion}, {this.FoodEaten}]";
        }
    }
}

 

 

0
Elena123456 avatar Elena123456 224 Точки

Hi Axiomatik ,

thanks for sharing your solution. I added some useful, but not required validation from your code in mine. And I checked my class hierarchy with yours and it's nearly the same. I find it very interesting to use enums  not classes for food. smiley

After checking the code in Judge  it was also 20/100. I can't belive where my bug was! It was in my Feed method, I have written this:

public override void FeedIt(Food food)

{

    if (!foodTypes.Any(f => f.Name == food.GetType().Name))

    {

        string exceptionMessages = String.Format(ExceptionMessages.InvalidFoodException, this.GetType().Name,food.GetType().Name);

        throw new ArgumentException(exceptionMessages);
    }

    int quantity = food.Quantity;

    for (int i = 0; i < quantity; i++)
    {

        base.Weight += IncrementIncreaseWeight;
    }

    base.FoodEaten += quantity;

}

 

but now my Feed method is without for loop:

public override void FeedIt(Food food)
{
if (!foodTypes.Any(f => f.Name == food.GetType().Name))
{
string exceptionMessages = String.Format(ExceptionMessages.InvalidFoodException, this.GetType().Name, food.GetType().Name);
throw new ArgumentException(exceptionMessages);
}
 
int quantity = food.Quantity;
base.Weight += quantity * IncrementIncreaseWeight;
base.FoodEaten += quantity;

}

 

100/100 https://github.com/elipopovadev/CSharp-OOP/tree/main/Polymorphism-Exercise/WildFarm

By the way you are right  it's really the best choice to start with exam preparation. And I will do it mixing with exercises from High quality code course (Design patterns).

Have a nice weekend!

 

1