Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class MainClass
- {
- enum CTypeA {Classic=0, Silver=1, Gold=2, Platinum=3}
- enum BrandA {Apple=0, Asus=1, Acer=2, Samsung=3}
- enum ModelA {A1=0, A2=1, B1=2, B2=3, C1=4, C2=5}
- enum AttributeA {Hot=0, Cold=1, Mild=2}
- public static void Main (string[] args)
- {
- // 一個可存放 10個Customer物件的 List(串列)
- List<Customer> Cust = new List<Customer>();
- // 一個可存放5個Computer物件的 List(串列)
- List<Computer> Comp = new List<Computer>();
- // 一個可存放5個Food物件的 List(串列)
- List<Food> Foob = new List<Food>();
- //應付金額輸出
- List<double> print4 = new List<double>();
- // 亂數
- Random w = new Random();
- // 產生10個Custome 物件的內容
- for(int i=0; i<10; i++)
- {
- Customer oneCust = new Customer();
- //SSN的值
- oneCust.SSN = SetSSN("C");
- //Cname的值
- oneCust.Cname = GetCust(i);
- //CType的值
- int b = w.Next(0,4);
- CTypeA bb = (CTypeA) b;
- oneCust.CType = bb.ToString();
- //Discount的值
- double inDiscount = w.Next(1, 10)*0.1;
- oneCust.SetDiscount(inDiscount);
- //丟到Cust裡
- Cust.Add(oneCust);
- // 產生個5個Computer和Food物件的內容
- Computer oneComp = new Computer();
- Food oneFoob = new Food();
- //Pid值
- oneComp.Pid = "Comp0"+(i+1);
- oneFoob.Pid = "Food0"+(i-4);
- //Price值
- oneComp.Price = w.Next(10,99)*1000;
- oneFoob.Price = w.Next(10,99)*10;
- //Brand的值
- int c = w.Next(0,4);
- BrandA cc = (BrandA) c;
- oneComp.Brand = cc.ToString();
- //Model的值
- int d = w.Next(0,6);
- ModelA dd = (ModelA) d;
- oneComp.Model = dd.ToString();
- //Attribute的值
- int e = w.Next(0,3);
- AttributeA ee = (AttributeA) e;
- oneFoob.Attribute = ee.ToString();
- //Deadline的值
- oneFoob.Deadline=""+RandomDay();
- if(i<5)
- {
- Comp.Add(oneComp);
- print4.Add(oneCust.GetDiscount()*oneComp.Price);
- }
- else
- {
- Foob.Add(oneFoob);
- print4.Add(oneCust.GetDiscount()*oneFoob.Price);
- }
- }
- List<string> print1 = new List<string>();
- List<string> print2 = new List<string>();
- List<string> print3 = new List<string>();
- Console.WriteLine("所有 Customer 的屬性和購買資料: ..........\n");
- // 輸出10個Custome 物件
- foreach(var i in Cust)
- {
- print1.Add("客戶: "+i.SSN + "," + i.Cname + "," + i.CType + "," + i.GetDiscount());
- }
- foreach(var i in Comp)
- {
- print2.Add("資訊設備: "+i.Pid + "," + i.Price + "," + i.Brand + "," + i.Model);
- }
- foreach(var i in Foob)
- {
- print3.Add("食物: "+i.Pid + "," + i.Price + "," + i.Attribute + ","+ i.Deadline);
- }
- //輸出
- for(int i=0;i<10;i++)
- {
- Console.WriteLine(print1[i]);
- if(i<5)
- {
- Console.WriteLine(print2[i]);
- }
- else
- {
- int j=i-5;
- Console.WriteLine(print3[j]);
- }
- Console.WriteLine("應付金額:"+print4[i]+"\n");
- }
- }
- //設SSN的值「C」開頭,再加上6位的數字。而且,這6個數字的奇位數字總和與偶位數字總和的差必須被10整除。
- public static string SetSSN(string c)
- {
- Random w = new Random();
- string math=""+w.Next(10000,100000);
- int sum=0;
- for(int i =0;i<math.Length;i++)
- {
- sum=i%2==0?sum+Int32.Parse(math.Substring(i,1)):sum-Int32.Parse(math.Substring(i,1));
- }
- sum=sum>0?sum:sum*-1;
- c=c+math+sum;
- return c;
- }
- //Customer物件的Cname,依序是Cust01、Cust02~
- public static string GetCust(int a)
- {
- string Beats = " ";
- if(a>=0 && a<=8){
- int A = a+1;
- Beats = "Cust0" + A.ToString();
- }
- else{
- Beats = "Cust10";
- }
- return Beats;
- }
- //Pid,依序是Comp01、Comp02、、、Comp05
- public static string GetPid(int a)
- {
- string Beats = " ";
- Beats = "Comp0" + a;
- return Beats;
- }
- //Deadline的值,隨機產生一個日期(從1995/1/1至今天)
- public static string RandomDay()
- {
- Random gen = new Random();
- DateTime start = Convert.ToDateTime("1995-1-1");
- int range = (DateTime.Today - start).Days;
- string myDateString =start.AddDays(gen.Next(range)).ToString("M/d/yyyy");
- return myDateString;
- }
- }
- // 父類別(基底類別)宣告一個Customer類別
- class Customer
- {
- //其中SSN、Cname和CType ,它們是public,且資料型態皆是string。另外還有一個Discount欄位/屬性,它是private,且資料型態是double。
- public string SSN { get; set; }
- public string Cname { get; set; }
- public string CType { get; set; }
- private double Discount;
- public void SetDiscount(double inDiscount)
- {
- Discount = inDiscount;
- }
- public double GetDiscount()
- {
- return Discount;
- }
- }
- //宣告一個Product父類別
- class Product
- {
- public string Pid { get; set; }
- public int Price { get; set; }
- }
- //二個它的子類別Computer和Food
- class Computer : Product
- {
- //分別是Brand和Model。它們是public且資料型別皆是string
- public string Brand { get; set; }
- public string Model { get; set; }
- }
- // 子類別(衍生類別)
- class Food: Product
- {
- //Attribute和Deadline,皆是public,且它們的資料型別都是string。
- public string Attribute{ get; set; }
- public string Deadline { get; set;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement