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;
- string20 middle;
- string20 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;
- string15 password;
- userInfoType info;
- char type;
- float creditLimit;
- float outstanding;
- arrBought cart;
- int nItems;
- struct userTag *pNext;
- };
- typedef struct userTag userType;
- typedef userType *ptrUser;
- /*void administrator(){
- }*/
- /*void shopper(){
- }*/
- int numUsers(ptrUser pUsers){
- int ctr=0;
- while(pUsers != NULL){
- ctr++;
- pUsers = pUsers->pNext;
- }
- return ctr;
- }
- void logIn(ptrUser pUsers){
- char user,pword;
- int attempts=0;
- do {
- printf("Enter Username: ");
- scanf("%s",&user);
- if (strcmp((*pUsers).username,user) == 0 && attempts < 3){
- do{
- printf("Enter Password: ");
- scanf("%s",&pword);
- if (strcmp (pUsers->password,pword)!=0){
- printf("Try again, %d try/s left\n",3-attempts);
- attempts++;
- }
- } while (attempts <= 3 && strcmp (pUsers->password,pword) !=0);
- /*if (strcmp(pUsers->password,input)==0)
- if (user.type == 's')
- shopper(&(*pUsers));
- else if (user.type == 'A')
- administrator(&(*pUsers))
- else if (attempts >= 3)*/
- }
- } while (strcmp (pUser->username,user));
- }
- 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("List is empty");
- else{
- pFind = search(*pFirst, username);
- if (pFind == NULL)
- printf("%s is not in the list\n",username);
- else //found note to delete
- if (pFind == *pFirst){
- *pFirst = (*pFirst)->pNext;
- free(pFind);
- pFind = NULL;
- }
- else { //delete from middle or end
- pRun = *pFirst;
- while (pRun->pNext != pFind){
- pRun = pRun->pNext;
- }
- pRun->pNext = pFind->pNext;
- free(pFind);
- pFind = NULL;
- }
- }
- }
- void displayAllRecur(ptrUser pUsers){
- if(pUsers != NULL){
- printf("%s\n",pUsers->username);
- displayAllRecur(pUsers->pNext);
- }
- }
- void freeAll(ptrUser * pFirst){
- ptrUser pDel;
- while(*pFirst != NULL){
- pDel = *pFirst;
- *pFirst = (*pFirst)->pNext;
- free(pDel);
- }
- }
- void displayAll(ptrUser pUsers){
- while(pUsers != NULL){
- printf("%s\n",pUsers->username);;
- pUsers = pUsers->pNext;
- }
- }
- 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("Enter address\n");
- 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';
- printf("Name: %s, %s %s\n",(*pInfo).name.last, pInfo -> name.first, pInfo -> name.middle);
- printf("Adress: %s",pInfo->address);
- }
- void signUp(userType *pUser){
- char cDump;
- char code;
- int check=0;
- do{
- printf("Enter username:");
- scanf("%s",(*pUser).username); //(*pUser).username can be (pUser -> username)
- //scanf("%s%c", (*pUser).username,&cDump);
- } while (strlen((*pUser).username) < 3 || strlen((*pUser).username) > 15);
- printf("%s\n",(*pUser).username);
- do{
- printf("Enter password:");
- //scanf("%s",(*pUser).password); //(*pUser).password can be (pUser -> password)
- scanf("%s%c", (*pUser).password,&cDump);
- } while (strlen((*pUser).password) < 6 || strlen((*pUser).password) > 15);
- printf("%s\n",(*pUser).password);
- getUserInfo(&(*pUser).info);
- //getUserInfo(pUser -> info);
- do{
- printf("Account Type:");
- printf("a - Administrator \n");
- printf("s - Shopper");
- scanf("%c",&(*pUser).type);
- if((*pUser).type== 's'){
- (*pUser).creditlimit = 5000.00;
- (*pUser).outstanding = 0.00;
- (*pUser).nItems = 0;
- }
- else if((*pUser).type == 'a'){
- while(code!="DLSU2017"){
- printf("Admin Code:");
- scanf("%s",&code);
- if(code == "DLSU2017")
- check=1;
- if(check=1)
- (*pUser).type = 'a';
- }
- }
- else printf("Invalid account type. Try Again");
- }while((*pUser).type != 'a' || (*pUser).type != 's');
- int main(){
- /*
- userType user;
- userType *pUsers = NULL;
- userType *pNew;
- */
- ptrUser pUsers = NULL;
- ptrUser pNew,/*pLast*/pRun,pTrail;
- string15 username;
- int opt,choice;
- int end = 1;
- char cDump;
- do{
- printf("Enter your option:");
- printf("1:Log In \n");
- printf("2:Sign Up \n");
- printf("3.Delete account\n");
- printf("4.Display all active accounts");
- printf("5.Close program \n");
- printf("Choice:");
- scanf("%d%c",&choice,&cDump);
- switch(opt){
- case 1:logIn(pUsers);
- case 2:pNew = malloc(sizeof(userType));
- signUp(pUsers);
- pNew -> pNext = NULL;
- if (pUsers == NULL) //first user
- pUsers = pNew; //pNew is first
- else if (strcmp (pUsers->username, pNew->username)>0){ //connect as first node
- pNew->pNext = pUsers;
- pUsers = pNew;
- }
- else { //modifying middle of the list
- pRun = pUsers ;
- while (pRun != NULL && strcmp(pRun->username, pNew->username) < 0) {
- pTrail = pRun;
- pRun = pRun->pNext;
- }
- pTrail->pNext = pNew;
- pNew->pNext=pRun;
- }
- break;
- case 3:printf ("What account do you want to delete?");
- scanf ("%s", username);
- deleteNode(&pUsers, username);
- break;
- case 4:displayAllRecur(pUsers);
- displayAll(pUsers);
- break;
- case 5:freeAll(&pUsers);
- end=0;
- }
- }while(end);
- return 0;
- }
- /* do{
- pNew=malloc(sizeof(userType));
- signUp(pNew);
- (*pNew).pNext = NULL; //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;
- printf("Another user?");
- scanf("%d%c",&opt,&cDump);
- }while(opt == 1);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement