Guest User

Untitled

a guest
Jun 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.67 KB | None | 0 0
  1. using System;
  2. namespace ParkingTicket
  3. {
  4. class Program
  5. {
  6. static void Main()
  7. {
  8. int speed;
  9. int yrInSchool;
  10. double fine;
  11. char choice = ' ';
  12. do
  13. {
  14. Console.Clear();
  15. speed = GetSpeed();
  16. if (speed <= 15)
  17. Console.WriteLine("No speeding fine to pay.");
  18. else
  19. {
  20. yrInSchool = GetYrInSchool();
  21. fine = CalculateFine(speed, yrInSchool);
  22. DisplayFine(fine);
  23. }
  24. choice = GetUserChoice();
  25. } while (choice != 'Q' && choice != 'q');
  26. }
  27. static int GetSpeed()
  28. {
  29. int speed;
  30. string userInput;
  31. try
  32. {
  33. Console.Write("Please enter the speed you were traveling: ");
  34. userInput = Console.ReadLine();
  35. speed = Convert.ToInt32(userInput);
  36. }
  37. catch
  38. {
  39. Console.WriteLine("an INVALID - PLEASE TRY AGAIN");
  40. Console.Write("Please press enter to continue....");
  41. userInput = Console.ReadLine();
  42. speed = GetSpeed(); // this is the recursion - calling myself
  43.  
  44. }
  45. return speed;
  46.  
  47. // code this method
  48. }
  49. static int GetYrInSchool()
  50. {
  51. string userEntry;
  52. int year;
  53.  
  54. /*************************************************************
  55. * modify this method to validate the year using a Try/Catch
  56. *************************************************************/
  57. Console.WriteLine("nClassifications");
  58. Console.WriteLine("tFreshman (enter 1)");
  59. Console.WriteLine("tSophomore (enter 2)");
  60. Console.WriteLine("tJunior (enter 3)");
  61. Console.WriteLine("tSenior (enter 4)");
  62.  
  63. try
  64. {
  65. Console.Write("Enter choice: ");
  66. userEntry = Console.ReadLine();
  67. year = Convert.ToInt32(userEntry);
  68. }
  69.  
  70. catch
  71. {
  72. Console.WriteLine("an INVALID - PLEASE TRY AGAIN");
  73. Console.Write("Please press enter to continue....");
  74. userEntry = Console.ReadLine();
  75. year = GetYrInSchool(); // this is the recursion - calling myself
  76. }
  77. return year;
  78. }
  79. static double CalculateFine(int speed, int year)
  80. {
  81. const double COST_PER_5_OVER = 87.50;
  82. const int SPEED_LIMIT = 15;
  83. const double INITIAL_FEE = 75.00;
  84. double fine = 0;
  85.  
  86. if (((year == 1) && (speed >= 15) || (speed <= 19)))
  87. {
  88. fine = INITIAL_FEE - 50.00;
  89. }
  90. else if (((year == 1) && (speed >= 20) || (speed >= 24)))
  91. {
  92. fine += (INITIAL_FEE - 50.00) + COST_PER_5_OVER;
  93. }
  94. else if (((year == 1) && (speed >= 25) || (speed <= 29)))
  95. {
  96. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 2);
  97. }
  98. else if (((year == 1) && (speed >= 30) || (speed <= 34)))
  99. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 3);
  100. else if (((year == 1) && (speed >= 35) || (speed <= 39)))
  101. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 4);
  102. else if (((year == 1) && (speed >= 40) || (speed <= 44)))
  103. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 5);
  104. else if (((year == 1) && (speed >= 45) || (speed <= 49)))
  105. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 6);
  106. if (((year == 1) && (speed >= 50) || (speed <= 54)))
  107. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 7);
  108. if (((year == 1) && (speed >= 55) || (speed <= 59)))
  109. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 8);
  110. if (((year == 1) && (speed >= 60) || (speed <= 64)))
  111. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 9);
  112. if (((year == 1) && (speed >= 65) || (speed <= 69)))
  113. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 10);
  114. if (((year == 1) && (speed >= 70) || (speed <= 74)))
  115. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 11);
  116. if (((year == 1) && (speed >= 75) || (speed <= 79)))
  117. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 12);
  118. if (((year == 1) && (speed >= 80) || (speed <= 84)))
  119. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 13);
  120. if (((year == 1) && (speed >= 85) || (speed <= 89)))
  121. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 14);
  122. if (((year == 1) && (speed >= 90) || (speed <= 94)))
  123. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 15);
  124. if (((year == 1) && (speed >= 95) || (speed <= 99)))
  125. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 16);
  126. if (((year == 1) && (speed >= 100) || (speed <= 104)))
  127. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 17);
  128. if (((year == 1) && (speed >= 105) || (speed <= 109)))
  129. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 18);
  130. if (((year == 1) && (speed >= 110) || (speed <= 114)))
  131. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 19);
  132. if (((year == 1) && (speed >= 115) || (speed <= 119)))
  133. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 20);
  134. if (((year == 1) && (speed >= 120) || (speed <= 124)))
  135. fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 21);
  136. else if (((year == 2) && (speed >= 16) || (speed <= 19)))
  137. fine = INITIAL_FEE;
  138. if (((year == 2) && (speed >= 20) || (speed <= 24)))
  139. fine = INITIAL_FEE + (COST_PER_5_OVER);
  140. if (((year == 2) && (speed >= 25) || (speed <= 29)))
  141. fine = INITIAL_FEE + (COST_PER_5_OVER * 2);
  142. if (((year == 2) && (speed >= 30) || (speed <= 34)))
  143. fine = INITIAL_FEE + (COST_PER_5_OVER * 3);
  144. if (((year == 2) && (speed >= 35) || (speed <= 39)))
  145. fine = INITIAL_FEE + (COST_PER_5_OVER * 3);
  146. if (((year == 2) && (speed >= 40) || (speed <= 44)))
  147. fine = INITIAL_FEE + (COST_PER_5_OVER * 4);
  148. if (((year == 2) && (speed >= 45) || (speed <= 49)))
  149. fine = INITIAL_FEE + (COST_PER_5_OVER * 5);
  150. if (((year == 2) && (speed >= 50) || (speed <= 54)))
  151. fine = INITIAL_FEE + (COST_PER_5_OVER * 6);
  152. if (((year == 2) && (speed >= 55) || (speed <= 59)))
  153. fine = INITIAL_FEE + (COST_PER_5_OVER * 7);
  154. if (((year == 2) && (speed >= 60) || (speed <= 64)))
  155. fine = INITIAL_FEE + (COST_PER_5_OVER * 8);
  156. if (((year == 2) && (speed >= 65) || (speed <= 69)))
  157. fine = INITIAL_FEE + (COST_PER_5_OVER * 9);
  158. if (((year == 2) && (speed >= 70) || (speed <= 74)))
  159. fine = INITIAL_FEE + (COST_PER_5_OVER * 10);
  160. if (((year == 2) && (speed >= 75) || (speed <= 79)))
  161. fine = INITIAL_FEE + (COST_PER_5_OVER * 11);
  162. if (((year == 2) && (speed >= 80) || (speed <= 84)))
  163. fine = INITIAL_FEE + (COST_PER_5_OVER * 12);
  164. if (((year == 2) && (speed >= 85) || (speed <= 89)))
  165. fine = INITIAL_FEE + (COST_PER_5_OVER * 13);
  166. if (((year == 2) && (speed >= 90) || (speed <= 94)))
  167. fine = INITIAL_FEE + (COST_PER_5_OVER * 14);
  168. if (((year == 2) && (speed >= 95) || (speed <= 99)))
  169. fine = INITIAL_FEE + (COST_PER_5_OVER * 15);
  170. if (((year == 2) && (speed >= 100) || (speed <= 104)))
  171. fine = INITIAL_FEE + (COST_PER_5_OVER * 16);
  172. if (((year == 2) && (speed >= 105) || (speed <= 109)))
  173. fine = INITIAL_FEE + (COST_PER_5_OVER * 17);
  174. if (((year == 2) && (speed >= 110) || (speed <= 114)))
  175. fine = INITIAL_FEE + (COST_PER_5_OVER * 18);
  176. if (((year == 2) && (speed >= 115) || (speed <= 119)))
  177. fine = INITIAL_FEE + (COST_PER_5_OVER * 19);
  178. if (((year == 2) && (speed >= 120) || (speed <= 124)))
  179. fine = INITIAL_FEE + (COST_PER_5_OVER * 20);
  180. if (((year == 2) && (speed >= 125) || (speed <= 129)))
  181. fine = INITIAL_FEE + (COST_PER_5_OVER * 21);
  182. else if (((year == 3) && (speed >= 16) || (speed <= 19)))
  183. fine = INITIAL_FEE + 50.00;
  184. if (((year == 3) && (speed >= 20) || (speed <= 24)))
  185. fine = INITIAL_FEE + 50.00 + (COST_PER_5_OVER);
  186. if (((year == 3) && (speed >= 25) || (speed <= 29)))
  187. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 2);
  188. if (((year == 3) && (speed >= 30) || (speed <= 34)))
  189. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 3);
  190. if (((year == 3) && (speed >= 35) || (speed <= 39)))
  191. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 4);
  192. if (((year == 3) && (speed >= 40) || (speed <= 44)))
  193. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 5);
  194. if (((year == 3) && (speed >= 45) || (speed <= 49)))
  195. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 6);
  196. if (((year == 3) && (speed >= 50) || (speed <= 54)))
  197. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 7);
  198. if (((year == 3) && (speed >= 55) || (speed <= 59)))
  199. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 8);
  200. if (((year == 3) && (speed >= 60) || (speed <= 64)))
  201. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 9);
  202. if (((year == 3) && (speed >= 65) || (speed <= 69)))
  203. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 10);
  204. if (((year == 3) && (speed >= 70) || (speed <= 74)))
  205. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 11);
  206. if (((year == 3) && (speed >= 75) || (speed <= 79)))
  207. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 12);
  208. if (((year == 3) && (speed >= 80) || (speed <= 84)))
  209. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 13);
  210. if (((year == 3) && (speed >= 85) || (speed <= 89)))
  211. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 14);
  212. if (((year == 3) && (speed >= 90) || (speed <= 94)))
  213. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 15);
  214. if (((year == 3) && (speed >= 95) || (speed <= 99)))
  215. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 16);
  216. if (((year == 3) && (speed >= 100) || (speed <= 104)))
  217. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 17);
  218. if (((year == 3) && (speed >= 105) || (speed <= 109)))
  219. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 18);
  220. if (((year == 3) && (speed >= 110) || (speed <= 114)))
  221. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 19);
  222. if (((year == 3) && (speed >= 115) || (speed <= 119)))
  223. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 20);
  224. if (((year == 3) && (speed >= 120) || (speed <= 124)))
  225. fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 21);
  226. else if (((year == 4) && (speed >= 16) || (speed <= 19)))
  227. fine = INITIAL_FEE + 100.00;
  228. if (((year == 4) && (speed >= 20) || (speed <= 24)))
  229. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER);
  230. if (((year == 4) && (speed >= 25) || (speed <= 29)))
  231. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 2);
  232. if (((year == 4) && (speed >= 30) || (speed <= 34)))
  233. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 3);
  234. if (((year == 4) && (speed >= 35) || (speed <= 39)))
  235. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 4);
  236. if (((year == 4) && (speed >= 40) || (speed <= 44)))
  237. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 5);
  238. if (((year == 4) && (speed >= 45) || (speed <= 49)))
  239. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 6);
  240. if (((year == 4) && (speed >= 100) || (speed <= 54)))
  241. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 7);
  242. if (((year == 4) && (speed >= 55) || (speed <= 59)))
  243. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 8);
  244. if (((year == 4) && (speed >= 60) || (speed <= 64)))
  245. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 9);
  246. if (((year == 4) && (speed >= 65) || (speed <= 69)))
  247. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 10);
  248. if (((year == 4) && (speed >= 70) || (speed <= 74)))
  249. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 11);
  250. if (((year == 4) && (speed >= 75) || (speed <= 79)))
  251. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 12);
  252. if (((year == 4) && (speed >= 80) || (speed <= 84)))
  253. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 13);
  254. if (((year == 4) && (speed >= 85) || (speed <= 89)))
  255. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 14);
  256. if (((year == 4) && (speed >= 90) || (speed <= 94)))
  257. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 15);
  258. if (((year == 4) && (speed >= 95) || (speed <= 99)))
  259. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 16);
  260. if (((year == 4) && (speed >= 100) || (speed <= 104)))
  261. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER);
  262. if (((year == 4) && (speed >= 105) || (speed <= 109)))
  263. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 18);
  264. if (((year == 4) && (speed >= 110) || (speed <= 114)))
  265. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 19);
  266. if (((year == 4) && (speed >= 115) || (speed <= 119)))
  267. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 20);
  268. if (((year == 4) && (speed >= 120) || (speed <= 124)))
  269. fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 21);
  270.  
  271. // finish coding this method
  272.  
  273. return fine;
  274. }
  275.  
  276. static void DisplayFine(double fine)
  277. {
  278. Console.WriteLine("Fine: {0:C}", fine);
  279. }
  280.  
  281. static char GetUserChoice()
  282. {
  283. Console.Write
  284. ("nPress "C" to [C]ontinue or "Q" to [Q]uit: ");
  285. string userEntry = Console.ReadLine();
  286. char choice = Convert.ToChar(userEntry);
  287. Console.WriteLine("------------------------------------");
  288. return choice;
  289. }
  290. }
  291. }
  292.  
  293. static double CalculateFine(int speed, int year)
  294. {
  295. const double COST_PER_5_OVER = 87.5;
  296. const int SPEED_LIMIT = 15;
  297. const double INITIAL_FEE = 75;
  298.  
  299. // The discount is 50 for each year, scaled so that year 1 is
  300. // -50, year 2 is 0, and so on.
  301. double discount = year * 50 - 100;
  302.  
  303. // This calculation assumes that speed >= SPEED_LIMIT
  304. int feeMultiplier = (speed - SPEED_LIMIT) / 5;
  305. double fine = feeMultiplier * COST_PER_5_OVER + INITIAL_FEE;
  306.  
  307. return discount + fine;
  308. }
  309.  
  310. static double CalculateFine(int speed, int year)
  311. {
  312. const double COST_PER_5_OVER = 87.50;
  313. const int SPEED_LIMIT = 15;
  314. const double INITIAL_FEE = 75.00;
  315. double fine = 0;
  316.  
  317. if(speed <= SPEED_LIMIT)
  318. {
  319. return 0; // No fine imposed
  320. }
  321.  
  322. fine = INITIAL_FEE;
  323.  
  324. // Adjust for the different years
  325. switch(year) {
  326. case 1:
  327. fine -= 50;
  328. break;
  329. case 2:
  330. // nowt
  331. break;
  332. case 3:
  333. fine += 50;
  334. break;
  335. case 4:
  336. fine += 100;
  337. }
  338.  
  339. // Add the remaining fine for each 5 miles over the limit
  340. // XXX: This is slightly different from yours, in that past 125,
  341. // it'll still keep adding COST_PER_5_OVER
  342. int perFiveOver = (int)Math.Floor((speed - SPEED_LIMIT) / 5);
  343. fine += (perFiveOver * COST_PER_5_OVER);
  344.  
  345. return fine;
  346.  
  347. }
  348.  
  349. ((year == 1) && (speed >= 50) && (speed <= 54))
  350.  
  351. const double COST_PER_5_OVER = 87.50;
  352. const int SPEED_LIMIT = 15;
  353. const double INITIAL_FEE = 75.00;
  354.  
  355. if (speed <= SPEED_LIMIT)
  356. {
  357. return 0;
  358. }
  359.  
  360. double yearSurcharge;
  361. switch (year)
  362. {
  363. case 1:
  364. yearSurcharge = -50;
  365. break;
  366. case 2:
  367. yearSurcharge = 0;
  368. break;
  369. case 3:
  370. yearSurcharge = 50;
  371. break;
  372. case 4:
  373. yearSurcharge = 100;
  374. break;
  375. default:
  376. yearSurcharge = 0;
  377. break;
  378. }
  379.  
  380. const int NUMBER_OF_FIVE_OVER = 21;
  381. int numberOfFiveOver = Math.Min((speed - SPEED_LIMIT) % 5; MAX_NUMBER_OF_FIVE_OVER)
  382.  
  383. return INITIAL_FEE + yearSurcharge + (numberOfFiveOver * COST_PER_5_OVER);
  384.  
  385. if (speed > SPEED_LIMIT) {
  386. return INITIAL_FEE
  387. + (year == 1 ? -50 : year == 3 ? 50 : year == 4 ? 100 : 0)
  388. + Math.Min((speed - SPEED_LIMIT) % 5, MAX_NUMBER_OF_FIVE_OVER) * COST_PER_5_OVER;
  389. } else {
  390. return 0;
  391. }
  392.  
  393. if(year==1) return calculateFineForYear1(speed);
  394. if(year==2) return ...
  395.  
  396. if(speed<50) return 0;
  397. if(speed<55) return ...;
  398. if(speed<60) return ...;
  399. if(speed<65) return ...
  400.  
  401. Int total_above_limit = (speed – SPEED_LIMIT);
  402. Int increments_of_5_above_limit = (total_above_limit)/5
  403. Return ( (INITIAL_FEE – 50) + COST_PER_5_OVER*increments_of_5_above_limit))
Add Comment
Please, Sign In to add comment