对象数组的使用_第1页
对象数组的使用_第2页
对象数组的使用_第3页
对象数组的使用_第4页
对象数组的使用_第5页
已阅读5页,还剩1页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 对象数组的使用 题目要求:实现超市购物结算并输出购物小票功能。首先显示所有商品信息,然后输入购物的商品编号和数量进行购物结算,最后输出购物小票。程序运行结果如图4.12所示。要求如下:商品单价显示为两位小数,商品的小计和总计显示为货币形式,保留两位小数购物小票的四列均为左对齐,商品名称列宽度为8,后三列宽度为4参考代码:商品类: / <summary> / 商品类 / </summary> class Goods int _id; / <summary> / 商品编号 / </summary> public int Id get return

2、_id; set _id = value; string _name; / <summary> / 商品名称 / </summary> public string Name get return _name; set _name = value; double _price; / <summary> / 商品价格 / </summary> public double Price get return _price; set _price = value; 购物类: / <summary> / 购物类 / </summary>

3、; class Shopping string _list; Goods goodsList; / <summary> / 初始化商品信息 / </summary> private void Initial() this.goodsList = new Goods3; this.goodsList0 = new Goods(); this.goodsList0.Name = "农夫山泉" this.goodsList0.Price = 1.025; this.goodsList1 = new Goods(); this.goodsList1.Name

4、 = "今麦郎碗面" this.goodsList1.Price = 3.5; this.goodsList2 = new Goods(); this.goodsList2.Name = "双汇火腿肠" this.goodsList2.Price = 12.6; / <summary> / 显示商品信息 / </summary> private void ShowGoodsInfo() Console.WriteLine("0,-4t1,-8t2:0.00","编号","商品名称&

5、quot;,"单价"); for (int i = 0; i < this.goodsList.Length; i+) Console.WriteLine("0,-4t1,-8t2:0.00", i + 1, this.goodsListi.Name, this.goodsListi.Price); / <summary> / 打印购物小票 / </summary> public void PrintList() this.Initial(); / 初始化商品信息 this.ShowGoodsInfo(); / 显示商品信息

6、 double sum = 0; / 购物金额 this._list = "nn-欢迎光临光辉超市-" this._list = string.Format("0n1,-8t2,-4:F2t3,-4t4,-4:F2", this._list, "商品名称", "单价", "数量", "小计"); do Console.Write("n输入商品编号:"); int num = int.Parse(Console.ReadLine(); if (num >

7、; 3 | num < 1) Console.WriteLine("请重新选择商品编号!"); continue; Console.Write("输入购买数量:"); int amount = int.Parse(Console.ReadLine(); / 添加购物信息到购物小票 this._list = string.Format("0n1,-8t2,-4:F2t3,-4t4,-4:C2", this._list, this.goodsListnum - 1.Name, this.goodsListnum - 1.Price, amount, this.goodsListnum - 1.Price * amount); / 计算购物总额 sum += this.goodsListnum - 1.Price * amount; Console.Write("输入e停止购物,输入其他任意键继续购物:"); if (Console.ReadLine() = "e") break; while (true); / 添加购物总额到购物小票 this._list = string.Format(&qu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论