千家信息网

C#实现商城购物功能的代码怎么写

发表于:2024-11-28 作者:千家信息网编辑
千家信息网最后更新 2024年11月28日,这篇文章主要介绍"C#实现商城购物功能的代码怎么写",在日常操作中,相信很多人在C#实现商城购物功能的代码怎么写问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"C#实现商
千家信息网最后更新 2024年11月28日C#实现商城购物功能的代码怎么写

这篇文章主要介绍"C#实现商城购物功能的代码怎么写",在日常操作中,相信很多人在C#实现商城购物功能的代码怎么写问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"C#实现商城购物功能的代码怎么写"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

商品类

namespace ShoppingSystem{    /*     * 商品信息包括:商品名称、商品价格、商品型号、商品描述等     */    ///     /// 商品类    ///     class Goods    {        ///         /// 商品名称        ///         private string goodName;        ///         /// 商品价格        ///         private float goodPrice;        ///         /// 商品型号        ///         private string[] goodModel = new string[2];        ///         /// 商品类别        ///         private string goodType;        ///         /// 商品描述        ///         private string goodDescribe;        ///         /// 卖家        ///         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;            }        }        ///         /// 构造函数,对各个变量赋值并添加商品描述        ///         /// 商品名        /// 商品价格        /// 商品编号        /// 商品型号        /// 商品类别        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 + "|";        }    }}

商品总库

namespace ShoppingSystem{    class GoodsSql    {        ///         /// 商品总库        ///         private Goods[] good = new Goods[20];        public Goods[] Good        {            get            {                return good;            }            set            {                good = value;            }        }    }}

用户类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ShoppingSystem{    ///     /// 用户类    ///     class User    {        ///         /// 用户名        ///         private string username;        ///         /// 用户余额        ///         private float userBalance;        ///         /// 购物车        ///         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;            }        }        ///         /// 加入购物车        ///         /// 要加入的商品        /// 要买的数量        public void BuyGoods(Goods good, int goodsNum)        {                        cart.AddGoods(good, goodsNum);            //TODO        }        ///         /// 结算并清空购物车        ///         public void CheckoutCart()        {            cart.CheckoutCart();        }    }}

卖家类

using System;namespace ShoppingSystem{    ///     /// 卖家类    ///     class Seller    {        ///         /// 卖家姓名        ///         private string sellerName;        ///         /// 卖家余额        ///         private float sellerBalance;        ///         /// 卖家商品数组        ///         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; i++)            {                if (sellerGoods[i] == null)                {                    sellerGoods[i] = good;                    good.Seller = this;                    for (int j = 0; j < goods.Good.Length; j++)                    {                        if (goods.Good[j] == null)                        {                            goods.Good[j] = good;                            break;                        }                    }                    Console.WriteLine("添加商品成功!");                    break;                }                if (i == sellerGoods.Length - 1)                {                    Console.WriteLine("添加商品失败!已达到可上架商品的上限!");                    return;                }            }        }        ///         /// 更新商品信息        ///         /// 要更新的商品        /// 商品名称        /// 商品价格        /// 商品编号        /// 商品型号        /// 商品种类        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("更新商品信息成功!");                return;            }            Console.WriteLine("更新商品信息失败!");        }    }}

服务类

namespace ShoppingSystem{    class Service    {        private Goods[] goods = new Goods[20];        public Goods[] Goods        {            get            {                return goods;            }            set            {                goods = value;            }        }        ///         /// 按类型搜索商品        ///         ///         ///         public void Search(string goodType, GoodsSql goods)        {            this.goods = new Goods[20];            int count = 0;            for (int i = 0; i < goods.Good.Length; i++)            {                if (goods.Good[i] != null && goods.Good[i].GoodType.Equals(goodType))                {                    this.goods[count++] = goods.Good[i];                }            }        }    }}

购物车类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ShoppingSystem{    ///     /// 购物车类    ///     class ShoppingCart    {        ///         /// 购物条目数组        ///         private ShoppingItems[] items;        ///         /// 购物费用总计        ///         private float totalPrice = 0.00f;        ///         /// 购物车所属用户        ///         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;            }        }        ///         /// 添加商品到购物车        ///         /// 要加入的商品        /// 要买的数量        public void AddGoods(Goods good, int goodsNum)        {            //若购物车条目为空,实例化购物车条目数组            if (items == null)            {                items = new ShoppingItems[10];            }            //加入商品条目到购物条目数组            for (int i = 0; i < items.Length; i++)            {                if (items[i] == null)                {                    items[i] = new ShoppingItems();                    items[i].Good = good;                    items[i].GoodsNum = goodsNum;                    totalPrice += good.GoodPrice * goodsNum;                    Console.WriteLine($"已将{good.GoodDescribe}数量:{goodsNum},加入购物车");                    break;                }                if (i == items.Length - 1)                {                    Console.WriteLine("购物车已满!");                }            }        }                ///         /// 结算并清空购物车        ///         public void CheckoutCart()        {            //判断购物车是否为空            if (items == null)            {                Console.WriteLine("您的购物车中没有商品!");                return;            }            foreach (var item in items)            {                if (item != null)                {                    item.Good.Seller.SellerBalance += item.Good.GoodPrice * item.GoodsNum;                    Console.WriteLine($"商品名称:{item.Good.GoodName}");                }            }            Console.WriteLine($"{user.Username}已经清空了购物车,共计花费{totalPrice}元");            user.UserBalance -= totalPrice;            items = null;            //TODO        }        public void selectCart()        {            if (items == null)            {                Console.WriteLine("您的购物车是空的!");                return;            }            foreach (var item in items)            {                Console.WriteLine($"{item.Good.GoodDescribe}数量:{item.GoodsNum}");            }        }    }}

购物车条目类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ShoppingSystem{    ///     /// 购物车中的商品条目类    ///     class ShoppingItems    {        ///         /// 商品        ///         private Goods good;        ///         /// 要买的数量        ///         private int goodsNum;        public Goods Good        {            get            {                return good;            }            set            {                good = value;            }        }        public int GoodsNum        {            get            {                return goodsNum;            }            set            {                goodsNum = value;            }        }    }}

服务台

这里其实应该封装不少东西,我偷懒了,不搞了

using System;namespace ShoppingSystem{    ///     /// 软件使用类    ///     class SoftwareUsage    {        ///         /// 获取用户指令        ///         ///         public string Order()        {            Console.WriteLine("请先输入指令:");            Console.WriteLine("0-退出,1-搜索,2-购买,3-清空并结算购物车,4-查询购物车");            return Console.ReadLine();        }    }}

程序入口main函数:

using System;namespace ShoppingSystem{    class Program    {        static void Main(string[] args)        {            GoodsSql goodsSql = new GoodsSql();            Service service = new Service();            User user = new User("岳翔", 10000000.00f);            SoftwareUsage use = new SoftwareUsage();            Seller seller01 = new Seller("卖家1", 10000.00f);            Seller seller02 = new Seller("卖家2", 10000.00f);            Seller seller03 = new Seller("卖家3", 10000.00f);            Goods good01 = new Goods("编程之美(图书)", 120.00f, new string[]{ "质量", "普通版" }, "图书");            Goods good02 = new Goods("编程之美(图书)", 145.00f, new string[]{ "质量", "精装版" }, "图书");            Goods good03 = new Goods("三毛流浪记(图书)", 20.00f, new string[]{ "质量", "普通版" }, "图书");            Goods good04 = new Goods("三毛流浪记(图书)", 25.00f, new string[]{ "质量", "精装版" }, "图书");            Goods good05 = new Goods("iPhone 100(手机)", 6000.00f, new string[]{ "RAM", "64GB" }, "手机");            Goods good06 = new Goods("iPhone 100(手机)", 7000.00f, new string[]{ "RAM", "128GB" }, "手机");            Goods good07 = new Goods("iPhone 100(手机)", 9000.00f, new string[]{ "RAM", "512GB" }, "手机");            Goods good08 = new Goods("Nokia(手机)", 1000.00f, new string[]{ "型号", "E63" }, "手机");            Goods good09 = new Goods("Nokia(手机)", 2000.00f, new string[]{ "型号", "N95" }, "手机");            Goods good10 = new Goods("Nokia(手机)", 2300.00f, new string[]{ "型号", "N97" }, "手机");            Goods good11 = new Goods("Mac Book Pro(电脑)", 18000.00f, new string[]{ "配置", "低配版" }, "电脑");            Goods good12 = new Goods("Mac Book Pro(电脑)", 20000.00f, new string[]{ "配置", "中配版" }, "电脑");            Goods good13 = new Goods("Mac Book Pro(电脑)", 22000.00f, new string[]{ "配置", "⾼配版" }, "电脑");            seller01.AddGood(good01, goodsSql);            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("购物结束!");                        return;                    case "1":                        Console.WriteLine("请输入搜索关键词:");                        string goodType = Console.ReadLine();                        service.Search(goodType, goodsSql);                        Goods[] goods = service.Goods;                        Console.WriteLine($"当前买家{user.Username}正在搜索商品:{goodType}");                        Console.WriteLine("-----------------------");                        foreach (var item in goods)                        {                            if (item != null)                            {                                Console.WriteLine($"商品名:{item.GoodName},商品类型({item.GoodModel[0]})" +                                    $"{item.GoodModel[1]},{item.GoodPrice}元");                            }                        }                        Console.WriteLine("-----------------------");                        break;                    case "2":                        if (service.Goods[0] == null)                        {                            Console.WriteLine("请先搜索选购!");                            break;                        }                        Console.WriteLine("请先输入商品编号:");                        int goodId = Int32.Parse(Console.ReadLine());                        Console.WriteLine("请先输入商品数量:");                        int goodsNum = Int32.Parse(Console.ReadLine());                        user.BuyGoods(service.Goods[goodId - 1], goodsNum);                        Console.WriteLine("-----------------------");                        break;                    case "3":                        user.CheckoutCart();                        Console.WriteLine($"账户余额:{user.UserBalance}");                        break;                    case "4":                        user.Cart.selectCart();                        break;                    default:                        Console.WriteLine("您输入的指令不正确!");                        break;                }            }        }    }}

到此,关于"C#实现商城购物功能的代码怎么写"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0