C# Console.ReadLine();

I am trying to create a little application which takes orders from a user and then adds up the total.  I want the user to be able to make more than one order on the menu.  Right now once the user enters an order number and presses the enter key, the command line screen dissappears.  I'm guessing I have to use an array somehow, but I have no idea where to begin.

Below is what I have so far and it does compile and run.  It's still a work in progress.

using System;
using System.Collections.Generic;
using System.Text;
 
namespace cafeMenu
{
    class menu
    {
        private const int HOT_CHOCOLATE                               = 1;
        private const int CROISSANT                                   = 2;
        private const int SALAMI_SANDWHICH                            = 3;
        private const int SODA                                        = 4;
        private const int CHEESECAKE                                  = 5;
        private const int LARGE_COFFEE                                = 6;
        private const int LARGE_COFFEE_AND_CHEESECAKE                 = 7;
        private const int HOT_CHOCOLATE_AND_CROISSANT                 = 8;
        private const int SALAMI_SANDWHICH_AND_SODA                   = 9;
        private const int HOT_CHOCOLATE_AND_CHEESECAKE                = 10;
        private const int SALAMI_SANDWHICH_AND_SODA_AND_CHEESECAKE    = 11;
 
 
        public void printMenu()
        {
            Console.WriteLine("-----------------------------");
            Console.WriteLine("----------CAFE MENU----------");
            Console.WriteLine(" 1. Hot Chocolate:  $1.50");
            Console.WriteLine(" 2. Croissant:  $2.50");
            Console.WriteLine(" 3. Salami Sandwhich:  $3.75");
            Console.WriteLine(" 4. Soda:  $1.75");
            Console.WriteLine(" 5. Cheesecake:  $4.50");
            Console.WriteLine(" 6. Large Coffee:  $3.00\n");
            Console.WriteLine("Combos");
            Console.WriteLine(" 7. Large Coffee and Cheesecake:  $7.00");
            Console.WriteLine(" 8. Hot Chocolate and Croissant:  $5.25");
            Console.WriteLine(" 9. Salami Sandwhich and Soda:  $5.75");
            Console.WriteLine(" 10. Hot Chocolate and Cheesecake:  $5.75");
            Console.WriteLine(" 11. Salami Sandwhich, Soda and cheesecake:  $9.50\n\n");
        }
 
        public void processOrder()
        {
            Console.WriteLine("Please enter the number to your order:");
            int ordNum = Convert.ToInt32(Console.ReadLine());            
 
            Console.WriteLine("Your order is " + ordNum);
        }
 
        static void Main(string[] args)
        {
          menu customer = new menu();
          customer.printMenu();
          customer.processOrder();
 
          Console.ReadLine();
        }
    }
}

Open in new window

LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
 
oxyooCommented:
I made some additions to the code, after every choice you should be directed back to the menu. Hitting "0" will exit the application.
NOTE: the class is named Program in the example below instead of menu as in your sample. Either two will work though, as long as you update the Main method.
Good Luck!

namespace cafeMenu
{
    class Program
    {
        private const int HOT_CHOCOLATE = 1;
        private const int CROISSANT = 2;
        private const int SALAMI_SANDWHICH = 3;
        private const int SODA = 4;
        private const int CHEESECAKE = 5;
        private const int LARGE_COFFEE = 6;
        private const int LARGE_COFFEE_AND_CHEESECAKE = 7;
        private const int HOT_CHOCOLATE_AND_CROISSANT = 8;
        private const int SALAMI_SANDWHICH_AND_SODA = 9;
        private const int HOT_CHOCOLATE_AND_CHEESECAKE = 10;
        private const int SALAMI_SANDWHICH_AND_SODA_AND_CHEESECAKE = 11;
 
 
        public void printMenu()
        {
            Console.Clear();
            Console.WriteLine("-----------------------------");
            Console.WriteLine("----------CAFE MENU----------");
            Console.WriteLine(" 1. Hot Chocolate:  $1.50");
            Console.WriteLine(" 2. Croissant:  $2.50");
            Console.WriteLine(" 3. Salami Sandwhich:  $3.75");
            Console.WriteLine(" 4. Soda:  $1.75");
            Console.WriteLine(" 5. Cheesecake:  $4.50");
            Console.WriteLine(" 6. Large Coffee:  $3.00\n");
            Console.WriteLine("Combos");
            Console.WriteLine(" 7. Large Coffee and Cheesecake:  $7.00");
            Console.WriteLine(" 8. Hot Chocolate and Croissant:  $5.25");
            Console.WriteLine(" 9. Salami Sandwhich and Soda:  $5.75");
            Console.WriteLine(" 10. Hot Chocolate and Cheesecake:  $5.75");
            Console.WriteLine(" 11. Salami Sandwhich, Soda and cheesecake:  $9.50\n\n");
            Console.WriteLine("-----------------------------");
            Console.WriteLine(" Press '0' to exit");
        }
 
        public int requestOrder()
        {
            Console.WriteLine("Please enter the number to your order:");
            return Convert.ToInt32(Console.ReadLine());
        }
 
        public void processOrder(int orderNumber)
        {
            Console.Clear();
 
            if (orderNumber == 0)
            {
                Console.WriteLine("Exiting application...");
            }
            else
            {
                Console.WriteLine("Your order is " + orderNumber);
            }
 
            Console.WriteLine("(Press any key to continue)");
            Console.ReadLine();
        }
 
        static void Main(string[] args)
        {
            bool exitApplication = false;
 
            while (!exitApplication)
            {
                Program customer = new Program();
                customer.printMenu();
                int orderNumber = customer.requestOrder();
                customer.processOrder(orderNumber);
 
                exitApplication = orderNumber == 0;
            }
        }
    }
}

Open in new window

0
 
oxyooCommented:
Just to clarify, the above example does not store the orders anywhere, it just enables you to make several orders.
If you wish to add store the orders, you could create a List<int> orders = new List<int>(); and in the requestOrder method store the orders made using orders.Add(...).

0
 
anarki_jimbelCommented:
I have a number of remarks on this draft of your application.

First, the application needs to have it's own APPLICATION MENU. For example:
M - View Cafe Menu
S - View Selected Orders
E - Exit The Cafe Application
etc.

Second, if you want to count Total at least you need some variables for prices.
Also you need somehow to associate order numbers with prices. I believe the best way is to use a hashtable or dictionary. I will use a Hashtable as generic Dictionary can be confusing at this stage I'm afraid. Hashtable: - order number is a key, price is a value.

OK, next task is to accumulate orders. oxyoo proposed to use a list. I don't like the idea. Say, I ordered cofee, croissant, cofee, croissant. If I want to display my order it will list it as a sequence above. But I want
cofee - 2
croissant - 2.
Therefore again we need to use a hashtable: ordernumber is a key, quantity is a value.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
anarki_jimbelCommented:
OK, I implemented some ideas, some ideas I leave for you.Ask questions if something isnot clear. The code is just a draft - I didn't care about menus etc as I didn't know about requirements...
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
 
namespace Menu
{
    class Menu{
        private const int HOT_CHOCOLATE                               = 1;
        private const int CROISSANT                                   = 2;
        private const int SALAMI_SANDWHICH                            = 3;
        private const int SODA                                        = 4;
        private const int CHEESECAKE                                  = 5;
        private const int LARGE_COFFEE                                = 6;
        private const int LARGE_COFFEE_AND_CHEESECAKE                 = 7;
        private const int HOT_CHOCOLATE_AND_CROISSANT                 = 8;
        private const int SALAMI_SANDWHICH_AND_SODA                   = 9;
        private const int HOT_CHOCOLATE_AND_CHEESECAKE                = 10;
        private const int SALAMI_SANDWHICH_AND_SODA_AND_CHEESECAKE    = 11;
 
        private double price1 = 1.5;
        private double price2 = 2.5;
        private double price3 = 3.75;
        private double price4 = 1.75;
        private double price5 = 4.5;
        private double price6 = 3.0;
        private double price7 = 7.0;
        private double price8 = 5.25;
        private double price9 = 5.75;
        private double price10 = 5.75;
        private double price11 = 9.5;
 
        private Hashtable itemPriceTable;
        private Hashtable ordersTable = new Hashtable();
 
        private void InitializeItemPriceTable()
        {
            itemPriceTable = new Hashtable();
            double[] prices = { price1, price2, price3, price4, price5, price6, price7, price8, price9, price10, price11 };
            for(int i = 0; i<prices.Length;i++)
            {
                // key - oredr number, value - corresponding price
                itemPriceTable.Add(i+1,prices[i]);
            }
        }
 
        public void printCafeMenu()
        {
            Console.WriteLine("-----------------------------");
            Console.WriteLine("----------CAFE MENU----------");
            Console.WriteLine(" 1. Hot Chocolate:  $1.50");
            Console.WriteLine(" 2. Croissant:  $2.50");
            Console.WriteLine(" 3. Salami Sandwhich:  $3.75");
            Console.WriteLine(" 4. Soda:  $1.75");
            Console.WriteLine(" 5. Cheesecake:  $4.50");
            Console.WriteLine(" 6. Large Coffee:  $3.00\n");
            Console.WriteLine("Combos");
            Console.WriteLine(" 7. Large Coffee and Cheesecake:  $7.00");
            Console.WriteLine(" 8. Hot Chocolate and Croissant:  $5.25");
            Console.WriteLine(" 9. Salami Sandwhich and Soda:  $5.75");
            Console.WriteLine(" 10. Hot Chocolate and Cheesecake:  $5.75");
            Console.WriteLine(" 11. Salami Sandwhich, Soda and cheesecake:  $9.50\n\n");
        }
 
        public void printApplicationMenu()
        {
            Console.WriteLine("=============================");
            Console.WriteLine("T - Show Total");
            Console.WriteLine();
        }
        public void processOrder(int orderNumber)
        {
            
            // add order to  orderstable
            if (ordersTable.ContainsKey(orderNumber))
            {
                // we have already such an order - just increase quantity by 1;
                ordersTable[orderNumber] = ((int)(ordersTable[orderNumber])) + 1;
            }
            else{
                ordersTable.Add(orderNumber,1);
   
            }
 
        }
 
        public void ShowTotal()
        {
            // iterate through ordersTable, calculate and print:
            double total = 0;
            foreach (int key in ordersTable.Keys)
            {
                // key is menu selection item
                int itemQuantity = (int)ordersTable[key];
                double price = (double)itemPriceTable[key];
                double itemTotal = itemQuantity * price;
                total += itemTotal;
            }
            Console.WriteLine("_________________________");
            Console.WriteLine("Total = " + string.Format("{0:c}", total));
            Console.WriteLine("_________________________");
 
        }
 
        static void Main(string[] args)
        {
          Menu customer = new Menu();
          customer.InitializeItemPriceTable();
          customer.printCafeMenu();
          customer.printApplicationMenu();
 
            // read input and figure out if input is order number or some command like 'T' - show Total
          string input = ""; 
          int orderNumber;
          bool isNumeric = true;
          while (input != "E")
          {
              while (isNumeric)
              {
                  Console.WriteLine("Please enter the number to your order or action option");
                  input =  Console.ReadLine();
                  isNumeric = int.TryParse(input, out orderNumber);
                  if (isNumeric)
                  {// really need to check if it's valid number - I skip that
                      customer.processOrder(orderNumber);
                  }
                  else
                  {
                      if (input == "T")
                      {
                        customer.ShowTotal();
                          // return to loop
                        isNumeric = true;
                      }
                      else if ((input == "E"))
                      {
                          Console.WriteLine("Good Bye");
                          Console.ReadLine();
                      }
                  }
              }
          }
 
         
 
 
          
 
         
        }
    }
 
}

Open in new window

0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
My apologies for the late reply.

I do have a question about the hash table used in processOrder().
It seems that if the order exists then increase by 1.
So, if my order is 1 from the menu and I made the same order again, then it will be 2.
If my order was 1 and 1 then the total will be 4.

As you can see, I'm a bit confused as to what's going on.

Please help clarify.

Thanks.
 public void processOrder(int orderNumber)
        {
            
            // add order to  orderstable
            if (ordersTable.ContainsKey(orderNumber))
            {
                // we have already such an order - just increase quantity by 1;
                ordersTable[orderNumber] = ((int)(ordersTable[orderNumber])) + 1;
            }
            else{
                ordersTable.Add(orderNumber,1);
   
            }
 
        }
 

Open in new window

0
 
anarki_jimbelCommented:
Didn't get that.Could you pls clarify:
"So, if my order is 1 from the menu and I made the same order again, then it will be 2.
If my order was 1 and 1 then the total will be 4."?

Why '4'? Are you talking about money value? But first orderis " 1. Hot Chocolate:  $1.50". So it would be $3.

Anyway, in the hashtable you are talking about I'm addin QUANTITY, not money value. So, when processing later you may find that for the hot choc we have e.g. 2 units. To find money value I use Hashtable itemPriceTable. I get prace for a hot choc, get number of hot chocs, multiply - yes, it will be $3. But in the hashtable we store just a number of hot chocolats.

I believe this way is more convenient, especially if you need to add functionality to remove some items from your order.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.