Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code of C# to realize the shopping function of mall?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to write the code for realizing the shopping function of shopping mall in C#". In the daily operation, I believe that many people have doubts about how to write the code for realizing the shopping function of shopping mall in C#. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation method. I hope it will be helpful to answer the doubt that "how to write the code for realizing shopping in C#". Next, please follow the editor to study!

Commodity category

Namespace ShoppingSystem {/ * * Commodity information includes: commodity name, commodity price, commodity model, commodity description, etc. * / class Goods {/ private string goodName / private float goodPrice; / Commodity Model / private string [] goodModel = new string [2]; / Commodity category / private string goodType / Product description / private string goodDescribe; / seller / 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 } / constructor Assign values to each variable and add commodity description / / commodity price / / commodity number / / commodity model / commodity category public Goods (string goodName, float goodPrice, string [] goodModel, string goodType) {this.goodName = goodName This.goodPrice = goodPrice; this.goodModel = goodModel; this.goodType = goodType; goodDescribe = goodName + goodModel [0] + "|" + goodModel [1] + "|" + goodPrice + "|";}

General storehouse of goods

Namespace ShoppingSystem {class GoodsSql {/ General Library of goods / private Goods [] good = new Goods [20]; public Goods [] Good {get {return good;} set {good = value }

User class

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ShoppingSystem {/ user class / class User {/ user name / private string username; / user balance / private float userBalance / Shopping cart / private ShoppingCart cart = new ShoppingCart (); public User (string username, float userBalance) {this.username = username; this.userBalance = userBalance; cart.User = 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 }} / add shopping cart / items to be added / quantity to be bought public void BuyGoods (Goods good, int goodsNum) {cart.AddGoods (good, goodsNum) / / TODO} / settle and empty the shopping cart / public void CheckoutCart () {cart.CheckoutCart ();}

Seller category

Using System;namespace ShoppingSystem {/ seller class / class Seller {/ seller name / private string sellerName; / seller balance / private float sellerBalance / seller goods array / private Goods [] sellerGoods = new Goods [5]; public Seller (string sellerName, float sellerBalance) {this.sellerName = sellerName; this.sellerBalance = 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 } / public void AddGood (Goods good,GoodsSql goods) {for (int I = 0; I < sellerGoods.Length) If (sellerGoods [I] = = null) {sellerGoods [I] = good; good.Seller = this; for (int j = 0; j < goods.Good.Length) If (goods.Good [j] = = null) {goods.Good [j] = good; break }} Console.WriteLine ("add goods successfully!") ; break;} if (I = = sellerGoods.Length-1) {Console.WriteLine ("failed to add goods! the limit of products that can be put on the shelf has been reached!") ; return } / Update Commodity Information / Commodity to be updated / / Commodity name / Commodity Price / Commodity number / Commodity Model / / Type of goods public void UpdateGoodInfo (Goods good String goodName, float goodPrice, string [] goodModel, string goodType) {if (good! = null) {good.GoodName = goodName Good.GoodModel = goodModel; good.GoodType = goodType; good.GoodDescribe = goodName + goodModel [0] + "|" + goodModel [1] + "|" + goodPrice + "|"; Console.WriteLine ("Product information updated successfully!") ; return;} Console.WriteLine ("failed to update product information!") ;}

Service class

Namespace ShoppingSystem {class Service {private Goods [] goods = new Goods [20]; public Goods [] Goods {get {return goods;} set {goods = value }} / search goods by type / public void Search (string goodType, GoodsSql goods) {this.goods = new Goods [20]; int count = 0; for (int I = 0; I < goods.Good.Length) ) {if (goods.Good [I]! = null & & goods.Good.GoodType.Equals (goodType)) {this.goods [count++] = goods.Good [I];}

Shopping cart

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ShoppingSystem {/ shopping cart class / class ShoppingCart {/ shopping items array / private ShoppingItems [] items / the total cost of shopping / private float totalPrice = 0.00f; / the user of the shopping cart / 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 }} / add goods to the shopping cart / items to be added / quantity to be bought public void AddGoods (Goods good, int goodsNum) {/ / if the shopping cart entry is empty Instantiate an array of shopping cart items if (items = = null) {items = new ShoppingItems [10] } / / add items to the shopping items array for (int I = 0; I < items.Length; iitems +) {if (items [I] = = null) {items [I] = new ShoppingItems (); items [I] .Good = good Items [I] .GoodsNum = goodsNum; totalPrice + = good.GoodPrice * goodsNum; Console.WriteLine ($"quantity of {good.GoodDescribe}: {goodsNum}, add to shopping cart"); break } if (I = = items.Length-1) {Console.WriteLine ("shopping cart is full!") } / settle and empty the shopping cart / public void CheckoutCart () {/ / determine whether the shopping cart is empty if (items = = null) {Console.WriteLine ("there are no items in your shopping cart!") ; return;} foreach (var item in items) {if (item! = null) {item.Good.Seller.SellerBalance + = item.Good.GoodPrice * item.GoodsNum; Console.WriteLine ($"Trade name: {item.Good.GoodName}") }} Console.WriteLine ($"{user.Username} has emptied the shopping cart at a total cost of {totalPrice} yuan"); user.UserBalance-= totalPrice; items = null / / TODO} public void selectCart () {if (items = = null) {Console.WriteLine ("your shopping cart is empty!") ; return;} foreach (var item in items) {Console.WriteLine ($"{item.Good.GoodDescribe} quantity: {item.GoodsNum}");}

Shopping cart item class

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ShoppingSystem {/ items in the shopping cart / class ShoppingItems {/ goods / private Goods good / / the quantity to be bought / private int goodsNum; public Goods Good {get {return good;} set {good = value }} public int GoodsNum {get {return goodsNum;} set {goodsNum = value;}

Service desk

In fact, there should be a lot of things encapsulated here. I've been lazy and quit.

Using System;namespace ShoppingSystem {/ Software usage class / class SoftwareUsage {/ get the user instruction / public string Order () {Console.WriteLine ("Please enter the instruction:") Console.WriteLine ("0-exit, 1-search, 2-Buy, 3-empty and Clearing Shopping cart, 4-query Shopping cart"); return Console.ReadLine ();}

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 ("Beauty of programming (Book)", 120.00f, new string [] {"quality", "ordinary Edition"}, "Book") Goods good02 = new Goods ("Beauty of programming (Book)", 145.00f, new string [] {"quality", "hardcover"}, "Book"); Goods good03 = new Goods ("Sanmao wandering (Book)", 20.00f, new string [] {"quality", "ordinary Edition"}, "Book") Goods good04 = new Goods (Sanmao vagrancy (Book), 25.00f, new string [] {"quality", "hardcover"}, "Book"); Goods good05 = new Goods ("iPhone 100 (Mobile phone)", 6000.00f, new string [] {"RAM", "64GB"}, "Mobile phone") Goods good06 = new Goods ("iPhone 100 (Mobile phone)", 7000.00f, new string [] {"RAM", "128GB"}, "Mobile"); Goods good07 = new Goods ("iPhone 100 (Mobile phone)", 9000.00f, new string [] {"RAM", "512GB"}, "Mobile") Goods good08 = new Goods ("Nokia", 1000.00f, new string [] {"Model", "E63"}, "Mobile"); Goods good09 = new Goods ("Nokia", 2000.00f, new string [] {"Model", "N95"}, "Mobile") Goods good10 = new Goods ("Nokia", 2300.00f, new string [] {"Model", "N97"}, "Mobile"); Goods good11 = new Goods ("Mac Book Pro (computer)", 18000.00f, new string [] {"configuration", "low configuration"}, "computer") Goods good12 = new Goods ("Mac Book Pro (computer)", 20000.00f, new string [] {"configuration", "medium version"}, "computer"); Goods good13 = new Goods ("Mac Book Pro (computer)", 22000.00f, new string [] {"configuration", "matching version"}, "computer"); seller01.AddGood (good01, computer) Seller01.AddGood (good02, goodsSql); seller01.AddGood (good03, goodsSql); seller01.AddGood (good04, goodsSql); seller01.AddGood (good05, goodsSql); seller02.AddGood (good06, goodsSql); seller02.AddGood (good07, goodsSql); seller02.AddGood (good08, goodsSql); seller02.AddGood (good09, goodsSql) Seller02.AddGood (good10, goodsSql); seller03.AddGood (good11, goodsSql); seller03.AddGood (good12, goodsSql); seller03.AddGood (good13, goodsSql); Console.Clear (); while (true) {string order = use.Order () Switch (order) {case "0": Console.WriteLine ("Shopping over!") ; return; case "1": Console.WriteLine ("Please enter search keywords:"); string goodType = Console.ReadLine (); service.Search (goodType, goodsSql); Goods [] goods = service.Goods Console.WriteLine ($"current buyer {user.Username} is searching for goods: {goodType}"); Console.WriteLine ("- -") Foreach (var item in goods) {if (item! = null) {Console.WriteLine ($"trade name: {item.GoodName}) Commodity type ({item.GoodModel [0]}) "+ $" {item.GoodModel [1]}, {item.GoodPrice} yuan ") }} Console.WriteLine ("- -"); break Case "2": if (service.Goods [0] = = null) {Console.WriteLine ("Please search for options first!") ; break;} Console.WriteLine ("Please enter the item number:"); int goodId = Int32.Parse (Console.ReadLine ()); Console.WriteLine ("Please enter the quantity of goods first:") Int goodsNum = Int32.Parse (Console.ReadLine ()); user.BuyGoods (service.Goods [goodID-1], goodsNum); Console.WriteLine ("- -"); break Case "3": user.CheckoutCart (); Console.WriteLine ($"account balance: {user.UserBalance}"); break; case "4": user.Cart.selectCart (); break Default: Console.WriteLine ("the instruction you entered is incorrect!") ; break;}} at this point, the study on "how to write the code for C# to realize the shopping function of the mall" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report