-8

I have an exercise that asks me to create 2 classes, each of which will carry some variables and i have to be able to access and modify them through properties.

public class FoodItem
{
        public string drinkname;
        public string drinkdescription;
        public int alcoholvolume;
        public double drinkcost;

         public FoodItem(string mfoodname)
         {
            foodname = mfoodname;
            fooddescription = "";
            peopleserved = 0;
            foodcost = 0.0;
         }
        public string Name
        {
            get
            {
                return foodname;
            }
            set
            {
                foodname = value;
            }

        }
        public string Description
        {
            get
            {
                return fooddescription;
            }
            set
            {
                fooddescription= value;
            }
        }
        public int NumberServed
        {
            get
            {
                return peopleserved;
            }
            set
            {
                    peopleserved = value;
            }
        }
        public double Cost
        {
            get
            {
                return foodcost;
            }
            set
            {
                foodcost = value-((value*23)/100);
            }
        }

        public void MaxPeopleServed()
        {
            if (peopleserved > 5 || peopleserved < 0)
            {
                Console.WriteLine("Invalid number of people served please enter a number between 1-5");
            }
        }
}

Here is my code.

The exercise asks that the peopleserved variable should be max 5.Can i do something like what my method does in the set property? Also i should check for numerical erroneous values. "In the event the number entered using the set property is erroneous, a default value of zero should be stored." What does that mean doesnt the constructor already initialize those? Thanks in Advance

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Thanice
  • 1
  • 1

2 Answers2

0

Change this:

public int NumberServed
    {
        get
        {
            return peopleserved;
        }
        set
        {
                peopleserved = value;
        }
    }

to this:

public int NumberServed
    {
        get
        {
            return peopleserved;
        }
        set
        {

                peopleserved = (value <= 5) ? value: 0;
        }
    }

This above code ensures that integral values from -2,147,483,648 to 5 inclusive can be assigned to the variable peopleserved. You may want to place more restrictions in the class to prevent erroneous values.

like so:

public int NumberServed
    {
        get
        {
            return peopleserved;
        }
        set
        {

                peopleserved = (value >= 0 && value <= 5) ? value: 0;
        }
    }
TheJackal
  • 435
  • 4
  • 19
0

Make all your data members private.

If they are public then there is no use of properties as they will be directly assignable. and refer @theJackal answer for the answer of the second part.

ankyAS
  • 301
  • 2
  • 11