Advertisement
Guest User

Untitled

a guest
Nov 24th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. #define FILENAME "loginDetails.txt"
  7. char buffer[255];
  8.  
  9. /* Create Account*/
  10. typedef struct{
  11.     char *firstname;
  12.     char *lastname;
  13.     char *address;
  14.     int mobileNum;
  15.     int dateOfBirth[3];
  16.     int accountNum;
  17. } userAccount;
  18.  
  19.  
  20. /* Login Account */
  21. typedef struct{
  22.     char *userName;
  23.     char *password;
  24. } Login;
  25.  
  26.  
  27. int mainMenu(void);
  28. bool login(void);
  29. void createAccount(void);
  30. void changeUserDets(userAccount *userDetails);
  31. float deposit(int balance);
  32. float withdraw(int balance);
  33. void viewCustomerBankInfo(userAccount *account);
  34. int editCustomerBankInfo(userAccount *customDetails);
  35. int delCustomerBankInfo(userAccount *customDetails);
  36.  
  37.  
  38. int main(void){
  39.    
  40.     int choice;
  41.     bool checkLoggedin;
  42.  
  43.     printf("---Bᴀɴᴋ Mᴀɴᴀɢᴇᴍᴇɴᴛ Sʏsᴛᴇᴍ---\n");
  44.  
  45.  
  46.     choice = mainMenu();
  47.     do{
  48.          switch(choice){
  49.             case 1:
  50.                 checkLoggedin = login();
  51.                 if(checkLoggedin == true){
  52.                     choice = mainMenu();
  53.                     break;
  54.                 }else{ printf("Incorrect Username/Password!\n"); }
  55.                 break;
  56.             case 2:
  57.                 createAccount();
  58.                 choice = mainMenu();
  59.                 break;
  60.             default:
  61.                 exit(0);
  62.                 break;
  63.         }
  64.     }while(choice != 3);
  65.  
  66.     return 0;
  67. }
  68.  
  69. int mainMenu(){
  70.  
  71.     char userChoice[10];
  72.  
  73.     printf("1 - Login\n");
  74.     printf("2 - Create Account\n");
  75.     printf("3 - Exit\n");
  76.     printf("Enter a option\n");
  77.  
  78.     fgets(userChoice, 10,  stdin);
  79.     int choice = atoi(userChoice);
  80.  
  81.     if(choice == 1){ return choice; }
  82.     else if(choice == 2){ return choice;}
  83.     else{ choice;}
  84.  
  85. }
  86.  
  87. //Get the login details and store in a file.
  88. bool login(){
  89.  
  90.  
  91.     FILE *loginDetails;
  92.     char userNameFile[100];
  93.     char passwordFile[100];
  94.  
  95.     loginDetails = fopen("loginDetails.txt", "r");
  96.  
  97.     if(!loginDetails){
  98.         perror(FILENAME);
  99.         return EXIT_FAILURE;
  100.     }
  101.  
  102.     Login *loginAccount = malloc(sizeof(*loginAccount));
  103.     loginAccount->userName = malloc(sizeof(*loginAccount->userName));
  104.     loginAccount->password = malloc(sizeof(*loginAccount->password));
  105.  
  106.     printf("Enter in UserName: ");
  107.     fgets(buffer, sizeof(buffer), stdin);
  108.     sscanf(buffer, "%s", loginAccount->userName);
  109.  
  110.     printf("Enter in PassWord: ");
  111.     fgets(buffer, sizeof(buffer), stdin);
  112.     sscanf(buffer, "%s", loginAccount->password);
  113.    
  114.     //Check to see if the credntials are correct.
  115.     while(!feof(loginDetails)){
  116.         fscanf(loginDetails, "%s %s",userNameFile, passwordFile);
  117.         if((strcmp(userNameFile, loginAccount->userName)) == 0 && (strcmp(passwordFile, loginAccount->password)) == 0){
  118.             printf("Thanks For Banking with Us!\n");
  119.             return true;
  120.             break;
  121.         }
  122.  
  123.     }
  124.  
  125.     free(loginAccount);
  126.     free(loginAccount->userName);
  127.     free(loginAccount->password);
  128.  
  129.     return false;
  130. }
  131.  
  132.  
  133.  
  134.  
  135. //User account creation stored on a file.
  136. void createAccount(){
  137.  
  138.     //Opening file for writing.
  139.     FILE *createDetails;
  140.     createDetails = fopen("createDetails.txt", "w");
  141.  
  142.     //Check if fopen() failed.
  143.     if(!createDetails){
  144.         perror("createDetails.txt");
  145.         return;
  146.     }
  147.  
  148.     userAccount *createUser = malloc(sizeof(*createUser));
  149.     Login *loginDetails = malloc(sizeof(*loginDetails));
  150.  
  151.     printf("--Account Details--\n");
  152.    
  153.     printf("Enter in FirstName: ");
  154.     createUser->firstname = malloc(sizeof(*createUser->firstname));
  155.     fgets(buffer, sizeof(buffer), stdin);
  156.     sscanf(buffer, "%s", createUser->firstname);
  157.  
  158.     printf("Enter in LastName: ");
  159.     createUser->lastname = malloc(sizeof(*createUser->lastname));
  160.     fgets(buffer ,sizeof(buffer), stdin);
  161.     sscanf(buffer, "%s", createUser->lastname);
  162.  
  163.     //Add firstname and lastname to file.
  164.     fprintf(createDetails, "%s %s", createUser->firstname, createUser->lastname);
  165.     fflush(createDetails);
  166.     free(createUser->firstname);
  167.     free(createUser->lastname);
  168.  
  169.     printf("Enter in Address: ");
  170.     createUser->address = malloc(sizeof(*createUser->address));
  171.     fgets(buffer, sizeof(buffer), stdin);
  172.     sscanf(buffer, "%s", createUser->address);
  173.  
  174.     printf("Enter in Mobile Number: ");
  175.     createUser->mobileNum = malloc(sizeof(*createUser->mobileNum));
  176.     fgets(buffer, sizeof(buffer), stdin);
  177.     sscanf(buffer, "%s", createUser->mobileNum);
  178.  
  179.     //Add the address and mobile number
  180.     fprintf(createDetails, "%s %d", createUser->address, createUser->mobileNum);
  181.     fflush(createDetails);
  182.     free(createUser->address);
  183.     free(createUser->mobileNum);
  184.  
  185.  
  186.     //Add the date of birth and account number.
  187.     printf("Enter Date Of Birth: ");
  188.     createUser->dateOfBirth = malloc(sizeof(*createUser->dateOfBirth));
  189.  
  190.  
  191.     return;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement