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;
- typedef struct{
- string8 code;
- int qty;
- }prodBoughtType;
- typedef prodBoughtType arrBought[MAX_CART];
- struct userTag{
- string15 username,
- password;
- userInfoType info;
- char type;
- float creditlimit,
- outstanding;
- arrBought cart;
- int items,
- nlogin,
- nacctype,
- nlock;
- struct userTag *pNext;
- };
- typedef struct userTag userType;
- typedef struct userTag * ptrUser;
- void displayAll(ptrUser pUsers){
- while (pUsers != NULL){
- printf("%s\n", pUsers -> username);
- pUsers = pUsers -> pNext;
- }
- }
- void freeAll(ptrUser * pFirst){
- ptrUser pDel;
- while (*pFirst != NULL){
- pDel = *pFirst;
- *pFirst = (*pFirst) -> pNext;
- free(pDel);
- }
- }
- 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, 21, 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';
- }
- void signUp(userType *pUser){
- string8 tempcode;
- char cDump;
- char acode[9]={"DLSU2017"};
- do{
- printf("Enter username: "); scanf("%s%c", (*pUser).username, &cDump);
- }while (strlen((*pUser).username) < 3 || strlen((*pUser).username) > 15);
- do{
- printf("Enter password: "); scanf("%s%c", pUser -> password, &cDump);
- }while (strlen(pUser -> password) < 6 || strlen(pUser -> password) > 15);
- do{
- printf("Type A for Admin and S for Shopper: "); scanf("%c%c", pUser->type, &cDump);
- if(pUser->type == 'A' || pUser->type == 'a'){
- printf("Enter authorization code: "); scanf("%s%c",tempcode,&cDump);
- while(strcmp(tempcode, acode)!=0)
- printf(" Wrong Authorization Code try again");
- }
- else if(pUser->type == 'S' || pUser->type == 's')
- printf("Congratulations you are now registered as a shopper!");
- }while (pUser->type != 'A' || pUser->type != 'a' || pUser->type != 'S' || pUser->type != 's');
- getUserInfo(&pUser -> info);
- }
- void displayAllRecur(ptrUser pUsers){
- if (pUsers != NULL){
- printf("%s\n", pUsers -> username);
- displayAllRecur(pUsers -> pNext);
- }
- }
- ptrUser search(ptrUser pFirst, string15 username){
- ptrUser pRun;
- pRun = pFirst;
- while (pRun != NULL && strcmp(username, pRun -> username) != 0){
- pRun = pRun -> pNext;
- }
- return pRun;
- }
- void deleteNode(ptrUser *pFirst, string15 username){
- ptrUser pFind, pRun;
- if (*pFirst == NULL)
- printf("The List is empty");
- else {
- pFind = search(*pFirst, username);
- if (pFind == NULL)
- printf("%s is not in the List\n", username);
- else { //found the node to be deleted
- if(pFind == *pFirst){ //deleting first node
- *pFirst = (*pFirst) -> pNext;
- }
- else{ //delete from middle or end
- pRun = *pFirst;
- while (pRun -> pNext == pFind)
- pRun = pRun -> pNext;
- pRun -> pNext = pFind -> pNext;
- }
- free(pFind);
- pFind = NULL;
- }
- }
- }
- void login(userType *pUsers, userInfoType *pInfo){
- int ctr;
- string15 userchk, passchk;
- do{
- printf("Enter Username:");scanf("%s",&userchk);
- printf("Enter Password:");scanf("%s",&passchk);
- while(pUsers != NULL){
- if(strcmp(pUsers -> username, userchk)!=0)
- {
- pUsers = pUsers->pNext;
- printf("Username Invalid try again");
- }
- else if(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)!=0)
- {
- pUsers = pUsers->pNext;
- printf("Incorrect Password try again");
- ctr++;
- }
- else if(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)==0)
- {
- if(pUsers->type=='A' || pUsers->type == 'a')
- printf("going to admin menu");
- else
- printf("welcome to the shopper menu");
- }
- }
- }while(ctr<=3);
- printf("counter exceeded account locked");
- pUsers->nlock = 1;
- }
- int main(){
- /*
- userType user;
- userType *pUsers = NULL,
- *pNew;
- */
- ptrUser pUsers = NULL,
- pNew, pLast;
- int opt,n;
- char cDump;
- ptrUser pRun, pTrail;
- printf("Welcome to the shopping kiosk\n\n");
- printf("type 1 if you would like to log in\n0 if you would like to sign up: ");
- scanf("%d",&n);
- switch(n){
- case 1:login(pUsers, &pUsers->info);break;
- default: do{
- /*
- pNew = malloc(sizeof(userType));
- signUp(pNew);
- pNew -> pNext = NULL;
- if (pUsers == NULL)
- pUsers = pNew;
- else
- pLast -> pNext = pNew;
- pLast = pNew;
- */
- pNew = malloc(sizeof(userType));
- signUp(pNew);
- pNew -> pNext = NULL;
- if (pUsers == NULL) //if list is empty
- pUsers = pNew;
- else if (strcmp(pUsers -> username, pNew -> username) > 0){ //conect as first node
- pNew -> pNext = pUsers;
- pUsers = pNew;
- }
- else { //modifying middle of list
- pRun = pUsers;
- while (pRun != NULL && strcmp(pRun -> username, pNew -> username) < 0){
- pTrail = pRun;
- pRun = pRun -> pNext;
- }
- pTrail -> pNext = pNew;
- pNew -> pNext = pRun;
- }
- printf("Type '1' to enter another user. Type '0' to return to log in/Sign up. "); scanf("%d%c", &opt, &cDump);
- } while (opt == 1);
- //use for view locked accounts displayAllRecur(pUsers);
- //displayAll(pUsers);
- break;
- }
- freeAll(&pUsers);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement