簡體   English   中英

C# 快餐菜單

[英]C# Fast Food Menu

我正在編寫一個顯示快餐菜單的程序,它允許用戶 select 一個項目。 然后,用戶輸入該項目的數量,並可以繼續選擇具有特定數量的項目,直到完成。 我遇到的麻煩是找出如何計算運行總數。 我是 c# 的新手,所以我只知道基礎知識。 在使用多種方法的同時跟蹤運行總計的最佳方法是什么? 此外,我對我的代碼中的任何批評持開放態度。 先感謝您。

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            
        bool ordering = true;
        string userInput;
        double itemPrice;
        double itemQuantity;
        double subTol;
        string response;
        
        void pricing()
        {
            Console.Write("Enter option 1, 2, 3: ");
            userInput = Console.ReadLine();
            switch (userInput) 
            {
                case "1":
                    itemPrice = 3.00;
                    Console.WriteLine("You have picked a burger.");
                    break;
                case "2":
                    itemPrice = 1.50;
                    Console.WriteLine("You have picked fries.");
                    break;
                case "3":
                    itemPrice = 1.00;
                    Console.WriteLine("You have picked a soda.");
                    break;
                default:
                    Console.WriteLine("That is not on our menu.");
                    pricing();
                    break;
            }
        }

        void quantity()
        {
            Console.Write("Enter quantity: ");
            itemQuantity = Convert.ToDouble(Console.ReadLine());
        }

        void subTotal()
        {
            subTol = itemQuantity * itemPrice;
            Console.WriteLine();
            Console.WriteLine("Your Subtotal: " + subTol);
        }
                
        while (ordering)
        {
            Console.WriteLine("What would you like from our menu?");
            Console.WriteLine("\n1. Burger ($3.00) \n2. Fries ($1.50) \n3. Soda ($1.00)");
            Console.WriteLine();
            
            pricing();
            quantity();
            subTotal();
            
            Console.Write("Would you like anything else? Y/N: ");
            response = Console.ReadLine();
            response = response.ToUpper();

            if (response == "Y")
            {
                ordering = true;
            }
            else
            {
                ordering = false;
                Console.WriteLine("Enjoy your meal!");
            }
                
        }
        
        }
    }
}

首先,對於If(response==“Y”)部分,您實際上可以只說ordering = response == “Y”; 此外,這只接受大寫的 y。 String.EqualsIgnoreCase() 或類似的東西可能會做得更好ordering = response.equalsIgnoreCase(“Y”);

您可以保留一份清單,記錄此人訂購的每件商品的數量,例如Int burgers = 0; 等等。 那么總的來說,你可以得到3*burgers + 1.5*fries等等。 但是,如果您想要自定義訂單,使用結構可能會更好。 例如,一個結構可以包含一個類型(漢堡或薯條)和一個價格,具體取決於它們添加了多少。 您也可以在每次訂購時將每件商品的價格加到總價中。 這似乎是一個有趣的項目,希望我能提供幫助!

為了跟蹤總數,最好的方法是有一個已訂購項目及其數量的列表,您可以在其中輸入 go 並從用戶那里下訂單。

當你想計算小計或總計時,你只需 go 並從列表中計算。

以下是關於如何通過對您共享的代碼進行一些更改來實現這一目標的一些見解。 查看評論以了解詳細信息和推理。

namespace ConsoleApp1{
  // creating record classes to store structured data
  record MenuItem(string Product, decimal Price);
  record Order(MenuItem Item, int Quantity);

  class Program {
    static void Main(string[] args) {
      // better to keep your menu options & prices in a central place, 
      // instead of hardcoding them everywhere
      List<MenuItem> menu = new() { 
        new ("Burger", 3.00M), 
        new ("Fries", 1.50M), 
        new ("Soda", 1.00M), 
      };
      // this is a list to store the ordered items so far
      List<Order> orders = new();

      // moved most of the procedures to separate functions
      do {
        PrintMenu(menu);
        var item = InputItem(menu);
        var quantity = InputQuantity();
        orders.Add(new Order(item, quantity));
        PrintSubTotal(orders);
      } while (CheckIfContinue());
      PrintTotal(orders);
      ByeBye();
    }

    static void PrintMenu(List<MenuItem> menu) {
      // print each menu entry in a loop
      Console.WriteLine("What would you like from our menu?");
      for (int i = 0; i < menu.Count; i++) {
        // adding 1 to the index for printing, because in c# indexes start at 0
        Console.WriteLine($"{i + 1}: {menu[i].Product} (${menu[i].Price})");
      }
      Console.WriteLine();
    }

    static MenuItem InputItem(List<MenuItem> menu) {
      // enter a loop, will be broken only if the input is correct
      while (true) {
        Console.Write($"Enter option: 1 to {menu.Count}: ");
        if (int.TryParse(Console.ReadLine(), out int i) && i > 0 && i <= menu.Count) {
          // must subtract 1 from the index, because indexes start from 0 in c#
          Console.WriteLine($"You have picked {i}: {menu[i - 1].Product}.\n");
          // if the input is a valid int
          return menu[i - 1];
        }
        Console.WriteLine("That is not in our menu.\n");
      }
    }

    static int InputQuantity() {
      // same thing here, will repeat the loop unless the input is valid
      while (true) {
        Console.Write("Enter quantity: ");
        if (int.TryParse(Console.ReadLine(), out int i) && i > 0) {
          return i;
        }
        Console.WriteLine("Invalid quantity.\n");
      }
    }

    static void PrintSubTotal(List<Order> orders) {
      // using LINQ to iterate the list of orders sum up the prices x quantities
      Console.WriteLine($"Your Subtotal: ${orders.Sum(o => o.Item.Price * o.Quantity)}\n");
    }

    static bool CheckIfContinue() {
      Console.Write("Would you like anything else? Y/N: ");
      // will return True if the input is y
      return Console.ReadLine()?.Trim().ToUpper() == "Y";
    }

    static void PrintTotal(List<Order> orders) {
      Console.WriteLine($"Your Order:");
      foreach (var order in orders) {
        // subtotal for each order item
        Console.WriteLine($"{order.Item.Product, -10} x{order.Quantity, -4}: ${order.Item.Price * order.Quantity}");        
      }
      // total for the whole order
      Console.WriteLine($"---------- Total: ${orders.Sum(o => o.Item.Price * o.Quantity)}\n");
    }

    static void ByeBye() {
      Console.WriteLine("Enjoy your meal!");
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM