模擬餐飲點餐結帳等行為
帳單輸出
/*
[Bill]
ID:123100000001
Destination:543
Total:310
--- Items ---
3 咖啡 $50
冰量 熱
大小 L $20
咖啡加料 肉桂粉 $10
咖啡加料 香草粉 $10
咖啡加料 九層塔 $10
2 咖啡 $50
冰量 冰
大小 L $20
--- Discount ---
剪報折10元 $10
--- Payment ---
現金 $100
VISA $210
*/
貼紙輸出
/*
[Sticker]
1/5
ID:123100000001
Destination:543
Total:310
### 咖啡 $50 ###
冰量 熱
大小 L $20
咖啡加料 肉桂粉 $10
咖啡加料 香草粉 $10
咖啡加料 九層塔 $10
2/5
ID:123100000001
Destination:543
Total:310
### 咖啡 $50 ###
冰量 熱
大小 L $20
咖啡加料 肉桂粉 $10
咖啡加料 香草粉 $10
咖啡加料 九層塔 $10
3/5
ID:123100000001
Destination:543
Total:310
### 咖啡 $50 ###
冰量 熱
大小 L $20
咖啡加料 肉桂粉 $10
咖啡加料 香草粉 $10
咖啡加料 九層塔 $10
4/5
ID:123100000001
Destination:543
Total:310
### 咖啡 $50 ###
冰量 冰
大小 L $20
5/5
ID:123100000001
Destination:543
Total:310
### 咖啡 $50 ###
冰量 冰
大小 L $20
*/
程式碼
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test1234
{
public static class POS
{
public static void Test()
{
var order = new Order
{
ID = "123100000001",
Destination = "543",
Items = new List<OrderItem>
{
new OrderItem { ID = "001", Name = "咖啡", Price = 50, Quantity = 3, UOM = "杯", Modifiers = new List<Modifier> {
new Modifier { ID = "冰量", Name = "冰量", Items = new List<ModifierItem> { new ModifierItem { ID = "冰量:熱", Name = "熱", Price = 0 } } },
new Modifier { ID = "大小", Name = "大小", Items = new List<ModifierItem> { new ModifierItem { ID = "大小:L", Name = "L", Price = 20 } } },
new Modifier { ID = "咖啡加料", Name = "咖啡加料", Items = new List<ModifierItem> {
new ModifierItem {ID="咖啡加料:肉桂粉", Name="肉桂粉", Price=10},
new ModifierItem {ID="咖啡加料:香草粉", Name="香草粉", Price=10},
new ModifierItem {ID="咖啡加料:九層塔", Name="九層塔", Price=10},
} }
}
},
new OrderItem { ID = "001", Name = "咖啡", Price = 50, Quantity = 2, UOM = "杯", Modifiers = new List<Modifier> {
new Modifier { ID = "冰量", Name = "冰量", Items = new List<ModifierItem> { new ModifierItem { ID = "冰量:冰", Name = "冰", Price = 0 } } },
new Modifier { ID = "大小", Name = "大小", Items = new List<ModifierItem> { new ModifierItem { ID = "大小:L", Name = "L", Price = 20 } } }
}
}
},
Payments = new List<PaymentItem>
{
new PaymentItem{ ID="現金", Name="現金", Category="直接支付", Amount=100 },
new PaymentItem{ ID="信用卡VISA", Name="VISA", Category="信用卡", Amount=210 }
},
Discounts = new List<DiscountItem>
{
new DiscountItem{ ID="2021剪報活動", Name="剪報折10元", Category="折價", Amount=10 }
}
};
var hub = new PrinterHub
{
new BillPrinter(),
new StickerPrinter()
};
hub.Print(order);
Console.ReadKey();
}
}
public class StickerPrinter : IPrinter
{
public void Print(Order order)
{
var sb = new StringBuilder();
sb.AppendLine("[Sticker]");
var count = order.Items.Sum(_ => _.Quantity);
var index = 1;
foreach (var i in order.Items)
{
for (int j = 0; j < i.Quantity; j++)
{
sb.AppendLine($"{index}/{count}");
index++;
sb.AppendLine($"ID:{order.ID}");
sb.AppendLine($"Destination:{order.Destination}");
sb.AppendLine($"Total:{order.Total}");
sb.AppendLine($"### {i.Name} ${i.Price} ###");
foreach (var m in i.Modifiers)
{
foreach (var mi in m.Items)
{
sb.AppendLine($" {m.Name} {mi.Name} {(mi.Price == 0 ? "" : "$"+mi.Price)}");
}
}
}
}
Console.WriteLine(sb);
}
static string PrintPrice(string input)
{
decimal v;
if (!decimal.TryParse(input, out v)) return "";
if (v == 0) return "";
return $"${v}";
}
}
public class BillPrinter : IPrinter
{
public void Print(Order order)
{
var sb = new StringBuilder();
sb.AppendLine("[Bill]");
sb.AppendLine($" ID:{order.ID}");
sb.AppendLine($" Destination:{order.Destination}");
sb.AppendLine($" Total:{order.Total}");
sb.AppendLine();
sb.AppendLine("--- Items ---");
foreach (var i in order.Items)
{
sb.AppendLine($"{i.Quantity} {i.Name} ${i.Price}");
foreach (var m in i.Modifiers)
{
foreach (var mi in m.Items)
{
sb.AppendLine($" {m.Name} {mi.Name} {(mi.Price == 0 ? "" : "$" + mi.Price)}");
}
}
}
sb.AppendLine("--- Discount ---");
foreach (var i in order.Discounts)
{
sb.AppendLine($" {i.Name} ${i.Amount}");
}
sb.AppendLine("--- Payment ---");
foreach (var i in order.Payments)
{
sb.AppendLine($" {i.Name} ${i.Amount}");
}
Console.WriteLine(sb);
}
}
public class PrinterHub : List<IPrinter>, IPrinter
{
public void Print(Order order)
{
foreach (var i in this)
{
i.Print(order);
}
}
}
public class DiscountItem
{
public string ID { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Amount { get; set; }
}
public class PaymentItem
{
public string ID { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Amount { get; set; }
}
public class OrderItem : IChargeable
{
public string ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public decimal Quantity { get; set; }
public string UOM { get; set; }
public List<Modifier> Modifiers { get; set; }
public decimal Compute(Order order, decimal current)
{
var total = Price * Quantity;
if (Modifiers != null)
foreach (var i in Modifiers)
total = i.Compute(order, total);
return current + total;
}
}
public class Order
{
public string ID { get; set; }
public string Destination { get; set; }
public decimal Total
{
get
{
var total = 0M;
if (Items != null)
foreach (var i in Items)
total = i.Compute(this, total);
if (Discounts != null)
foreach (var i in Discounts)
total -= i.Amount;
return total;
}
}
public List<OrderItem> Items { get; set; }
public List<PaymentItem> Payments { get; set; }
public List<DiscountItem> Discounts { get; set; }
}
public class ModifierItem : IChargeable
{
public string ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public decimal Compute(Order order, decimal current)
{
return current + Price;
}
}
public class Modifier : IChargeable
{
public string ID { get; set; }
public string Name { get; set; }
public List<ModifierItem> Items { get; set; }
public decimal Compute(Order order, decimal current)
{
if (Items == null || Items.Count < 1) return current;
var total = 0M;
foreach (var i in Items)
total = i.Compute(order, total);
return total + current;
}
}
/// <summary>收費項目</summary>
public interface IChargeable
{
decimal Compute(Order order, decimal current);
}
/// <summary>列印或顯示資料的裝置</summary>
public interface IPrinter
{
void Print(Order order);
}
}