This article shares the specific code for C# to implement shopping function in malls for your reference. The specific content is as follows
Product category
namespace ShoppingSystem { /* * Product information includes: product name, product price, product model, product description, etc. */ /// <summary> /// Product category /// </summary> class Goods { /// <summary> /// Product Name /// </summary> private string goodName; /// <summary> /// Product price /// </summary> private float goodPrice; /// <summary> /// Product model /// </summary> private string[] goodModel = new string[2]; /// <summary> /// Product category /// </summary> private string goodType; /// <summary> /// Product Description /// </summary> private string goodDescribe; /// <summary> /// Seller /// </summary> private Seller seller; public Seller Seller { get { return seller; } set { seller = value; } } public string GoodName { get { return goodName; } set { goodName = value; } } public float GoodPrice { get { return goodPrice; } set { goodPrice = value; } } public string[] GoodModel { get { return goodModel; } set { goodModel = value; } } public string GoodType { get { return goodType; } set { goodType = value; } } public string GoodDescribe { get { return goodDescribe; } set { goodDescribe = value; } } /// <summary> /// Constructor, assign values to each variable and add product description /// </summary> /// <param name="goodName">Product name</param> /// <param name="goodPrice">Product Price</param> /// <param name="goodId">Product number</param> /// <param name="goodModel">Product model</param> /// <param name="goodType">Product Category</param> public Goods(string goodName, float goodPrice, string[] goodModel, string goodType) { = goodName; = goodPrice; = goodModel; = goodType; goodDescribe = goodName + goodModel[0] + "|" + goodModel[1] + "|" + goodPrice + "|"; } } }
Product Library
namespace ShoppingSystem { class GoodsSql { /// <summary> /// Total product library /// </summary> private Goods[] good = new Goods[20]; public Goods[] Good { get { return good; } set { good = value; } } } }
User class
using System; using ; using ; using ; using ; namespace ShoppingSystem { /// <summary> /// User class /// </summary> class User { /// <summary> /// username /// </summary> private string username; /// <summary> /// User balance /// </summary> private float userBalance; /// <summary> /// Shopping cart /// </summary> private ShoppingCart cart = new ShoppingCart(); public User(string username, float userBalance) { = username; = userBalance; = this; } public string Username { get { return username; } set { username = value; } } public float UserBalance { get { return userBalance; } set { userBalance = value; } } public ShoppingCart Cart { get { return cart; } set { cart = value; } } /// <summary> /// add to the cart /// </summary> /// <param name="good">Products to be added</param> /// <param name="goodsNum">Quantity to buy</param> public void BuyGoods(Goods good, int goodsNum) { (good, goodsNum); //TODO } /// <summary> /// Check and clear the shopping cart /// </summary> public void CheckoutCart() { (); } } }
Seller category
using System; namespace ShoppingSystem { /// <summary> /// Seller category /// </summary> class Seller { /// <summary> /// Seller's name /// </summary> private string sellerName; /// <summary> /// Seller's balance /// </summary> private float sellerBalance; /// <summary> /// Seller's product array /// </summary> private Goods[] sellerGoods = new Goods[5]; public Seller(string sellerName, float sellerBalance) { = sellerName; = sellerBalance; } public string SellerName { get { return sellerName; } set { sellerName = value; } } public float SellerBalance { get { return sellerBalance; } set { sellerBalance = value; } } public Goods[] SellerGoods { get { return sellerGoods; } set { sellerGoods = value; } } /// <summary> /// New products are on the shelves /// </summary> /// <param name="good"></param> public void AddGood(Goods good,GoodsSql goods) { for (int i = 0; i < ; i++) { if (sellerGoods[i] == null) { sellerGoods[i] = good; = this; for (int j = 0; j < ; j++) { if ([j] == null) { [j] = good; break; } } ("Add product successfully!"); break; } if (i == - 1) { ("Add product failed! The maximum number of items available for listing has been reached!"); return; } } } /// <summary> /// Update product information /// </summary> /// <param name="good">Products to be updated</param> /// <param name="goodName">Product name</param> /// <param name="goodPrice">Product Price</param> /// <param name="goodId">Product number</param> /// <param name="goodModel">Product model</param> /// <param name="goodType">Product Type</param> public void UpdateGoodInfo(Goods good, string goodName, float goodPrice, string[] goodModel, string goodType) { if (good != null) { = goodName; = goodModel; = goodType; = goodName + goodModel[0] + "|" + goodModel[1] + "|" + goodPrice + "|"; ("Updated product information successfully!"); return; } ("Update product information failed!"); } } }
Service category
namespace ShoppingSystem { class Service { private Goods[] goods = new Goods[20]; public Goods[] Goods { get { return goods; } set { goods = value; } } /// <summary> /// Search for products by type /// </summary> /// <param name="goodType"></param> /// <param name="goods"></param> public void Search(string goodType, GoodsSql goods) { = new Goods[20]; int count = 0; for (int i = 0; i < ; i++) { if ([i] != null && [i].(goodType)) { [count++] = [i]; } } } } }
Shopping cart category
using System; using ; using ; using ; using ; namespace ShoppingSystem { /// <summary> /// Shopping cart category /// </summary> class ShoppingCart { /// <summary> /// Array of shopping entries /// </summary> private ShoppingItems[] items; /// <summary> /// Total shopping fee /// </summary> private float totalPrice = 0.00f; /// <summary> /// Users to which the shopping cart belongs /// </summary> private User user; public ShoppingItems[] Items { get { return items; } set { items = value; } } public float TotalPrice { get { return totalPrice; } set { totalPrice = value; } } public User User { get { return user; } set { user = value; } } /// <summary> /// Add items to cart /// </summary> /// <param name="good">Products to be added</param> /// <param name="goodsNum">Quantity to buy</param> public void AddGoods(Goods good, int goodsNum) { //If the shopping cart entry is empty, instantiate the shopping cart entry array if (items == null) { items = new ShoppingItems[10]; } //Add product entries to the shopping entries array for (int i = 0; i < ; i++) { if (items[i] == null) { items[i] = new ShoppingItems(); items[i].Good = good; items[i].GoodsNum = goodsNum; totalPrice += * goodsNum; ($"Already{}quantity:{goodsNum},add to the cart"); break; } if (i == - 1) { ("The shopping cart is full!"); } } } /// <summary> /// Check and clear the shopping cart /// </summary> public void CheckoutCart() { //Judge whether the shopping cart is empty if (items == null) { ("There is no item in your shopping cart!"); return; } foreach (var item in items) { if (item != null) { += * ; ($"Product Name:{}"); } } ($"{}The shopping cart has been cleared,Total expenses{totalPrice}Yuan"); -= totalPrice; items = null; //TODO } public void selectCart() { if (items == null) { ("Your cart is empty!"); return; } foreach (var item in items) { ($"{}quantity:{}"); } } } }
Shopping cart entry category
using System; using ; using ; using ; using ; namespace ShoppingSystem { /// <summary> /// Product entry category in the shopping cart /// </summary> class ShoppingItems { /// <summary> /// Product /// </summary> private Goods good; /// <summary> /// The quantity to buy /// </summary> private int goodsNum; public Goods Good { get { return good; } set { good = value; } } public int GoodsNum { get { return goodsNum; } set { goodsNum = value; } } } }
desk
Actually, there should be a lot of things here. I'm lazy and don't do it anymore
using System; namespace ShoppingSystem { /// <summary> /// Software usage class /// </summary> class SoftwareUsage { /// <summary> /// Get user instructions /// </summary> /// <returns></returns> public string Order() { ("Please enter the command first:"); ("0-Exit, 1-Search, 2-Purchase, 3-Empty and settle the shopping cart, 4-Inquiry shopping cart"); return (); } } }
Program entry main function:
using System; namespace ShoppingSystem { class Program { static void Main(string[] args) { GoodsSql goodsSql = new GoodsSql(); Service service = new Service(); User user = new User("Yue Xiang", 10000000.00f); SoftwareUsage use = new SoftwareUsage(); Seller seller01 = new Seller("Seller 1", 10000.00f); Seller seller02 = new Seller("Seller 2", 10000.00f); Seller seller03 = new Seller("Seller 3", 10000.00f); Goods good01 = new Goods("The beauty of programming(books)", 120.00f, new string[]{ "quality", "Normal Edition" }, "books"); Goods good02 = new Goods("The beauty of programming(books)", 145.00f, new string[]{ "quality", "Hardcover" }, "books"); Goods good03 = new Goods("Sanmao Wandering(books)", 20.00f, new string[]{ "quality", "Normal Edition" }, "books"); Goods good04 = new Goods("Sanmao Wandering(books)", 25.00f, new string[]{ "quality", "Hardcover" }, "books"); Goods good05 = new Goods("iPhone 100(cell phone)", 6000.00f, new string[]{ "RAM", "64GB" }, "cell phone"); Goods good06 = new Goods("iPhone 100(cell phone)", 7000.00f, new string[]{ "RAM", "128GB" }, "cell phone"); Goods good07 = new Goods("iPhone 100(cell phone)", 9000.00f, new string[]{ "RAM", "512GB" }, "cell phone"); Goods good08 = new Goods("Nokia(cell phone)", 1000.00f, new string[]{ "model", "E63" }, "cell phone"); Goods good09 = new Goods("Nokia(cell phone)", 2000.00f, new string[]{ "model", "N95" }, "cell phone"); Goods good10 = new Goods("Nokia(cell phone)", 2300.00f, new string[]{ "model", "N97" }, "cell phone"); Goods good11 = new Goods("Mac Book Pro(computer)", 18000.00f, new string[]{ "Configuration", "Low version" }, "computer"); Goods good12 = new Goods("Mac Book Pro(computer)", 20000.00f, new string[]{ "Configuration", "Middle version" }, "computer"); Goods good13 = new Goods("Mac Book Pro(computer)", 22000.00f, new string[]{ "Configuration", "High-end version" }, "computer"); (good01, goodsSql); (good02, goodsSql); (good03, goodsSql); (good04, goodsSql); (good05, goodsSql); (good06, goodsSql); (good07, goodsSql); (good08, goodsSql); (good09, goodsSql); (good10, goodsSql); (good11, goodsSql); (good12, goodsSql); (good13, goodsSql); (); while (true) { string order = (); switch (order) { case "0": ("Shopping is over!"); return; case "1": ("Please enter the search keyword:"); string goodType = (); (goodType, goodsSql); Goods[] goods = ; ($"Current buyer{}Searching for products:{goodType}"); ("-----------------------"); foreach (var item in goods) { if (item != null) { ($"Product Name:{},Product Type({[0]})" + $"{[1]},{}Yuan"); } } ("-----------------------"); break; case "2": if ([0] == null) { ("Please search and purchase first!"); break; } ("Please enter the product number first:"); int goodId = (()); ("Please enter the quantity of the product first:"); int goodsNum = (()); ([goodId - 1], goodsNum); ("-----------------------"); break; case "3": (); ($"Account balance:{}"); break; case "4": (); break; default: ("The command you entered is incorrect!"); break; } } } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.