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)
- printf("Password is okay");
- 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%c", &cTempType, &cDump);
- 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], arrUserCode[16];
- char authcode[9]={"DLSU2017"};
- char cDump;
- ptrUser pLogin, pPassword;
- while (loggedin == 0){
- printf("Enter username: ");
- scanf("%s", arrTempUser);fflush(stdin);
- printf("Enter password: ");
- scanf("%s", arrTempPW);fflush(stdin);
- pLogin = search(pUsers, arrTempUser);
- pPassword = search(pUsers, pUsers->password);
- if (pLogin == NULL)
- printf("Not found \n");
- if (strcmp(pLogin->username, arrTempUser)==0){
- if (strcmp(pPassword->password, arrTempPW)==0){
- printf("Username is fine \n");
- printf("Password is fine \n");
- loggedin = 1;
- }
- else
- printf("Something is wrong with password \n");
- }
- else printf("Something is wrong with username \n");
- printf("%s", arrTempUser);
- printf("%d", strcmp(pLogin->username, arrTempUser));
- if (pUsers->type == 'A'){
- printf("Input authorization code: ");
- scanf("%s%c", arrInputCode, &cDump);
- while (strcmp(arrInputCode,authcode)!=0){
- printf("Incorrect code, please try again: ");
- scanf("%s%c", arrInputCode, &cDump);
- }
- loggedin = 1;
- printf("Admin menu");
- }
- if (pUsers->type == 'S'){
- printf("Shopper Menu");
- loggedin = 1;
- }
- }
- }
- 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);
- 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);
- switch (choice){
- case 1: menu (pUsers, pNew, pLast);
- break;
- case 2: signIn(pUsers);
- break;
- default:freeAll(&pUsers);
- exit (0);
- }
- }while (opt == 1);
- freeAll(&pUsers);
- }
- void ShopperMenu (userType *pUsers, userType *pNew, userType *pLast)
- {
- int choice = 0;
- printf("[1] Modify User Info \n");
- printf("[2] Browse All Products \n");
- printf("[3] Browse Products by Category \n");
- printf("[4] Browse Products on Sale \n");
- printf("[5] Add to Cart \n");
- printf("[6] View Cart \n");
- printf("[7] Check Out \n");
- printf("[8] Edit Cart Items \n");
- printf("[0] Return to Shopper Menu");
- scanf("%d", choice);
- switch (choice){
- case 1: //view locked
- break;
- case 2: //unlock specific
- break;
- case 3: //unlock all
- break;
- case 4: //outstanding
- break;
- case 5: //outstanding
- break;
- case 6: //outstanding
- break;
- case 7: //outstanding
- break;
- case 8: //outstanding
- break;
- default: menu (pUsers, pNew, pLast);
- break;
- }
- }
- int main ()
- {
- userType *pUsers = NULL,
- *pNew = NULL,
- *pLast = NULL;
- ptrUser pRun, pTrail;
- menu (pUsers, pNew, pLast);
- //displayAll(pUsers);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement