hello
hello

Reputation: 13

I am trying to take a user input and display

This is my code. I am trying to take a user input and display a FoodItem, but the way I have done it only allows for one input to be entered without doing lots of "or" statements in the while loop. Also to print the profile based on an input. Is there any way to print this multiple time?

namespace excercise_4_week_6
{
    class Program
    {
        double totalCost;
        string inputString = "";

        void run()
        {
            Console.WriteLine("==++     Food Menu     ++==");
            Console.WriteLine();

            Console.WriteLine("Please enter the item you would like to order: ");

            Console.WriteLine();

            FoodItem Pizza = new FoodItem("Pizza", "Margherita Pizza", 4, 9.99);
            FoodItem Burger = new FoodItem("Burger", "Burger with Cheese and chips", 1, 8.99);

            while (inputString == "" || inputString == Pizza.Name)
                {
                    inputString = Console.ReadLine();
                    Console.WriteLine();

                    if (inputString == Pizza.Name)
                    {
                        Pizza.PrintFoodProfile();
                        totalCost = totalCost + Pizza.Cost;
                        Console.WriteLine("The total cost is: £" + totalCost);
                        Console.WriteLine();
                    }
                }
        }

        static void Main(string[] args)
        {
          Program excercise_4_week_6 = new Program();
          excercise_4_week_6.run();
       }
    }

    public class FoodItem
    {
        string name;
        string description;
        int numberServed;
        double cost;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

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

        public double Cost
        {
            get { return cost; }
            set { cost = value; }
        }

        public FoodItem(string name, string description, int numberServed, double cost)
        {
            this.name = name;
            this.description = description;
            this.numberServed = numberServed;
            this.cost = cost;
        }

        public void PrintFoodProfile()
        {
            Console.WriteLine("Name: " + name);
            Console.WriteLine("Description: " + description);
            Console.WriteLine("The number of people this serves: " + numberServed);
            Console.WriteLine("Cost: £" + cost);
            Console.WriteLine("---------------------------------------");
            Console.WriteLine();
        }
    }
}

Upvotes: 0

Views: 121

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112712

Put your food items into a dictionary:

var foodItems =
    new Dictionary<string, FoodItem>(StringComparer.CurrentCultureIgnoreCase);

foodItem.Add("Pizza", new FoodItem("Pizza", "Margherita Pizza", 4, 9.99));
foodItem.Add("Burger", new FoodItem("Burger", "Burger with Cheese & chips", 1, 8.99));

Now you can find food items by name like this:

FoodItem foodItem;
if (foodItems.TryGetValue(userInput, out foodItem)) {
    Console.WriteLine(foodItem.Name);
} else {
    Console.WriteLine("not found!");
}

You can loop through all the food items like this:

double totalCost = 0;
foreach (FoodItem foodItem in foodItems.Values) {
    totalCost += foodItem.Cost;
    Console.WriteLine("{0} = {1:0.00}", foodItem.Name, foodItem.Cost);
}
Console.WriteLine("Total cost = {1:0.00}", foodItem.Cost);

If you only need to loop through the food items, but don't have to look up single items by name, then adding the food items to a List<FoodItem> would be sufficient.

Upvotes: 1

ashkufaraz
ashkufaraz

Reputation: 5307

you want remove while?

instead of

 while (inputString == "" || inputString == Pizza.Name)
  {
      inputString = Console.ReadLine();
      Console.WriteLine();

      if (inputString == Pizza.Name)
      {
          Pizza.PrintFoodProfile();
          totalCost = totalCost + Pizza.Cost;
          Console.WriteLine("The total cost is: £" + totalCost);
          Console.WriteLine();
      }
  }

use this

    if (inputString == Pizza.Name)
   {
       Pizza.PrintFoodProfile();
       //get NumberServed
       Pizza.NumberServed = Console.ReadLine();
       totalCost = Pizza.NumberServed * Pizza.Cost;
   }
   else if (inputString == Burger.Name)
   {
       Burger.PrintFoodProfile();
       //get NumberServed
       Burger.NumberServed = Console.ReadLine();
       totalCost = Burger.NumberServed * Burger.Cost;
    }
    Console.WriteLine("The total cost is: £" + totalCost);

for remove while

Upvotes: 0

Related Questions