Guest User

Untitled

a guest
Feb 22nd, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. #include <stdio.h> // This defines the standard IO functions
  2. #include <stdlib.h> // This is for atoi
  3. #include <unistd.h> // This is for sleep
  4. #include <string.h> // This is for strlen
  5. #include "atm.h" // This defines the account structure
  6.  
  7. struct account accounts[MAXACCTS]; // Array of account structures
  8.  
  9. /*
  10. * Declare functions used in the program.
  11. * This is REQUIRED for functions which return non-integer values.
  12. */
  13. int load_account_data();
  14. int save_account_data();
  15. struct account *validate_account();
  16.  
  17. // This is the entry point for the atm program.
  18. int
  19. main(int argc, char **argv)
  20. {
  21. // Declare local variables
  22. int acct_in; // User account number
  23. float amount; // User amount for deposit or withdraw
  24. int fields; // Number of fields processed by scanf
  25. int error; // Function returns error code, 0 = OK
  26. char input[80]; // Data from the keyboard
  27. int keep_going; // Flag - keep the loops going
  28. struct account *my_acct; // Struct pointer for the current record
  29. int pin_in; // User PIN
  30.  
  31. // 1) Call load_account_data with the structure to load
  32. error = load_account_data(accounts);
  33. if (error) {
  34. printf("Error: Can not open the bank database file.\n");
  35. return 1;
  36. } else if (error >= 2) {
  37. printf("Error: Problems with bank database file.\n");
  38. return 2;
  39. }
  40.  
  41. for (;;) { // person loop
  42. system("cls"); // clears the screen. "clear" replaces "cls" in Linux/UNIX
  43. printf("Welcome to the ATM Machine!\n\n");
  44. for (keep_going = 1; keep_going;) { // account loop
  45. printf("Please enter your account number: ");
  46. fgets(input, sizeof(input), stdin);
  47. if(input[0] == '0') return 0;
  48. if(input[0] == 'x') return 0;
  49. acct_in = atoi(input);
  50.  
  51. printf("Please enter your PIN: ");
  52. fgets(input, sizeof(input), stdin);
  53. if(input[0] == '0') return 0;
  54. if(input[0] == 'x') return 0;
  55. pin_in = atoi(input);
  56.  
  57. // validate the account and PIN against the bank data
  58. my_acct = validate_account(accounts, acct_in, pin_in);
  59. if (my_acct == NULL) {
  60. printf("Invalid account or PIN.\n");
  61. printf("Please try again.\n");
  62. } else {
  63. input[0] = '\0'; // Set for the first time through
  64. for (; keep_going;) { // transaction loop
  65. system("cls"); // Windows clear screen command
  66. printf("Welcome %s %s!\n\n",
  67. my_acct->first_name, my_acct->last_name);
  68. printf("Please choose from the following options:\n\n");
  69. printf("\tB - Balance\n");
  70. printf("\tD - Deposit\n");
  71. printf("\tW - Withdraw\n");
  72. printf("\tE - Exit\n\n");
  73. switch(input[0]) {
  74. case '\0': // This handles the first time through
  75. break;
  76. case 'B':
  77. case 'b': // Balance Logic
  78. printf("Your balance is $%.2f\n",
  79. my_acct->balance);
  80. break;
  81. case 'D':
  82. case 'd': // Deposit logic
  83. printf("Amount: $");
  84. fgets(input, sizeof(input), stdin);
  85. fields = sscanf(input, "%f", &amount);
  86. if (fields <= 0)
  87. printf("Error: Non-numeric amount\n");
  88. else if (amount < 0)
  89. printf("Error: Can NOT transact negative numbers.\n");
  90. else if (amount > 100000)
  91. printf("Error: Can not transact more than $100,000.\n");
  92. else {
  93. my_acct->balance += amount;
  94. printf("Your new balance is $%.2f\n",
  95. my_acct->balance);
  96. }
  97. break;
  98. case 'W':
  99. case 'w': // Withdraw logic
  100. printf("Amount: $");
  101. fgets(input, sizeof(input), stdin);
  102. fields = sscanf(input, "%f", &amount);
  103. if (fields <= 0)
  104. printf("Error: Non-numeric amount\n");
  105. else if (amount < 0)
  106. printf("Error: Can NOT transact negative numbers.\n");
  107. else if (amount > 100000)
  108. printf("Error: Can not transact more than $100,000.\n");
  109. else if (amount > my_acct->balance)
  110. printf("Error: Insufficient funds.\n");
  111. else {
  112. my_acct->balance -= amount;
  113. printf("Your new balance is $%.2f\n\n",
  114. my_acct->balance);
  115. }
  116. break;
  117. case 'E':
  118. case 'e':
  119. case 'X':
  120. case 'x': // Exit logic
  121. printf("Thank you for using the ATM machine.\n\n");
  122. // terminate the transaction and account loops
  123. keep_going = 0;
  124. error = save_account_data(accounts);
  125. if (error >= 1) {
  126. printf("Can not save the database file.\n");
  127. return 1;
  128. }
  129. // This is 2000 milliseconds, not seconds
  130. sleep(2000); // Wait for the user to see the previous message
  131. break;
  132. default:
  133. printf("Unknown command %c", input[0]);
  134. } // switch
  135. if (keep_going) { // Don't ask for input for exit
  136. printf("\nPlease enter your transaction: ");
  137. fgets(input, sizeof(input), stdin);
  138. }
  139. } // Transaction loop
  140. } // If-else
  141. } // Account loop
  142. } // Person loop
  143. } // Main
  144.  
  145. /*
  146. * Read in the accounts.data file into a structure
  147. * which is passed in as an argument.
  148. */
  149. int
  150. load_account_data(struct account *acct)
  151. {
  152. // Define local variables
  153. int fields;
  154. FILE *file_in;
  155.  
  156. // Open the data file
  157. file_in = fopen("accounts.data", "r");
  158. if (file_in == NULL) return 1;
  159.  
  160. // loop through all the records
  161. for (;; acct++) {
  162. // Read one record into variables
  163. fields = fscanf(file_in, "%s %s %d %d %f",
  164. acct->last_name, acct->first_name,
  165. &acct->acct_number, &acct->pin,
  166. &acct->balance);
  167. // If End Of File, break out of loop
  168. if (fields == EOF) break;
  169. // check for non processed fields
  170. if (fields < 5) return 2;
  171. // check for garbage numbers
  172. // || = logical OR
  173. if (acct->acct_number <= 0 ||
  174. acct->pin <= 0 ||
  175. acct->balance <= 0) return 3;
  176. // check for garbage names
  177. if (strlen(acct->last_name) == 0 ||
  178. strlen(acct->first_name) == 0) return 4;
  179. }
  180. // Mark end of data
  181. acct->pin = 0;
  182. // Close the data file
  183. fclose(file_in);
  184. // Return to the calling function
  185. return 0;
  186. }
  187.  
  188. /*
  189. * Save all the data in the account structure
  190. * which is passed in as an argument.
  191. */
  192. int
  193. save_account_data(struct account *acct)
  194. {
  195. FILE *file_in;
  196.  
  197. // Open the data file
  198. file_in = fopen("accounts.data", "w");
  199. if (file_in == NULL) return 1;
  200.  
  201. // loop through all the records
  202. for (; acct->pin; acct++) {
  203. // Read one record into variables
  204. fprintf(file_in, "%s %s %d %d %6.2f\n",
  205. acct->last_name, acct->first_name,
  206. acct->acct_number, acct->pin,
  207. acct->balance);
  208. }
  209. // Close the data file
  210. fclose(file_in);
  211. // Return to the calling function
  212. return 0;
  213. }
  214.  
  215. /*
  216. * Check each member of the bank data to see if the acct and PIN match
  217. * Return the structure address for a match
  218. * Return NULL for no match
  219. */
  220. struct account *
  221. validate_account(struct account *acct, int acct_in, int pin_in)
  222. {
  223. // loop through all the records
  224. for (; acct->pin; acct++) {
  225. if (acct->acct_number == acct_in &&
  226. acct->pin == pin_in) return(acct);
  227. }
  228. return(NULL);
  229. }
Add Comment
Please, Sign In to add comment