Guest User

Untitled

a guest
Nov 16th, 2018
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.84 KB | None | 0 0
  1. /*
  2. * The ATM class is a mockup of a functional ATM containing
  3. * check balance, withdraw funds and transfer funds options.
  4. *
  5. * Author: Ben Harris
  6. * Date: September, 2012
  7. */
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11.  
  12. namespace ATM_Assignment_2010
  13. {
  14.  
  15. class ATM
  16. {
  17.  
  18. //Arrays used for storing balance and account name information
  19. private static readonly List<decimal> Balances = new List<decimal> { 0, 901.45m, 450, 1500, 2000 };
  20. private static readonly List<string> Accounts = new List<string> { "Blank", "Savings Account", "Debit Account",
  21. "Credit Account", "Line of Credit Account" };
  22.  
  23. private const int SavingsAccount = 1;
  24. private const int DebitAccount = 2;
  25. private const int CreditAccount = 3;
  26. private const int LineOfCredit = 4;
  27.  
  28. private const int QuitOption = 0;
  29. private const int CheckBalanceOption = 1;
  30. private const int WithdrawOption = 2;
  31. private const int TransferOption = 3;
  32.  
  33.  
  34.  
  35. // ------------------- MAIN FUNCTION --------------------- //
  36.  
  37. static void Main(string[] args)
  38. {
  39. bool finished = false;
  40.  
  41. while (!finished)
  42. {
  43. finished = MainMenu();
  44. }
  45.  
  46. Console.WriteLine("Thankyou for using BencoBank. Have a nice day.");
  47. Console.WriteLine("Press any key to exit.");
  48. Console.ReadKey();
  49. } // End Main
  50.  
  51.  
  52. // ------------- MENU + CHOICE CHECKING ------------------------- //
  53.  
  54. /// <summary>
  55. /// Prints the main menu of the ATM and waits for a valid choice
  56. /// before performing it. Entering 0 will exit the program.
  57. ///
  58. /// pre: finished boolean in Main method must be set to false
  59. /// post: gives menu option to Perform method for validation.
  60. /// </summary>
  61. /// <returns>Returns a boolean - true if program should quit</returns>
  62. static bool MainMenu()
  63. {
  64. Console.Clear();
  65. Console.WriteLine("=============================================================");
  66. Console.WriteLine("Welcome to BencoBank.");
  67. Console.WriteLine("=============================================================");
  68. Console.WriteLine("\nPlease choose an option, or press 0 to quit.\n");
  69. Console.WriteLine("1. Check Balance\n2. Withdrawl\n3. Transfer\n");
  70. Console.Write("Enter option:");
  71.  
  72. var validMenuChoices = new List<int> { 0, 1, 2, 3 };
  73. int choice;
  74. while (!int.TryParse(Console.ReadLine(), out choice) && !validMenuChoices.Contains(choice))
  75. {
  76. // While the input can't be parsed, and the choice isnt valid
  77. Console.WriteLine("Bad input, please try again");
  78. }
  79. Console.WriteLine("\n");
  80.  
  81. if (choice == QuitOption)
  82. {
  83. return true;
  84. }
  85.  
  86. Perform(choice);
  87. return false;
  88. } // End MainMenu
  89.  
  90.  
  91. /// <summary>
  92. /// Checks the menu choice selected in MainMenu method to see if it is an acceptable option.
  93. /// pre: menu selection must be made in MainMenu method.
  94. /// post: program continues to relevant method for checking, withdrawl or transfer.
  95. /// </summary>
  96. /// <param name="choice">Valid </param>
  97. static void Perform(int choice)
  98. {
  99. switch (choice)
  100. {
  101. case CheckBalanceOption:
  102. CheckBalance();
  103. break;
  104. case WithdrawOption:
  105. Withdraw();
  106. break;
  107. case TransferOption:
  108. Transfer();
  109. break;
  110. }
  111. }
  112. // End of Perform()
  113.  
  114. // ------------------ ACCOUNT SELECTION METHODS --------------------- //
  115.  
  116. /// <summary>
  117. /// Lists account menu, used to avoid repetition in other methods.
  118. /// </summary>
  119. static void ListAccounts()
  120. {
  121. Console.WriteLine("1. Savings\n2. Debit\n3. Credit\n4. Line of Credit\n");
  122. Console.Write("Enter option:");
  123. }
  124. // End of ListAccounts()
  125.  
  126. /// <summary>
  127. /// Used for account selection of CheckBalance and Withdraw methods.
  128. /// User selects account and method checks if account is an acceptable option,
  129. /// then returns the account number to previous method.
  130. /// pre: specific menu action method has been called
  131. /// post: returns account option to be saved as a variable in action method.
  132. /// </summary>
  133. /// <returns>Returns account option to be used as variable in method.</returns>
  134. static int SelectAccount()
  135. {
  136. int account;
  137. var validAccounts = new List<int> { 1, 2, 3, 4 };
  138.  
  139. while (!int.TryParse(Console.ReadLine(), out account) && !validAccounts.Contains(account))
  140. {
  141. Console.WriteLine("You have selected an invalid account, {0}. Please choose again.");
  142. ListAccounts();
  143. }
  144.  
  145. return account;
  146. }
  147.  
  148.  
  149. // End of SelectAccount()
  150.  
  151. // ------------------- CHECK BALANCE --------------------- //
  152.  
  153. /// <summary>
  154. /// Account balance check method, calls SelectAccount and then forwards
  155. /// that account number to DisplayBalance method to finish the action.
  156. /// pre: user has selected check balance menu option in MainMenu
  157. /// post: continues to DisplayBalance method which prints balance information.
  158. /// </summary>
  159. static void CheckBalance()
  160. {
  161. Console.WriteLine("Please select an account to check the balance, or press 0 to cancel.");
  162. int account = SelectAccount();
  163. if (account != QuitOption)
  164. {
  165. DisplayBalance(account);
  166. }
  167. }
  168.  
  169. /// <summary>
  170. /// Finishes balance checking action by printing balance information and returning to main menu.
  171. /// pre: Account has been selected by CheckBalance method
  172. /// post: Returns to main menu upon user key entry.
  173. /// </summary>
  174. /// <param name="whichAccount">Given to method by CheckBalance method, provides relevant
  175. /// account number for balances and accounts array.</param>
  176. static void DisplayBalance(int whichAccount)
  177. {
  178. Console.WriteLine("\n\n\nCurrent Date:" + DateTime.Now + "\n");
  179. Console.WriteLine("Your {0} contains {1:C} \n", Accounts[whichAccount], Balances[whichAccount]);
  180. Console.WriteLine("Press any key to return to the main menu.");
  181. Console.ReadKey();
  182. Console.Clear();
  183. }
  184.  
  185. // End of DisplayBalance()
  186.  
  187.  
  188.  
  189. // ------------------- WITHDRAW ---------------------- //
  190.  
  191. /// <summary>
  192. /// Account withdraw method, calls SelectAccount and then forwards
  193. /// account number to WithdrawAmount method for amount entry and checking.
  194. /// pre: user has selected the withdraw option from the MainMenu method.
  195. /// post: account number is handed to WithdrawAmount method, which then
  196. /// takes a withdraw amount and checks that value.
  197. /// </summary>
  198. static void Withdraw()
  199. {
  200. Console.WriteLine("Please select an account to withdraw from, or press 0 to cancel.");
  201. int account = SelectAccount();
  202.  
  203. if (account != QuitOption)
  204. {
  205. WithdrawAmount(account);
  206. }
  207. }
  208.  
  209. /// <summary>
  210. /// Asks user for amount to withdraw, then checks for invalid amounts. If the amount is
  211. /// a valid amount, withdraws the amount from the accounts balance in the balances array
  212. /// and a bank fee if applicable, and then hands amount to DispenseCash method.
  213. /// pre: Account has been selected in Withdraw()
  214. /// post: amount is handed to DispenseCash to complete withdraw transaction.
  215. /// </summary>
  216. /// <param name="whichAccount">
  217. /// Given to method by Withdraw method, provides relevant account number
  218. /// for balances and accounts arrays.
  219. /// </param>
  220. static void WithdrawAmount(int whichAccount)
  221. {
  222. const decimal minimumBalance = 10.0m;
  223. const decimal bankFee = 0.10m;
  224. Console.WriteLine("How much would you like to withdraw?\n");
  225. Console.WriteLine("Enter amount:");
  226.  
  227. var validAmount = false;
  228. while (!validAmount)
  229. {
  230. decimal withdrawAmount;
  231. while (!decimal.TryParse(Console.ReadLine(), out withdrawAmount))
  232. {
  233. Console.WriteLine("Please enter a valid dollar value");
  234. }
  235. // Checks to see if withdraw will take balance below minimum
  236. if (withdrawAmount >= (Balances[whichAccount] - minimumBalance))
  237. {
  238. Console.WriteLine("\nInsufficient funds. Please try again.\n");
  239. }
  240. // Checks for special cases of 10 or 30, and for cent values
  241. else if (withdrawAmount % 10 != 0 || withdrawAmount == 10 || withdrawAmount == 30)
  242. {
  243. Console.WriteLine("Cannot withdraw {0:C} from your account." +
  244. "This ATM only has $20 and $50 notes.", withdrawAmount);
  245. }
  246. else
  247. {
  248. validAmount = true;
  249. }
  250.  
  251. //Hands acceptable amount to DispenseCash
  252. Balances[whichAccount] = Balances[whichAccount] - withdrawAmount;
  253. if (whichAccount == CreditAccount || whichAccount == DebitAccount)
  254. {
  255. Balances[whichAccount] = Balances[whichAccount] - bankFee;
  256. }
  257. DispenseCash(withdrawAmount);
  258. }
  259. }
  260. // End of WithdrawAmount
  261.  
  262. /// <summary>
  263. /// Uses division and modulus operators to figure out how many 50 dollar notes and
  264. /// 20 dollar notes are handed out by the ATM. Prints information and returns to
  265. /// MainMenu method upon user key entry.
  266. /// pre: correct amount has been entered into the WithdrawAmount method.
  267. /// post: returns to MainMenu after method completion.
  268. /// </summary>
  269. /// <param name="amount">
  270. /// Value handed to method by WithdrawAmount methodm specifies how
  271. /// much the user wants to withdraw from their account.
  272. /// </param>
  273. static void DispenseCash(decimal amount)
  274. {
  275. int fiftyNotes = 0;
  276. int twentyNotes = 0;
  277. int leftoverAmount;
  278. int dispenseAmount = Convert.ToInt16(amount);
  279.  
  280. //Gets amount of 50 dollar notes in amount, then finds leftover amount.
  281. fiftyNotes = dispenseAmount / 50;
  282. leftoverAmount = dispenseAmount % 50;
  283.  
  284. //If leftover is 10 or 30, removes one 50 dollar note and adds 50 dollars to leftover
  285. //so it can be given in 20 dollar notes.
  286. if (leftoverAmount != 0 && leftoverAmount == 10 || leftoverAmount == 30)
  287. {
  288. leftoverAmount = leftoverAmount + 50;
  289. fiftyNotes = fiftyNotes - 1;
  290. }
  291.  
  292. //Gets amount of 20 dollar notes in leftover amount.
  293. twentyNotes = leftoverAmount / 20;
  294. leftoverAmount = leftoverAmount % 20;
  295.  
  296. Console.WriteLine("\n\nThe current date is : {0}\n", DateTime.Now);
  297. Console.WriteLine("{0:C0} successfully withdrawn. " +
  298. "Please take your notes from the ATM.\n", amount);
  299. Console.WriteLine("{0} $20 notes expected\n{1} $50 notes expected.\n", twentyNotes, fiftyNotes);
  300. Console.WriteLine("Press any key to return to the main menu.");
  301. Console.ReadKey();
  302. Console.Clear();
  303.  
  304.  
  305. }
  306. // End of DispenseCash
  307.  
  308. // -------------------- TRANSFER ----------------------- //
  309.  
  310. /// <summary>
  311. /// Method for transfer option. Selects to and from accounts, checks if they are different,
  312. /// then forwards both account selections to the TransferAmount method for completion of
  313. /// transfer action.
  314. /// pre: Transfer has been selected in MainMenu method.
  315. /// post: Two account values handed to TransferAmount method for completion.
  316. /// </summary>
  317. static void Transfer()
  318. {
  319. int accountFrom = 0;
  320. int accountTo = 0;
  321. Console.WriteLine("Please select an account to transfer from, or press 0 to cancel.");
  322. accountFrom = SelectAccount();
  323.  
  324. if (accountFrom == QuitOption)
  325. {
  326. MainMenu();
  327. }
  328.  
  329. else
  330. {
  331. Console.WriteLine("Please select an account to transfer to, or press 0 to cancel.");
  332. accountTo = SelectAccount();
  333. }
  334.  
  335. if (accountTo == QuitOption)
  336. {
  337. MainMenu();
  338. }
  339.  
  340. else if (accountFrom == accountTo)
  341. {
  342. Console.WriteLine("You have selected the same account. Please try again.");
  343. Transfer();
  344. }
  345.  
  346. else
  347. {
  348. TransferAmount(accountFrom, accountTo);
  349. }
  350. }
  351.  
  352. /// <summary>
  353. /// Completes transfer action. User enters a value which is then checked for invalid amounts,
  354. /// if amount is valid, completes transfer and charges bank fees where applicable, then
  355. /// returns to MainMenu method upon user key entry.
  356. /// pre: two valid accounts have been selected from Transfer method
  357. /// post: prints completed transfer information and returns to main menu upon user key entry.
  358. /// </summary>
  359. /// <param name="fromAccount">Account to transfer money from, recieved from Transfer method</param>
  360. /// <param name="toAccount">Account to transfer money to, recieved from Transfer method</param>
  361. static void TransferAmount(int fromAccount, int toAccount)
  362. {
  363. decimal amount = 0;
  364. decimal bankFee = 0.50m;
  365. decimal minimumBalance = 10.0m;
  366. Console.WriteLine("How much would you like to transfer? Enter amount or press 0 to cancel.");
  367. Console.Write("Enter amount: ");
  368. amount = Convert.ToDecimal(Console.ReadLine());
  369.  
  370. if (amount == 0)
  371. {
  372. MainMenu();
  373. }
  374.  
  375. // Checks to see if transfer would take account below minimum balance.
  376. else if (amount > Convert.ToDecimal(Balances[fromAccount]) - minimumBalance)
  377. {
  378. Console.WriteLine("Insufficient funds, please choose again.");
  379. TransferAmount(fromAccount, toAccount);
  380. }
  381.  
  382. // If not below minimum balance, completes transfer and subtracts fee.
  383. else
  384. {
  385. Balances[fromAccount] = ((Balances[fromAccount] - amount) - bankFee);
  386. Balances[toAccount] = (Balances[toAccount] + amount);
  387. Console.WriteLine("new balance for {0} is {1}",
  388. Accounts[fromAccount], Balances[fromAccount]);
  389. Console.WriteLine("new balance for {0} is {1}",
  390. Accounts[toAccount], Balances[toAccount]);
  391. }
  392. } // End TransferAmount()
  393.  
  394.  
  395.  
  396. }
  397. }
Add Comment
Please, Sign In to add comment