Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1.  
  2. namespace VendingMachine
  3. {
  4. /// <summary>
  5. /// The class <CODE>VendingMachine</CODE> represents vending machines
  6. /// which sell bottles of soda.Each vending machine has a (changeable)
  7. /// price per bottle, and is operated by first inserting cash into the
  8. /// machine(method<CODE> InsertMoney</CODE>; the method can be called
  9. /// multiple times to incrementally add money) and then finishing the
  10. /// purchase with the method<CODE> buyBottle</CODE>. A vending machine
  11. /// object keeps track of the amount of money that it has "earned" from
  12. /// bottle sales.
  13. /// <P>
  14. /// For the purposes of this class, all bottles are assumed to be
  15. /// identical, and the machine is supposed to contain an endless supply
  16. /// of coins to give as change to users.
  17. /// </summary>
  18. public class VendingMachine
  19. {
  20. private int bottleCount;
  21. private double bottlePrice;
  22. private double earnedCash;
  23. private double insertedCash;
  24.  
  25. /// <summary>
  26. /// Creates a new vending machine with the given number of bottles in it,
  27. /// the given price per bottle, and no cash.
  28. /// </summary>
  29. /// <param name="initialBottleCount">The amount of bottles when a new
  30. /// machine is created (int).</param>
  31. /// <param name="bottlePrice">The price of one bottle (double).</param>
  32. public VendingMachine(int initialBottleCount, double bottlePrice)
  33. {
  34. this.bottleCount = initialBottleCount;
  35. this.bottlePrice = bottlePrice;
  36. this.insertedCash = 0;
  37. }
  38.  
  39. /// <summary>
  40. /// Gets and Sets the price per bottle of soda for this machine.
  41. /// </summary>
  42. public double BottlePrice
  43. {
  44. get
  45. {
  46. return this.bottlePrice;
  47. }
  48. set
  49. {
  50. this.bottlePrice = value;
  51. }
  52. }
  53.  
  54. /// <summary>
  55. /// Determines if the machine is sold out. I.e., determines if
  56. /// there are no more bottles in the machine.
  57. /// </summary>
  58. /// <returns>Returns a boolean value indicating if there are no
  59. /// bottles in the machine (bool).</returns>
  60. public bool IsSoldOut()
  61. {
  62. if(bottleCount < 0)
  63. {
  64. return true;
  65. }
  66. return false;
  67. }
  68.  
  69. /// <summary>
  70. /// Inserts a sum of money into the machine. This corresponds to the user
  71. /// putting coins in the machine. The inserted money is not considered
  72. /// part of the machine's earnings yet; instead, it can be used to
  73. /// make a bottle purchase. It is possible to insert more money into the
  74. /// machine than is required for making a purchase.
  75. /// </summary>
  76. /// <param name="insertedSum">A (positive) sum of money to insert.</param>
  77. ////
  78. public void InsertMoney(double insertedSum)
  79. {
  80. insertedCash += insertedSum;
  81. }
  82.  
  83.  
  84. /// <summary>
  85. /// Adds a number of bottles to the machine.
  86. /// <summary>
  87. /// <param name="numberOfNewBottles">A positive number indicating how
  88. /// many bottles to add (int).</param>
  89. public void AddBottles(int numberOfNewBottles)
  90. {
  91. bottleCount += numberOfNewBottles;
  92. }
  93.  
  94.  
  95. /// <summary>
  96. /// Empties the machine's cashbox. That is, returns the amount of money
  97. /// the machine has earned so far from bottle sales and resets the
  98. /// earnings counter to zero.
  99. /// <summary>
  100. /// <returns>The amount of money that was in the cashbox before emptying (double).</returns>
  101. ////
  102. public double EmptyCashbox()
  103. {
  104. double cash = earnedCash;
  105. earnedCash = 0;
  106. return cash;
  107. }
  108.  
  109.  
  110. /// <summary>
  111. /// Buys a bottle from the machine, assuming there is enough money inserted,
  112. /// and all bottles have not yet been sold. Buying a bottle causes money to
  113. /// be added to the machine's earnings and a bottle to be removed from stock.
  114. /// <summary>
  115. /// <returns>The change given to the buyer (inserted money minus bottle price),
  116. /// or a negative value in case the bottle could not be bought (double).</returns>
  117. public double BuyBottle()
  118. {
  119. if(bottlePrice > insertedCash)
  120. {
  121. return -1;
  122. }
  123. else
  124. {
  125. insertedCash -= bottlePrice;
  126. bottleCount -= 1;
  127. earnedCash += bottlePrice;
  128. return insertedCash;
  129. }
  130. }
  131.  
  132. /// <summary>
  133. /// Returns the status of the machine. The string contains the number of bottles left,
  134. /// bottle price and earned money in the form:
  135. ///
  136. /// Bottles left: 3
  137. /// Bottle price: 1,45 euroa
  138. /// Money earned: 205,25 euroa
  139. /// </summary>
  140. /// <returns></returns>
  141. public override string ToString()
  142. {
  143. return string.Format("Bottles left: {0}\nBottle price: {1} euroa\nMoney earned: {2} euroa", bottleCount, bottlePrice, earnedCash);
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement