Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace LabSabloan7._2State
  8. {
  9. class HasCoinState : State
  10. {
  11. public HasCoinState(VendingMachine Machine) : base(Machine)
  12. {
  13. }
  14. public override bool BuyProduct()
  15. {
  16. Console.WriteLine("You can retreive the product.");
  17. this.Machine.SetMachineState(Machine.SoldState);
  18. return true;
  19. }
  20.  
  21. public override bool Dispense()
  22. {
  23. Console.WriteLine("You ");
  24. return false;
  25. }
  26.  
  27. public override bool EjectCoin()
  28. {
  29. Console.WriteLine("You can retreive your coin.");
  30. this.Machine.SetMachineState(Machine.NoCoinState);
  31. return true;
  32. }
  33.  
  34. public override bool InsertCoin()
  35. {
  36. Console.WriteLine("You do not has posibility to insert coin, becouse is already inserted.");
  37. return false;
  38. }
  39. }
  40. }
  41.  
  42. using System;
  43. using System.Collections.Generic;
  44. using System.Linq;
  45. using System.Text;
  46. using System.Threading.Tasks;
  47.  
  48. namespace LabSabloan7._2State
  49. {
  50.  
  51. class NoCoinState : State {
  52.  
  53. public NoCoinState(VendingMachine Machine) : base(Machine)
  54. {
  55. }
  56. public override bool BuyProduct()
  57. {
  58. Console.WriteLine("Please insert coin!");
  59. return false;
  60. }
  61.  
  62. public override bool Dispense()
  63. {
  64. Console.WriteLine("Please insert coin and chose a product!");
  65. return false;
  66. }
  67.  
  68. public override bool EjectCoin()
  69. {
  70. Console.WriteLine("You do not has coin to eject!");
  71. return false;
  72. }
  73.  
  74. public override bool InsertCoin()
  75. {
  76. Console.WriteLine("Coins has been inserted sucsessfully!");
  77. this.Machine.SetMachineState(Machine.HasCoinState);
  78. return true;
  79. }
  80. }
  81. }
  82. using System;
  83. using System.Collections.Generic;
  84. using System.Linq;
  85. using System.Text;
  86. using System.Threading.Tasks;
  87.  
  88. namespace LabSabloan7._2State
  89. {
  90. public enum EUserOption
  91. {
  92. InsertCoin,
  93. EjectCoin,
  94. BuyProduct,
  95. InspectMachine,
  96. FillMachine,
  97. Exit
  98. }
  99. class Program
  100. {
  101. static void Main(string[] args)
  102. {
  103. VendingMachine _vendingMachine = new VendingMachine();
  104.  
  105. Console.WriteLine(" 1.Insert coin\n" +
  106. " 2.Eject coin\n" +
  107. " 3.Buy product\n" +
  108. " 4.Refill\n" +
  109. " 5.Inspect machine\n" +
  110. " 6.Exit\n");
  111.  
  112.  
  113. while (true)
  114. {
  115.  
  116. switch (Console.ReadKey(true).KeyChar)
  117. {
  118. case '1':
  119. _vendingMachine.UpdateState(EUserOption.InsertCoin);
  120. break;
  121. case '2':
  122. _vendingMachine.UpdateState(EUserOption.EjectCoin);
  123. break;
  124. case '3':
  125. _vendingMachine.UpdateState(EUserOption.BuyProduct);
  126. break;
  127. case '4':
  128. _vendingMachine.UpdateState(EUserOption.FillMachine);
  129. break;
  130. case '5':
  131. _vendingMachine.UpdateState(EUserOption.InspectMachine);
  132. break;
  133. case '6':
  134. _vendingMachine.UpdateState(EUserOption.Exit);
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. using System;
  142. using System.Collections.Generic;
  143. using System.Linq;
  144. using System.Text;
  145. using System.Threading.Tasks;
  146.  
  147. namespace LabSabloan7._2State
  148. {
  149. class SoldOutState : State
  150. {
  151. public SoldOutState(VendingMachine Machine) : base(Machine)
  152. {
  153. }
  154.  
  155. public override bool BuyProduct()
  156. {
  157. Console.WriteLine("Sold out!");
  158. return false;
  159. }
  160.  
  161. public override bool Dispense()
  162. {
  163. Console.WriteLine("Sold out!");
  164. return false;
  165. }
  166.  
  167. public override bool EjectCoin()
  168. {
  169. Console.WriteLine("You do not has coins inserted.");
  170. return false;
  171. }
  172.  
  173. public override bool InsertCoin()
  174. {
  175. Console.WriteLine("You do not has posibility to insert coin, becouse sold is out.");
  176. return false;
  177. }
  178. }
  179. }
  180.  
  181. using System;
  182. using System.Collections.Generic;
  183. using System.Linq;
  184. using System.Text;
  185. using System.Threading.Tasks;
  186.  
  187. namespace LabSabloan7._2State
  188. {
  189. class SoldState : State
  190. {
  191. public SoldState(VendingMachine Machine) : base(Machine)
  192. {
  193. }
  194.  
  195. public override bool BuyProduct()
  196. {
  197. Console.WriteLine("Please insert coins!");
  198. return false;
  199. }
  200.  
  201. public override bool Dispense()
  202. {
  203. Console.WriteLine("Please insert coins!");
  204. return false;
  205. }
  206.  
  207. public override bool EjectCoin()
  208. {
  209. Console.WriteLine("You do not has coin to eject!");
  210. return false;
  211. }
  212.  
  213. public override bool InsertCoin()
  214. {
  215. Console.WriteLine("Coins has been inserted sucsessfully!");
  216. this.Machine.SetMachineState(Machine.HasCoinState);
  217. return true;
  218. }
  219. }
  220. }
  221. using System;
  222. using System.Collections.Generic;
  223. using System.Linq;
  224. using System.Text;
  225. using System.Threading.Tasks;
  226.  
  227. namespace LabSabloan7._2State
  228. {
  229. abstract class State
  230. {
  231. public VendingMachine Machine { get; set; }
  232.  
  233. public abstract bool InsertCoin();
  234. public abstract bool EjectCoin();
  235. public abstract bool BuyProduct();
  236. public abstract bool Dispense();
  237.  
  238. public State(VendingMachine machine)
  239. {
  240. this.Machine = machine;
  241. }
  242. }
  243. }
  244. using System;
  245. using System.Collections.Generic;
  246. using System.Linq;
  247. using System.Text;
  248. using System.Threading.Tasks;
  249.  
  250. namespace LabSabloan7._2State
  251. {
  252. class VendingMachine
  253. {
  254. private int capacity;
  255. private State maschineState;
  256. public State NoCoinState;
  257. public State SoldOutState;
  258. public State SoldState;
  259. public State HasCoinState;
  260.  
  261. public VendingMachine()
  262. {
  263. NoCoinState = new NoCoinState(this);
  264. SoldOutState = new SoldOutState(this);
  265. SoldState = new SoldState(this);
  266. HasCoinState = new HasCoinState(this);
  267. this.capacity = 10;
  268. this.maschineState = NoCoinState;
  269. }
  270.  
  271. public void UpdateState(EUserOption option)
  272. {
  273. switch (option)
  274. {
  275. case EUserOption.InsertCoin:
  276. InsertCoin();
  277. break;
  278. case EUserOption.EjectCoin:
  279. EjectCoin();
  280. break;
  281. case EUserOption.BuyProduct:
  282. BuyProduct();
  283. break;
  284. case EUserOption.FillMachine:
  285. ReFill();
  286. break;
  287. case EUserOption.InspectMachine:
  288. Inspect();
  289. break;
  290. case EUserOption.Exit:
  291. maschineState = NoCoinState;
  292. break;
  293. }
  294. }
  295.  
  296. private void ReFill()
  297. {
  298. this.capacity = 10;
  299. }
  300.  
  301. private bool InsertCoin()
  302. {
  303. return maschineState.InsertCoin();
  304. }
  305.  
  306. private bool EjectCoin()
  307. {
  308. return maschineState.EjectCoin();
  309. }
  310.  
  311. private bool BuyProduct()
  312. {
  313. if (maschineState.BuyProduct())
  314. {
  315. capacity--;
  316. if (IsEmpty())
  317. {
  318. maschineState = SoldOutState;
  319. }
  320. return true;
  321. }
  322. return false;
  323. }
  324.  
  325. private void Inspect()
  326. {
  327. Console.WriteLine("The capacity is:"+capacity);
  328. }
  329.  
  330. public bool IsEmpty()
  331. {
  332. if (capacity == 0){
  333. return true;
  334. }
  335. return false;
  336. }
  337.  
  338. public void SetMachineState(State state)
  339. {
  340. maschineState = state;
  341. }
  342.  
  343. }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement