Moortiii

C - ATM Machine

Oct 15th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "bank.h"
  4.  
  5. // TO:DO Create an account automatically for the user upon creating a new user
  6. // TO:DO Handle spaces inside cell-phone input in createAccount();
  7. // TO:DO Add area codes for cell phone
  8.  
  9. // Create the main bank object
  10. void createBank( struct Bank *bank ) {
  11.     strcpy(bank->name, "Bank of Morten");
  12.     strcpy(bank->owner, "Morten Hauge");
  13.     bank->customer_index = 0;
  14. }
  15.  
  16. // Add a new customer to the bank
  17. void createCustomer(struct Bank *bank) {
  18.     struct Customer customer;
  19.     char input[35];
  20.     char first_name[35], last_name[35], address[35], city[35];
  21.     long int cell;
  22.     int zip;
  23.  
  24.     // Fill out necessary customer information
  25.     printf("You are creating a new customer.\n\n");
  26.  
  27.     printf("Enter first name:\n\n");
  28.     fgets(input, sizeof(input), stdin);
  29.     input[strlen(input) - 1] = '\0';
  30.     sscanf(input, "%s", first_name);
  31.     strcpy(customer.first_name, first_name);
  32.  
  33.     printf("Enter last name:\n\n");
  34.     fgets(input, sizeof(input), stdin);
  35.     input[strlen(input) - 1] = '\0';
  36.     sscanf(input, "%s", last_name);
  37.     strcpy(customer.last_name, last_name);
  38.  
  39.     printf("Enter address:\n\n");
  40.     fgets(input, sizeof(input), stdin);
  41.     input[strlen(input) - 1] = '\0';
  42.     sscanf(input, "%30[^\n]", address);
  43.     strcpy(customer.address, address);
  44.  
  45.     printf("Enter zip:\n\n");
  46.     fgets(input, sizeof(input), stdin);
  47.     input[strlen(input) - 1] = '\0';
  48.     sscanf(input, "%i", &zip);
  49.     customer.zip = zip;
  50.  
  51.     printf("Enter city:\n\n");
  52.     fgets(input, sizeof(input), stdin);
  53.     input[strlen(input) - 1] = '\0';
  54.     sscanf(input, "%s", city);
  55.     strcpy(customer.city, city);
  56.  
  57.     printf("Enter cell number:\n\n");
  58.     fgets(input, sizeof(input), stdin);
  59.     input[strlen(input) - 1] = '\0';
  60.     sscanf(input, "%li", &cell);
  61.     customer.cell = cell;
  62.  
  63.     customer.num_accounts = 0;
  64.     printf("\n"); // For cleaner formatting
  65.  
  66.     bank->customers[bank->customer_index] = customer;
  67.     bank->customer_index += 1; // The next customer now gets a unique ID
  68. }
  69.  
  70. // Add a new bank account to an already existing user, each user can have 10 accounts
  71. void createAccount( struct Bank *bank ) {
  72.     printf("You have chosen to create a new account\n\n");
  73.     listCustomers( bank );
  74.     printf("Please enter a customer id:\n\n");
  75.     int id = promptForId();
  76.     char input[35], name[35];
  77.  
  78.     if(id > bank->customer_index) {
  79.         printf("There is no customer with that ID");
  80.     } else {
  81.         struct Account account;
  82.         struct Customer customer = bank->customers[id];
  83.         printf("Enter account name:\n\n");
  84.         fgets(input, sizeof(input), stdin);
  85.         input[strlen(input) - 1] = '\0';
  86.         sscanf(input, "%30[^\n]", name);
  87.  
  88.         strcpy(account.account_type, name);
  89.         account.owner_id = customer.id;
  90.         account.balance = 1000;
  91.         customer.accounts[customer.num_accounts] = account;
  92.         bank->customers[id] = customer;
  93.         customer.num_accounts += 1;
  94.  
  95.         printf("\n"); // For cleaner formatting
  96.     }
  97. }
  98.  
  99. // Print information about a given user
  100. void printCustomer( struct Bank *bank ) {
  101.     printf("You have chosen to print information about a customer\n\n");
  102.     listCustomers( bank );
  103.     printf("Please enter a customer id:\n\n");
  104.     int id = promptForId();
  105.  
  106.     if(id >= bank->customer_index) {
  107.         printf("There is no customer with that id\n");
  108.     } else {
  109.         struct Customer customer = bank->customers[id];
  110.         printf("First name: %s\n", customer.first_name);
  111.         printf("Last name: %s\n", customer.last_name);
  112.         printf("Mobile: %li\n", customer.cell);
  113.         printf("Address: %s\n", customer.address);
  114.         printf("Zip Code: %i\n", customer.zip);
  115.         printf("\n"); // For cleaner formatting
  116.     }
  117. }
  118.  
  119. // Print information about a given account
  120. void printAccount( struct Bank *bank ) {
  121.     printf("You have chosen to print information about an account\n\n");
  122.     listCustomers( bank );
  123.     printf("Please enter a customer id:\n\n");
  124.     int id = promptForId();
  125.  
  126.     if(id >= bank->customer_index) {
  127.         printf("There is no customer with that id\n");
  128.     } else {
  129.         struct Customer customer = bank->customers[id];
  130.         printf("Please enter an account number\n\n");
  131.         int account_id = promptForId();
  132.         if(account_id > customer.num_accounts) {
  133.             printf("There is no account with that id\n\n");
  134.         } else {
  135.             struct Account account = customer.accounts[account_id];
  136.             printf("Account type: %s\n", account.account_type);
  137.             printf("Balance: %g\n", account.balance);
  138.             printf("Account owner: %s %s", customer.first_name, customer.last_name);
  139.             printf("\n"); // For cleaner formatting
  140.         }
  141.     }
  142. }
  143.  
  144. // List all current customers of the bank
  145. void listCustomers( struct Bank *bank ) {
  146.     printf("Customers:\n");
  147.     for(int i = 0; i < bank->customer_index; i++) {
  148.         struct Customer customer = bank->customers[i];
  149.         printf("[%i]: %s %s\n", i, customer.first_name, customer.last_name);
  150.     }
  151.     printf("\n");
  152. }
  153.  
  154. // Ask the user to enter an id and returns it - can be used for customer id, account id etc.
  155. int promptForId() {
  156.     char id_input[20];
  157.     int id = 0;
  158.     fgets(id_input, sizeof(id_input), stdin);
  159.     id_input[strlen(id_input) - 1] = '\0';
  160.     sscanf(id_input, "%i", &id);
  161.     return id;
  162. }
  163.  
  164. // Main loop of the program - might consider having no arguments and creating the bank inside it directly
  165. void bankChoiceLoop( struct Bank *bank ) {
  166.     createBank ( bank );
  167.     int user_answer;
  168.     char user_input[15];
  169.  
  170.     do {
  171.         printf("1. Create new customer\n");
  172.         printf("2. Create new account\n");
  173.         printf("3. Show customer information\n");
  174.         printf("4. Show account information\n");
  175.         printf("5. Exit the program\n");
  176.         printf("\n"); // For cleaner formatting
  177.         fgets(user_input, sizeof(user_input), stdin);
  178.         user_input[strlen(user_input) - 1] = '\0';
  179.         sscanf(user_input, "%i", &user_answer);
  180.  
  181.         switch(user_answer) {
  182.             case 1:
  183.                 createCustomer( bank );
  184.                 break;
  185.             case 2:
  186.                 createAccount( bank );
  187.                 break;
  188.             case 3:
  189.                 printCustomer( bank );
  190.                 break;
  191.             case 4:
  192.                 printAccount( bank );
  193.                 break;
  194.             case 5:
  195.                 printf("You have chosen to exit the program\n");
  196.                 break;
  197.             default: // Handle wrong input from the user
  198.                 printf("Something went wrong, please review your input\n\n");
  199.                 continue;
  200.         }
  201.     } while(user_answer != 5);
  202. }
Advertisement
Add Comment
Please, Sign In to add comment