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.
- Fritz Edron M.Calimag, 11621206
- **********************************************************************************************************************/
- #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,
- nlock;
- struct userTag *pNext;
- };
- typedef struct userTag userType;
- typedef struct userTag * ptrUser;
- struct stockcateg
- {
- string15 category;
- struct stockinfo{
- string15 supplier,
- product;
- string8 pCode;
- int Qavail,
- Qsold;
- float purchasePrice,
- sellingPrice,
- discountRate;
- struct stockinfo *pNextstockinfo;
- };
- struct stockcateg *pNextstockcateg;
- };
- typedef struct stockcateg stockcategtype;
- typedef struct stockcateg * ptrStockcateg;
- typedef struct stockinfo stocktype;
- typedef struct stockinfo * ptrStock;
- /*Function Name: displayAll
- Function Required Input Parameters: pUsers
- Function Expected Return Data: N/A
- Function Description and or Algorithm: This function displays all the users*/
- void displayAll(ptrUser pUsers){
- while (pUsers != NULL){
- printf("%s\n", pUsers -> username);
- pUsers = pUsers -> pNext;
- }
- }
- /*Function Name: freeAll
- Function Required Input Parameters: *pFirst
- Function Expected Return Data: N/A
- Function Description and or Algorithm: This function frees all the users*/
- void freeAll(ptrUser * pFirst){
- ptrUser pDel;
- while (*pFirst != NULL){
- pDel = *pFirst;
- *pFirst = (*pFirst) -> pNext;
- free(pDel);
- }
- }
- /*Function Name: freeAllstockver
- Function Required Input Parameters: *pFirststock
- Function Expected Return Data: N/A
- Function Description and or Algorithm: This function frees all the stocks*/
- void freeAllstockver(ptrStock * pFirststock){
- ptrStock pDelete;
- while (*pFirststock != NULL){
- pDelete = *pFirststock;
- *pFirststock = (*pFirststock) -> pNextstockinfo;
- free(pDelete);
- }
- }
- /*Function Name: getUserInfo
- Function Required Input Parameters: *pInfo
- Function Expected Return Data: N/A
- Function Description and or Algorithm: This function gets the users information(Full name & address)*/
- 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';
- }
- /*Function Name: signUp
- Function Required Input Parameters: *pUser, *pUsers
- Function Expected Return Data: N/A
- Function Description and or Algorithm: This function allows the user to sign up*/
- void signUp(userType *pUser, userType *pUsers){
- string8 tempcode;
- char cDump;
- char acode[9]="DLSU2017";
- do{
- printf("Enter username: "); scanf("%s%c", (*pUser).username, &cDump);
- while(pUsers != NULL)
- if(strcmp((*pUser).username, pUsers->username)==0)
- {
- pUsers = pUsers->pNext;
- printf("Username Taken try again\n");
- 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");
- printf("Enter authorization code: "); scanf("%s%c",tempcode,&cDump);
- }
- else if(pUser->type == 'S' || pUser->type == 's'){
- printf("Congratulations you are now registered as a shopper!\n");
- pUsers->creditlimit = 5000.00;
- pUsers->outstanding = 0.00;
- }
- else
- printf("Invalid Input please try again:");
- }while (pUser->type != 'A' && pUser->type != 'a' && pUser->type != 'S' && pUser->type != 's');
- getUserInfo(&pUser -> info);
- }
- /*Function Name: displayAllRecur
- Function Required Input Parameters: pUsers
- Function Expected Return Data: N/A
- Function Description and or Algorithm: This Function display all the users */
- 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;
- }
- /*Function Name: deleteNode
- Function Required Input Parameters: *pFirst, username
- Function Expected Return Data: N/A
- Function Description and or Algorithm: This function deletes unwanted nodes */
- 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;
- }
- }
- }
- /*Function Name: login
- Function Required Input Parameters: *pUsers
- Function Expected Return Data: integer value
- Function Description and or Algorithm: This function allows the user to log in and locks the users account if he exceeds the counter(3) */
- int login(userType *pUsers){
- int ctr=0;
- string15 userchk, passchk;
- printf("Enter Username:");scanf("%s",userchk);
- printf("Enter Password:");scanf("%s",passchk);
- while(pUsers != NULL){
- while(strcmp(pUsers -> username, userchk)!=0){
- pUsers = pUsers->pNext;
- printf("Username Invalid try again\n");
- }
- while(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)!=0)
- {
- ctr++;
- pUsers = pUsers->pNext;
- if(ctr==3){
- printf("counter exceeded account locked... Exiting program");
- pUsers->nlock = 1;
- return 0;
- }
- printf("Incorrect Password try again\n");
- printf("Enter Password:");scanf("%s",&passchk);
- }
- if(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)==0)
- {
- if(pUsers->type=='A' || pUsers->type == 'a')
- {
- printf("going to admin menu\n");
- return 2;
- }
- else if(pUsers->type=='S' || pUsers->type == 's'){
- printf("welcome to the shopper menu\n");
- return 1;
- }
- }
- }
- }
- /*Function Name: lockedacss
- Function Required Input Parameters: *pUsers
- Function Expected Return Data: N/A
- Function Description and or Algorithm: This function displays all the locked accounts*/
- void lockedaccs(userType *pUsers){
- printf("Here are all the locked accounts\n");
- do{
- if(pUsers->nlock==1)
- printf("%s\n",pUsers->username);
- pUsers = pUsers->pNext;
- }while(pUsers!=NULL);
- }
- /*Function Name: manageaccs
- Function Required Input Parameters: *pUsers
- Function Expected Return Data: N/A
- Function Description and or Algorithm: to give the user access to Manage Accounts*/
- void manageaccs(userType *pUsers){
- int g;
- string15 userchk;
- printf("Welcome to Manage Accounts Menu");
- do{
- printf("Type 1 to View Locked Accounts\nType 2 to Unlock Specific Account\nType 3 To Unlock All Locked Accounts\nType 4 to View Accounts with Outstanding Balance\nType 5 to Return to Administrator Menu\n");
- scanf("%d",&g);
- switch(g){
- case 1:lockedaccs(pUsers);break;
- case 2:printf("Type the username of the account you wish to unlock:\n");scanf("%s", &userchk);
- lockedaccs(pUsers);
- do{
- pUsers = pUsers->pNext;
- if(strcmp(pUsers -> username, userchk)==0)
- pUsers->nlock = 0;
- printf("User unlocked");
- }while(pUsers!=NULL);break;
- case 3:printf("Unlocking All Locked Accounts");
- do{
- pUsers = pUsers->pNext;
- pUsers->nlock = 0;
- }while(pUsers!=NULL && pUsers->nlock==1);break;
- case 4:printf("Displaying All Accounts with Outstanding Balance");
- do{
- pUsers = pUsers->pNext;
- if(pUsers->outstanding<0)
- printf("%s", pUsers->username);
- }while(pUsers!=NULL);break;
- case 5:printf("Returning to Admin Menu");break;
- }
- }while(g!=5);
- }
- /*Function Name: addstock
- Function Required Input Parameters: *pNewStock, *pStock, *pStockcateg, *pLastStock, *pNewStcateg, *pLastStcateg
- Function Expected Return Data: N/A
- Function Description and or Algorithm: This Function adds new stocks as long as you dont end the loop */
- void addstock(stocktype *pNewStock, stocktype *pStock, stockcategtype *pStockcateg, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
- int n,g,nqa,nqs;
- float fpp,fusp,fdr;
- char cDump;
- char temp[3] = {0};
- char icateg,isupp,iprod;
- do{//add stockcategtypes later to account for unique categories and str pNewStock for others
- printf("Enter Category:");fgets(pStockcateg->category, 16, stdin);
- printf("You entered %s Category\n",pStockcateg->category);
- printf("Input the name of the supplier:");scanf("%s%c",pNewStock->supplier,&cDump);
- printf("Input name of product:");scanf("%s%c",pNewStock->product,&cDump);
- printf("Enter Available Product Quantity: "); scanf("%d%c", &nqa, &cDump);pNewStock->Qavail = nqa;
- printf("Input Price of the Product: ");scanf("%f%c", &fpp, &cDump);pNewStock->purchasePrice = fpp;
- printf("Input the Unit Selling Price of the product: ");scanf("%f%c", &fusp, &cDump);pNewStock->sellingPrice = fusp;
- printf("Input the Discount Rate of the product: ");scanf("%f%c", &fdr, &cDump);pNewStock->discountRate = fdr;
- printf("type 1 to add new stock\n0 to return to Manage Stocks Menu:");scanf("%d",&n);
- if(n!=1 || n!=0 )
- printf("Invalid Input... Returning you to Manage Stocks Menu");
- }while(n!=0);
- }
- /*Function Name: managestock
- Function Required Input Parameters: *pStock, *pNewStock, *pStockcateg, *pLastStock, *pNewStcateg, *pLastStcateg
- Function Expected Return Data: N/A
- Function Description and or Algorithm: To give the user access to Manage Stocks Menu */
- void managestock(stocktype *pStock, stocktype *pNewStock, stockcategtype *pStockcateg, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
- int g,a;
- printf("Welcome to Manage Stocks Menu");
- do{
- printf("Type 1 to Add new stock\nType 2 to View All Stocks\nType 3 To View stocks by category\nType 4 to View stocks to reorder\nType 5 to Modify Stock Info\n Type 6 to Restock\nType 7 to Save Inventory\n Type 8 to Update Inventory from file\nType 9 to return to Admin Menu:\n");
- scanf("%d",&g);
- switch(g){
- case 1:printf("Adding New Stock");
- do{
- pNewStock = malloc(sizeof(stocktype));//Based almost everything from signup
- addstock(pNewStock,pStock,pStockcateg,pLastStock,pNewStcateg,pLastStcateg);
- pNewStock->pNextstockinfo=NULL;
- pNewStcateg->pNextstockcateg=NULL;
- if(pStock==NULL && pStockcateg){
- pStock=pNewStock;
- pStockcateg=pNewStcateg;
- }
- else
- pLastStock->pNextstockinfo = pNewStock;
- pLastStcateg->pNextstockcateg = pNewStcateg;
- pLastStock = pNewStock;
- pLastStcateg = pNewStcateg;
- }while(a!=0);break;
- case 2:printf("Viewing All Stocks");break;
- case 3:printf("Viewing Stocks by Category");break;
- case 4:printf("View Stocks to reorder");break;
- case 5:printf("Modifying Stock Info");break;
- case 6:printf("Restocking");break;
- case 7:printf("Saving Inventory");break;
- case 8:printf("Updating Inventory from file");break;
- case 9:printf("Returning to Admin menu");break;
- }
- }while(g!=9);
- }
- /*Function Name: AdminMenu
- Function Required Input Parameters: *pUsers, *pInfo, *pNew, *pLast, *end, *pStock, *pNewStock, *pStcateg, *pLastStock, *pNewStcateg, *pLastStcateg
- Function Expected Return Data: N/A
- Function Description and or Algorithm:To give the user access to admin menu */
- void AdminMenu (userType *pUsers, userInfoType *pInfo, userType *pNew, userType *pLast, int *end, stocktype *pStock, stocktype *pNewStock, stockcategtype *pStcateg, int *n, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
- int i;
- printf("Welcome to admin menu\n");
- do{
- printf("Type 1 to go to Manage Accounts Menu\nType 2 to go to Manage Stocks Menu\nType 3 To Prepare Delivery Receipt\nType 4 to Shutdown Kiosk\nType 5 to Log out");
- scanf("%d",&i);
- switch(i){
- case 1:printf("Going to Manage Accounts Menu please wait\n");
- manageaccs(pUsers);break;
- case 2:printf("Going to Manage Stocks Menu please wait\n");
- managestock(pStock, pNewStock, pStcateg, pLastStock,pNewStcateg,pLastStcateg);break;
- case 3:printf("Preparing Delivery Receipt Please Wait\n")/*phase 2*/;break;
- case 4:printf("Shutting Down\n");*end=1;break;//passes 1 to end so that it'll terminate the program
- case 5:printf("Logging Out Goodbye\n");*n=1;break;//assign 1 to n so that it'll return to sign up
- }
- }while(i!=4 && i!=5);
- }
- /*Function Name: ModifyInfo
- Function Required Input Parameters: *pInfo
- Function Expected Return Data: N/A
- Function Description and or Algorithm: To let the user modify his information */
- void ModifyInfo(userInfoType *pInfo){
- int n;
- printf("Type 1 to Modify First Name\nType 2 to Modify Middle Name\nType 3 To Modify Last Name\nType 4 To Modify Address\n");
- scanf("%d",n);
- switch(n){
- case 1:printf("First Name: "); fgets(pInfo -> name.first, 21, stdin);break;
- case 2:printf("Middle Name: "); fgets(pInfo -> name.middle, 21, stdin);break;
- case 3:printf("Last Name: "); fgets(pInfo -> name.last, 21, stdin);break;
- case 4:printf("Enter Address: "); fgets(pInfo -> address, 51, stdin);break;
- }
- printf("Your new name 'Last Name,First Name, Middle Name\n'%s,%s%s \n", pInfo -> name.last, pInfo -> name.first, pInfo -> name.middle);
- printf("Your new Address\n%s", pInfo->address);
- 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';
- }
- /*Function Name:ShopperMenu
- Function Required Input Parameters: *pUsers, *pInfo, *n
- Function Expected Return Data:N/A
- Function Description and or Algorithm: To access shopper menu */
- void ShopperMenu(userType *pUsers, userInfoType *pInfo, int *n){
- int g;
- printf("Welcome to shopper menu\n");
- printf("Type 1 to Modify User Info\nType 2 to Browse All Products\nType 3 To Browse Products by category\nType 4 to Browse Products on Sale\nType 5 to Add to Cart\n Type 6 to View Cart\nType 7 to Settle OutStanding Balance\n Type 8 to Log Out:");
- scanf("%d",&g);
- switch(g){
- case 1:printf("Modify your info\n");
- ModifyInfo(&pUsers->info);break;
- case 2:printf("Browsing All Products");break;
- case 3:printf("Browse Products by Category");break;
- case 4:printf("Browse Products on Sale");break;
- case 5:printf("Adding to cart");break;
- case 6:printf("Viewing Cart");break;
- case 7:printf("Settling Outstanding Balance");break;
- case 8:printf("Logging Out Goodbye\n");*n=1 ;break;
- }
- }
- int main(){
- /*
- userType user;
- userType *pUsers = NULL,
- *pNew;
- */
- ptrUser pUsers = NULL,
- pNew, pLast;
- ptrStock pStock = NULL,
- pNewStock,pLastStock;
- ptrStockcateg pStcateg = NULL,
- pNewStcateg,pLastStcateg;
- int end=1,n;
- char cDump;
- ptrUser pRun, pTrail;
- do{
- printf("Welcome to the shopping kiosk\n\n");
- printf("type 2 if you would like to log in\n1 if you would like to sign up\n0 if you would like to exit the program: ");
- scanf("%d",&n);
- switch(n){
- case 2:if(login(pUsers)==2)
- AdminMenu(pUsers, &pUsers->info, pNew, pLast, &end, pStock, pNewStock, pStcateg, &n, pLastStock, pNewStcateg, pLastStcateg);
- else if(login(pUsers)==1)
- ShopperMenu(pUsers, &pUsers->info, &n);
- else if(login(pUsers)==0)
- end=0;break;
- default:
- /*
- 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,pUsers);
- 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 Return to Log in/Sign up menu or '0' To exit the program. "); scanf("%d%c", &end, &cDump);
- break;
- //use for view locked accounts displayAllRecur(pUsers);
- //displayAll(pUsers);
- }
- } while (end != 0);
- freeAll(&pUsers);
- freeAllstockver(&pStock);
- return 0;
- }
Add Comment
Please, Sign In to add comment