Browse Source

Merge branch 'feature/update-current-customer-account-balance' into feature/withdraw-money-system

remotes/origin/feature/withdraw-money-system
Shivam Chaudhary 2 years ago
parent
commit
a6f6c3ae66
  1. 1
      .gitignore
  2. 20
      src/CustomerData.txt
  3. 4
      src/CustomerProperties.h
  4. 97
      src/LoginCustomer.c
  5. 10
      src/LoginCustomer.h
  6. 2
      src/_file_information.h
  7. BIN
      src/a.exe
  8. 63
      src/currentCustomerAccountBalance.c
  9. 10
      src/currentCustomerAccountBalance.h
  10. 6
      src/lineReplacer.h
  11. 81
      src/updateCustomerAccountBalance.c
  12. 11
      src/updateCustomerAccountBalance.h
  13. 9
      team.md
  14. 21
      tests/test_LoginCustomer.c

1
.gitignore

@ -1 +1,2 @@
.DS_Store
.vscode

20
src/CustomerData.txt

@ -0,0 +1,20 @@
1234=example
ID=1234
forename=Max
Surname=Mustermann
password=example
balance=5000
1327=example
ID=1327
forename=Max
Surname=Mustermann
password=example
balance=1500
1666=example
ID=1666
forename=Max
Surname=Mustermann
password=example
balance=240

4
src/CustomerProperties.h

@ -0,0 +1,4 @@
typedef struct Customer{
char *ID, *forename, *surname, *password;
float balance;
}customer_t;

97
src/LoginCustomer.c

@ -0,0 +1,97 @@
#include "LoginCustomer.h"
bool checkLogin(bool loginSuccessful)
{
return (loginSuccessful) ? true : false;
}
void collectCustomerDataForLogin(int attempts)
{
customer_t c;
c.ID = calloc(15+1,sizeof(char));
c.password = calloc(15+1, sizeof(char));
char digitCharacterFromUser, passwordCharacterFromUser;
int IDLengthCounter = 0, passwordLengthCounter = 0;
const int IDMaxLength = 16, passwordMaxLength = 16;
printf("Enter ID:\n");
while(((digitCharacterFromUser=getchar()) != '\n')&&IDLengthCounter<IDMaxLength){
if(digitCharacterFromUser>='0'&&digitCharacterFromUser<='9'){
*(c.ID+IDLengthCounter) = digitCharacterFromUser;
}
else{
printf("Character entered is not a digit. Aborting.\n");
exit(-1);
}
++IDLengthCounter;
}
*(c.ID+IDLengthCounter) = '\0';
if(IDLengthCounter>=IDMaxLength){
printf("ID entered is too long. Aborting.\n");
exit(-1);
}
printf("Enter password:\n");
while((passwordCharacterFromUser=getchar())!='\n'&&passwordLengthCounter<passwordMaxLength){
*(c.password+passwordLengthCounter) = passwordCharacterFromUser;
++passwordLengthCounter;
}
*(c.password+passwordLengthCounter) = '\0';
if(passwordLengthCounter>=passwordMaxLength){
printf("Password entered is too long. Aborting.\n");
exit(-1);
}
customer_t *ptr = &c;
bool loginSuccessful = loginCustomer(ptr);
if(loginSuccessful ) {
printf("Welcome to the menu!\n");
//call menu()
}else if(!loginSuccessful && attempts < MAX_LOGIN_ATTEMPTS){
printf("You have %d attempts left.\n", MAX_LOGIN_ATTEMPTS - attempts);
collectCustomerDataForLogin(++attempts);
}else{
printf("Maximum number of attempts reached. Program terminates.\n");
exit(-1);
//call error()
}
}
bool loginCustomer(customer_t *c)
{
bool foundCustomerEntryInFile = false;
char *searchForThisString = generateCheckString(c->ID,c->password);
char *lineFromCustomerFile = calloc(40,sizeof(char));
FILE *readCustomerFile = fopen("CustomerData.txt", "r");
if(readCustomerFile==NULL){
printf("Could not find file. Aborting.\n");
exit(-1);
//call error()
}
while((fscanf(readCustomerFile,"%s",lineFromCustomerFile)!=EOF)){
if(strcmp(searchForThisString,lineFromCustomerFile)==0){
foundCustomerEntryInFile = true;
break;
}
}
if(checkLogin(foundCustomerEntryInFile)){
printf("Login successful.\n");
return foundCustomerEntryInFile;
}else{
printf("Login not successful.\n");
}
fclose(readCustomerFile);
return foundCustomerEntryInFile;
}
char *generateCheckString(char *ID, char *password){
int checkStringLength = strlen(ID) + 1 + strlen(password) + 1;
char *checkString = calloc(checkStringLength, sizeof(char));
checkString = strcat(ID,"=");
checkString = strcat(checkString,password);
*(checkString+checkStringLength) = '\0';
return checkString;
}

10
src/LoginCustomer.h

@ -0,0 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "CustomerProperties.h"
#define MAX_LOGIN_ATTEMPTS 3
char *generateCheckString(char *, char*);
bool checkLogin(bool);
void collectCustomerDataForLogin(int);
bool loginCustomer(customer_t *);

2
src/_file_information.h

@ -0,0 +1,2 @@
#define MAX_LENGTH 100
#define CUSTOMER_DATA_FILE "CustomerData.txt"

BIN
src/a.exe

63
src/currentCustomerAccountBalance.c

@ -0,0 +1,63 @@
#include "currentCustomerAccountBalance.h"
float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]) {
float balance = 0;
char *token = strtok(balance_String, "="); // separates string to two parts
while (token != NULL) {
if (atoi(token) != 0) {
balance = atof(token); // converts string to float
break;
}
token = strtok(NULL, "=");
}
return balance;
}
float readFileAndGetAvailableBalance(FILE *file, char stringID[MAX_LENGTH]) {
float balance = 0;
bool keep_reading = true;
char buffer[MAX_LENGTH];
char balance_String[MAX_LENGTH];
while(keep_reading) {
fgets(buffer, MAX_LENGTH, file);
if (feof(file)) {
keep_reading = false;
}
else if(strstr(buffer, stringID)) {
for (int i = 0; i < 4; i++) {
fgets(buffer, MAX_LENGTH, file);
}
strcpy(balance_String, buffer);
balance = fetchBalanceFromBalanceString(balance_String);
keep_reading = false;
}
}
return balance;
}
float getAvailableAccountBalance(int user_id) {
float availableBalance = 0;
char stringID[MAX_LENGTH] = "ID=";
char user_id_as_string[MAX_LENGTH];
sprintf(user_id_as_string, "%d", user_id); // converts user_id to string
strcat(stringID, user_id_as_string);
// Now stringID is "ID=user_id"
FILE *file = fopen(CUSTOMER_DATA_FILE, "r");
if(file == 0) {
printf("Error: customer data file cannot be opened!\n");
return 0;
}
else {
availableBalance = readFileAndGetAvailableBalance(file, stringID);
}
fclose(file);
return availableBalance;
}

10
src/currentCustomerAccountBalance.h

@ -0,0 +1,10 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "_file_information.h"
float getAvailableAccountBalance(int user_id);
float fetchBalanceFromBalanceString(char balance_String[MAX_LENGTH]);
float readFileAndGetAvailableBalance(FILE *file, char stringID[MAX_LENGTH]);

6
src/lineReplacer.h

@ -0,0 +1,6 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
void replaceLineInFile(const char* file_name, int line, const char* new_line); //replaces the line at "line" on the file "file_name", with the new line "new_line".

81
src/updateCustomerAccountBalance.c

@ -0,0 +1,81 @@
#include "updateCustomerAccountBalance.h"
#include "currentCustomerAccountBalance.c"
#include "lineReplacer.h"
void replaceLineInFile(const char* file_name, int line, const char* new_line){
FILE* file = fopen(file_name, "r");
if (file == NULL) {
printf("Error opening file!\n");
return;
}
char current_string[1024];
int current_line = 1;
char *temp_file_name = "temp.txt";
FILE* temp_file = fopen(temp_file_name, "w");
if (temp_file == NULL) {
printf("Error creating temp file!\n");
fclose(file);
return;
}
while (fgets(current_string, sizeof(current_string), file) != NULL) {
if (current_line == line) {
fprintf(temp_file, "%s", new_line);
fputs("\n", temp_file);
} else {
fprintf(temp_file, "%s", current_string);
}
current_line++;
}
fclose(file);
fclose(temp_file);
if(remove(file_name)!=0){
printf("could not remove the original file!");
} // Remove the original file
if(rename(temp_file_name, file_name)!=0){
printf("could not rename!");
} // Rename the temp file to the original file
}
void replaceBalanceInString(float replacementBalance, int currentLine) {
char newBalanceLine[MAX_LENGTH] = "balance=";
char balance_as_string[MAX_LENGTH];
sprintf(balance_as_string, "%g", replacementBalance); //converts replacement balance to string
strcat(newBalanceLine, balance_as_string);
replaceLineInFile("CustomerData.txt",currentLine,newBalanceLine);
}
bool updateAvailableAccountBalance(int user_id, float newBalance){
bool keep_reading = true;
char buffer[MAX_LENGTH];
char stringID[MAX_LENGTH] = "ID=";
char user_id_as_string[MAX_LENGTH];
char balance_String[MAX_LENGTH];
int currentLine=0;
sprintf(user_id_as_string, "%d", user_id); // converts user_id to string
strcat(stringID, user_id_as_string);
FILE *file = fopen("CustomerData.txt", "r+");
printf(stringID);
while(keep_reading) {
fgets(buffer, MAX_LENGTH, file);
currentLine++;
if (feof(file)) {
keep_reading = false;
}
else if(strstr(buffer, stringID)) { //found the customer
fgets(buffer, MAX_LENGTH, file);
fgets(buffer, MAX_LENGTH, file);
fgets(buffer, MAX_LENGTH, file);
fgets(buffer, MAX_LENGTH, file);
strcpy(balance_String, buffer);
currentLine+=4;
keep_reading = false;
}
}
fclose(file);;
replaceBalanceInString(newBalance,currentLine);
}

11
src/updateCustomerAccountBalance.h

@ -0,0 +1,11 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 100
bool updateAvailableAccountBalance(int user_id, float newBalance);
void replaceBalanceInString(float replacementBalance, int currentLine);

9
team.md

@ -1,9 +0,0 @@
# Bankmanagement-System
- Can Hacioglu, Fdlt3817
- Atharva Kishor Naik, fdai7514
- Julius Philipp Engel, fdai7057
- Shivam Chaudhary, fdlt3781
- Mohamed Yahya Dahi, fdai6618
- Haytham Daoula, fdai7207

21
tests/test_LoginCustomer.c

@ -0,0 +1,21 @@
#include <unity.h>
#include "LoginCustomer.h"
void setUp(){};
void tearDown(){};
void test_checkLogin()
{
/*arrange*/
bool expected_test_values_compute_to_true[] = {4==4,true==true, 1==1, false==false, 'z'=='z', '='=='=',0x1A==0x1A};
int length_1 = sizeof(expected_test_values_compute_to_true)/sizeof(bool);
bool expected_test_values_compute_to_false[] = {4!=4,true==false,1==0,false==true,'z'=='x','!'==')',0x1A==0x2B};
int length_2 = sizeof(expected_test_values_compute_to_false)/sizeof(bool);
/*act and assertions*/
for(int i=0;i<7;++i) {
TEST_ASSERT_TRUE(checkLogin(expected_test_values_compute_to_true[i]));
}
for(int i=0;i<7;++i){
TEST_ASSERT_FALSE(checkLogin(expected_test_values_compute_to_false[i]));
}
}
Loading…
Cancel
Save