98 lines
2.5 KiB
C#
98 lines
2.5 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using HK;
|
|||
|
|
|||
|
namespace Runtime
|
|||
|
{
|
|||
|
public class ShoppingCart
|
|||
|
{
|
|||
|
public string userName = string.Empty;
|
|||
|
public string userModie = string.Empty;
|
|||
|
public string userEmail = string.Empty;
|
|||
|
public List<ShoppingCartData> datas = new List<ShoppingCartData>();
|
|||
|
public string totalQuantity = "0";
|
|||
|
public string itemsSubTotal = "0";
|
|||
|
public string discount = "0";
|
|||
|
public string totalAmount = "0";
|
|||
|
|
|||
|
public void AddData(ShoppingCartData data)
|
|||
|
{
|
|||
|
datas.Add(data);
|
|||
|
UpdateDatas();
|
|||
|
}
|
|||
|
|
|||
|
public void RemoveData(ShoppingCartData data)
|
|||
|
{
|
|||
|
datas.Remove(data);
|
|||
|
UpdateDatas();
|
|||
|
}
|
|||
|
|
|||
|
public void Reset()
|
|||
|
{
|
|||
|
datas.Clear();
|
|||
|
UpdateDatas();
|
|||
|
}
|
|||
|
|
|||
|
public void UpdateDatas()
|
|||
|
{
|
|||
|
totalQuantity = datas.Count.ToString();
|
|||
|
itemsSubTotal = string.Empty;
|
|||
|
discount = string.Empty;
|
|||
|
totalAmount = string.Empty;
|
|||
|
|
|||
|
int i = 0, d = 0, t = 0;
|
|||
|
foreach (var shoppingCartData in datas)
|
|||
|
{
|
|||
|
i += shoppingCartData.bookAmount * shoppingCartData.count;
|
|||
|
d += shoppingCartData.bookDiscount * shoppingCartData.count;
|
|||
|
t += shoppingCartData.bookAmount * shoppingCartData.count;
|
|||
|
}
|
|||
|
|
|||
|
itemsSubTotal = i.ToString();
|
|||
|
discount = d.ToString();
|
|||
|
totalAmount = t.ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public class ShoppingCartData
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// name
|
|||
|
/// </summary>
|
|||
|
public string bookName;
|
|||
|
/// <summary>
|
|||
|
/// 价格
|
|||
|
/// </summary>
|
|||
|
public int bookAmount;
|
|||
|
/// <summary>
|
|||
|
/// 优惠
|
|||
|
/// </summary>
|
|||
|
public int bookDiscount;
|
|||
|
/// <summary>
|
|||
|
/// 数量
|
|||
|
/// </summary>
|
|||
|
public int count;
|
|||
|
public byte[] previewImage;
|
|||
|
public byte[] tifImage;
|
|||
|
}
|
|||
|
|
|||
|
public class ShoppingCartManager : ManagerBase<ShoppingCartManager>
|
|||
|
{
|
|||
|
private ShoppingCart shoppingCart = new();
|
|||
|
public ShoppingCart ShoppingCart => shoppingCart;
|
|||
|
|
|||
|
public void AddToCart(ShoppingCartData data)
|
|||
|
{
|
|||
|
shoppingCart.AddData(data);
|
|||
|
}
|
|||
|
|
|||
|
public void RemoveFromCart(ShoppingCartData data)
|
|||
|
{
|
|||
|
shoppingCart.RemoveData(data);
|
|||
|
}
|
|||
|
|
|||
|
public void ResetCart()
|
|||
|
{
|
|||
|
shoppingCart.Reset();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|