Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class MainClass
  5. {
  6. enum CTypeA {Classic=0, Silver=1, Gold=2, Platinum=3}
  7. enum BrandA {Apple=0, Asus=1, Acer=2, Samsung=3}
  8. enum ModelA {A1=0, A2=1, B1=2, B2=3, C1=4, C2=5}
  9. enum AttributeA {Hot=0, Cold=1, Mild=2}
  10.  
  11. public static void Main (string[] args)
  12. {
  13. // 一個可存放 10個Customer物件的 List(串列)
  14. List<Customer> Cust = new List<Customer>();
  15. // 一個可存放5個Computer物件的 List(串列)
  16. List<Computer> Comp = new List<Computer>();
  17. // 一個可存放5個Food物件的 List(串列)
  18. List<Food> Foob = new List<Food>();
  19. //應付金額輸出
  20. List<double> print4 = new List<double>();
  21. // 亂數
  22. Random w = new Random();
  23. // 產生10個Custome 物件的內容
  24. for(int i=0; i<10; i++)
  25. {
  26. Customer oneCust = new Customer();
  27. //SSN的值
  28. oneCust.SSN = SetSSN("C");
  29. //Cname的值
  30. oneCust.Cname = GetCust(i);
  31. //CType的值
  32. int b = w.Next(0,4);
  33. CTypeA bb = (CTypeA) b;
  34. oneCust.CType = bb.ToString();
  35. //Discount的值
  36. double inDiscount = w.Next(1, 10)*0.1;
  37. oneCust.SetDiscount(inDiscount);
  38. //丟到Cust裡
  39. Cust.Add(oneCust);
  40.  
  41. // 產生個5個Computer和Food物件的內容
  42. Computer oneComp = new Computer();
  43. Food oneFoob = new Food();
  44. //Pid值
  45. oneComp.Pid = "Comp0"+(i+1);
  46. oneFoob.Pid = "Food0"+(i-4);
  47. //Price值
  48. oneComp.Price = w.Next(10,99)*1000;
  49. oneFoob.Price = w.Next(10,99)*10;
  50. //Brand的值
  51. int c = w.Next(0,4);
  52. BrandA cc = (BrandA) c;
  53. oneComp.Brand = cc.ToString();
  54. //Model的值
  55. int d = w.Next(0,6);
  56. ModelA dd = (ModelA) d;
  57. oneComp.Model = dd.ToString();
  58. //Attribute的值
  59. int e = w.Next(0,3);
  60. AttributeA ee = (AttributeA) e;
  61. oneFoob.Attribute = ee.ToString();
  62. //Deadline的值
  63. oneFoob.Deadline=""+RandomDay();
  64. if(i<5)
  65. {
  66. Comp.Add(oneComp);
  67. print4.Add(oneCust.GetDiscount()*oneComp.Price);
  68. }
  69. else
  70. {
  71. Foob.Add(oneFoob);
  72. print4.Add(oneCust.GetDiscount()*oneFoob.Price);
  73. }
  74. }
  75. List<string> print1 = new List<string>();
  76. List<string> print2 = new List<string>();
  77. List<string> print3 = new List<string>();
  78. Console.WriteLine("所有 Customer 的屬性和購買資料: ..........\n");
  79. // 輸出10個Custome 物件
  80. foreach(var i in Cust)
  81. {
  82. print1.Add("客戶: "+i.SSN + "," + i.Cname + "," + i.CType + "," + i.GetDiscount());
  83. }
  84. foreach(var i in Comp)
  85. {
  86. print2.Add("資訊設備: "+i.Pid + "," + i.Price + "," + i.Brand + "," + i.Model);
  87. }
  88. foreach(var i in Foob)
  89. {
  90. print3.Add("食物: "+i.Pid + "," + i.Price + "," + i.Attribute + ","+ i.Deadline);
  91. }
  92. //輸出
  93. for(int i=0;i<10;i++)
  94. {
  95. Console.WriteLine(print1[i]);
  96. if(i<5)
  97. {
  98. Console.WriteLine(print2[i]);
  99. }
  100. else
  101. {
  102. int j=i-5;
  103. Console.WriteLine(print3[j]);
  104. }
  105. Console.WriteLine("應付金額:"+print4[i]+"\n");
  106. }
  107.  
  108. }
  109. //設SSN的值「C」開頭,再加上6位的數字。而且,這6個數字的奇位數字總和與偶位數字總和的差必須被10整除。
  110. public static string SetSSN(string c)
  111. {
  112. Random w = new Random();
  113. string math=""+w.Next(10000,100000);
  114. int sum=0;
  115. for(int i =0;i<math.Length;i++)
  116. {
  117. sum=i%2==0?sum+Int32.Parse(math.Substring(i,1)):sum-Int32.Parse(math.Substring(i,1));
  118. }
  119. sum=sum>0?sum:sum*-1;
  120. c=c+math+sum;
  121. return c;
  122. }
  123. //Customer物件的Cname,依序是Cust01、Cust02~
  124. public static string GetCust(int a)
  125. {
  126. string Beats = " ";
  127. if(a>=0 && a<=8){
  128. int A = a+1;
  129. Beats = "Cust0" + A.ToString();
  130. }
  131. else{
  132. Beats = "Cust10";
  133. }
  134. return Beats;
  135. }
  136. //Pid,依序是Comp01、Comp02、、、Comp05
  137. public static string GetPid(int a)
  138. {
  139. string Beats = " ";
  140. Beats = "Comp0" + a;
  141. return Beats;
  142. }
  143. //Deadline的值,隨機產生一個日期(從1995/1/1至今天)
  144. public static string RandomDay()
  145. {
  146. Random gen = new Random();
  147. DateTime start = Convert.ToDateTime("1995-1-1");
  148. int range = (DateTime.Today - start).Days;
  149. string myDateString =start.AddDays(gen.Next(range)).ToString("M/d/yyyy");
  150. return myDateString;
  151. }
  152. }
  153.  
  154. // 父類別(基底類別)宣告一個Customer類別
  155. class Customer
  156. {
  157. //其中SSN、Cname和CType ,它們是public,且資料型態皆是string。另外還有一個Discount欄位/屬性,它是private,且資料型態是double。
  158. public string SSN { get; set; }
  159. public string Cname { get; set; }
  160. public string CType { get; set; }
  161. private double Discount;
  162.  
  163. public void SetDiscount(double inDiscount)
  164. {
  165. Discount = inDiscount;
  166. }
  167. public double GetDiscount()
  168. {
  169. return Discount;
  170. }
  171.  
  172. }
  173. //宣告一個Product父類別
  174. class Product
  175. {
  176. public string Pid { get; set; }
  177. public int Price { get; set; }
  178. }
  179. //二個它的子類別Computer和Food
  180. class Computer : Product
  181. {
  182. //分別是Brand和Model。它們是public且資料型別皆是string
  183. public string Brand { get; set; }
  184. public string Model { get; set; }
  185. }
  186.  
  187. // 子類別(衍生類別)
  188. class Food: Product
  189. {
  190. //Attribute和Deadline,皆是public,且它們的資料型別都是string。
  191. public string Attribute{ get; set; }
  192. public string Deadline { get; set;}
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement