Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************************************************************
- This is to certify that this project is my own work, based on my personal efforts in studying and applying the concepts
- learned. I have constructed the functions and their respective algorithms and corresponding code by myself. The program
- was run, tested, and debugged by my own efforts. I further certify that I have not copied in part or whole or otherwise
- plagiarized the work of other students and/or persons.
- Alexandra Rotor Reyes, DLSU ID# 11622105
- **********************************************************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_ITEMS 100
- typedef char string8[9];
- typedef char string15[16];
- typedef char string20[21];
- typedef char string50[51];
- 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_ITEMS];
- typedef struct aboutUser
- {
- string15 username,
- password;
- userInfoType info;
- int type; /* administrator or shopper? */
- float creditLimit,
- outstanding;
- arrBought cart;
- int nItems,
- ctr,
- isLocked;
- struct aboutUser *next;
- } userNode;
- typedef struct aboutStock
- {
- string8 pCode;
- string15 category,
- supplier,
- product;
- int avbl,
- sold;
- float purchasPrice,
- sellingPrice,
- discountRate;
- struct aboutStock *next;
- } stockNode;
- void viewLocked (userNode *user)
- {
- int ctr=0;
- userNode *temp=NULL;
- temp=user;
- printf("View Locked Accounts:\n");
- do
- {
- if (temp->isLocked==1 && temp->type==2)
- printf("Account #%d: %s\n", ctr, temp->username);
- temp=temp->next;
- } while (temp!=NULL);
- }
- void unlockSpecific (userNode *user)
- {
- string15 iUser;
- userNode *temp=NULL;
- int compare=2, choice=0, close=0;
- temp=user;
- printf("Unlock a specific account: \n");
- do
- {
- printf("Enter username of the account you wish to unlock: \n");
- scanf("%s", iUser);
- compare=strcmp(iUser, temp->username);
- do
- {
- if (temp->isLocked==1 && compare==0)
- {
- temp->isLocked=0;
- temp=temp->next;
- }
- } while (temp!=NULL);
- printf("Username: %s has been unlocked.\n");
- printf("Would you like to unlock another user? \n");
- printf("1. Yes\n");
- printf("2. No\n");
- do
- {
- switch (choice)
- {
- case 1: close=0;
- break;
- case 2: close=1;
- break;
- default: printf("Wrong input. Try again.\n");
- }
- } while (choice!=1 && choice!=2);
- } while (close==1);
- }
- void unlockAll (userNode *user)
- {
- userNode *temp=NULL;
- temp=user;
- printf("Unlock all accounts: \n");
- printf("Loading...\n");
- do
- {
- if (temp->isLocked==1)
- temp->isLocked=0;
- temp=temp->next;
- } while (temp!=NULL);
- printf("All accounts have been unlocked!\n");
- }
- void viewOutBal (userNode *user)
- {
- userNode *temp=NULL;
- printf("View Outstanding Balance: \n");
- do
- {
- if (temp->outstanding <= 0.00)
- printf("Sorry, your outstanding balance is empty!\n");
- else if (temp->outstanding > 0.00)
- {
- printf("Username: %s", temp->username);
- printf("Outstanding Balance is: %.2f\n", temp->outstanding);
- }
- temp=temp->next;
- } while (temp!=NULL);
- }
- void manageAccounts (userNode *user)
- {
- int aChoice;
- userNode *temp=NULL;
- do
- {
- printf(" === MANAGE ACCOUNTS MENU === \n");
- printf("1. View Locked Accounts\n");
- printf("2. Unlock Specific Account\n");
- printf("3. Unlock All Locked Accounts\n");
- printf("4. View Accounts with Outstanding Balance\n");
- printf("5. Return to Administrator Menu\n");
- scanf("%d", &aChoice);
- switch (aChoice)
- {
- case 1: viewLocked (user);
- break;
- case 2: unlockSpecific (user);
- break;
- case 3: unlockAll (user);
- break;
- case 4: viewOutBal (user);
- break;
- case 5: break;
- }
- } while (aChoice!=5);
- }
- void addNewStock (stockNode *stockInfo, int *stockctr)
- {
- printf("Add new stocks: \n");
- }
- void viewAllStocks (stockNode *stockInfo, int *stockctr)
- {
- }
- void modStockInfo ()
- {
- }
- void restock ()
- {
- }
- void manageStocks (stockNode *stockInfo, int *stockctr)
- {
- int aChoice=0;
- do
- {
- printf("\n === MANAGE STOCKS MENU === \n");
- printf("1. Add New Stock\n");
- printf("2. View All Stocks\n");
- printf("3. View Stocks by Category (PHASE 2)\n");
- printf("4. View Stocks to Reorder (PHASE 2)\n");
- printf("5. Modify Stock Info\n");
- printf("6. Restock\n");
- printf("7. Save Inventory (PHASE 2)\n");
- printf("8. Update Inventory from File (PHASE 2)\n");
- scanf("%d", &aChoice);
- /*
- switch (aChoice)
- {
- case 1: addNewStock (stockInfo, stockctr);
- break;
- case 2: viewAllStocks ();
- break;
- case 3: break;
- case 4: break;
- case 5: modStockInfo ();
- break;
- case 6: restock ();
- break;
- case 7: break;
- case 8: break;
- } */
- } while (aChoice>0 && aChoice<9);
- }
- void adminMenu (userNode *user, int *close, stockNode *stockInfo, int *stockctr)
- {
- int adChoice=0;
- do
- {
- printf("\n === ADMINISTRATOR MENU === \n");
- printf("1. Manage Accounts Menu\n");
- printf("2. Manage Stocks Menu\n");
- printf("3. Prepare Delivery Receipt (PHASE 2)\n");
- printf("4. Shutdown Kiosk \n");
- printf("5. Log Out \n");
- scanf("%d", &adChoice);
- switch (adChoice)
- {
- case 1: manageAccounts(user);
- break;
- case 2: manageStocks(stockInfo, stockctr);
- break;
- case 3: break;
- case 4: *close=1;
- break;
- case 5: break;
- default: printf("Invalid input. Please try again!\n");
- }
- } while (adChoice!=4 && adChoice!=5);
- }
- void modUserInfo (userNode *user)
- {
- }
- void browseAllProd ()
- {
- }
- /*
- void shopperMenu (userNode *user, int *close)
- {
- int sChoice=0;
- do
- {
- printf("\n === SHOPPER MENU === \n");
- 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. Settle Outstanding Balance\n");
- printf("8. Log Out\n");
- switch (sChoice)
- {
- case 1: modUserInfo ();
- break;
- case 2: browseAllProd ();
- break;
- case 3: break;
- case 4: break;
- case 5: break;
- case 6: break;
- case 7: break;
- case 8: break;
- }
- } while (choice!=8);
- } */
- void login (userNode *user)
- {
- int pchk=1, compare=2, found=0, close=1, stockctr=0;
- char cDump;
- string15 uname, pword;
- userNode *temp=NULL;
- stockNode *stockInfo=NULL;
- temp=user;
- if (temp!=NULL)
- {
- do
- {
- printf(" === LOGIN === \n");
- printf("Enter username: ");
- scanf("%s%c", uname, &cDump);
- while (temp!=NULL && found==0)
- {
- compare=strcmp(uname, temp->username);
- if (compare==0)
- found=1;
- else if (compare!=0)
- temp=temp->next;
- }
- while (temp->isLocked==1 && found==0)
- {
- printf("\nUsername does not exist. Try again.\n\n");
- printf(" === LOGIN === \n");
- printf("Enter username: ");
- scanf("%s%c", uname, &cDump);
- }
- } while (found!=1);
- compare=2;
- found=0;
- temp->ctr=0;
- do
- {
- printf("Enter password: ");
- scanf("%s%c", temp->password, &cDump);
- compare=strcmp(temp->password, user->password);
- if (compare==0)
- {
- switch (temp->type)
- {
- case 1: printf("Welcome back, Admin!\n");
- close=1;
- adminMenu (user, &close, stockInfo, &stockctr);
- break;
- case 2: printf("Welcome back, Shopper!\n");
- close=1;
- // shopperMenu(user, &close);
- break;
- }
- }
- else
- {
- temp->ctr++;
- printf("Incorrect password. Try again. %1 out of 3 tries\n", temp->ctr);
- printf("Enter password: ");
- scanf("%s%c", temp->password, &cDump);
- if (temp->ctr>=3 && temp->isLocked==0 && close==0)
- {
- user->isLocked=1;
- printf("You have tried logging in too many times.\n");
- printf("Please contact the administrator.\n");
- }
- }
- } while (close==0 && temp->ctr>=3);
- }
- else
- printf("There are not any users yet! Sign up first.\n");
- }
- /* to ask which type of account the user prefers */
- void typeofAccount (userNode *pNew)
- {
- /* for authorization code */
- string8 ac;
- int choice=0, close=1, change=0;
- do
- {
- printf("\nChoose your type of account: \n");
- printf("1. Administrator\n");
- printf("2. Shopper\n");
- printf("Enter choice: ");
- scanf("%d", &choice);
- while (choice!=1 && choice!=2)
- {
- // system("cls");
- printf("Invalid choice. Try again.\n");
- printf("Choose your type of account: \n");
- printf("1. Administrator\n");
- printf("2. Shopper\n");
- printf("Enter choice: ");
- scanf("%d", &choice);
- }
- pNew->type=choice;
- switch (pNew->type)
- {
- case 1: do
- {
- printf("Input authorization code: ");
- scanf("%s", ac);
- if (strcmp(ac, "DLSU2017")==0)
- {
- printf("\nHello New Admin!\n");
- // printf("Opening Administrator Menu...\n");
- // adminMenu(pNew, close);
- }
- else
- {
- printf("\nSorry, invalid authorization code.\n");
- printf("1. Try again\n");
- printf("2. Change account type\n");
- printf("Enter choice: ");
- scanf("%d", &change);
- while (change!=1 && change!=2)
- printf("\nInvalid input!\n");
- switch (change)
- {
- case 1: close=1;
- break;
- case 2: close=0;
- break;
- }
- }
- } while (strcmp(ac, "DLSU2017")!=0 && close!=0);
- break;
- case 2: printf("Hello Shopper!");
- pNew->creditLimit=5000.00;
- pNew->outstanding=0.00;
- pNew->nItems=0;
- pNew->isLocked=0;
- /* to tell the user more information about his/her account */
- printf("\nYour Credit Limit is: PhP %.2f.\n", pNew->creditLimit);
- printf("Your Outstanding Balance is: PhP %.2f.\n", pNew->outstanding);
- printf("Your Cart currently contains: %d items.\n\n", pNew->nItems);
- break;
- default: printf("Invalid input!\n");
- }
- } while (close==0 && change==2);
- /* recap for the user */
- printf("\n === REMEMBER ===\n");
- printf("Username is: %s\n", pNew->username);
- printf("Password is: %s\n\n", pNew->password);
- }
- void getUserInfo (userInfoType *pInfo)
- {
- getchar();
- 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: ");
- 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("%s, %s %s \n", pInfo->name.last, pInfo->name.first, pInfo->name.middle);
- }
- void signUp (userNode *user, userNode *pNew)
- {
- int go=0, chk=0, i=0, compare=2, accept=0;
- char cDump;
- string15 uname, pword;
- if (user==NULL)
- {
- /* first account to sign up */
- do
- {
- printf("=== SIGN UP ===\n");
- printf("Enter username: ");
- scanf("%s%c", uname, &cDump);
- } while ((strlen(uname) < 3 || strlen(uname) > 15));
- strcpy (pNew->username, uname);
- }
- else
- {
- /* second account and onwards to sign up */
- do
- {
- printf("=== SIGN UP ===\n");
- printf("Enter username: ");
- scanf("%s%c", uname, &cDump);
- if (strlen(uname) >=3 && strlen(uname)<=15)
- {
- pNew=user;
- compare=strcmp(pNew->username, uname);
- }
- if (compare!=0) //either 1 or -1
- {
- accept=1;
- pNew->next=pNew;
- strcpy (pNew->username, uname);
- //printf("Username is: %s\n", pNew->username);
- }
- else if (compare==0)
- {
- accept=0;
- printf("Username unavailable. Try again.\n");
- }
- } while (strlen(uname) < 3 || strlen(uname) > 15 || accept==0);
- }
- printf("Username is: %s\n", pNew->username);
- accept=0;
- do
- {
- printf("Enter password: ");
- scanf("%s", pword);
- if (strlen(pword) >= 6 || strlen(pword) <= 15)
- {
- for (i=0; i<strlen(pword); i++)
- {
- if (!((pword[i] >= 'A' && pword[i] <= 'Z') || (pword[i] >= 'a' && pword[i] <= 'z')))
- {
- accept=1;
- chk=1;
- strcpy (pNew->password, pword);
- }
- }
- if (accept!=1)
- printf("Invalid password. Input at least 1 non-letter.\n");
- }
- else
- accept=0;
- } while (chk==0 || accept!=1 || strlen(pword) < 6 || strlen(pword) > 15);
- printf("Password is: %s\n", pNew->password);
- getUserInfo(&pNew->info); /* (&pUser->info) */
- }
- void displayAllRecur (userNode *pUser)
- {
- if (pUser!=NULL)
- {
- printf("%s \n", pUser->next);
- displayAllRecur (pUser->next);
- }
- }
- void freeAll (userNode *user)
- {
- userNode *pDel;
- pDel=NULL;
- while (user != NULL)
- {
- pDel=user;
- user=user->next;
- free(pDel);
- }
- }
- int main()
- {
- int choice=0, close=0, nChoice=0;
- userNode *user, *pNew, *pLast, *pRun, *pTrail;
- stockNode *pStocks;
- user=NULL;
- pNew=NULL; //pFirst
- pLast=NULL;
- pRun=NULL;
- pTrail=NULL;
- pStocks=NULL;
- printf("\t\t\t\t\t\t========== WELCOME! ==========\n\n");
- do
- {
- printf("=== MAIN MENU ===\n");
- printf("1. Login\n");
- printf("2. Sign-up\n");
- printf("3. Display\n");
- printf("Enter choice: ");
- scanf("%d", &choice);
- printf("\n");
- while(choice!=1 && choice!=2)
- {
- system("cls");
- printf("Try Again!\n");
- printf("=== MAIN MENU ===\n");
- printf("1. Login\n");
- printf("2. Sign-up\n");
- printf("3. Display\n");
- printf("Enter choice: ");
- scanf("%d", &choice);
- printf("\n");
- }
- switch (choice)
- {
- case 1: printf("You chose to login!\n\n");
- login(user);
- break;
- case 2: printf("You chose to sign-up!\n\n");
- do
- {
- pNew=malloc(sizeof(userNode));
- signUp(user, pNew);
- typeofAccount(pNew);
- pNew->next=NULL;
- if (user==NULL)
- user=pNew;
- else if (strcmp(user->username, pNew->username)>0)
- {
- pNew->next=user;
- user=pNew;
- }
- else
- {
- pRun=user;
- while (pRun!=NULL && strcmp(pRun->username, pNew->username)<0)
- {
- pTrail=pRun;
- pRun=pRun->next;
- }
- // if (pTrail!=NULL)
- pTrail->next=pNew;
- pNew->next=pRun;
- }
- do
- {
- printf("Would you like to add another user?\n");
- printf("1. Yes\n");
- printf("2. No\n");
- scanf("%d", &nChoice);
- } while (nChoice!=1 && nChoice!=2);
- } while (nChoice==1);
- break;
- case 3: printf("You chose to display all accounts!\n");
- displayAllRecur(user);
- break;
- default: printf("Invalid input.\n");
- }
- } while (nChoice==2 || nChoice==0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement