Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define FILE_DATA "clients.dat"
- #define NAME_SIZE 12
- #define SURNAME_SIZE 31
- #define ADRESS_SIZE 51
- #define PESEL_SIZE 13
- #define NUMBER_SIZE 15
- #define CHOICE_SIZE 6
- #define MAX_BALANCE 100000000
- typedef struct {
- int acc_number;
- char name[NAME_SIZE];
- char surname[SURNAME_SIZE];
- char adress[ADRESS_SIZE];
- char PESEL[PESEL_SIZE];
- char ind_number[NUMBER_SIZE];
- float balance;
- } custAccount;
- int confirm ();
- void createAccount(void);
- void list(void);
- void searchMenu(void);
- void searchCode(char choice[], int option);
- void display(custAccount cust);
- void transfer(void);
- void payOrTake(int option);
- int isStringCorrect(char string[]);
- int isAdressCorrect(char adress[]);
- int isFloat(char num[]);
- int isInt(char num[]);
- int isAccount(int a_no, int a_max);
- int decision(void);
- int matchFound(custAccount cust);
- int main() {
- int option;
- int is;
- char temp[CHOICE_SIZE];
- do{
- do {
- system("clear");
- printf("=== BANKING SYSTEM ===\n"
- "== Choose one option from below:\n"
- "== 1. Create account\n"
- "== 2. Find account\n"
- "== 3. List all accounts\n"
- "== 4. Money transfer\n"
- "== 5. Make a payment\n"
- "== 6. Make a withdrawal\n"
- "== 7. Close system\n"
- "\t>> ");
- fgets(temp, CHOICE_SIZE, stdin);
- option = atoi(temp);
- is = isInt(temp);
- } while (option < 1 || option > 7 || !is);
- if(option >= 1 && option <= 7 && is) {
- switch(option) {
- case 1:
- printf("=== ACCOUNT CREATOR ===\n");
- createAccount();
- break;
- case 2:
- searchMenu();
- break;
- case 3:
- printf("=== ACCOUNT LIST ===\n");
- list();
- break;
- case 4:
- transfer();
- break;
- case 5:
- payOrTake(1);
- break;
- case 6:
- payOrTake(2);
- break;
- case 7:
- printf("=== EXIT ===\n");
- break;
- default:
- printf("=== WRONG OPTION ===\n");
- break;
- }
- } else printf("=== Chose 1-7 ===\n");
- } while(option != 7);
- return 0;
- }
- void createAccount() {
- int acc_number;
- int is_big;
- char temp_sum[NUMBER_SIZE];
- custAccount cust;
- FILE *fp = NULL;
- fp = fopen(FILE_DATA, "ab");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_END);
- acc_number = ftell(fp) / sizeof(cust);
- printf("= Give details of the account no. %d:\n", acc_number + 1);
- /*name*/
- do {
- printf("= Name: ");
- fgets(cust.name, NAME_SIZE, stdin);
- } while(!isStringCorrect(cust.name));
- if(strlen(cust.name) == NAME_SIZE - 1) {
- while(getchar() != '\n');
- cust.name[NAME_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s\n", cust.name);
- }
- /*surname*/
- do {
- printf("= Surname: ");
- fgets(cust.surname, SURNAME_SIZE, stdin);
- } while(!isStringCorrect(cust.surname));
- if(strlen(cust.surname) == SURNAME_SIZE - 1) {
- while(getchar() != '\n');
- cust.surname[SURNAME_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s\n", cust.surname);
- }
- /*adress*/
- do {
- printf("= Adress: ");
- fgets(cust.adress, ADRESS_SIZE, stdin);
- } while(!isAdressCorrect(cust.adress));
- if(strlen(cust.adress) == ADRESS_SIZE - 1) {
- while(getchar() != '\n');
- cust.adress[ADRESS_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s\n", cust.adress);
- }
- /*PESEL*/
- do {
- printf("= PESEL: ");
- fgets(cust.PESEL, PESEL_SIZE, stdin);
- } while(!isInt(cust.PESEL));
- if(strlen(cust.PESEL) >= PESEL_SIZE - 1) {
- while(getchar() != '\n');
- cust.PESEL[PESEL_SIZE - 2] = '\n';
- printf("== Number oversize, number will be shorten to %s", cust.PESEL);
- }
- /*id number*/
- do {
- printf("= ID number: ");
- fgets(cust.ind_number, NUMBER_SIZE, stdin);
- } while(!isInt(cust.ind_number));
- if(strlen(cust.ind_number) >= NUMBER_SIZE - 1) {
- while(getchar() != '\n');
- cust.ind_number[NUMBER_SIZE - 2] = '\n';
- printf("== Number oversize, number will be shorten to %s", cust.ind_number);
- }
- /*balance*/
- do {
- is_big = 0;
- printf("= Balance: ");
- fgets(temp_sum, NUMBER_SIZE, stdin);
- if(strlen(temp_sum) == NUMBER_SIZE - 1) {
- while(getchar() != '\n');
- temp_sum[NUMBER_SIZE - 2] = '\n';
- }
- cust.balance = atof(temp_sum);
- if(cust.balance > MAX_BALANCE) {
- printf("== Maksimum balance is %d, try to type again\n", MAX_BALANCE);
- is_big = 1;
- }
- } while(!isFloat(temp_sum) || is_big);
- if (decision() == 0)
- return;
- cust.acc_number = acc_number + 1;
- if(fwrite(&cust, sizeof(cust), 1, fp) != 1) {
- perror("=== Error writing to databse ===");
- exit(EXIT_FAILURE);
- }
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- printf("=== ACCOUNT CREATED ===");
- getchar();
- }
- void list() {
- FILE *fp = NULL;
- custAccount cust_list;
- fp = fopen(FILE_DATA, "rb");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_SET);
- while(feof(fp) == 0 && fread(&cust_list, sizeof(cust_list), 1, fp)) {
- display(cust_list);
- }
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- printf("=== Press any key... ===");
- getchar();
- }
- void searchMenu() {
- int option;
- int is;
- char temp[CHOICE_SIZE];
- char choice[ADRESS_SIZE];
- do {
- do {
- system("clear");
- printf("=== ACCOUNT SEARCH ===\n");
- printf("== You want to search by:\n");
- printf("== 1. Name\n");
- printf("== 2. Surname\n");
- printf("== 3. Adress\n");
- printf("== 4. PESEL\n");
- printf("== 5. Account number\n");
- printf("== 6. Balance\n");
- printf("== 7. ID number\n");
- printf("== 8. Back to menu\n");
- printf("\t>> ");
- fgets(temp, CHOICE_SIZE, stdin);
- option = atoi(temp);
- is = isInt(temp);
- } while(option < 1 || option > 8 || !is);
- if(option >= 1 && option <= 8 && is) {
- switch(option) {
- case 1:
- do {
- printf("= Type name to search >> ");
- fgets(choice, NAME_SIZE, stdin);
- } while(!isStringCorrect(choice));
- if(strlen(choice) == NAME_SIZE - 1) {
- while(getchar() != '\n');
- choice[NAME_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s", choice);
- }
- searchCode(choice, option);
- break;
- case 2:
- do {
- printf("= Type surname to search >> ");
- fgets(choice, SURNAME_SIZE, stdin);
- } while(!isStringCorrect(choice));
- if(strlen(choice) == SURNAME_SIZE - 1) {
- while(getchar() != '\n');
- choice[SURNAME_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s", choice);
- }
- searchCode(choice, option);
- break;
- case 3:
- do {
- printf("= Type adress to search >> ");
- fgets(choice, ADRESS_SIZE, stdin);
- } while(!isStringCorrect(choice));
- if(strlen(choice) == ADRESS_SIZE - 1) {
- while(getchar() != '\n');
- choice[ADRESS_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s", choice);
- }
- searchCode(choice, option);
- break;
- case 4:
- do {
- printf("= Type PESEL to search >> ");
- fgets(choice, PESEL_SIZE, stdin);
- }while(!isInt(choice));
- searchCode(choice, option);
- break;
- case 5:
- do {
- printf("= Type acccount no. to search >> ");
- fgets(choice, 5, stdin);
- }while(!isInt(choice));
- searchCode(choice, option);
- break;
- case 6:
- do {
- printf("= Give balance to search >> ");
- fgets(choice, NUMBER_SIZE, stdin);
- }while(!isFloat(choice));
- searchCode(choice, option);
- break;
- case 7:
- do {
- printf("= Type ID no. to search >> ");
- fgets(choice, NUMBER_SIZE, stdin);
- }while(!isInt(choice));
- searchCode(choice, option);
- break;
- }
- }
- } while(option != 8);
- }
- void searchCode(char choice[], int option) {
- custAccount cust;
- int is_match = 0;
- FILE *fp = NULL;
- fp = fopen(FILE_DATA, "rb");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_SET);
- while(feof(fp) == 0 && fread(&cust, sizeof(cust), 1, fp)) {
- if(option == 1 && (strcmp(choice, cust.name) == 0)) {
- is_match = matchFound(cust);
- }
- else if(option == 2 && (strcmp(choice, cust.surname) == 0)) {
- is_match = matchFound(cust);
- }
- else if(option == 3 && strcmp(choice, cust.adress)) {
- is_match = matchFound(cust);
- }
- else if(option == 4 && (strcmp(choice, cust.PESEL) == 0)) {
- is_match = matchFound(cust);
- }
- else if(option == 5 && (atoi(choice) == cust.acc_number)) {
- is_match = matchFound(cust);
- }
- else if(option == 6 && (atoi(choice) == cust.balance)) {
- is_match = matchFound(cust);
- }
- else if(option == 7 && (strcmp(choice, cust.ind_number) == 0)) {
- is_match = matchFound(cust);
- }
- }
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- if(!is_match) {
- printf("=== No matches found ===\n");
- }
- printf("=== Press any key... ===");
- getchar();
- }
- void transfer() {
- int acc_no;
- int acc_from;
- int acc_to;
- int d = 0;
- float to_spend;
- float cash;
- char temp[NUMBER_SIZE];
- custAccount cust;
- FILE *fp = NULL;
- fp = fopen(FILE_DATA, "r+b");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_END);
- acc_no = ftell(fp) / sizeof(cust);
- rewind(fp);
- system("clear");
- printf("=== MONEY TRANSFER ===\n");
- do {
- printf("= From which account you want to transfer money? [1-%d] >> ", acc_no);
- fgets(temp, NUMBER_SIZE, stdin);
- acc_from = atoi(temp);
- } while(!isInt(temp) || !isAccount(acc_from, acc_no));
- do {
- printf("= To which account you want to transfer money? [1-%d] >> ", acc_no);
- fgets(temp, NUMBER_SIZE, stdin);
- acc_to = atoi(temp);
- } while(!isInt(temp) || !isAccount(acc_to, acc_no) || acc_to == acc_from);
- fseek(fp, (acc_from - 1) * sizeof(cust), SEEK_SET);
- fread(&cust, sizeof(cust), 1, fp);
- to_spend = cust.balance;
- printf("== On the account no.%d there is %.2f PLN\n", acc_from, cust.balance);
- if(cust.balance == 0) {
- printf("== Account is out of cash\n");
- /*printf("=== TRANSFER TERMINATED ===");*/
- }
- else {
- do {
- printf("= How much money You want to transfer? [Type only numbers and keep the range]\n\t>> ");
- fgets(temp, NUMBER_SIZE, stdin);
- cash = atof(temp);
- } while(!isFloat(temp) || cash > to_spend);
- d = decision();
- if(d) {
- cust.balance -= cash;
- fseek(fp, (acc_from - 1) * sizeof(cust), SEEK_SET);
- fwrite(&cust, sizeof(cust), 1, fp);
- printf("== On the account no.%d there is %.2f PLN now\n", acc_from, cust.balance);
- fseek(fp, (acc_to - 1) * sizeof(cust), SEEK_SET);
- fread(&cust, sizeof(cust), 1, fp);
- cust.balance += cash;
- fseek(fp, (acc_to - 1) * sizeof(cust), SEEK_SET);
- fwrite(&cust, sizeof(cust), 1, fp);
- printf("== And on the account no.%d there is %.2f PLN now\n", acc_to, cust.balance);
- }
- }
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- if(d) {
- printf("=== TRANSFER COMPLETED ===\n");
- }
- else {
- printf("=== TRANSFER TERMINATED ===\n");
- }
- printf("=== Press any key... ===");
- getchar();
- }
- void payOrTake(int option) {
- int acc_all;
- int acc_no;
- int d;
- float cash;
- float to_take;
- char temp[NUMBER_SIZE];
- custAccount cust;
- FILE *fp = NULL;
- if(option == 1) printf("=== PAYMENT ===\n");
- if(option == 2) printf("=== WITHDRAWAL ===\n");
- system("clear");
- fp = fopen(FILE_DATA, "r+b");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_END);
- acc_all = ftell(fp) / sizeof(cust);
- rewind(fp);
- do {
- printf("= Which account to execute [1-%d] >> ", acc_all);
- fgets(temp, NUMBER_SIZE, stdin);
- acc_no = atoi(temp);
- } while(!isInt(temp) || !isAccount(acc_no, acc_all));
- fseek(fp, (acc_no - 1) * sizeof(cust), SEEK_SET);
- fread(&cust, sizeof(cust), 1, fp);
- to_take = cust.balance;
- printf("== There is %.2f PLN on the account\n", to_take);
- if(option == 1) {
- do {
- printf("= Amount of money to pay in >> ");
- fgets(temp, NUMBER_SIZE, stdin);
- cash = atof(temp);
- } while(!isFloat(temp));
- d = decision();
- if(d) cust.balance += cash;
- }
- if(option == 2 && to_take > 0) {
- do {
- printf("= Amount of money to take >> ");
- fgets(temp, NUMBER_SIZE, stdin);
- cash = atof(temp);
- if(cash > to_take) {
- printf("== You can withdraw up to %.2f PLN\n", to_take);
- }
- } while(!isFloat(temp) || cash > to_take);
- d = decision();
- if(d) cust.balance -= cash;
- }
- if(option == 2 && to_take <= 0) {
- printf("== Account is out of cash\n");
- d = 0;
- }
- fseek(fp, (acc_no - 1) * sizeof(cust), SEEK_SET);
- fwrite(&cust, sizeof(cust), 1, fp);
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- if(d) {
- printf("=== TRANSACTION COMPLETED ===\n");
- }
- else {
- printf("=== TRANSACTION TERMINATED ===\n");
- }
- printf("=== Press any key... ===");
- getchar();
- }
- void display(custAccount cust_list) {
- printf("=== Account number %d.\n", cust_list.acc_number);
- printf("== Name:\t%s", cust_list.name);
- printf("== Surname:\t%s", cust_list.surname);
- printf("== Adress:\t%s", cust_list.adress);
- printf("== PESEL:\t%s", cust_list.PESEL);
- printf("== ID number:\t%s", cust_list.ind_number);
- printf("== Balance:\t%.2f PLN", cust_list.balance);
- printf("\n\n");
- }
- int isStringCorrect(char string[]) {
- int i;
- for(i = 0; i < strlen(string) - 1; i++) {
- if(!isalpha(string[i])) {
- printf("=== Type letters only ===\n");
- return 0;
- }
- }
- return 1;
- }
- int isAdressCorrect(char adress[]) {
- int i;
- for(i = 0; i < strlen(adress) - 1; i++) {
- if(!isalpha(adress[i]) && adress[i] != '/' && adress[i] != ' ' && !isdigit(adress[i])) {
- printf("=== Type leterrs, digits, space and '/' only ===\n");
- return 0;
- }
- }
- return 1;
- }
- int isFloat(char num[]) {
- int i;
- for(i = 0; i < strlen(num) - 1; i++) {
- if(!isdigit(num[i]) && num[i] != '.') {
- printf("=== Not a proper number (positive numbers only) ===\n");
- return 0;
- }
- }
- return 1;
- }
- int isInt(char num[]) {
- int i;
- for(i = 0; i < strlen(num) - 1; i++) {
- if(!isdigit(num[i])) {
- printf("=== Not a number (positive numbers only) ===\n");
- return 0;
- }
- }
- return 1;
- }
- int isAccount(int a_no, int a_max) {
- if(a_no > a_max || a_no < 0) {
- printf("=== Account number out of range, type from 1-%d ===\n", a_max);
- return 0;
- }
- return 1;
- }
- int decision() {
- char c;
- do {
- printf("= Are you sure you want to execute? [y/n] >> ");
- c = getchar();
- } while(c != 'y' && c != 'Y' && c != 'n' && c != 'N');
- getchar();
- if(c == 'y' || c == 'Y') return 1;
- if(c == 'n' || c == 'N') return 0;
- return 0;
- }
- int matchFound(custAccount cust) {
- printf("=== Match found ===\n");
- display(cust);
- return 1;
- }
- int input_string (char *output, int max_size) { //
- int c, i;
- while ( isspace(( c = getchar())));
- ungetc(c, stdin);
- for (i = 0; ( c = getchar()) != '\n' && c != EOF && c != '\0' && i < max_size - 1; ++i) {
- output[i] = (char) c;
- }
- if ( c != '\n' && c != EOF && c != '\0')
- while (( c = getchar()) != '\n' && c != EOF && c != '\0' );
- output[i] = '\0';
- return i;
- }
- RAW Paste Data
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define FILE_DATA "clients.dat"
- #define NAME_SIZE 12
- #define SURNAME_SIZE 31
- #define ADRESS_SIZE 51
- #define PESEL_SIZE 13
- #define NUMBER_SIZE 15
- #define CHOICE_SIZE 6
- #define MAX_BALANCE 100000000
- typedef struct {
- int acc_number;
- char name[NAME_SIZE];
- char surname[SURNAME_SIZE];
- char adress[ADRESS_SIZE];
- char PESEL[PESEL_SIZE];
- char ind_number[NUMBER_SIZE];
- float balance;
- } custAccount;
- int confirm ();
- void createAccount(void);
- void list(void);
- void searchMenu(void);
- void searchCode(char choice[], int option);
- void display(custAccount cust);
- void transfer(void);
- void payOrTake(int option);
- int isStringCorrect(char string[]);
- int isAdressCorrect(char adress[]);
- int isFloat(char num[]);
- int isInt(char num[]);
- int isAccount(int a_no, int a_max);
- int decision(void);
- int matchFound(custAccount cust);
- int main() {
- int option;
- int is;
- char temp[CHOICE_SIZE];
- do{
- do {
- system("clear");
- printf("=== BANKING SYSTEM ===\n"
- "== Choose one option from below:\n"
- "== 1. Create account\n"
- "== 2. Find account\n"
- "== 3. List all accounts\n"
- "== 4. Money transfer\n"
- "== 5. Make a payment\n"
- "== 6. Make a withdrawal\n"
- "== 7. Close system\n"
- "\t>> ");
- fgets(temp, CHOICE_SIZE, stdin);
- option = atoi(temp);
- is = isInt(temp);
- } while (option < 1 || option > 7 || !is);
- if(option >= 1 && option <= 7 && is) {
- switch(option) {
- case 1:
- printf("=== ACCOUNT CREATOR ===\n");
- createAccount();
- break;
- case 2:
- searchMenu();
- break;
- case 3:
- printf("=== ACCOUNT LIST ===\n");
- list();
- break;
- case 4:
- transfer();
- break;
- case 5:
- payOrTake(1);
- break;
- case 6:
- payOrTake(2);
- break;
- case 7:
- printf("=== EXIT ===\n");
- break;
- default:
- printf("=== WRONG OPTION ===\n");
- break;
- }
- } else printf("=== Chose 1-7 ===\n");
- } while(option != 7);
- return 0;
- }
- void createAccount() {
- int acc_number;
- int is_big;
- char temp_sum[NUMBER_SIZE];
- custAccount cust;
- FILE *fp = NULL;
- fp = fopen(FILE_DATA, "ab");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_END);
- acc_number = ftell(fp) / sizeof(cust);
- printf("= Give details of the account no. %d:\n", acc_number + 1);
- /*name*/
- do {
- printf("= Name: ");
- fgets(cust.name, NAME_SIZE, stdin);
- } while(!isStringCorrect(cust.name));
- if(strlen(cust.name) == NAME_SIZE - 1) {
- while(getchar() != '\n');
- cust.name[NAME_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s\n", cust.name);
- }
- /*surname*/
- do {
- printf("= Surname: ");
- fgets(cust.surname, SURNAME_SIZE, stdin);
- } while(!isStringCorrect(cust.surname));
- if(strlen(cust.surname) == SURNAME_SIZE - 1) {
- while(getchar() != '\n');
- cust.surname[SURNAME_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s\n", cust.surname);
- }
- /*adress*/
- do {
- printf("= Adress: ");
- fgets(cust.adress, ADRESS_SIZE, stdin);
- } while(!isAdressCorrect(cust.adress));
- if(strlen(cust.adress) == ADRESS_SIZE - 1) {
- while(getchar() != '\n');
- cust.adress[ADRESS_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s\n", cust.adress);
- }
- /*PESEL*/
- do {
- printf("= PESEL: ");
- fgets(cust.PESEL, PESEL_SIZE, stdin);
- } while(!isInt(cust.PESEL));
- if(strlen(cust.PESEL) >= PESEL_SIZE - 1) {
- while(getchar() != '\n');
- cust.PESEL[PESEL_SIZE - 2] = '\n';
- printf("== Number oversize, number will be shorten to %s", cust.PESEL);
- }
- /*id number*/
- do {
- printf("= ID number: ");
- fgets(cust.ind_number, NUMBER_SIZE, stdin);
- } while(!isInt(cust.ind_number));
- if(strlen(cust.ind_number) >= NUMBER_SIZE - 1) {
- while(getchar() != '\n');
- cust.ind_number[NUMBER_SIZE - 2] = '\n';
- printf("== Number oversize, number will be shorten to %s", cust.ind_number);
- }
- /*balance*/
- do {
- is_big = 0;
- printf("= Balance: ");
- fgets(temp_sum, NUMBER_SIZE, stdin);
- if(strlen(temp_sum) == NUMBER_SIZE - 1) {
- while(getchar() != '\n');
- temp_sum[NUMBER_SIZE - 2] = '\n';
- }
- cust.balance = atof(temp_sum);
- if(cust.balance > MAX_BALANCE) {
- printf("== Maksimum balance is %d, try to type again\n", MAX_BALANCE);
- is_big = 1;
- }
- } while(!isFloat(temp_sum) || is_big);
- if (decision() == 0)
- return;
- cust.acc_number = acc_number + 1;
- if(fwrite(&cust, sizeof(cust), 1, fp) != 1) {
- perror("=== Error writing to databse ===");
- exit(EXIT_FAILURE);
- }
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- printf("=== ACCOUNT CREATED ===");
- getchar();
- }
- void list() {
- FILE *fp = NULL;
- custAccount cust_list;
- fp = fopen(FILE_DATA, "rb");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_SET);
- while(feof(fp) == 0 && fread(&cust_list, sizeof(cust_list), 1, fp)) {
- display(cust_list);
- }
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- printf("=== Press any key... ===");
- getchar();
- }
- void searchMenu() {
- int option;
- int is;
- char temp[CHOICE_SIZE];
- char choice[ADRESS_SIZE];
- do {
- do {
- system("clear");
- printf("=== ACCOUNT SEARCH ===\n");
- printf("== You want to search by:\n");
- printf("== 1. Name\n");
- printf("== 2. Surname\n");
- printf("== 3. Adress\n");
- printf("== 4. PESEL\n");
- printf("== 5. Account number\n");
- printf("== 6. Balance\n");
- printf("== 7. ID number\n");
- printf("== 8. Back to menu\n");
- printf("\t>> ");
- fgets(temp, CHOICE_SIZE, stdin);
- option = atoi(temp);
- is = isInt(temp);
- } while(option < 1 || option > 8 || !is);
- if(option >= 1 && option <= 8 && is) {
- switch(option) {
- case 1:
- do {
- printf("= Type name to search >> ");
- fgets(choice, NAME_SIZE, stdin);
- } while(!isStringCorrect(choice));
- if(strlen(choice) == NAME_SIZE - 1) {
- while(getchar() != '\n');
- choice[NAME_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s", choice);
- }
- searchCode(choice, option);
- break;
- case 2:
- do {
- printf("= Type surname to search >> ");
- fgets(choice, SURNAME_SIZE, stdin);
- } while(!isStringCorrect(choice));
- if(strlen(choice) == SURNAME_SIZE - 1) {
- while(getchar() != '\n');
- choice[SURNAME_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s", choice);
- }
- searchCode(choice, option);
- break;
- case 3:
- do {
- printf("= Type adress to search >> ");
- fgets(choice, ADRESS_SIZE, stdin);
- } while(!isStringCorrect(choice));
- if(strlen(choice) == ADRESS_SIZE - 1) {
- while(getchar() != '\n');
- choice[ADRESS_SIZE - 2] = '\n';
- printf("== String oversize, text will be shorten to %s", choice);
- }
- searchCode(choice, option);
- break;
- case 4:
- do {
- printf("= Type PESEL to search >> ");
- fgets(choice, PESEL_SIZE, stdin);
- }while(!isInt(choice));
- searchCode(choice, option);
- break;
- case 5:
- do {
- printf("= Type acccount no. to search >> ");
- fgets(choice, 5, stdin);
- }while(!isInt(choice));
- searchCode(choice, option);
- break;
- case 6:
- do {
- printf("= Give balance to search >> ");
- fgets(choice, NUMBER_SIZE, stdin);
- }while(!isFloat(choice));
- searchCode(choice, option);
- break;
- case 7:
- do {
- printf("= Type ID no. to search >> ");
- fgets(choice, NUMBER_SIZE, stdin);
- }while(!isInt(choice));
- searchCode(choice, option);
- break;
- }
- }
- } while(option != 8);
- }
- void searchCode(char choice[], int option) {
- custAccount cust;
- int is_match = 0;
- FILE *fp = NULL;
- fp = fopen(FILE_DATA, "rb");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_SET);
- while(feof(fp) == 0 && fread(&cust, sizeof(cust), 1, fp)) {
- if(option == 1 && (strcmp(choice, cust.name) == 0)) {
- is_match = matchFound(cust);
- }
- else if(option == 2 && (strcmp(choice, cust.surname) == 0)) {
- is_match = matchFound(cust);
- }
- else if(option == 3 && strcmp(choice, cust.adress)) {
- is_match = matchFound(cust);
- }
- else if(option == 4 && (strcmp(choice, cust.PESEL) == 0)) {
- is_match = matchFound(cust);
- }
- else if(option == 5 && (atoi(choice) == cust.acc_number)) {
- is_match = matchFound(cust);
- }
- else if(option == 6 && (atoi(choice) == cust.balance)) {
- is_match = matchFound(cust);
- }
- else if(option == 7 && (strcmp(choice, cust.ind_number) == 0)) {
- is_match = matchFound(cust);
- }
- }
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- if(!is_match) {
- printf("=== No matches found ===\n");
- }
- printf("=== Press any key... ===");
- getchar();
- }
- void transfer() {
- int acc_no;
- int acc_from;
- int acc_to;
- int d = 0;
- float to_spend;
- float cash;
- char temp[NUMBER_SIZE];
- custAccount cust;
- FILE *fp = NULL;
- fp = fopen(FILE_DATA, "r+b");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_END);
- acc_no = ftell(fp) / sizeof(cust);
- rewind(fp);
- system("clear");
- printf("=== MONEY TRANSFER ===\n");
- do {
- printf("= From which account you want to transfer money? [1-%d] >> ", acc_no);
- fgets(temp, NUMBER_SIZE, stdin);
- acc_from = atoi(temp);
- } while(!isInt(temp) || !isAccount(acc_from, acc_no));
- do {
- printf("= To which account you want to transfer money? [1-%d] >> ", acc_no);
- fgets(temp, NUMBER_SIZE, stdin);
- acc_to = atoi(temp);
- } while(!isInt(temp) || !isAccount(acc_to, acc_no) || acc_to == acc_from);
- fseek(fp, (acc_from - 1) * sizeof(cust), SEEK_SET);
- fread(&cust, sizeof(cust), 1, fp);
- to_spend = cust.balance;
- printf("== On the account no.%d there is %.2f PLN\n", acc_from, cust.balance);
- if(cust.balance == 0) {
- printf("== Account is out of cash\n");
- /*printf("=== TRANSFER TERMINATED ===");*/
- }
- else {
- do {
- printf("= How much money You want to transfer? [Type only numbers and keep the range]\n\t>> ");
- fgets(temp, NUMBER_SIZE, stdin);
- cash = atof(temp);
- } while(!isFloat(temp) || cash > to_spend);
- d = decision();
- if(d) {
- cust.balance -= cash;
- fseek(fp, (acc_from - 1) * sizeof(cust), SEEK_SET);
- fwrite(&cust, sizeof(cust), 1, fp);
- printf("== On the account no.%d there is %.2f PLN now\n", acc_from, cust.balance);
- fseek(fp, (acc_to - 1) * sizeof(cust), SEEK_SET);
- fread(&cust, sizeof(cust), 1, fp);
- cust.balance += cash;
- fseek(fp, (acc_to - 1) * sizeof(cust), SEEK_SET);
- fwrite(&cust, sizeof(cust), 1, fp);
- printf("== And on the account no.%d there is %.2f PLN now\n", acc_to, cust.balance);
- }
- }
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- if(d) {
- printf("=== TRANSFER COMPLETED ===\n");
- }
- else {
- printf("=== TRANSFER TERMINATED ===\n");
- }
- printf("=== Press any key... ===");
- getchar();
- }
- void payOrTake(int option) {
- int acc_all;
- int acc_no;
- int d;
- float cash;
- float to_take;
- char temp[NUMBER_SIZE];
- custAccount cust;
- FILE *fp = NULL;
- if(option == 1) printf("=== PAYMENT ===\n");
- if(option == 2) printf("=== WITHDRAWAL ===\n");
- system("clear");
- fp = fopen(FILE_DATA, "r+b");
- if(fp == NULL) {
- perror("=== Error opening the file ===");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_END);
- acc_all = ftell(fp) / sizeof(cust);
- rewind(fp);
- do {
- printf("= Which account to execute [1-%d] >> ", acc_all);
- fgets(temp, NUMBER_SIZE, stdin);
- acc_no = atoi(temp);
- } while(!isInt(temp) || !isAccount(acc_no, acc_all));
- fseek(fp, (acc_no - 1) * sizeof(cust), SEEK_SET);
- fread(&cust, sizeof(cust), 1, fp);
- to_take = cust.balance;
- printf("== There is %.2f PLN on the account\n", to_take);
- if(option == 1) {
- do {
- printf("= Amount of money to pay in >> ");
- fgets(temp, NUMBER_SIZE, stdin);
- cash = atof(temp);
- } while(!isFloat(temp));
- d = decision();
- if(d) cust.balance += cash;
- }
- if(option == 2 && to_take > 0) {
- do {
- printf("= Amount of money to take >> ");
- fgets(temp, NUMBER_SIZE, stdin);
- cash = atof(temp);
- if(cash > to_take) {
- printf("== You can withdraw up to %.2f PLN\n", to_take);
- }
- } while(!isFloat(temp) || cash > to_take);
- d = decision();
- if(d) cust.balance -= cash;
- }
- if(option == 2 && to_take <= 0) {
- printf("== Account is out of cash\n");
- d = 0;
- }
- fseek(fp, (acc_no - 1) * sizeof(cust), SEEK_SET);
- fwrite(&cust, sizeof(cust), 1, fp);
- if(fclose(fp)) {
- perror("=== Error closing the file ===");
- exit(EXIT_FAILURE);
- }
- if(d) {
- printf("=== TRANSACTION COMPLETED ===\n");
- }
- else {
- printf("=== TRANSACTION TERMINATED ===\n");
- }
- printf("=== Press any key... ===");
- getchar();
- }
- void display(custAccount cust_list) {
- printf("=== Account number %d.\n", cust_list.acc_number);
- printf("== Name:\t%s", cust_list.name);
- printf("== Surname:\t%s", cust_list.surname);
- printf("== Adress:\t%s", cust_list.adress);
- printf("== PESEL:\t%s", cust_list.PESEL);
- printf("== ID number:\t%s", cust_list.ind_number);
- printf("== Balance:\t%.2f PLN", cust_list.balance);
- printf("\n\n");
- }
- int isStringCorrect(char string[]) {
- int i;
- for(i = 0; i < strlen(string) - 1; i++) {
- if(!isalpha(string[i])) {
- printf("=== Type letters only ===\n");
- return 0;
- }
- }
- return 1;
- }
- int isAdressCorrect(char adress[]) {
- int i;
- for(i = 0; i < strlen(adress) - 1; i++) {
- if(!isalpha(adress[i]) && adress[i] != '/' && adress[i] != ' ' && !isdigit(adress[i])) {
- printf("=== Type leterrs, digits, space and '/' only ===\n");
- return 0;
- }
- }
- return 1;
- }
- int isFloat(char num[]) {
- int i;
- for(i = 0; i < strlen(num) - 1; i++) {
- if(!isdigit(num[i]) && num[i] != '.') {
- printf("=== Not a proper number (positive numbers only) ===\n");
- return 0;
- }
- }
- return 1;
- }
- int isInt(char num[]) {
- int i;
- for(i = 0; i < strlen(num) - 1; i++) {
- if(!isdigit(num[i])) {
- printf("=== Not a number (positive numbers only) ===\n");
- return 0;
- }
- }
- return 1;
- }
- int isAccount(int a_no, int a_max) {
- if(a_no > a_max || a_no < 0) {
- printf("=== Account number out of range, type from 1-%d ===\n", a_max);
- return 0;
- }
- return 1;
- }
- int decision() {
- char c;
- do {
- printf("= Are you sure you want to execute? [y/n] >> ");
- c = getchar();
- } while(c != 'y' && c != 'Y' && c != 'n' && c != 'N');
- getchar();
- if(c == 'y' || c == 'Y') return 1;
- if(c == 'n' || c == 'N') return 0;
- return 0;
- }
- int matchFound(custAccount cust) {
- printf("=== Match found ===\n");
- display(cust);
- return 1;
- }
- int input_string (char *output, int max_size) { //
- int c, i;
- while ( isspace(( c = getchar())));
- ungetc(c, stdin);
- for (i = 0; ( c = getchar()) != '\n' && c != EOF && c != '\0' && i < max_size - 1; ++i) {
- output[i] = (char) c;
- }
- if ( c != '\n' && c != EOF && c != '\0')
- while (( c = getchar()) != '\n' && c != EOF && c != '\0' );
- output[i] = '\0';
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement