Browse Source

Merge branch 'feature/customer-creation' into 'development'

merging feature/customer-creation into development

See merge request fdai7057/bankmanagement-system!9
remotes/origin/feature/customer-login
fdai7057 2 years ago
parent
commit
44921acb9f
  1. 7
      build-project.sh
  2. 6
      project.yml
  3. 93
      src/createCustomer.c
  4. 8
      src/createCustomer.h
  5. 7
      src/customerProperties.h
  6. 10
      src/main.c
  7. 5
      src/mainMenu.c
  8. 1
      src/mainMenu.h
  9. 0
      tests/support/.gitkeep
  10. 26
      tests/test_CreateCustomer.c

7
build-project.sh

@ -1,6 +1,7 @@
clear
ceedling test:all
cd src/
gcc -o main main.c mainMenu.c
./main
rm main
gcc main.c createCustomer.c -o prog
./prog
rm prog
cd ..

6
project.yml

@ -31,12 +31,12 @@
:paths:
:test:
- +:test/**
- -:test/support
- +:tests/**
- -:tests/support
:source:
- src/**
:support:
- test/support
- tests/support
:libraries: []
:defines:

93
src/createCustomer.c

@ -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);
}
}

8
src/createCustomer.h

@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include "customerProperties.h"
int generateID();
void collectCustomerProperties();
void writeCustomerPropertiesToFile(customer_t *);

7
src/customerProperties.h

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

10
src/main.c

@ -1,8 +1,4 @@
#include "mainMenu.h"
int main() {
ageInput();
return 0;
#include "createCustomer.h"
int main(){
collectCustomerProperties();
}

5
src/mainMenu.c

@ -1,4 +1,4 @@
#include "mainMenu.h"
#include"mainMenu.h"
bool agePermission(int age)
@ -55,6 +55,8 @@ void ageInput()
menuInput();
menuInput();
break;
}
@ -129,4 +131,3 @@ void showMenu()
}

1
src/mainMenu.h

@ -11,6 +11,7 @@ bool checkIfInteger(char* userInput);
bool chooseOption(int choiceInput);
void ageInput();
void menuInput();
void showMenu();
void menuInput();

0
tests/support/.gitkeep

26
tests/test_CreateCustomer.c

@ -0,0 +1,26 @@
#include <unity.h>
#include <limits.h>
#include "createCustomer.h"
void setUp(){}
void tearDown(){}
void test_generateID(){
const int test_values = USHRT_MAX;
/*initialize blocks by calling generateID()*/
int *numbers = calloc(test_values, sizeof(int));
for(int i=0;i<test_values;++i)
{
*(numbers+i) = generateID();
printf("%d\n", *(numbers+i));
}
/*assertions, range checking*/
int delta = 5000000, expected = 5000000;
for(int i=0;i<test_values;++i)
{
TEST_ASSERT_INT_WITHIN(delta, expected,*(numbers+i));
}
}
Loading…
Cancel
Save