How to write a constructor that accepts a single argument?

674 Views Asked by At

I am creating something like a kiosk program where you order food from using windows forms. I have to create a Toppings Class where it contains three fields that are arrays.

  • ToppingList as an array of bool
  • ToppingNames as an array of string
  • ToppingPrices as an array of double

The instructions say that the constructor must accept a single argument: the length of all three arrays which are parallel arrays.

I am not sure how to do this. I have researched and understand how parallel arrays work but I do not know how to implement and get all three lengths in a single argument. I am not sure if I am doing this correctly?

This is what I have so far:

namespace DeliAndPizza
{
    class Toppings
    {
        bool[] ToppingList = { false, false, false, false, false, false, false, false, false, false, false, false };
        string[] ToppingNames = { "Bacon", "Extra Cheese", "Hot Peppers", "Mayo", "Mushrooms", "Oil", "Onion", "Onion", "Oregano", "Peppers", "Sausage" };
        double[] ToppingPrices = {1.00, 1.50, 0.00, 0.00, 1.00, 0.00, 0.00, 1.00, 0.00, 1.00, 1.00, 0.00 };

        public Toppings()
        {
        }

        public Toppings(bool[] list, string[] name, double[] price)
        {
            this.ToppingList = list;
            this.ToppingNames = name;
            this.ToppingPrices = price;
        }
    }
}

Here is the given class diagram:

click here

1

There are 1 best solutions below

0
On BEST ANSWER

Assuming all arrays are the same length, you just need to pass the length parameter as the single argument to the constructor and initialize the arrays' lengths there, rather than defining the length where you've defined the fields:

namespace DeliAndPizza
{
    class Toppings
    {
        bool[] ToppingList;
        string[] ToppingNames;
        double[] ToppingPrices;

        public Toppings(): this(12) {} //default length is 12
        public Toppings(int length)
        {
            ToppingList = new bool[length];
            ToppingNames = new string[length];
            ToppingPrices = new double[length];
        }

        public Toppings(bool[] list, string[] name, double[] price)
        {
            this.ToppingList = list;
            this.ToppingNames = name;
            this.ToppingPrices = price;
        }
    }
}

One side note; currently you're using the definitions like bool[] ToppingList;. If you wanted to be able to access this value on an instance of a class (rather than only internally within the class), you'd need to make them public / ideally convert them to properties, like so:

public bool[] ToppingList {get;set;}

If you don't want them available externally, the naming convention would be to use a lowercase first letter; e.g.

bool[] toppingList;

Here's the MS documentation on naming standards.

The official guide only determines externally visible names; so private fields defined in your class aren't covered by the above doc. However, there are conventions.