HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Abstract Pet class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
petabstractclass

Problem

As a student of C# right now, I am trying my best to get a grasp on many of the systems being taught. I just completed Week 1 of a C# 102 class and the class was assigned a bit of homework. I am curious, from a beginners point of view, how you might restructure this code to make it more readable and understandable.

For context, here is a word for word description of what the homework was supposed to be.


Create an abstract Pet class that has abstract functionality (at least
3 methods). It must have a "string Name" read-only (he didnt mean
readonly keyword) that is filled by a parameter to the constructor. At
least one of the methods must take in parameters, and at least one of
the methods must return a value instead of printing things to the
console.


Create four implementations of the Pet class that all appropriately
implement the different methods.


Allow the user to enter 0 to many pets (can be any type) and store
them into a List object


After the user is done entering in pets allow the user to:


A: select a pet


B: perform an operation


C: allow the user to repeat everything

Thanks for your time. And if you do find yourself answering, please keep in mind, we have not gone over Interfaces yet. That is the next class. The things we learned in this class had mostly to do with virtual, override, abstract members and classes.

```
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 string Name { get; private set; }
public string Breed { get; private set; }

//Happiness relates to playing with the pet
public PetMood Mood { get; private set; }

//Pet hunger level
public HungerLevel H

Solution

A huge amount of that code is 'garbage', afraid to say.

You already have all the basic pet data stored in the Pet class... so there is no need to duplicate it all in each pet type class (Dog/Cat, etc.).

For example, your Cat class should remove all the duplication in then simply be:

public class Cat : Pet
{
    public Cat(string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated) : 
        base(name, breed, mood, hunger, isVaccinated)
    {
        // base constructor takes care of everything.
    }

    ......

}


Then, in your other methods you should reference the 'public' base variables Name instead of _name, etc.

Finally, you should add values to the Pet class that indicate thresholds for things like mood and hunger. All the currently 'abstract' methods on the Pet should instead be full methods, but should be parameterized. For example, the method on Dog:

public override PetMood PlayWithPet()
    {
        if ((int)_mood < 4)
        {
            Console.WriteLine("You threw a frisby!");
            return _mood += 1;
        }
        return _mood;
    }


Should instead be on Pet, and would look like:

public PetMood PlayWithPet()
    {
        if ((int)Mood < MoodThreshold)
        {
            Console.WriteLine(PlayMessage);
            return Mood += 1;
        }
        return Mood;
    }


Then, when you construct Dog you should also add all the thresholds and messages that are needed in the Pet class.

By doing this, the actual implementations ( Dog, Cat, etc.) become simple classes with just a constructor and no methods at all. The Pet Base class does all the heavy-lifting, and the code is in one place only.

Code Snippets

public class Cat : Pet
{
    public Cat(string name, string breed, PetMood mood, HungerLevel hunger, bool isVaccinated) : 
        base(name, breed, mood, hunger, isVaccinated)
    {
        // base constructor takes care of everything.
    }

    ......

}
public override PetMood PlayWithPet()
    {
        if ((int)_mood < 4)
        {
            Console.WriteLine("You threw a frisby!");
            return _mood += 1;
        }
        return _mood;
    }
public PetMood PlayWithPet()
    {
        if ((int)Mood < MoodThreshold)
        {
            Console.WriteLine(PlayMessage);
            return Mood += 1;
        }
        return Mood;
    }

Context

StackExchange Code Review Q#37962, answer score: 10

Revisions (0)

No revisions yet.