Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# Console.ReadLine();

Posted on 2009-02-10
6
Medium Priority
?
1,212 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Isaac
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
oxyoo earned 1000 total points
ID: 23604470
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
 
LVL 10

Expert Comment

by:oxyoo
ID: 23604513
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
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 23616519
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1000 total points
ID: 23617678
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
 
LVL 5

Author Comment

by:Isaac
ID: 23779637
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
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 23779857
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question