Browse Source

implementation of collectCustomerProperties()

remotes/origin/feature/customer-creation
fdai7057 2 years ago
parent
commit
814c0eb148
  1. 73
      src/CreateCustomer.c
  2. 2
      src/CreateCustomer.h
  3. 7
      src/CustomerProperties.h

73
src/CreateCustomer.c

@ -1,6 +1,14 @@
#include "CreateCustomer.h"
#include "CustomerProperties.h"
int main()
{
collectCustomerProperties();
}
int generateID()
{
srand(time(0));
const int MIN = 1000000, MAX = 10000000;
int p_val = rand() % (MAX);
if(p_val<MIN)
@ -9,3 +17,68 @@ int generateID()
}
return p_val;
}
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);
}

2
src/CreateCustomer.h

@ -1,4 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int generateID();
void collectCustomerProperties();

7
src/CustomerProperties.h

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