|
@ -0,0 +1,93 @@ |
|
|
|
|
|
#include "createCustomer.h" |
|
|
|
|
|
/*Code written by Julius Philipp Engel, fdai7057*/ |
|
|
|
|
|
int generateID() |
|
|
|
|
|
{ |
|
|
|
|
|
srand(clock()); |
|
|
|
|
|
const int MIN = 1000000, MAX = 9000001; |
|
|
|
|
|
int pseudoRandomIDForCustomer = (rand() % MAX) + MIN; |
|
|
|
|
|
return pseudoRandomIDForCustomer; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void collectCustomerProperties() |
|
|
|
|
|
{ |
|
|
|
|
|
customer_t instance; |
|
|
|
|
|
instance.forename = calloc(15+1,sizeof(char)); |
|
|
|
|
|
instance.surname = calloc(15+1,sizeof(char)); |
|
|
|
|
|
instance.password = calloc(20+1,sizeof(char)); |
|
|
|
|
|
instance.ID = generateID(); |
|
|
|
|
|
int letterCounter = 0; |
|
|
|
|
|
int letterMaximum = 15; |
|
|
|
|
|
char userInput=' '; |
|
|
|
|
|
bool inputTooLong = false; |
|
|
|
|
|
printf("To create a new user, enter the information required below.\n"); |
|
|
|
|
|
printf("Enter forename (max. 15 letters):\n"); |
|
|
|
|
|
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){ |
|
|
|
|
|
*(instance.forename+letterCounter) = userInput; |
|
|
|
|
|
++letterCounter; |
|
|
|
|
|
if(letterCounter>=letterMaximum){ |
|
|
|
|
|
inputTooLong = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if(inputTooLong){ |
|
|
|
|
|
printf("Forename too long. Aborting.\n"); |
|
|
|
|
|
exit(-1); |
|
|
|
|
|
}else{ |
|
|
|
|
|
*(instance.forename+letterCounter) = '\0'; |
|
|
|
|
|
letterCounter = 0; |
|
|
|
|
|
} |
|
|
|
|
|
printf("Enter surname (max. 15 letters):\n"); |
|
|
|
|
|
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){ |
|
|
|
|
|
*(instance.surname+letterCounter) = userInput; |
|
|
|
|
|
++letterCounter; |
|
|
|
|
|
if(letterCounter>=letterMaximum){ |
|
|
|
|
|
inputTooLong = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if(inputTooLong){ |
|
|
|
|
|
printf("Surname too long. Aborting.\n"); |
|
|
|
|
|
exit(-1); |
|
|
|
|
|
}else{ |
|
|
|
|
|
*(instance.surname+letterCounter) = '\0'; |
|
|
|
|
|
letterCounter = 0; |
|
|
|
|
|
} |
|
|
|
|
|
printf("Enter password (max. 20 letters):\n"); |
|
|
|
|
|
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){ |
|
|
|
|
|
*(instance.password+letterCounter) = userInput; |
|
|
|
|
|
++letterCounter; |
|
|
|
|
|
if(letterCounter>=letterMaximum){ |
|
|
|
|
|
inputTooLong = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if(inputTooLong){ |
|
|
|
|
|
printf("Password too long. Aborting.\n"); |
|
|
|
|
|
exit(-1); |
|
|
|
|
|
} |
|
|
|
|
|
*(instance.password+letterCounter) = '\0'; |
|
|
|
|
|
letterCounter = 0; |
|
|
|
|
|
printf("Enter balance:\n"); |
|
|
|
|
|
scanf("%f",&instance.balance); |
|
|
|
|
|
|
|
|
|
|
|
printf("Account successfully created. Your ID is: %d. Note it somewhere!\n",instance.ID); |
|
|
|
|
|
customer_t *referenceToCustomerInstance = &instance; |
|
|
|
|
|
writeCustomerPropertiesToFile(referenceToCustomerInstance); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void writeCustomerPropertiesToFile(customer_t *referenceToCustomerInstance) |
|
|
|
|
|
{ |
|
|
|
|
|
FILE *customerData = fopen("CustomerData.txt","a"); |
|
|
|
|
|
if(customerData!=NULL){ |
|
|
|
|
|
/*calling generateCheckString() at this place in future*/ |
|
|
|
|
|
fprintf(customerData,"ID=%d\nForename=%s\nSurname=%s\nPassword=%s\nBalance=%.4f€\n\n", |
|
|
|
|
|
referenceToCustomerInstance->ID,referenceToCustomerInstance->forename, |
|
|
|
|
|
referenceToCustomerInstance->surname,referenceToCustomerInstance->password, |
|
|
|
|
|
referenceToCustomerInstance->balance); |
|
|
|
|
|
fclose(customerData); |
|
|
|
|
|
} |
|
|
|
|
|
else{ |
|
|
|
|
|
printf("Error when accessing the file.\n"); |
|
|
|
|
|
exit(-1); |
|
|
|
|
|
} |
|
|
|
|
|
} |