Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.85 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Lesson2
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. // Названия валют
  10. const string FirstCurrencyName = "золото";
  11. const string SecondCurrencyName = "серебро";
  12. const string ThirdCurrencyName = "медь";
  13.  
  14. // Валюта в кармане клиента
  15. float firstCurrencyAmount;
  16. float secondCurrencyAmount;
  17. float thirdCurrencyAmount;
  18.  
  19. // Курс валют
  20. float firstInSecond = 10f;
  21. float firstInThird = 20f;
  22. float secondInFirst = 1 / firstInSecond;
  23. float thirdInFirst = 1 / firstInThird;
  24. float secondInThird = firstInThird / firstInSecond;
  25. float thirdInSecond = 1 / secondInThird;
  26.  
  27. bool quit = false;
  28. string nextAction;
  29.  
  30. Console.WriteLine("Введите ваше количество денег");
  31. Console.Write($"{FirstCurrencyName}: ");
  32. firstCurrencyAmount = Convert.ToSingle(Console.ReadLine());
  33.  
  34. Console.Write($"{SecondCurrencyName}: ");
  35. secondCurrencyAmount = Convert.ToSingle(Console.ReadLine());
  36.  
  37. Console.Write($"{ThirdCurrencyName}: ");
  38. thirdCurrencyAmount = Convert.ToSingle(Console.ReadLine());
  39.  
  40. Console.WriteLine();
  41.  
  42. do
  43. {
  44. Console.Write("Доступные действия: обмен валюты (1), узнать свой баланс (2), выйти из программы (q) ");
  45. nextAction = Console.ReadLine().ToLower();
  46. Console.WriteLine();
  47.  
  48. Console.Clear();
  49.  
  50. if (nextAction == "1")
  51. {
  52. string sourceCurrency;
  53. string targetCurrency;
  54. float sourceAmount;
  55. float pocketAmount;
  56. float targetAmount;
  57.  
  58. Console.WriteLine($"Обмен валюты: {FirstCurrencyName}, {SecondCurrencyName}, {ThirdCurrencyName}");
  59.  
  60. Console.Write("Введите название валюты, которую будете обменивать: ");
  61. sourceCurrency = Console.ReadLine().ToLower();
  62.  
  63. bool isCurrencyCorrect = sourceCurrency == FirstCurrencyName ||
  64. sourceCurrency == SecondCurrencyName ||
  65. sourceCurrency == ThirdCurrencyName;
  66.  
  67. // Сразу проверяем правильность ввода названия
  68. if (isCurrencyCorrect)
  69. {
  70. Console.Write("Введите сумму валюты, которую будете обменивать: ");
  71. sourceAmount = Convert.ToSingle(Console.ReadLine());
  72.  
  73. bool isAmountCorrect = false;
  74.  
  75. switch (sourceCurrency)
  76. {
  77. case FirstCurrencyName:
  78. pocketAmount = firstCurrencyAmount;
  79. break;
  80. case SecondCurrencyName:
  81. pocketAmount = secondCurrencyAmount;
  82. break;
  83. case ThirdCurrencyName:
  84. pocketAmount = thirdCurrencyAmount;
  85. break;
  86. default:
  87. pocketAmount = 0f;
  88. break;
  89. }
  90.  
  91. isAmountCorrect = sourceAmount <= pocketAmount;
  92.  
  93. if (isAmountCorrect)
  94. {
  95. Console.Write("Введите название валюты, на которую будете обменивать: ");
  96. targetCurrency = Console.ReadLine().ToLower();
  97.  
  98. isCurrencyCorrect = targetCurrency == FirstCurrencyName ||
  99. targetCurrency == SecondCurrencyName ||
  100. targetCurrency == ThirdCurrencyName;
  101.  
  102. if (targetCurrency == sourceCurrency)
  103. {
  104. Console.WriteLine($"Вы собираетесь обменять {sourceCurrency} на {targetCurrency}. Это бессмысленно.");
  105. Console.ReadLine();
  106. }
  107. else if (isCurrencyCorrect)
  108. {
  109. float exchangeRate;
  110.  
  111. if ((sourceCurrency == FirstCurrencyName) && (targetCurrency == SecondCurrencyName))
  112. {
  113. exchangeRate = firstInSecond;
  114. }
  115. else if ((sourceCurrency == FirstCurrencyName) && (targetCurrency == ThirdCurrencyName))
  116. {
  117. exchangeRate = firstInThird;
  118. }
  119. else if ((sourceCurrency == SecondCurrencyName) && (targetCurrency == FirstCurrencyName))
  120. {
  121. exchangeRate = secondInFirst;
  122. }
  123. else if ((sourceCurrency == SecondCurrencyName) && (targetCurrency == ThirdCurrencyName))
  124. {
  125. exchangeRate = secondInThird;
  126. }
  127. else if ((sourceCurrency == ThirdCurrencyName) && (targetCurrency == FirstCurrencyName))
  128. {
  129. exchangeRate = thirdInFirst;
  130. }
  131. else if ((sourceCurrency == ThirdCurrencyName) && (targetCurrency == SecondCurrencyName))
  132. {
  133. exchangeRate = thirdInSecond;
  134. }
  135. else
  136. {
  137. exchangeRate = 0f;
  138. }
  139.  
  140. targetAmount = sourceAmount * exchangeRate;
  141.  
  142. switch (targetCurrency)
  143. {
  144. case FirstCurrencyName:
  145. firstCurrencyAmount += targetAmount;
  146. break;
  147. case SecondCurrencyName:
  148. secondCurrencyAmount += targetAmount;
  149. break;
  150. case ThirdCurrencyName:
  151. thirdCurrencyAmount += targetAmount;
  152. break;
  153. }
  154. switch (sourceCurrency)
  155. {
  156. case FirstCurrencyName:
  157. firstCurrencyAmount -= sourceAmount;
  158. break;
  159. case SecondCurrencyName:
  160. secondCurrencyAmount -= sourceAmount;
  161. break;
  162. case ThirdCurrencyName:
  163. thirdCurrencyAmount -= sourceAmount;
  164. break;
  165. }
  166.  
  167. Console.WriteLine($"Вы поменяли {sourceCurrency} в количестве {sourceAmount}" +
  168. $" на {targetCurrency} в количестве {targetAmount}");
  169. Console.ReadLine();
  170. }
  171. else
  172. {
  173. Console.WriteLine("Введено неверное название валюты, на которую хотите обменять.");
  174. Console.ReadLine();
  175. }
  176. }
  177. else
  178. {
  179. Console.WriteLine($"У вас недостаточно валюты {sourceCurrency}.");
  180. Console.ReadLine();
  181. }
  182. }
  183. else
  184. {
  185. Console.WriteLine("Введено неверное название валюты, которую хотите обменять.");
  186. Console.ReadLine();
  187. }
  188. }
  189. else if (nextAction == "2")
  190. {
  191. Console.WriteLine($"Ваш баланс:\nВалюта\t\tСумма");
  192. Console.WriteLine($"{FirstCurrencyName}\t\t{firstCurrencyAmount:f2}\n" +
  193. $"{SecondCurrencyName}\t\t{secondCurrencyAmount:f2}\n" +
  194. $"{ThirdCurrencyName}\t\t{thirdCurrencyAmount:f2}");
  195. Console.WriteLine();
  196. }
  197. else if (nextAction == "q")
  198. {
  199. quit = true;
  200. }
  201. else
  202. {
  203. Console.WriteLine("Введена неверная команда, повторите ввод.");
  204. Console.WriteLine();
  205. }
  206. } while (!quit);
  207. }
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement