using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test5
{
class Program
{
static void Main(string[] args)
{
Shopping myShopping = new Shopping();
myShopping.PrintList();
Console.ReadLine();
}
/// <summary>
/// 商品类
/// </summary>
public class Goods
{
/// <summary>
/// 商品名称
/// </summary>
private string name; public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// 商品价格
/// </summary>
private double price; public double Price
{
get { return price; }
set { price = value; }
}
}
/// <summary>
/// 购物类
/// </summary>
public class Shopping
{ private string list; public string List
{
get { return list; }
set { list = value; }
}
Goods[] goodsList; internal Goods[] GoodsList
{
get { return goodsList; }
set { goodsList = value; }
}
private void Inital()
{
this.GoodsList = new Goods[3];
this.GoodsList[0] = new Goods();
this.GoodsList[0].Name="农夫山泉";
this.GoodsList[0].Price=1.02;
this.GoodsList[1]=new Goods();
this.GoodsList[1].Name="今麦郎碗面";
this.GoodsList[1].Price=3.5;
this.GoodsList[2]=new Goods();
this.GoodsList[2].Name="双汇火腿肠";
this.GoodsList[2].Price=12.6;
}
private void ShowGoodsInfo()
{
Console.WriteLine("{0,-4}\t{1,-8}\t{2:0.00}","编号","商品名称","单价");
for (int i = 0; i < this.GoodsList.Length; i++)
{
Console.WriteLine("{0,-4}\t{1,-8}\t{2:0.00}",i+1,this.GoodsList[i].Name,this.GoodsList[i].Price);
}
}
public void PrintList()
{
this.Inital();
this.ShowGoodsInfo();
int a, b;
int count = 0;
string c = "";
do
{
Console.Write("输入商品编号:");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("输入购买数量:");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("输入e停止购物,输入其他任意继续购物:");
c = Console.ReadLine();
count++;
} while (!c.Equals("e"));
double sum = 0.0;
this.List = "\n\n-----------欢迎光临光辉超市--------------";
this.List = string.Format("{0}\n{1,-8}\t{2,-4:F2}\t{3,-4}\t{4,-4:F2}",this.List,"商品名称","单价","数量","小计");
for (int i = 0; i < this.GoodsList.Length; i++)
{
this.List = string.Format("{0}\n{1,-8}\t{2,-4:F2}\t{3,-4}\t{4,-4:C2}",this.List,this.GoodsList[i].Name,this.GoodsList[i].Price,b,this.GoodsList[i].Price*b);
sum += this.GoodsList[i].Price * b;
}
Console.WriteLine(this.List);
Console.WriteLine("\n\n总计:{0}",sum);
}
}
}
}
运行后会出现数量是默认的1.求修改后的代码!
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test5
{
class Program
{
static void Main(string[] args)
{
Shopping myShopping = new Shopping();
myShopping.PrintList();
Console.ReadLine();
}
/// <summary>
/// 商品类
/// </summary>
public class Goods
{
/// <summary>
/// 商品名称
/// </summary>
private string name; public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// 商品价格
/// </summary>
private double price; public double Price
{
get { return price; }
set { price = value; }
}
}
/// <summary>
/// 购物类
/// </summary>
public class Shopping
{ private string list; public string List
{
get { return list; }
set { list = value; }
}
Goods[] goodsList; internal Goods[] GoodsList
{
get { return goodsList; }
set { goodsList = value; }
}
private void Inital()
{
this.GoodsList = new Goods[3];
this.GoodsList[0] = new Goods();
this.GoodsList[0].Name="农夫山泉";
this.GoodsList[0].Price=1.02;
this.GoodsList[1]=new Goods();
this.GoodsList[1].Name="今麦郎碗面";
this.GoodsList[1].Price=3.5;
this.GoodsList[2]=new Goods();
this.GoodsList[2].Name="双汇火腿肠";
this.GoodsList[2].Price=12.6;
}
private void ShowGoodsInfo()
{
Console.WriteLine("{0,-4}\t{1,-8}\t{2:0.00}","编号","商品名称","单价");
for (int i = 0; i < this.GoodsList.Length; i++)
{
Console.WriteLine("{0,-4}\t{1,-8}\t{2:0.00}",i+1,this.GoodsList[i].Name,this.GoodsList[i].Price);
}
}
public void PrintList()
{
this.Inital();
this.ShowGoodsInfo();
int a, b;
int count = 0;
string c = "";
do
{
Console.Write("输入商品编号:");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("输入购买数量:");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("输入e停止购物,输入其他任意继续购物:");
c = Console.ReadLine();
count++;
} while (!c.Equals("e"));
double sum = 0.0;
this.List = "\n\n-----------欢迎光临光辉超市--------------";
this.List = string.Format("{0}\n{1,-8}\t{2,-4:F2}\t{3,-4}\t{4,-4:F2}",this.List,"商品名称","单价","数量","小计");
for (int i = 0; i < this.GoodsList.Length; i++)
{
this.List = string.Format("{0}\n{1,-8}\t{2,-4:F2}\t{3,-4}\t{4,-4:C2}",this.List,this.GoodsList[i].Name,this.GoodsList[i].Price,b,this.GoodsList[i].Price*b);
sum += this.GoodsList[i].Price * b;
}
Console.WriteLine(this.List);
Console.WriteLine("\n\n总计:{0}",sum);
}
}
}
}
运行后会出现数量是默认的1.求修改后的代码!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货