本文实例为大家分享了c#实现商城购物功能的具体代码,供大家参考,具体内容如下
商品类
namespace shoppingsystem{ /* * 商品信息包括:商品名称、商品价格、商品型号、商品描述等 */ /// <summary> /// 商品类 /// </summary> class goods { /// <summary> /// 商品名称 /// </summary> private string goodname; /// <summary> /// 商品价格 /// </summary> private float goodprice; /// <summary> /// 商品型号 /// </summary> private string[] goodmodel = new string[2]; /// <summary> /// 商品类别 /// </summary> private string goodtype; /// <summary> /// 商品描述 /// </summary> private string gooddescribe; /// <summary> /// 卖家 /// </summary> private ller ller; public ller ller { get { return ller; } t { ller = value; } } public string goodname { get { return goodname; } t { goodname = value; } } public float goodprice { get { return goodprice; } t { goodprice = value; } } public string[] goodmodel { get { return goodmodel; } t { goodmodel = value; } } public string goodtype { get { return goodtype; } t { goodtype = value; } } public string gooddescribe { get { return gooddescribe; } t { gooddescribe = value; } } /// <summary> /// 构造函数,对各个变量赋值并添加商品描述 /// </summary> /// <param name="goodname">商品名</param> /// <param name="goodprice">商品价格</param> /// <param name="goodid">商品编号</param> /// <param name="goodmodel">商品型号</param> /// <param name="goodtype">商品类别</param> 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 { /// <summary> /// 商品总库 /// </summary> private goods[] good = new goods[20]; public goods[] good { get { return good; } t { good = value; } } }}
用户类
using system;using system.collections.generic;using system.linq;using system.text;using system.threading.tasks;namespace shoppingsystem{ /// <summary> /// 用户类 /// </summary> class ur { /// <summary> /// 用户名 /// </summary> private string urname; /// <summary> /// 用户余额 /// </summary> private float urbalance; /// <summary> /// 购物车 /// </summary> private shoppingcart cart = new shoppingcart(); public ur(string urname, float urbalance) { this.urname = urname; this.urbalance = urbalance; cart.ur = this; } public string urname { get { return urname; } t { urname = value; } } public float urbalance { get { return urbalance; } t { urbalance = value; } } public shoppingcart cart { get { return cart; } t { cart = value; } } /// <summary> /// 加入购物车 /// </summary> /// <param name="good">要加入的商品</param> /// <param name="goodsnum">要买的数量</param> public void buygoods(goods good, int goodsnum) { cart.addgoods(good, goodsnum); //todo } /// <summary> /// 结算并清空购物车 /// </summary> public void checkoutcart() { cart.checkoutcart(); } }}
卖家类
using system;namespace shoppingsystem{ /// <summary> /// 卖家类 /// </summary> class ller { /// <summary> /// 卖家姓名 /// </summary> private string llername; /// <summary> /// 卖家余额 /// </summary> private float llerbalance; /// <summary> /// 奥运会女排规则卖家商品数组 /// </summary> private goods[] llergoods = new goods[5]; public ller(string llername, float llerbalance) { this.llername = llername; this.llerbalance = llerbalance; } public string llername { get { return llername; } t { llername = value; } } public float llerbalance { get { return llerbalance; } t { llerbalance = value; } } public goods[] llergoods { get { return llergoods; } t { llergoods = value; } } /// <summary> /// 上架新商品 /// </summary> /// <param name="good"></param> public void addgood(goods good,goodssql goods) { for (int i = 0; i < llergoods.length; i++) { if (llergoods[i] == null) { llergoods[i] = good; good.ller = 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 == llergoods.length - 1) { console.writeline("添加商品失败!已达到可上架商品的上限!"); return; } } } /// <summary> /// 更新商品信息 /// </summary> /// <param name="good">要更新的商品</param> /// <param name="goodname">商品名称</param> /// <param name="goodprice">商品价格</param> /// <param name="goodid">商品编号</param> /// <param name="goodmodel">商品型号</param> /// <param name="goodtype">商品种类</param> public void updategoodinfo(goods good, string goo哈尔滨远东理工学院dname, 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 rvice { private goods[] goods = new goods[20]; public goods[] goods { get { return goods; } t { goods = value; } } /// <summary> /// 按类型搜索商品 /// </summary> /// <param name="goodtype"></param> /// <param name="goods"></param> public void arch(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{ /// <summary> /// 购物车类 /// </summary> class shoppingcart { /// <summary> /// 购物条目数组 /// </summary> private shoppingitems[] items; /// <summary> /// 购物费用总计 /// </summary> private float totalprice = 0.00f; /// <summary> /// 购物车所属用户 /// </summary> private ur ur; public shoppingitems[] items { get { return items; } t { items = value; } } public float totalprice { get { return totalprice; } t { totalprice = value; } } public ur ur { get { return ur; } t { ur = value; } } /// <summary> /// 添加商品到购物车 /// </summary> /// <param name="good">要加入的商品</param> /// <param name="goodsnum">要买的数量</param> 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("购物车已满!"); } } } /// <summary> /// 结算并清空购物车 /// </summary> public void checkoutcart() { //判断购物车是否为空 if (items == null) { console.writeline("您的购物车中没有商品!"); return; } foreach (var item in items) { if (item != null) { item.good.ller.llerbalance += item.good.goodprice * item.goodsnum; console.writeline($"商品名称:{item.good.goodname}"); } } console.writeline($"{ur.urname}已经清空了购物车,共计花费{totalprice}元"); ur.urbalance -= totalprice; items = null; //todo } public void lectcart() { 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{ /// <summary> /// 购物车中的商品条目类 /// </summary> class shoppingitems { /// <summary> /// 商品 /// </summary> private goods good; /// <summary> /// 要买的数量 /// </summary> private int goodsnum; public goods good { get { return good; } t { good = value; } } public int goodsnum { get { return goodsnum; } t { goodsnum = value; } } }}
服务台
这里其实应该封装不少东西,我偷懒了,不搞了
using system;namespace shoppingsystem{ /// <summary> /// 软件使用类 /// </summary> class softwareusage { /// <summary> /// 获取用户指令 /// </summary> /// <returns></returns> 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(); rvice rvice = new rvice(); ur ur = new ur("岳翔", 10000000.00f); softwareusage u = new softwareusage(); ller ller01 = new ller("卖家1", 10000.00f); ller ller02 = new ller("卖家2", 10000.00f); ller ller03 = new ller("卖家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[]{ "配置", "⾼配版" }, "电脑"); ller01.addgood(good01, goodssql); ller01.addgood(good02, goodssql); ller01.addgood(good03, goodssql); l快乐麦肯ler01.addgood(good04, goodssql); ller01.addgood(good05, goodssql); ller02.addgood(good06, goodssql); ller02.addgood(good07, goodssql); ller02.addgood(good08, goodssql); ller02.addgood(good09, goodssql); ller02.addgood(good10, goodssql); ller03.addgood(good11, goodssql); ller03.addgood(good12, goodssql); ller03.addgood(good13, goodssql); console.clear(); while (true) { string order = u.order(); switch (order) { ca "0": console.writeline("购物结束!"); return; ca "1": console.writeline("请输入搜索关键词:"); string goodtype = console.readline(); rvice.arch(goodtype, goodssql); goods[] goods = rvice.goods; console.writeline($"当前买家{ur.urname}正在搜索商品:{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; ca "2": if (rvice.goods[0] == null) { console.writeline("请先搜索选购!"); break; } console.writeline("请先输入商品编号:"); int goodid = int32.par(console.readline()); console.writeline("请先输入商品数量:"); int goodsnum = int32.par(console.readline()); ur.buygoods(rvice.goods[goodid - 1], goodsnum); console.writeline("-----------------------"); break; ca "3": ur.checkoutcart(); console.writeline($"账户余额:{ur.urbalance}"); break; ca "4": ur.cart.lectcart(); break; default: console.writeline("您输入的指令不正确!"); break; } } } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 23:36:02,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/bc18bc4b6edfa8f52908482e110a7ead.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#面向对象之模拟实现商城购物功能.doc
本文 PDF 下载地址:C#面向对象之模拟实现商城购物功能.pdf
留言与评论(共有 0 条评论) |