對象數組的使用_第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

提交評論