0

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 3
    What exactly don't you understand in your assignment. It seems you already created a constructor, so why can't you make one with just one argument? – Patrick Hofman Nov 23 '18 at 08:53
  • 1
    You say you want the length of all three arrays as a single argument. It looks like they're all the same length. So `public Toppings(int length)` would do it. – JohnLBevan Nov 23 '18 at 08:54
  • 1
    Shouldn't the three array be properties? Then simply initialize them to the count given in the constructor (instead of 12 in te declaration).. – TaW Nov 23 '18 at 08:54
  • NB: If you wanted to support different lengths, you could make the single argument `int[3] lengths` or define a struct/class with the 3 lengths as properties and pass an instance of that. – JohnLBevan Nov 23 '18 at 08:55
  • What about using an array of tuples like `Tuple[]`? – Fruchtzwerg Nov 23 '18 at 08:57
  • 1
    What about not messing with their assignment @Fruchtzwerg ;) . Homework is never efficient... And even then, use a class. – Patrick Hofman Nov 23 '18 at 08:58

1 Answers1

1

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178