Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_CART 100
- typedef char string15[16];
- typedef char string20[21];
- typedef char string50[51];
- typedef char string8[9];
- typedef struct {
- string20 first,middle,last;
- }nameType;
- typedef struct{
- nameType name;
- string50 address;
- }userInfoType; //for user information
- typedef struct{
- string8 code;
- int qty;
- }prodBoughtType; //for the cart
- typedef prodBoughtType arrBought[MAX_CART];
- struct userTag{
- string15 username,
- password;
- userInfoType info;
- char type;
- float creditlimit,
- outstanding;
- arrBought cart;
- int nItems;
- struct userTag *pNext;
- };
- typedef struct userTag userType;
- typedef userType * ptrUser;
- void signUp (userType *pUser)
- {
- char cDump, choice, cTempType = '\0';
- char arrInputCode[9];
- char arrCompareCode[9] = {"DLSU2017"};
- do{
- printf("Enter username: ");
- scanf("%s%c", (*pUser).username,&cDump); //placing the information directly within the container. Username is inside pUser
- }while (strlen((*pUser).username)<3 || strlen((*pUser).username)>15);
- do{
- printf("Enter password: ");
- scanf("%s%c", (*pUser).password,&cDump); //placing the information directly within the container. Username is inside pUser
- if (strlen((*pUser).password)<6){
- printf("Password too short, please try again. \n");
- printf("Enter password: ");
- scanf("%s%c", (*pUser).password,&cDump);
- }
- if (strlen((*pUser).password)>15){
- printf("Password too long, please try again. \n");
- printf("Enter password: ");
- scanf("%s%c", (*pUser).password,&cDump);
- }
- if (ValidPass(pUser)==1)
- pUser->password;
- else{
- printf("Error, password must contain non-letter character.\n");
- printf("Enter password: ");
- scanf("%s%c", (*pUser).password,&cDump);
- }
- }while (strlen((*pUser).password)<6 || strlen((*pUser).password)>15);
- printf("Create [S]hopper or [A]dministrator account? \n");
- printf("Input: ");
- scanf("%c", &cTempType);
- if(cTempType != 's' && cTempType != 'S' && cTempType != 'a' && cTempType != 'A'){
- printf("Invalid input, please try again: \n");
- while (cTempType != 's' && cTempType != 'S' && cTempType != 'a' && cTempType != 'A'){
- printf("Input: ");
- scanf("%c%c",&cTempType, &cDump);
- }
- }
- if (cTempType == 's' || cTempType == 'S'){
- (*pUser).type = 'S';
- printf("Account Type: Shopper \n");
- }
- else if (cTempType == 'a' || cTempType == 'A')
- printf("Input authorization code: ");
- while (strcmp(arrInputCode,arrCompareCode)!=0){
- scanf("%s%c", arrInputCode,&cDump);
- if (strcmp(arrInputCode,arrCompareCode)==0){
- (*pUser).type = 'A';
- //AdminChoice();
- printf("Account Type: Admin \n");
- }
- else {
- printf("Invalid authorization code, try again. \n");
- printf("Input authorization code: ");
- scanf("%s%c", arrInputCode,&cDump);
- }
- }
- }
- void getUserInfo (userInfoType *pInfo)
- {
- printf("Enter name: \n");
- printf("First Name: ");
- fgets((*pInfo).name.first,21,stdin);
- printf("Middle Name: ");
- fgets((*pInfo).name.middle,21,stdin);
- printf("Last Name: ");
- fgets((*pInfo).name.last,51,stdin);
- printf("%s,%s,%s \n", (*pInfo).name.last,(*pInfo).name.first,(*pInfo).name.middle);
- printf("Enter address: ");
- fgets((*pInfo).address,51,stdin);
- (*pInfo).address[strlen((*pInfo).address)-1]='\0';
- (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
- (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
- (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
- }
- int ValidPass (userType *pUser)
- {
- int i, valid=0;
- /*while (i<strlen(pUser->password)){
- if ((pUser->password[i]) < 'a' || (pUser->password[i]) > 'z')
- if ((pUser->password[i]) < 'A' || (pUser->password)[i] > 'Z')
- valid = 1;
- i++;
- }
- */
- for(i=0;i<(strlen(pUser->password));i++){
- if (pUser->password[i] < 'a' || pUser->password[i] >'z')
- if (pUser->password[i] < 'A' || pUser->password[i] >'Z')
- valid=1;
- }
- return valid;
- }
- ptrUser search (ptrUser pFirst, string15 username)
- {
- ptrUser pRun;
- pRun = pFirst;
- while (pRun != NULL && strcmp(username, pRun->username)!=0)
- pRun = pRun->pNext;
- return pRun;
- }
- void displayAll (userType *pUsers)
- {
- while (pUsers!=NULL){ //stop traversing when there is no more new address
- printf("%s\n", pUsers->username);
- pUsers = pUsers->pNext; //node traversal
- }
- }
- void signIn (userType *pUsers)
- {
- int loggedin=0;
- char arrTempUser[16], arrTempPW[16], arrInputCode[9];
- char authcode[9]={"DLSU2017"};
- while (loggedin == 0){
- printf("Enter username: \n");
- fgets(arrTempUser,16,stdin);
- printf("Enter password: \n");
- fgets(arrTempPW,16,stdin);
- search(pUsers, pUsers->username);
- if (strcmp(arrTempUser,pUsers->username)!=0)
- printf("Not found \n");
- signIn (pUsers);
- if (pUsers->type == 'A'){
- printf("Input authorization code: ");
- scanf("%s", arrInputCode);
- while (strcmp(arrInputCode,authcode)!=0)
- if (strcmp(arrInputCode,authcode)==0){
- printf("Admin");
- loggedin == 1;
- }
- else {
- printf("Invalid authorization code, try again.");
- scanf("%s", arrInputCode);
- }
- }
- if (pUsers->type == 'S')
- loggedin == 1;
- printf("Shopper");
- }
- }
- void freeAll (ptrUser *pFirst)
- {
- userType *pDel;
- while(*pFirst != NULL){
- pDel = *pFirst;
- *pFirst = (*pFirst)->pNext;
- free(pDel);
- }
- }
- void menu (userType *pUsers, userType *pNew, userType *pLast)
- {
- int opt,
- choice=0;
- char cDump;
- printf("MAIN MENU \n");
- printf("[1] Sign Up \n");
- printf("[2] Log In \n");
- printf("Choice: ");
- scanf("%d%c", &choice,&cDump);
- switch (choice){
- case 1: do {
- pNew = malloc(sizeof(userType));
- signUp(pNew);
- (*pNew).pNext = NULL;
- if (pUsers == NULL) //if initially empty
- pUsers = pNew; //pNew is the first node
- else //connect nodes to the list
- pLast->pNext = pNew;
- pLast = pNew;
- getUserInfo (&pUsers->info);
- printf("Press [1] to create a new user or [0] to return to main menu: ");
- scanf("%d%c", &opt,&cDump);
- if (opt == 0)
- menu (pUsers, pNew, pLast);
- }while(opt == 0);
- break;
- case 2: signIn(pUsers);
- break;
- default:freeAll(&pUsers);
- exit (0);
- }
- }
- int main ()
- {
- userType *pUsers = NULL,
- *pNew,
- *pLast;
- menu (pUsers, pNew, pLast);
- //displayAll(pUsers);
- freeAll(&pUsers);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement