Advertisement
Guest User

Goods.cs

a guest
Feb 20th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Text;
  5. using Newtonsoft.Json;
  6.  
  7.  
  8. namespace Task2
  9. {
  10. /// <summary>
  11. /// abstract class to describe goods properties
  12. /// </summary>
  13. abstract class Goods
  14. {
  15. public Goods(string name, double costs)
  16. {
  17. this.Name = name;
  18. this.Costs = costs;
  19. }
  20.  
  21. public string Name { get; set; }
  22. public double Costs { get; set; }
  23.  
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <returns>values โ€‹โ€‹of all fields of the class instance</returns>
  28. public virtual string getInfo()
  29. {
  30. Trace.WriteLine("Getting info about" + this.Name);
  31. return JsonConvert.SerializeObject(this);
  32. }
  33.  
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. /// <returns>fresh goods or not</returns>
  38. public abstract bool isFresh();
  39. }
  40.  
  41. /// <summary>
  42. /// class to describe products properties
  43. /// </summary>
  44. class Product : Goods
  45. {
  46. public Product(string name, double costs, DateTime produceDate, DateTime freshUntill) :
  47. base(name, costs)
  48. {
  49. this.ProduceDate = produceDate;
  50. this.FreshUntill = freshUntill;
  51. }
  52.  
  53. public DateTime ProduceDate { get; set; }
  54. public DateTime FreshUntill { get; set; }
  55.  
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. /// <returns>fresh goods or not</returns>
  60. public override bool isFresh()
  61. {
  62. bool isFresh = DateTime.Compare(DateTime.Now, FreshUntill) <= 0;
  63. Trace.WriteLine("Getting fresh status of " + this.Name + " product");
  64. Trace.WriteLineIf(isFresh, "The product " + this.Name + " is fresh");
  65. Trace.WriteLineIf(!isFresh, "The product " + this.Name + " is NOT fresh");
  66. return isFresh;
  67. }
  68.  
  69. }
  70.  
  71. /// <summary>
  72. /// class to describe consigment properties
  73. /// </summary>
  74. class Consigment : Goods
  75. {
  76. public Consigment(int batch, string name, double costs, DateTime produceDate, DateTime freshUntill) :
  77. base(name, costs)
  78. {
  79. this.Batch = batch;
  80. this.ProduceDate = produceDate;
  81. this.FreshUntill = freshUntill;
  82. }
  83.  
  84. public int Batch;
  85. public DateTime ProduceDate { get; set; }
  86. public DateTime FreshUntill { get; set; }
  87.  
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <returns>fresh goods or not</returns>
  92. public override bool isFresh()
  93. {
  94. bool isFresh = DateTime.Compare(DateTime.Now, FreshUntill) <= 0;
  95. Trace.WriteLine("Getting fresh status of " + this.Name + " consigment");
  96. Trace.WriteLineIf(isFresh, "The consigment " + this.Name + " is fresh");
  97. Trace.WriteLineIf(!isFresh, "The consigment " + this.Name + " is NOT fresh");
  98. return isFresh;
  99. }
  100. }
  101. /// <summary>
  102. /// class to describe set of goods properties
  103. /// </summary>
  104. class Set : Goods
  105. {
  106. public Set(string name, double costs, Product[] products) :
  107. base(name, costs)
  108. {
  109. this.Products = products;
  110. }
  111.  
  112. public Product[] Products;
  113.  
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. /// <returns>whether all items in the set are fresh or not </returns>
  118. public override bool isFresh()
  119. {
  120. bool isFresh = Array.TrueForAll(Products, x => DateTime.Compare(DateTime.Now, x.FreshUntill) <= 0);
  121. Trace.WriteLine("Getting fresh status of " + this.Name + " consigment");
  122. Trace.WriteLineIf(isFresh, "The set " + this.Name + " is fresh");
  123. Trace.WriteLineIf(!isFresh, "The set " + this.Name + " is NOT fresh");
  124. return isFresh;
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement