Advertisement
Guest User

Untitled

a guest
May 29th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 34.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define FILE_DATA "clients.dat"
  7. #define NAME_SIZE 12
  8. #define SURNAME_SIZE 31
  9. #define ADRESS_SIZE 51
  10. #define PESEL_SIZE 13
  11. #define NUMBER_SIZE 15
  12. #define CHOICE_SIZE 6
  13. #define MAX_BALANCE 100000000
  14.  
  15. typedef struct {
  16.     int acc_number;
  17.     char name[NAME_SIZE];
  18.     char surname[SURNAME_SIZE];
  19.     char adress[ADRESS_SIZE];
  20.     char PESEL[PESEL_SIZE];
  21.     char ind_number[NUMBER_SIZE];
  22.     float balance;
  23. } custAccount;
  24. int confirm ();
  25. void createAccount(void);
  26. void list(void);
  27. void searchMenu(void);
  28. void searchCode(char choice[], int option);
  29. void display(custAccount cust);
  30. void transfer(void);
  31. void payOrTake(int option);
  32. int isStringCorrect(char string[]);
  33. int isAdressCorrect(char adress[]);
  34. int isFloat(char num[]);
  35. int isInt(char num[]);
  36. int isAccount(int a_no, int a_max);
  37. int decision(void);
  38. int matchFound(custAccount cust);
  39.  
  40. int main() {
  41.     int option;
  42.     int is;
  43.     char temp[CHOICE_SIZE];
  44.  
  45.     do{
  46.         do {
  47.             system("clear");
  48.             printf("=== BANKING SYSTEM ===\n"
  49.                     "== Choose one option from below:\n"
  50.                     "== 1. Create account\n"
  51.                     "== 2. Find account\n"
  52.                     "== 3. List all accounts\n"
  53.                     "== 4. Money transfer\n"
  54.                     "== 5. Make a payment\n"
  55.                     "== 6. Make a withdrawal\n"
  56.                     "== 7. Close system\n"
  57.                     "\t>> ");
  58.             fgets(temp, CHOICE_SIZE, stdin);
  59.             option = atoi(temp);
  60.             is = isInt(temp);
  61.         } while (option < 1 || option > 7 || !is);
  62.        
  63.         if(option >= 1 && option <= 7 && is) {
  64.             switch(option) {
  65.             case 1:
  66.                 printf("=== ACCOUNT CREATOR ===\n");
  67.                 createAccount();
  68.                 break;
  69.             case 2:
  70.                 searchMenu();
  71.                 break;
  72.             case 3:
  73.                 printf("=== ACCOUNT LIST ===\n");
  74.                 list();
  75.                 break;
  76.             case 4:
  77.                 transfer();
  78.                 break;
  79.             case 5:
  80.                 payOrTake(1);
  81.                 break;
  82.             case 6:
  83.                 payOrTake(2);
  84.                 break;
  85.             case 7:
  86.                 printf("=== EXIT ===\n");
  87.                 break;
  88.             default:
  89.                 printf("=== WRONG OPTION ===\n");
  90.                 break;
  91.             }
  92.         } else printf("=== Chose 1-7 ===\n");
  93.        
  94.     } while(option != 7);
  95.    
  96.     return 0;
  97. }
  98.  
  99. void createAccount() {
  100.     int acc_number;
  101.     int is_big;
  102.     char temp_sum[NUMBER_SIZE];
  103.     custAccount cust;
  104.    
  105.     FILE *fp = NULL;
  106.     fp = fopen(FILE_DATA, "ab");
  107.     if(fp == NULL) {
  108.         perror("=== Error opening the file ===");
  109.         exit(EXIT_FAILURE);
  110.     }
  111.     fseek(fp, 0, SEEK_END);
  112.     acc_number = ftell(fp) / sizeof(cust);
  113.     printf("= Give details of the account no. %d:\n", acc_number + 1);
  114.    
  115.     /*name*/
  116.     do {
  117.         printf("= Name: ");
  118.         fgets(cust.name, NAME_SIZE, stdin);
  119.     } while(!isStringCorrect(cust.name));
  120.     if(strlen(cust.name) == NAME_SIZE - 1) {
  121.             while(getchar() != '\n');
  122.             cust.name[NAME_SIZE - 2] = '\n';
  123.             printf("== String oversize, text will be shorten to %s\n", cust.name);
  124.     }
  125.    
  126.    
  127.     /*surname*/
  128.     do {
  129.         printf("= Surname: ");
  130.         fgets(cust.surname, SURNAME_SIZE, stdin);
  131.     } while(!isStringCorrect(cust.surname));
  132.     if(strlen(cust.surname) == SURNAME_SIZE - 1) {
  133.         while(getchar() != '\n');
  134.         cust.surname[SURNAME_SIZE - 2] = '\n';
  135.         printf("== String oversize, text will be shorten to %s\n", cust.surname);
  136.     }
  137.    
  138.  
  139.     /*adress*/
  140.     do {
  141.         printf("= Adress: ");
  142.         fgets(cust.adress, ADRESS_SIZE, stdin);
  143.     } while(!isAdressCorrect(cust.adress));
  144.     if(strlen(cust.adress) == ADRESS_SIZE - 1) {
  145.         while(getchar() != '\n');
  146.         cust.adress[ADRESS_SIZE - 2] = '\n';
  147.         printf("== String oversize, text will be shorten to %s\n", cust.adress);
  148.     }
  149.  
  150.  
  151.     /*PESEL*/
  152.     do {
  153.         printf("= PESEL: ");
  154.         fgets(cust.PESEL, PESEL_SIZE, stdin);
  155.     } while(!isInt(cust.PESEL));
  156.     if(strlen(cust.PESEL) >= PESEL_SIZE - 1) {
  157.         while(getchar() != '\n');
  158.         cust.PESEL[PESEL_SIZE - 2] = '\n';
  159.         printf("== Number oversize, number will be shorten to %s", cust.PESEL);
  160.     }
  161.    
  162.     /*id number*/
  163.     do {
  164.         printf("= ID number: ");
  165.         fgets(cust.ind_number, NUMBER_SIZE, stdin);
  166.     } while(!isInt(cust.ind_number));
  167.     if(strlen(cust.ind_number) >= NUMBER_SIZE - 1) {
  168.         while(getchar() != '\n');
  169.         cust.ind_number[NUMBER_SIZE - 2] = '\n';
  170.         printf("== Number oversize, number will be shorten to %s", cust.ind_number);
  171.     }
  172.    
  173.     /*balance*/
  174.     do {
  175.         is_big = 0;
  176.         printf("= Balance: ");
  177.         fgets(temp_sum, NUMBER_SIZE, stdin);
  178.         if(strlen(temp_sum) == NUMBER_SIZE - 1) {
  179.             while(getchar() != '\n');
  180.             temp_sum[NUMBER_SIZE - 2] = '\n';
  181.         }
  182.         cust.balance = atof(temp_sum);
  183.         if(cust.balance > MAX_BALANCE) {
  184.             printf("== Maksimum balance is %d, try to type again\n", MAX_BALANCE);
  185.             is_big = 1;
  186.         }
  187.        
  188.     } while(!isFloat(temp_sum) || is_big);
  189.    
  190.     if (decision() == 0)
  191.         return;
  192.  
  193.     cust.acc_number = acc_number + 1;
  194.    
  195.     if(fwrite(&cust, sizeof(cust), 1, fp) != 1) {
  196.         perror("=== Error writing to databse ===");
  197.         exit(EXIT_FAILURE);
  198.     }
  199.    
  200.     if(fclose(fp)) {
  201.         perror("=== Error closing the file ===");
  202.         exit(EXIT_FAILURE);
  203.     }
  204.     printf("=== ACCOUNT CREATED ===");
  205.     getchar();
  206.    
  207. }
  208.  
  209. void list() {
  210.     FILE *fp = NULL;
  211.     custAccount cust_list;
  212.     fp = fopen(FILE_DATA, "rb");
  213.     if(fp == NULL) {
  214.         perror("=== Error opening the file ===");
  215.         exit(EXIT_FAILURE);
  216.     }
  217.     fseek(fp, 0, SEEK_SET);
  218.     while(feof(fp) == 0 && fread(&cust_list, sizeof(cust_list), 1, fp)) {
  219.         display(cust_list);
  220.     }
  221.    
  222.     if(fclose(fp)) {
  223.         perror("=== Error closing the file ===");
  224.         exit(EXIT_FAILURE);
  225.     }
  226.     printf("=== Press any key... ===");
  227.     getchar();
  228. }
  229.  
  230. void searchMenu() {
  231.     int option;
  232.     int is;
  233.     char temp[CHOICE_SIZE];
  234.     char choice[ADRESS_SIZE];
  235.    
  236.     do {
  237.         do {
  238.             system("clear");
  239.             printf("=== ACCOUNT SEARCH ===\n");
  240.             printf("== You want to search by:\n");
  241.             printf("== 1. Name\n");
  242.             printf("== 2. Surname\n");
  243.             printf("== 3. Adress\n");
  244.             printf("== 4. PESEL\n");
  245.             printf("== 5. Account number\n");
  246.             printf("== 6. Balance\n");
  247.             printf("== 7. ID number\n");
  248.             printf("== 8. Back to menu\n");
  249.             printf("\t>> ");
  250.             fgets(temp, CHOICE_SIZE, stdin);
  251.             option = atoi(temp);
  252.             is = isInt(temp);
  253.         } while(option < 1 || option > 8 || !is);
  254.         if(option >= 1 && option <= 8 && is) {
  255.             switch(option) {
  256.                 case 1:
  257.                     do {
  258.                         printf("= Type name to search >> ");
  259.                         fgets(choice, NAME_SIZE, stdin);
  260.                     } while(!isStringCorrect(choice));
  261.                     if(strlen(choice) == NAME_SIZE - 1) {
  262.                         while(getchar() != '\n');
  263.                         choice[NAME_SIZE - 2] = '\n';
  264.                         printf("== String oversize, text will be shorten to %s", choice);
  265.                     }
  266.                    
  267.                     searchCode(choice, option);
  268.                     break;
  269.                 case 2:
  270.                     do {
  271.                         printf("= Type surname to search >> ");
  272.                         fgets(choice, SURNAME_SIZE, stdin);
  273.                     } while(!isStringCorrect(choice));
  274.                     if(strlen(choice) == SURNAME_SIZE - 1) {
  275.                         while(getchar() != '\n');
  276.                         choice[SURNAME_SIZE - 2] = '\n';
  277.                         printf("== String oversize, text will be shorten to %s", choice);
  278.                     }
  279.                    
  280.                     searchCode(choice, option);
  281.                     break;
  282.                 case 3:
  283.                     do {
  284.                         printf("= Type adress to search >> ");
  285.                         fgets(choice, ADRESS_SIZE, stdin);
  286.                     } while(!isStringCorrect(choice));
  287.                     if(strlen(choice) == ADRESS_SIZE - 1) {
  288.                         while(getchar() != '\n');
  289.                         choice[ADRESS_SIZE - 2] = '\n';
  290.                         printf("== String oversize, text will be shorten to %s", choice);
  291.                     }
  292.                    
  293.                     searchCode(choice, option);
  294.                     break;
  295.                 case 4:
  296.                     do {
  297.                         printf("= Type PESEL to search >> ");
  298.                         fgets(choice, PESEL_SIZE, stdin);
  299.                     }while(!isInt(choice));
  300.                    
  301.                     searchCode(choice, option);
  302.                     break;
  303.  
  304.                 case 5:
  305.                     do {
  306.                         printf("= Type acccount no. to search >> ");
  307.                         fgets(choice, 5, stdin);
  308.                     }while(!isInt(choice));
  309.                    
  310.                     searchCode(choice, option);
  311.                     break;
  312.                 case 6:
  313.                     do {
  314.                         printf("= Give balance to search >> ");
  315.                         fgets(choice, NUMBER_SIZE, stdin);
  316.                     }while(!isFloat(choice));
  317.                    
  318.                     searchCode(choice, option);
  319.                     break;
  320.                 case 7:
  321.                     do {
  322.                         printf("= Type ID no. to search >> ");
  323.                         fgets(choice, NUMBER_SIZE, stdin);
  324.                     }while(!isInt(choice));
  325.                    
  326.                     searchCode(choice, option);
  327.                     break;
  328.             }
  329.         }
  330.     } while(option != 8);
  331. }
  332.  
  333. void searchCode(char choice[], int option) {
  334.     custAccount cust;
  335.     int is_match = 0;
  336.    
  337.     FILE *fp = NULL;
  338.     fp = fopen(FILE_DATA, "rb");
  339.     if(fp == NULL) {
  340.         perror("=== Error opening the file ===");
  341.         exit(EXIT_FAILURE);
  342.     }
  343.     fseek(fp, 0, SEEK_SET);
  344.     while(feof(fp) == 0 && fread(&cust, sizeof(cust), 1, fp)) {
  345.        
  346.         if(option == 1 && (strcmp(choice, cust.name) == 0)) {
  347.             is_match = matchFound(cust);
  348.         }
  349.         else if(option == 2 && (strcmp(choice, cust.surname) == 0)) {
  350.             is_match = matchFound(cust);
  351.         }
  352.         else if(option == 3 && strcmp(choice, cust.adress)) {
  353.             is_match = matchFound(cust);
  354.         }
  355.         else if(option == 4 && (strcmp(choice, cust.PESEL) == 0)) {
  356.             is_match = matchFound(cust);
  357.         }
  358.         else if(option == 5 && (atoi(choice) == cust.acc_number)) {
  359.             is_match = matchFound(cust);
  360.         }
  361.         else if(option == 6 && (atoi(choice) == cust.balance)) {
  362.             is_match = matchFound(cust);
  363.         }
  364.         else if(option == 7 && (strcmp(choice, cust.ind_number) == 0)) {
  365.             is_match = matchFound(cust);
  366.         }
  367.     }
  368.    
  369.     if(fclose(fp)) {
  370.         perror("=== Error closing the file ===");
  371.         exit(EXIT_FAILURE);
  372.     }
  373.     if(!is_match) {
  374.         printf("=== No matches found ===\n");
  375.     }
  376.     printf("=== Press any key... ===");
  377.     getchar();
  378.    
  379. }
  380.  
  381. void transfer() {
  382.     int acc_no;
  383.     int acc_from;
  384.     int acc_to;
  385.     int d = 0;
  386.     float to_spend;
  387.     float cash;
  388.     char temp[NUMBER_SIZE];
  389.     custAccount cust;
  390.    
  391.     FILE *fp = NULL;
  392.     fp = fopen(FILE_DATA, "r+b");
  393.     if(fp == NULL) {
  394.         perror("=== Error opening the file ===");
  395.         exit(EXIT_FAILURE);
  396.     }
  397.     fseek(fp, 0, SEEK_END);
  398.     acc_no = ftell(fp) / sizeof(cust);
  399.     rewind(fp);
  400.  
  401.     system("clear");
  402.     printf("=== MONEY TRANSFER ===\n");
  403.  
  404.     do {
  405.         printf("= From which account you want to transfer money? [1-%d] >> ", acc_no);
  406.         fgets(temp, NUMBER_SIZE, stdin);
  407.         acc_from = atoi(temp);
  408.     } while(!isInt(temp) || !isAccount(acc_from, acc_no));
  409.    
  410.     do {
  411.         printf("= To which account you want to transfer money? [1-%d] >> ", acc_no);
  412.         fgets(temp, NUMBER_SIZE, stdin);
  413.         acc_to = atoi(temp);
  414.     } while(!isInt(temp) || !isAccount(acc_to, acc_no) || acc_to == acc_from);
  415.  
  416.  
  417.     fseek(fp, (acc_from - 1) * sizeof(cust), SEEK_SET);
  418.     fread(&cust, sizeof(cust), 1, fp);
  419.     to_spend = cust.balance;
  420.  
  421.     printf("== On the account no.%d there is %.2f PLN\n", acc_from, cust.balance);
  422.    
  423.     if(cust.balance == 0) {
  424.         printf("== Account is out of cash\n");
  425.         /*printf("=== TRANSFER TERMINATED ===");*/
  426.     }
  427.     else {
  428.         do {
  429.             printf("= How much money You want to transfer? [Type only numbers and keep the range]\n\t>> ");
  430.             fgets(temp, NUMBER_SIZE, stdin);
  431.             cash = atof(temp);
  432.         } while(!isFloat(temp) || cash > to_spend);
  433.    
  434.         d = decision();
  435.         if(d) {
  436.             cust.balance -= cash;
  437.             fseek(fp, (acc_from - 1) * sizeof(cust), SEEK_SET);
  438.             fwrite(&cust, sizeof(cust), 1, fp);
  439.             printf("== On the account no.%d there is %.2f PLN now\n", acc_from, cust.balance);
  440.            
  441.             fseek(fp, (acc_to - 1) * sizeof(cust), SEEK_SET);
  442.             fread(&cust, sizeof(cust), 1, fp);
  443.             cust.balance += cash;
  444.             fseek(fp, (acc_to - 1) * sizeof(cust), SEEK_SET);
  445.             fwrite(&cust, sizeof(cust), 1, fp);
  446.             printf("== And on the account no.%d there is %.2f PLN now\n", acc_to, cust.balance);
  447.         }
  448.     }
  449.    
  450.    
  451.     if(fclose(fp)) {
  452.         perror("=== Error closing the file ===");
  453.         exit(EXIT_FAILURE);
  454.     }
  455.     if(d) {
  456.         printf("=== TRANSFER COMPLETED ===\n");
  457.     }
  458.     else {
  459.         printf("=== TRANSFER TERMINATED ===\n");
  460.     }
  461.     printf("=== Press any key... ===");
  462.     getchar();
  463. }
  464.  
  465. void payOrTake(int option) {
  466.     int acc_all;
  467.     int acc_no;
  468.     int d;
  469.     float cash;
  470.     float to_take;
  471.     char temp[NUMBER_SIZE];
  472.     custAccount cust;
  473.    
  474.     FILE *fp = NULL;
  475.    
  476.     if(option == 1) printf("=== PAYMENT ===\n");
  477.     if(option == 2) printf("=== WITHDRAWAL ===\n");
  478.     system("clear");
  479.  
  480.     fp = fopen(FILE_DATA, "r+b");
  481.     if(fp == NULL) {
  482.         perror("=== Error opening the file ===");
  483.         exit(EXIT_FAILURE);
  484.     }
  485.     fseek(fp, 0, SEEK_END);
  486.     acc_all = ftell(fp) / sizeof(cust);
  487.     rewind(fp);
  488.     do {
  489.         printf("= Which account to execute [1-%d] >> ", acc_all);
  490.         fgets(temp, NUMBER_SIZE, stdin);
  491.         acc_no = atoi(temp);
  492.     } while(!isInt(temp) || !isAccount(acc_no, acc_all));
  493.    
  494.     fseek(fp, (acc_no - 1) *  sizeof(cust), SEEK_SET);
  495.     fread(&cust, sizeof(cust), 1, fp);
  496.     to_take = cust.balance;
  497.     printf("== There is %.2f PLN on the account\n", to_take);
  498.    
  499.     if(option == 1) {
  500.         do {
  501.             printf("= Amount of money to pay in >> ");
  502.             fgets(temp, NUMBER_SIZE, stdin);
  503.             cash = atof(temp);
  504.         } while(!isFloat(temp));
  505.         d = decision();
  506.         if(d) cust.balance += cash;
  507.     }
  508.     if(option == 2 && to_take > 0) {
  509.         do {
  510.             printf("= Amount of money to take >> ");
  511.             fgets(temp, NUMBER_SIZE, stdin);
  512.             cash = atof(temp);
  513.             if(cash > to_take) {
  514.                 printf("== You can withdraw up to %.2f PLN\n", to_take);
  515.             }
  516.         } while(!isFloat(temp) || cash > to_take);
  517.         d = decision();
  518.         if(d) cust.balance -= cash;
  519.     }
  520.     if(option == 2 && to_take <= 0) {
  521.         printf("== Account is out of cash\n");
  522.         d = 0;
  523.     }
  524.    
  525.     fseek(fp, (acc_no - 1) *  sizeof(cust), SEEK_SET);
  526.     fwrite(&cust, sizeof(cust), 1, fp);
  527.    
  528.     if(fclose(fp)) {
  529.         perror("=== Error closing the file ===");
  530.         exit(EXIT_FAILURE);
  531.     }
  532.     if(d) {
  533.         printf("=== TRANSACTION COMPLETED ===\n");
  534.     }
  535.     else {
  536.         printf("=== TRANSACTION TERMINATED ===\n");
  537.     }
  538.     printf("=== Press any key... ===");
  539.     getchar();
  540. }
  541.  
  542. void display(custAccount cust_list) {
  543.         printf("=== Account number %d.\n", cust_list.acc_number);
  544.         printf("== Name:\t%s", cust_list.name);
  545.         printf("== Surname:\t%s", cust_list.surname);
  546.         printf("== Adress:\t%s", cust_list.adress);
  547.         printf("== PESEL:\t%s", cust_list.PESEL);
  548.         printf("== ID number:\t%s", cust_list.ind_number);
  549.         printf("== Balance:\t%.2f PLN", cust_list.balance);
  550.         printf("\n\n");
  551. }
  552.  
  553. int isStringCorrect(char string[]) {
  554.     int i;
  555.    
  556.     for(i = 0; i < strlen(string) - 1; i++) {
  557.         if(!isalpha(string[i])) {
  558.             printf("=== Type letters only ===\n");
  559.             return 0;
  560.         }
  561.     }
  562.     return 1;
  563. }
  564.  
  565. int isAdressCorrect(char adress[]) {
  566.     int i;
  567.    
  568.     for(i = 0; i < strlen(adress) - 1; i++) {
  569.         if(!isalpha(adress[i]) && adress[i] != '/' && adress[i] != ' ' && !isdigit(adress[i])) {
  570.             printf("=== Type leterrs, digits, space and '/' only ===\n");
  571.             return 0;
  572.         }
  573.     }
  574.     return 1;
  575. }
  576.  
  577. int isFloat(char num[]) {
  578.     int i;
  579.    
  580.     for(i = 0; i < strlen(num) - 1; i++) {
  581.         if(!isdigit(num[i]) && num[i] != '.') {
  582.             printf("=== Not a proper number (positive numbers only) ===\n");
  583.             return 0;
  584.         }
  585.     }
  586.     return 1;
  587.    
  588. }
  589.  
  590. int isInt(char num[]) {
  591.     int i;
  592.    
  593.     for(i = 0; i < strlen(num) - 1; i++) {
  594.         if(!isdigit(num[i])) {
  595.             printf("=== Not a number (positive numbers only) ===\n");
  596.             return 0;
  597.         }
  598.     }
  599.     return 1;
  600.    
  601. }
  602.  
  603. int isAccount(int a_no, int a_max) {
  604.     if(a_no > a_max || a_no < 0) {
  605.         printf("=== Account number out of range, type from 1-%d ===\n", a_max);
  606.         return 0;
  607.     }
  608.     return 1;
  609. }
  610.  
  611. int decision() {
  612.     char c;
  613.     do {
  614.         printf("= Are you sure you want to execute? [y/n] >> ");
  615.         c = getchar();
  616.     } while(c != 'y' && c != 'Y' && c != 'n' && c != 'N');
  617.     getchar();
  618.     if(c == 'y' || c == 'Y') return 1;
  619.     if(c == 'n' || c == 'N') return 0;
  620.     return 0;
  621. }
  622.  
  623. int matchFound(custAccount cust) {
  624.     printf("=== Match found ===\n");
  625.     display(cust);
  626.     return 1;
  627. }
  628.  
  629. int input_string (char *output, int max_size) { //
  630.     int c, i;
  631.     while ( isspace(( c = getchar())));
  632.     ungetc(c, stdin);
  633.     for (i = 0; ( c = getchar()) != '\n' && c != EOF && c != '\0' && i < max_size - 1; ++i) {
  634.         output[i] = (char) c;
  635.     }
  636.     if ( c != '\n' && c != EOF && c != '\0')
  637.         while (( c = getchar()) != '\n' && c != EOF && c != '\0' );
  638.     output[i] = '\0';
  639.     return i;
  640. }
  641. RAW Paste Data
  642.  
  643. #include <stdio.h>
  644. #include <stdlib.h>
  645. #include <string.h>
  646. #include <ctype.h>
  647.  
  648. #define FILE_DATA "clients.dat"
  649. #define NAME_SIZE 12
  650. #define SURNAME_SIZE 31
  651. #define ADRESS_SIZE 51
  652. #define PESEL_SIZE 13
  653. #define NUMBER_SIZE 15
  654. #define CHOICE_SIZE 6
  655. #define MAX_BALANCE 100000000
  656.  
  657. typedef struct {
  658.     int acc_number;
  659.     char name[NAME_SIZE];
  660.     char surname[SURNAME_SIZE];
  661.     char adress[ADRESS_SIZE];
  662.     char PESEL[PESEL_SIZE];
  663.     char ind_number[NUMBER_SIZE];
  664.     float balance;
  665. } custAccount;
  666. int confirm ();
  667. void createAccount(void);
  668. void list(void);
  669. void searchMenu(void);
  670. void searchCode(char choice[], int option);
  671. void display(custAccount cust);
  672. void transfer(void);
  673. void payOrTake(int option);
  674. int isStringCorrect(char string[]);
  675. int isAdressCorrect(char adress[]);
  676. int isFloat(char num[]);
  677. int isInt(char num[]);
  678. int isAccount(int a_no, int a_max);
  679. int decision(void);
  680. int matchFound(custAccount cust);
  681.  
  682. int main() {
  683.     int option;
  684.     int is;
  685.     char temp[CHOICE_SIZE];
  686.  
  687.     do{
  688.         do {
  689.             system("clear");
  690.             printf("=== BANKING SYSTEM ===\n"
  691.                     "== Choose one option from below:\n"
  692.                     "== 1. Create account\n"
  693.                     "== 2. Find account\n"
  694.                     "== 3. List all accounts\n"
  695.                     "== 4. Money transfer\n"
  696.                     "== 5. Make a payment\n"
  697.                     "== 6. Make a withdrawal\n"
  698.                     "== 7. Close system\n"
  699.                     "\t>> ");
  700.             fgets(temp, CHOICE_SIZE, stdin);
  701.             option = atoi(temp);
  702.             is = isInt(temp);
  703.         } while (option < 1 || option > 7 || !is);
  704.        
  705.         if(option >= 1 && option <= 7 && is) {
  706.             switch(option) {
  707.             case 1:
  708.                 printf("=== ACCOUNT CREATOR ===\n");
  709.                 createAccount();
  710.                 break;
  711.             case 2:
  712.                 searchMenu();
  713.                 break;
  714.             case 3:
  715.                 printf("=== ACCOUNT LIST ===\n");
  716.                 list();
  717.                 break;
  718.             case 4:
  719.                 transfer();
  720.                 break;
  721.             case 5:
  722.                 payOrTake(1);
  723.                 break;
  724.             case 6:
  725.                 payOrTake(2);
  726.                 break;
  727.             case 7:
  728.                 printf("=== EXIT ===\n");
  729.                 break;
  730.             default:
  731.                 printf("=== WRONG OPTION ===\n");
  732.                 break;
  733.             }
  734.         } else printf("=== Chose 1-7 ===\n");
  735.        
  736.     } while(option != 7);
  737.    
  738.     return 0;
  739. }
  740.  
  741. void createAccount() {
  742.     int acc_number;
  743.     int is_big;
  744.     char temp_sum[NUMBER_SIZE];
  745.     custAccount cust;
  746.    
  747.     FILE *fp = NULL;
  748.     fp = fopen(FILE_DATA, "ab");
  749.     if(fp == NULL) {
  750.         perror("=== Error opening the file ===");
  751.         exit(EXIT_FAILURE);
  752.     }
  753.     fseek(fp, 0, SEEK_END);
  754.     acc_number = ftell(fp) / sizeof(cust);
  755.     printf("= Give details of the account no. %d:\n", acc_number + 1);
  756.    
  757.     /*name*/
  758.     do {
  759.         printf("= Name: ");
  760.         fgets(cust.name, NAME_SIZE, stdin);
  761.     } while(!isStringCorrect(cust.name));
  762.     if(strlen(cust.name) == NAME_SIZE - 1) {
  763.             while(getchar() != '\n');
  764.             cust.name[NAME_SIZE - 2] = '\n';
  765.             printf("== String oversize, text will be shorten to %s\n", cust.name);
  766.     }
  767.    
  768.    
  769.     /*surname*/
  770.     do {
  771.         printf("= Surname: ");
  772.         fgets(cust.surname, SURNAME_SIZE, stdin);
  773.     } while(!isStringCorrect(cust.surname));
  774.     if(strlen(cust.surname) == SURNAME_SIZE - 1) {
  775.         while(getchar() != '\n');
  776.         cust.surname[SURNAME_SIZE - 2] = '\n';
  777.         printf("== String oversize, text will be shorten to %s\n", cust.surname);
  778.     }
  779.    
  780.  
  781.     /*adress*/
  782.     do {
  783.         printf("= Adress: ");
  784.         fgets(cust.adress, ADRESS_SIZE, stdin);
  785.     } while(!isAdressCorrect(cust.adress));
  786.     if(strlen(cust.adress) == ADRESS_SIZE - 1) {
  787.         while(getchar() != '\n');
  788.         cust.adress[ADRESS_SIZE - 2] = '\n';
  789.         printf("== String oversize, text will be shorten to %s\n", cust.adress);
  790.     }
  791.  
  792.  
  793.     /*PESEL*/
  794.     do {
  795.         printf("= PESEL: ");
  796.         fgets(cust.PESEL, PESEL_SIZE, stdin);
  797.     } while(!isInt(cust.PESEL));
  798.     if(strlen(cust.PESEL) >= PESEL_SIZE - 1) {
  799.         while(getchar() != '\n');
  800.         cust.PESEL[PESEL_SIZE - 2] = '\n';
  801.         printf("== Number oversize, number will be shorten to %s", cust.PESEL);
  802.     }
  803.    
  804.     /*id number*/
  805.     do {
  806.         printf("= ID number: ");
  807.         fgets(cust.ind_number, NUMBER_SIZE, stdin);
  808.     } while(!isInt(cust.ind_number));
  809.     if(strlen(cust.ind_number) >= NUMBER_SIZE - 1) {
  810.         while(getchar() != '\n');
  811.         cust.ind_number[NUMBER_SIZE - 2] = '\n';
  812.         printf("== Number oversize, number will be shorten to %s", cust.ind_number);
  813.     }
  814.    
  815.     /*balance*/
  816.     do {
  817.         is_big = 0;
  818.         printf("= Balance: ");
  819.         fgets(temp_sum, NUMBER_SIZE, stdin);
  820.         if(strlen(temp_sum) == NUMBER_SIZE - 1) {
  821.             while(getchar() != '\n');
  822.             temp_sum[NUMBER_SIZE - 2] = '\n';
  823.         }
  824.         cust.balance = atof(temp_sum);
  825.         if(cust.balance > MAX_BALANCE) {
  826.             printf("== Maksimum balance is %d, try to type again\n", MAX_BALANCE);
  827.             is_big = 1;
  828.         }
  829.        
  830.     } while(!isFloat(temp_sum) || is_big);
  831.    
  832.     if (decision() == 0)
  833.         return;
  834.  
  835.     cust.acc_number = acc_number + 1;
  836.    
  837.     if(fwrite(&cust, sizeof(cust), 1, fp) != 1) {
  838.         perror("=== Error writing to databse ===");
  839.         exit(EXIT_FAILURE);
  840.     }
  841.    
  842.     if(fclose(fp)) {
  843.         perror("=== Error closing the file ===");
  844.         exit(EXIT_FAILURE);
  845.     }
  846.     printf("=== ACCOUNT CREATED ===");
  847.     getchar();
  848.    
  849. }
  850.  
  851. void list() {
  852.     FILE *fp = NULL;
  853.     custAccount cust_list;
  854.     fp = fopen(FILE_DATA, "rb");
  855.     if(fp == NULL) {
  856.         perror("=== Error opening the file ===");
  857.         exit(EXIT_FAILURE);
  858.     }
  859.     fseek(fp, 0, SEEK_SET);
  860.     while(feof(fp) == 0 && fread(&cust_list, sizeof(cust_list), 1, fp)) {
  861.         display(cust_list);
  862.     }
  863.    
  864.     if(fclose(fp)) {
  865.         perror("=== Error closing the file ===");
  866.         exit(EXIT_FAILURE);
  867.     }
  868.     printf("=== Press any key... ===");
  869.     getchar();
  870. }
  871.  
  872. void searchMenu() {
  873.     int option;
  874.     int is;
  875.     char temp[CHOICE_SIZE];
  876.     char choice[ADRESS_SIZE];
  877.    
  878.     do {
  879.         do {
  880.             system("clear");
  881.             printf("=== ACCOUNT SEARCH ===\n");
  882.             printf("== You want to search by:\n");
  883.             printf("== 1. Name\n");
  884.             printf("== 2. Surname\n");
  885.             printf("== 3. Adress\n");
  886.             printf("== 4. PESEL\n");
  887.             printf("== 5. Account number\n");
  888.             printf("== 6. Balance\n");
  889.             printf("== 7. ID number\n");
  890.             printf("== 8. Back to menu\n");
  891.             printf("\t>> ");
  892.             fgets(temp, CHOICE_SIZE, stdin);
  893.             option = atoi(temp);
  894.             is = isInt(temp);
  895.         } while(option < 1 || option > 8 || !is);
  896.         if(option >= 1 && option <= 8 && is) {
  897.             switch(option) {
  898.                 case 1:
  899.                     do {
  900.                         printf("= Type name to search >> ");
  901.                         fgets(choice, NAME_SIZE, stdin);
  902.                     } while(!isStringCorrect(choice));
  903.                     if(strlen(choice) == NAME_SIZE - 1) {
  904.                         while(getchar() != '\n');
  905.                         choice[NAME_SIZE - 2] = '\n';
  906.                         printf("== String oversize, text will be shorten to %s", choice);
  907.                     }
  908.                    
  909.                     searchCode(choice, option);
  910.                     break;
  911.                 case 2:
  912.                     do {
  913.                         printf("= Type surname to search >> ");
  914.                         fgets(choice, SURNAME_SIZE, stdin);
  915.                     } while(!isStringCorrect(choice));
  916.                     if(strlen(choice) == SURNAME_SIZE - 1) {
  917.                         while(getchar() != '\n');
  918.                         choice[SURNAME_SIZE - 2] = '\n';
  919.                         printf("== String oversize, text will be shorten to %s", choice);
  920.                     }
  921.                    
  922.                     searchCode(choice, option);
  923.                     break;
  924.                 case 3:
  925.                     do {
  926.                         printf("= Type adress to search >> ");
  927.                         fgets(choice, ADRESS_SIZE, stdin);
  928.                     } while(!isStringCorrect(choice));
  929.                     if(strlen(choice) == ADRESS_SIZE - 1) {
  930.                         while(getchar() != '\n');
  931.                         choice[ADRESS_SIZE - 2] = '\n';
  932.                         printf("== String oversize, text will be shorten to %s", choice);
  933.                     }
  934.                    
  935.                     searchCode(choice, option);
  936.                     break;
  937.                 case 4:
  938.                     do {
  939.                         printf("= Type PESEL to search >> ");
  940.                         fgets(choice, PESEL_SIZE, stdin);
  941.                     }while(!isInt(choice));
  942.                    
  943.                     searchCode(choice, option);
  944.                     break;
  945.  
  946.                 case 5:
  947.                     do {
  948.                         printf("= Type acccount no. to search >> ");
  949.                         fgets(choice, 5, stdin);
  950.                     }while(!isInt(choice));
  951.                    
  952.                     searchCode(choice, option);
  953.                     break;
  954.                 case 6:
  955.                     do {
  956.                         printf("= Give balance to search >> ");
  957.                         fgets(choice, NUMBER_SIZE, stdin);
  958.                     }while(!isFloat(choice));
  959.                    
  960.                     searchCode(choice, option);
  961.                     break;
  962.                 case 7:
  963.                     do {
  964.                         printf("= Type ID no. to search >> ");
  965.                         fgets(choice, NUMBER_SIZE, stdin);
  966.                     }while(!isInt(choice));
  967.                    
  968.                     searchCode(choice, option);
  969.                     break;
  970.             }
  971.         }
  972.     } while(option != 8);
  973. }
  974.  
  975. void searchCode(char choice[], int option) {
  976.     custAccount cust;
  977.     int is_match = 0;
  978.    
  979.     FILE *fp = NULL;
  980.     fp = fopen(FILE_DATA, "rb");
  981.     if(fp == NULL) {
  982.         perror("=== Error opening the file ===");
  983.         exit(EXIT_FAILURE);
  984.     }
  985.     fseek(fp, 0, SEEK_SET);
  986.     while(feof(fp) == 0 && fread(&cust, sizeof(cust), 1, fp)) {
  987.        
  988.         if(option == 1 && (strcmp(choice, cust.name) == 0)) {
  989.             is_match = matchFound(cust);
  990.         }
  991.         else if(option == 2 && (strcmp(choice, cust.surname) == 0)) {
  992.             is_match = matchFound(cust);
  993.         }
  994.         else if(option == 3 && strcmp(choice, cust.adress)) {
  995.             is_match = matchFound(cust);
  996.         }
  997.         else if(option == 4 && (strcmp(choice, cust.PESEL) == 0)) {
  998.             is_match = matchFound(cust);
  999.         }
  1000.         else if(option == 5 && (atoi(choice) == cust.acc_number)) {
  1001.             is_match = matchFound(cust);
  1002.         }
  1003.         else if(option == 6 && (atoi(choice) == cust.balance)) {
  1004.             is_match = matchFound(cust);
  1005.         }
  1006.         else if(option == 7 && (strcmp(choice, cust.ind_number) == 0)) {
  1007.             is_match = matchFound(cust);
  1008.         }
  1009.     }
  1010.    
  1011.     if(fclose(fp)) {
  1012.         perror("=== Error closing the file ===");
  1013.         exit(EXIT_FAILURE);
  1014.     }
  1015.     if(!is_match) {
  1016.         printf("=== No matches found ===\n");
  1017.     }
  1018.     printf("=== Press any key... ===");
  1019.     getchar();
  1020.    
  1021. }
  1022.  
  1023. void transfer() {
  1024.     int acc_no;
  1025.     int acc_from;
  1026.     int acc_to;
  1027.     int d = 0;
  1028.     float to_spend;
  1029.     float cash;
  1030.     char temp[NUMBER_SIZE];
  1031.     custAccount cust;
  1032.    
  1033.     FILE *fp = NULL;
  1034.     fp = fopen(FILE_DATA, "r+b");
  1035.     if(fp == NULL) {
  1036.         perror("=== Error opening the file ===");
  1037.         exit(EXIT_FAILURE);
  1038.     }
  1039.     fseek(fp, 0, SEEK_END);
  1040.     acc_no = ftell(fp) / sizeof(cust);
  1041.     rewind(fp);
  1042.  
  1043.     system("clear");
  1044.     printf("=== MONEY TRANSFER ===\n");
  1045.  
  1046.     do {
  1047.         printf("= From which account you want to transfer money? [1-%d] >> ", acc_no);
  1048.         fgets(temp, NUMBER_SIZE, stdin);
  1049.         acc_from = atoi(temp);
  1050.     } while(!isInt(temp) || !isAccount(acc_from, acc_no));
  1051.    
  1052.     do {
  1053.         printf("= To which account you want to transfer money? [1-%d] >> ", acc_no);
  1054.         fgets(temp, NUMBER_SIZE, stdin);
  1055.         acc_to = atoi(temp);
  1056.     } while(!isInt(temp) || !isAccount(acc_to, acc_no) || acc_to == acc_from);
  1057.  
  1058.  
  1059.     fseek(fp, (acc_from - 1) * sizeof(cust), SEEK_SET);
  1060.     fread(&cust, sizeof(cust), 1, fp);
  1061.     to_spend = cust.balance;
  1062.  
  1063.     printf("== On the account no.%d there is %.2f PLN\n", acc_from, cust.balance);
  1064.    
  1065.     if(cust.balance == 0) {
  1066.         printf("== Account is out of cash\n");
  1067.         /*printf("=== TRANSFER TERMINATED ===");*/
  1068.     }
  1069.     else {
  1070.         do {
  1071.             printf("= How much money You want to transfer? [Type only numbers and keep the range]\n\t>> ");
  1072.             fgets(temp, NUMBER_SIZE, stdin);
  1073.             cash = atof(temp);
  1074.         } while(!isFloat(temp) || cash > to_spend);
  1075.    
  1076.         d = decision();
  1077.         if(d) {
  1078.             cust.balance -= cash;
  1079.             fseek(fp, (acc_from - 1) * sizeof(cust), SEEK_SET);
  1080.             fwrite(&cust, sizeof(cust), 1, fp);
  1081.             printf("== On the account no.%d there is %.2f PLN now\n", acc_from, cust.balance);
  1082.            
  1083.             fseek(fp, (acc_to - 1) * sizeof(cust), SEEK_SET);
  1084.             fread(&cust, sizeof(cust), 1, fp);
  1085.             cust.balance += cash;
  1086.             fseek(fp, (acc_to - 1) * sizeof(cust), SEEK_SET);
  1087.             fwrite(&cust, sizeof(cust), 1, fp);
  1088.             printf("== And on the account no.%d there is %.2f PLN now\n", acc_to, cust.balance);
  1089.         }
  1090.     }
  1091.    
  1092.    
  1093.     if(fclose(fp)) {
  1094.         perror("=== Error closing the file ===");
  1095.         exit(EXIT_FAILURE);
  1096.     }
  1097.     if(d) {
  1098.         printf("=== TRANSFER COMPLETED ===\n");
  1099.     }
  1100.     else {
  1101.         printf("=== TRANSFER TERMINATED ===\n");
  1102.     }
  1103.     printf("=== Press any key... ===");
  1104.     getchar();
  1105. }
  1106.  
  1107. void payOrTake(int option) {
  1108.     int acc_all;
  1109.     int acc_no;
  1110.     int d;
  1111.     float cash;
  1112.     float to_take;
  1113.     char temp[NUMBER_SIZE];
  1114.     custAccount cust;
  1115.    
  1116.     FILE *fp = NULL;
  1117.    
  1118.     if(option == 1) printf("=== PAYMENT ===\n");
  1119.     if(option == 2) printf("=== WITHDRAWAL ===\n");
  1120.     system("clear");
  1121.  
  1122.     fp = fopen(FILE_DATA, "r+b");
  1123.     if(fp == NULL) {
  1124.         perror("=== Error opening the file ===");
  1125.         exit(EXIT_FAILURE);
  1126.     }
  1127.     fseek(fp, 0, SEEK_END);
  1128.     acc_all = ftell(fp) / sizeof(cust);
  1129.     rewind(fp);
  1130.     do {
  1131.         printf("= Which account to execute [1-%d] >> ", acc_all);
  1132.         fgets(temp, NUMBER_SIZE, stdin);
  1133.         acc_no = atoi(temp);
  1134.     } while(!isInt(temp) || !isAccount(acc_no, acc_all));
  1135.    
  1136.     fseek(fp, (acc_no - 1) *  sizeof(cust), SEEK_SET);
  1137.     fread(&cust, sizeof(cust), 1, fp);
  1138.     to_take = cust.balance;
  1139.     printf("== There is %.2f PLN on the account\n", to_take);
  1140.    
  1141.     if(option == 1) {
  1142.         do {
  1143.             printf("= Amount of money to pay in >> ");
  1144.             fgets(temp, NUMBER_SIZE, stdin);
  1145.             cash = atof(temp);
  1146.         } while(!isFloat(temp));
  1147.         d = decision();
  1148.         if(d) cust.balance += cash;
  1149.     }
  1150.     if(option == 2 && to_take > 0) {
  1151.         do {
  1152.             printf("= Amount of money to take >> ");
  1153.             fgets(temp, NUMBER_SIZE, stdin);
  1154.             cash = atof(temp);
  1155.             if(cash > to_take) {
  1156.                 printf("== You can withdraw up to %.2f PLN\n", to_take);
  1157.             }
  1158.         } while(!isFloat(temp) || cash > to_take);
  1159.         d = decision();
  1160.         if(d) cust.balance -= cash;
  1161.     }
  1162.     if(option == 2 && to_take <= 0) {
  1163.         printf("== Account is out of cash\n");
  1164.         d = 0;
  1165.     }
  1166.    
  1167.     fseek(fp, (acc_no - 1) *  sizeof(cust), SEEK_SET);
  1168.     fwrite(&cust, sizeof(cust), 1, fp);
  1169.    
  1170.     if(fclose(fp)) {
  1171.         perror("=== Error closing the file ===");
  1172.         exit(EXIT_FAILURE);
  1173.     }
  1174.     if(d) {
  1175.         printf("=== TRANSACTION COMPLETED ===\n");
  1176.     }
  1177.     else {
  1178.         printf("=== TRANSACTION TERMINATED ===\n");
  1179.     }
  1180.     printf("=== Press any key... ===");
  1181.     getchar();
  1182. }
  1183.  
  1184. void display(custAccount cust_list) {
  1185.         printf("=== Account number %d.\n", cust_list.acc_number);
  1186.         printf("== Name:\t%s", cust_list.name);
  1187.         printf("== Surname:\t%s", cust_list.surname);
  1188.         printf("== Adress:\t%s", cust_list.adress);
  1189.         printf("== PESEL:\t%s", cust_list.PESEL);
  1190.         printf("== ID number:\t%s", cust_list.ind_number);
  1191.         printf("== Balance:\t%.2f PLN", cust_list.balance);
  1192.         printf("\n\n");
  1193. }
  1194.  
  1195. int isStringCorrect(char string[]) {
  1196.     int i;
  1197.    
  1198.     for(i = 0; i < strlen(string) - 1; i++) {
  1199.         if(!isalpha(string[i])) {
  1200.             printf("=== Type letters only ===\n");
  1201.             return 0;
  1202.         }
  1203.     }
  1204.     return 1;
  1205. }
  1206.  
  1207. int isAdressCorrect(char adress[]) {
  1208.     int i;
  1209.    
  1210.     for(i = 0; i < strlen(adress) - 1; i++) {
  1211.         if(!isalpha(adress[i]) && adress[i] != '/' && adress[i] != ' ' && !isdigit(adress[i])) {
  1212.             printf("=== Type leterrs, digits, space and '/' only ===\n");
  1213.             return 0;
  1214.         }
  1215.     }
  1216.     return 1;
  1217. }
  1218.  
  1219. int isFloat(char num[]) {
  1220.     int i;
  1221.    
  1222.     for(i = 0; i < strlen(num) - 1; i++) {
  1223.         if(!isdigit(num[i]) && num[i] != '.') {
  1224.             printf("=== Not a proper number (positive numbers only) ===\n");
  1225.             return 0;
  1226.         }
  1227.     }
  1228.     return 1;
  1229.    
  1230. }
  1231.  
  1232. int isInt(char num[]) {
  1233.     int i;
  1234.    
  1235.     for(i = 0; i < strlen(num) - 1; i++) {
  1236.         if(!isdigit(num[i])) {
  1237.             printf("=== Not a number (positive numbers only) ===\n");
  1238.             return 0;
  1239.         }
  1240.     }
  1241.     return 1;
  1242.    
  1243. }
  1244.  
  1245. int isAccount(int a_no, int a_max) {
  1246.     if(a_no > a_max || a_no < 0) {
  1247.         printf("=== Account number out of range, type from 1-%d ===\n", a_max);
  1248.         return 0;
  1249.     }
  1250.     return 1;
  1251. }
  1252.  
  1253. int decision() {
  1254.     char c;
  1255.     do {
  1256.         printf("= Are you sure you want to execute? [y/n] >> ");
  1257.         c = getchar();
  1258.     } while(c != 'y' && c != 'Y' && c != 'n' && c != 'N');
  1259.     getchar();
  1260.     if(c == 'y' || c == 'Y') return 1;
  1261.     if(c == 'n' || c == 'N') return 0;
  1262.     return 0;
  1263. }
  1264.  
  1265. int matchFound(custAccount cust) {
  1266.     printf("=== Match found ===\n");
  1267.     display(cust);
  1268.     return 1;
  1269. }
  1270.  
  1271. int input_string (char *output, int max_size) { //
  1272.     int c, i;
  1273.     while ( isspace(( c = getchar())));
  1274.     ungetc(c, stdin);
  1275.     for (i = 0; ( c = getchar()) != '\n' && c != EOF && c != '\0' && i < max_size - 1; ++i) {
  1276.         output[i] = (char) c;
  1277.     }
  1278.     if ( c != '\n' && c != EOF && c != '\0')
  1279.         while (( c = getchar()) != '\n' && c != EOF && c != '\0' );
  1280.     output[i] = '\0';
  1281.     return i;
  1282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement