C#控制台商品售卖程序实现类小票全选购商品输出问题
问题解决思路
你代码中已经用ProductList字典存储了用户所有选购的商品及对应数量,无法输出全量商品列表的核心原因是没有遍历该字典生成小票内容,只需在找零计算完成后、询问用户是否再次购物前新增小票打印逻辑即可。
另外你原有代码的折扣计算逻辑存在问题:当前仅对总金额减0.7/0.5PHP,不符合常规折扣规则,默认s对应7折(总金额0.7)、v对应5折(总金额0.5),修改代码中同步修正了该问题。
修改后完整代码
using System; using System.Collections.Generic; namespace KpopMerchShop { class Program { static void Main(string[] args) { int choice, quanti, decide, total, cash; double change, actualPayTotal; string dcount; while (true) { Console.Clear(); string w = "WELCOME "; Console.SetCursorPosition((Console.WindowWidth - w.Length) / 2, Console.CursorTop); Console.WriteLine(w); Console.WriteLine(""); System.Threading.Thread.Sleep(2000); string p = "HERE'S OUR MERCHANDISES! "; Console.SetCursorPosition((Console.WindowWidth - p.Length) / 2, Console.CursorTop); Console.WriteLine(p); System.Threading.Thread.Sleep(2000); string[] products = { "[1]BLACKPINK Lightstick ", "[2]DREAMCATCHER Seasons Greetings", "[3]RED VELVET Summer Package"}; for (int g = 0; g < products.Length; g++) { Console.WriteLine(products[g]); } Dictionary<int, int> ProductList = new Dictionary<int, int>(); while (true) { Console.Write("Pick your product: "); choice = int.Parse(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("BLACKPINK Lightstick 1500php"); break; case 2: Console.WriteLine("DREAMCATCHER Seasons Greetings 920php"); break; case 3: Console.WriteLine("RED VELVET Summer Package 980php"); break; } Console.WriteLine("Quantity of product: "); quanti = int.Parse(Console.ReadLine()); if (!ProductList.ContainsKey(choice)) ProductList.Add(choice, quanti); else ProductList[choice] += quanti; System.Threading.Thread.Sleep(2000); Console.WriteLine("[1] Add more products \t [2] Pay: "); decide = int.Parse(Console.ReadLine()); if (decide == 2) break; } total = 0; foreach (int key in ProductList.Keys) switch (key) { case 1: total += ProductList[key] * 1500; break; case 2: total += ProductList[key] * 920; break; case 3: total += ProductList[key] * 980; break; default: total += 0; break; }; Console.WriteLine("To Pay: " + total + "php"); Console.Write("Cash: "); cash = Convert.ToInt32(Console.ReadLine()); Console.Write("Discount[s]suki [v]voucher: "); dcount = Console.ReadLine(); // 修正折扣计算逻辑 if (dcount == "s") { actualPayTotal = total * 0.7; change = cash - actualPayTotal; } else if (dcount == "v") { actualPayTotal = total * 0.5; change = cash - actualPayTotal; } else { actualPayTotal = total; change = cash - actualPayTotal; } Console.WriteLine("Actual payment amount: " + actualPayTotal.ToString("F2") + "php"); Console.WriteLine("Change: " + change.ToString("F2") + "php"); // 新增小票打印逻辑 Console.WriteLine("\n====================================="); string receiptTitle = "KPOP周边结算小票"; Console.SetCursorPosition((Console.WindowWidth - receiptTitle.Length) / 2, Console.CursorTop); Console.WriteLine(receiptTitle); Console.WriteLine("-------------------------------------"); Console.WriteLine("{0,-30} | {1,8} | {2,9} | {3,9}", "商品名称", "购买数量", "单价(PHP)", "小计(PHP)"); Console.WriteLine("-------------------------------------"); foreach (var item in ProductList) { string productName = ""; int price = 0; switch (item.Key) { case 1: productName = "BLACKPINK 应援棒"; price = 1500; break; case 2: productName = "DREAMCATCHER 季节问候台历"; price = 920; break; case 3: productName = "RED VELVET 夏日周边包"; price = 980; break; } int subTotal = price * item.Value; Console.WriteLine("{0,-30} | {1,10} | {2,9} | {3,9}", productName, item.Value, price, subTotal); } Console.WriteLine("-------------------------------------"); string discountType = dcount switch { "s" => "Suki会员7折", "v" => "优惠券5折", _ => "无优惠" }; Console.WriteLine("{0,-30}:{1,18} PHP", "原始应付总金额", total); Console.WriteLine("{0,-30}:{1,18:F2} PHP", "优惠后应付金额", actualPayTotal); Console.WriteLine("{0,-30}:{1,18} PHP", "实付现金", cash); Console.WriteLine("{0,-30}:{1,20}", "优惠类型", discountType); Console.WriteLine("{0,-30}:{1,18:F2} PHP", "找零金额", change); Console.WriteLine("=====================================\n"); // 小票打印逻辑结束 Console.WriteLine("Another shopping? [1]Yes [2] No"); choice = int.Parse(Console.ReadLine()); if (choice == 2) break; } } } }
内容的提问来源于stack exchange,提问作者MULTI FANDOM
相关产品推荐
相关产品推荐

