C# 电子商务系统开发:用户、商品、订单、支付、购物车、售后服务、仓库、钱包实现
//用户类 public class User { public string Name { get; set; } public string Password { get; set; } public ShoppingCart Cart { get; set; } public Wallet Wallet { get; set; }
public User(string name, string password)
{
Name = name;
Password = password;
Cart = new ShoppingCart();
Wallet = new Wallet();
}
public void Register()
{
//注册逻辑
}
public bool Login()
{
//登录逻辑
return true;
}
public void Pay(PaymentTool tool, Order order)
{
Wallet.Pay(tool, order);
}
public void Return(Order order)
{
//退货逻辑
}
public void Exchange(Order order)
{
//换货逻辑
}
}
//商品类 public class Product { public string Name { get; set; } public string Type { get; set; } public decimal Price { get; set; } public string Description { get; set; } }
//订单类 public class Order { public Product Product { get; set; } public User User { get; set; } public int Quantity { get; set; } public string Address { get; set; } }
//支付工具类 public abstract class PaymentTool { public abstract void Pay(decimal amount); public abstract void Receive(decimal amount); }
//支付宝类 public class Alipay : PaymentTool { public override void Pay(decimal amount) { //支付宝支付逻辑 }
public override void Receive(decimal amount)
{
//支付宝收款逻辑
}
}
//银行卡类 public class BankCard : PaymentTool { public override void Pay(decimal amount) { //银行卡支付逻辑 }
public override void Receive(decimal amount)
{
//银行卡收款逻辑
}
}
//购物车类 public class ShoppingCart { private Dictionary<Product, int> products = new Dictionary<Product, int>();
public void AddProduct(Product product, int quantity)
{
if (products.ContainsKey(product))
{
products[product] += quantity;
}
else
{
products.Add(product, quantity);
}
}
public void RemoveProduct(Product product, int quantity)
{
if (products.ContainsKey(product))
{
if (products[product] >= quantity)
{
products[product] -= quantity;
}
else
{
throw new Exception('购物车中没有足够的商品');
}
}
else
{
throw new Exception('购物车中没有该商品');
}
}
}
//钱包类
public class Wallet
{
private List
public void BindPaymentTool(PaymentTool tool)
{
paymentTools.Add(tool);
}
public void UnbindPaymentTool(PaymentTool tool)
{
paymentTools.Remove(tool);
}
public void Pay(PaymentTool tool, Order order)
{
tool.Pay(order.Product.Price * order.Quantity);
}
public void Receive(PaymentTool tool, decimal amount)
{
tool.Receive(amount);
}
}
//售后服务类 public class CustomerService { public void Return(Order order) { Warehouse.Inbound(order.Product, order.Quantity); order.User.Wallet.Receive(new Alipay(), order.Product.Price * order.Quantity); }
public void Exchange(Order order)
{
Warehouse.Inbound(order.Product, order.Quantity);
Warehouse.Outbound(new Product(), order.Quantity);
}
}
//仓库类 public sealed class Warehouse { private static Warehouse instance = null; private static readonly object padlock = new object();
private Dictionary<Product, int> products = new Dictionary<Product, int>();
public static Warehouse Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Warehouse();
}
return instance;
}
}
}
public void Inbound(Product product, int quantity)
{
if (products.ContainsKey(product))
{
products[product] += quantity;
}
else
{
products.Add(product, quantity);
}
}
public void Outbound(Product product, int quantity)
{
if (products.ContainsKey(product))
{
if (products[product] >= quantity)
{
products[product] -= quantity;
}
else
{
throw new Exception('库存不足');
}
}
else
{
throw new Exception('库存不足');
}
}
}
原文地址: https://www.cveoy.top/t/topic/oa9l 著作权归作者所有。请勿转载和采集!