C#控制檯版超市購物系統
阿新 • • 發佈:2019-02-13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace shop
{
class Product
{
private int num;
public int Num
{
get { return num; }
set { num = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private double price;
public double Price
{
get { return price; }
set { price = value; }
}
//顯示自身資訊
public virtual void Show()
{
Console.WriteLine("編號:{0}\t名稱:{1}\t價格{2}",num,name,price);
}
}
class Apple : Product
{
private string form="山西";
public string Form
{
get { return form; }
set { form = value; }
}
private string brand="紅富士";
public string Brand
{
get { return brand; }
set { brand = value; }
}
public Apple()
{
this.Num = 1;
this.Name = "蘋果";
this.Price = 5;
}
public override void Show()
{
Console.WriteLine("編號:{0}\t名稱:{1}\t價格:{2}\t品牌:{3}\t產地:{4}",Num,Name,Price,brand,form);
}
}
class Noodle : Product
{
private string brand = "康師傅";
public string Brand
{
get { return brand; }
set { brand = value; }
}
string text = "牛肉味";
public string Text
{
get { return text; }
set { text = value; }
}
public Noodle()
{
Num = 2;
Name = "泡麵";
Price = 3;
}
public override void Show()
{
Console.WriteLine("編號:{0}\t名稱:{1}\t價格:{2}\t品牌:{3}\t口味:{4}",Num,Name,Price,brand,text);
}
}
class Banana : Product
{
string type = "霸王蕉";
public string Type
{
get { return type; }
set { type = value; }
}
string from = "香港";
public string From
{
get { return from; }
set { from = value; }
}
public Banana()
{
Num = 3;
Name = "香蕉";
Price = 4;
}
public override void Show()
{
Console.WriteLine("編號:{0}\t名稱:{1}\t價格:{2}\t種類:{3}\t產地:{4}",Num,Name,Price,Type,From);
}
}
class Program
{
static void Main(string[] args)
{
List<Product> shopList = new List<Product>();
Console.WriteLine("**********************坑爹超市***********************");
Apple a = new Apple();
a.Show();
Noodle n = new Noodle();
n.Show();
Banana b = new Banana();
b.Show();
Console.WriteLine("*****************************************************");
while (true)
{
Console.WriteLine("請輸入商品編號:(輸入是0結束購買)");
int th= int.Parse(Console.ReadLine());
if (th==0)
{
break;
}
else if (th==1)
{
shopList.Add(a);
}
else if (th==2)
{
shopList.Add(n);
}
else if (th==3)
{
shopList.Add(b);
}
}
Console.WriteLine("*****************歡迎光臨坑爹超市********************");
Console.WriteLine("坑爹超市清單不坑爹,清單如下:");
double sum=0;
for (int i = 0; i < shopList.Count; i++)
{
sum+=shopList[i].Price;
}
foreach (Product item in shopList)
{
item.Show();
}
Console.WriteLine("總價:{0}",sum);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace shop
{
class Product
{
private int num;
public int Num
{
get { return num; }
set { num = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private double price;
public double Price
{
get { return price; }
set { price = value; }
}
//顯示自身資訊
public virtual void Show()
{
Console.WriteLine("編號:{0}\t名稱:{1}\t價格{2}",num,name,price);
}
}
class Apple : Product
{
private string form="山西";
public string Form
{
get { return form; }
set { form = value; }
}
private string brand="紅富士";
public string Brand
{
get { return brand; }
set { brand = value; }
}
public Apple()
{
this.Num = 1;
this.Name = "蘋果";
this.Price = 5;
}
public override void Show()
{
Console.WriteLine("編號:{0}\t名稱:{1}\t價格:{2}\t品牌:{3}\t產地:{4}",Num,Name,Price,brand,form);
}
}
class Noodle : Product
{
private string brand = "康師傅";
public string Brand
{
get { return brand; }
set { brand = value; }
}
string text = "牛肉味";
public string Text
{
get { return text; }
set { text = value; }
}
public Noodle()
{
Num = 2;
Name = "泡麵";
Price = 3;
}
public override void Show()
{
Console.WriteLine("編號:{0}\t名稱:{1}\t價格:{2}\t品牌:{3}\t口味:{4}",Num,Name,Price,brand,text);
}
}
class Banana : Product
{
string type = "霸王蕉";
public string Type
{
get { return type; }
set { type = value; }
}
string from = "香港";
public string From
{
get { return from; }
set { from = value; }
}
public Banana()
{
Num = 3;
Name = "香蕉";
Price = 4;
}
public override void Show()
{
Console.WriteLine("編號:{0}\t名稱:{1}\t價格:{2}\t種類:{3}\t產地:{4}",Num,Name,Price,Type,From);
}
}
class Program
{
static void Main(string[] args)
{
List<Product> shopList = new List<Product>();
Console.WriteLine("**********************坑爹超市***********************");
Apple a = new Apple();
a.Show();
Noodle n = new Noodle();
n.Show();
Banana b = new Banana();
b.Show();
Console.WriteLine("*****************************************************");
while (true)
{
Console.WriteLine("請輸入商品編號:(輸入是0結束購買)");
int th= int.Parse(Console.ReadLine());
if (th==0)
{
break;
}
else if (th==1)
{
shopList.Add(a);
}
else if (th==2)
{
shopList.Add(n);
}
else if (th==3)
{
shopList.Add(b);
}
}
Console.WriteLine("*****************歡迎光臨坑爹超市********************");
Console.WriteLine("坑爹超市清單不坑爹,清單如下:");
double sum=0;
for (int i = 0; i < shopList.Count; i++)
{
sum+=shopList[i].Price;
}
foreach (Product item in shopList)
{
item.Show();
}
Console.WriteLine("總價:{0}",sum);
}
}
}