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.
- DE CASTRO, Julian Paolo Ramirez, DLSU ID# 11612231
- ******************************************************************************************************************/
- /**** ADDITIONAL STATEMENT: Some/Most variables used below are the ones given in class. Phase 2 is not complete.****/
- #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 char string9[10];
- typedef struct userName{
- string20 first,
- middle,
- last;
- }nameType;
- typedef struct{
- nameType name;
- string50 address;
- }userInfoType;
- typedef struct cartcontent{
- string8 code;
- int qty;
- }prodBoughtType;
- typedef prodBoughtType arrBought[MAX_CART];
- /* FOR BINARY FILES
- typedef struct{
- string15 username,
- password;
- userInfoType info;
- char type;
- float creditLimit,
- outstanding;
- arrBought cart;
- int accountVar,
- nTry;
- int items;
- }dataType;
- */
- struct userTag{
- string15 username,
- password;
- userInfoType info;
- char type;
- float creditLimit,
- outstanding;
- arrBought cart;
- int accountVar,
- nTry;
- int items;
- struct userTag *pNext;
- };
- typedef struct userTag userType;
- typedef struct userTag * ptrUser;
- typedef struct stocks{
- string15 category;
- string15 supplier;
- string15 product;
- string8 prodCode;
- int inStock;
- int sold;
- float buyingPrice;
- float sellingPrice;
- float discountPercent;
- }stockInfo;
- /* Function displayAll displays all accounts
- @param ptrUser pUsers: pointer to the link list
- */
- void displayAll(ptrUser pUsers){//displays all accounts made during the session
- printf("\n -All Accounts Made- \n");
- while (pUsers != NULL){
- printf("%s\n", pUsers -> username);
- pUsers = pUsers -> pNext;
- }
- }
- /* Function freeAll frees all malloc used
- @param ptrUser * pFirst: pointer to the first node in
- link list created
- */
- void freeAll(ptrUser * pFirst){//Frees all malloc used
- ptrUser pDel;
- while (*pFirst != NULL){
- pDel = *pFirst;
- *pFirst = (*pFirst) -> pNext;
- free(pDel);
- }
- }
- /* Function getUserInfo obtains the user information by asking the
- current user to input his/her data
- userInfoType * pInfo: stores info into its structure
- */
- void getUserInfo(userInfoType * pInfo){//Contains User Lists
- 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){
- char cDump;
- 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);
- getUserInfo(&pUser -> info);
- }*/
- /* Function signUp allows the user to create an admin or shopper account
- @param userType * pSignUp: node that will contain the current users sign up sheet and info
- @param ptrUser pUsers: pointer to the link list
- */
- void signUp (userType * pSignUp, ptrUser pUsers){//Sign-Up Function; adds a new user to the list
- char cDump, type = '\0', specialChar = 0, sameUser;
- char adminInput[9], adminCode[9] = {"DLSU2017"} ;
- ptrUser pTemp, pFirst = pUsers;
- do{//Username Creator
- sameUser = 0;
- printf("Enter a username (must have 3-15 characters): "); scanf("%s%c", pSignUp -> username, &cDump);
- if ((strlen(pSignUp -> username)) < 3 || (strlen(pSignUp -> username)) > 15)
- printf("Your Username is either too short or too long, please try again. \n");
- while(pUsers != NULL && !sameUser){
- pTemp = pUsers;
- pUsers = pUsers -> pNext;
- if (!strcmp(pTemp -> username, pSignUp -> username)){
- printf("Username taken, please think of a new one. \n");
- sameUser = 1;
- }
- }
- pUsers = pFirst;
- }while((strlen(pSignUp -> username)) < 3 || (strlen(pSignUp -> username)) > 15 || sameUser);
- do{//Password Creator for a new user
- printf("Enter a password (must have 6-15 character \nand must have at least 1 non-letter): \n ");
- scanf("%s%c", pSignUp -> password, &cDump);
- specialChar=passwordVal(pSignUp);
- if (!specialChar)
- printf("Invalid. Please have at least one non-letter in your password \n");
- if ((strlen(pSignUp->password)) < 6 || (strlen(pSignUp->password)) > 15 )
- printf("Invalid length. Please try again \n");
- }while(((strlen(pSignUp->password)) < 6 || (strlen(pSignUp->password)) > 15) || !specialChar);
- getUserInfo(&pSignUp -> info);
- do{//Asks the user which type of account he/she wants to make
- pSignUp -> accountVar = -1;
- printf("Choose account type. [A]dministrator or [S]hopper: "); scanf("%c%c", &type, &cDump);
- if (type == 'A' || type == 'a'){
- printf("Enter authorization code: "); scanf("%s%c", adminInput, &cDump);
- if (strcmp(adminInput, adminCode) == 0){
- printf("Code Accepted \n");
- pSignUp -> accountVar = 1;
- }
- else printf("Wrong Code, try again \n");
- }
- else if (type == 'S' || type == 's' ){
- pSignUp -> accountVar = 0;
- pSignUp -> creditLimit = 5000.0;
- pSignUp -> outstanding = 0.0;
- pSignUp -> items = 0;
- }
- else
- printf("Invalid Input, please try again \n");
- }while(pSignUp -> accountVar != 1 && pSignUp -> accountVar != 0);
- if(pSignUp -> accountVar)//Displays Admin Account Info
- printf(" \n Admin Account Made. \n Username: %s \n Password: %s \n Account Type: Administrator \n Name: %s %s %s \n Address: %s \n \n" ,
- pSignUp -> username,
- pSignUp -> password,
- pSignUp -> info.name.first,
- pSignUp -> info.name.middle,
- pSignUp -> info.name.last,
- pSignUp -> info.address);
- else//Displays Shopper Account Info
- printf(" \n Shopper Account Made. \n Username: %s \n Password: %s \n Account Type: Shopper \n Name: %s %s %s \n Address: %s \n Credit Limit: P%.2f \n Outstanding Balance: P%.2f \n Cart is empty. \n \n",
- pSignUp -> username,
- pSignUp -> password,
- pSignUp -> info.name.first,
- pSignUp -> info.name.middle,
- pSignUp -> info.name.last,
- pSignUp -> info.address,
- pSignUp -> creditLimit,
- pSignUp -> outstanding);
- }
- /* Function search is used to find a specific username
- @param ptrUser pFirst: points to the first node of the link list
- @param string15 username: contains the usernames of all made accounts
- @return pRun: returns if the username was found or not
- */
- ptrUser search(ptrUser pFirst, string15 username){
- ptrUser pRun;
- pRun = pFirst;
- while (pRun != NULL && strcmp(username, pRun -> username) != 0){
- pRun = pRun -> pNext;
- }
- return pRun;
- }
- /* Function passwordVal checks if the user has inputed a valid password
- @param userType * user: points to the list of usernames
- @return result: returns if the password is valid or not
- */
- int passwordVal(userType * user){//Password Validator
- int i;
- int result = 0;
- for(i=0; i<(strlen(user -> password)); i++)
- if (user -> password[i] < 'a' || user -> password[i] >'z')
- if (user -> password[i] < 'A' || user -> password[i] > 'Z')
- result = 1;
- return result;
- }
- /* Function userLogin allows the current user to login into an existing account
- @param ptrUser pUsers: points to the link list of users
- @param ptrUser * pCurrentUser: points to the current user using the computer
- @return account: checks and reaturns if the account is valid and was be able to logged into
- */
- int userLogin (ptrUser pUsers, ptrUser * pCurrentUser){//Log-In fucntion; allows the user to access his/her account
- int validUser = 0, validPassword = 0, account = -1;
- string15 LogInInput;
- string15 passwordInput;
- ptrUser pTemp, pFirst = pUsers;
- do{
- printf("Username: "); scanf("%s", LogInInput);
- printf("Password: "); scanf("%s", passwordInput);
- while (pUsers != NULL && !validUser){
- pTemp = pUsers;
- pUsers = pUsers -> pNext;
- if (!strcmp(LogInInput, pTemp -> username) && (pTemp->nTry)!=3)
- validUser = 1;
- if (!strcmp(passwordInput, pTemp -> password) && (pTemp->nTry)!=3)
- validPassword = 1;
- if (validPassword && validUser && (pTemp->nTry)!=3){
- *pCurrentUser = pTemp;
- account = pTemp -> accountVar;
- }
- }
- if ((pTemp->nTry)>=3)
- printf("\n +++Exeeded number of tries. Account has been locked. \n Please Contact an Administrator.+++ \n");
- else if (!validUser)
- printf("\n Wrong Username, please try again. \n");
- else if (!validPassword){
- (pTemp -> nTry)++;
- printf("\n Wrong password, you have %d attemp/s left. \n", 3 - (pTemp -> nTry));
- }
- else
- return account;
- pUsers = pFirst;
- validUser = 0;
- validPassword = 0;
- } while ((!validUser && !validPassword) || (pTemp -> nTry)<3);
- return -1;
- }
- /* Function deleteNode is used to delete unwanted accounts from the list
- @param ptrUser * pFirst: points to the first node of the link list
- @param string15 username: deletes unwanted username
- */
- void deleteNode(ptrUser *pFirst, string15 username){//a fucntion where you can search and delete a 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 saveToTextFile allows the user to save the usernames and passwords of the users who made accounts during the
- current session
- @param ptrUser pFirst: the first node of the link list
- */
- void saveToTextFile(ptrUser pFirst){
- FILE *pText;
- string20 strName;
- if (pFirst == NULL)
- printf("There is nothing to save.\n");
- else{
- printf("\nEnter Filename: "); scanf("%s", strName);
- pText = fopen(strName, "wt"); // fopen(filename, filemade);
- if (pText != NULL){
- while (pFirst != NULL){
- fprintf(pText, "%s %s\n", pFirst -> username, pFirst -> password); //fprintf file formatted printing
- pFirst = pFirst -> pNext;
- }
- fclose(pText);
- }
- else printf("Error in Saving File...\n");
- }
- }
- /* Function loadToText allows the user to load the usernames and passwords of the users who made accounts during the
- current session
- @param string20 strFile: allows the user to open a external txt file to load from
- @param ptrUser * pFirst: points to the first node of the link list
- */
- void loadFromText(string20 strFile, ptrUser * pFirst){
- FILE * pFile;
- string20 user, pass;
- ptrUser pNew;
- char ch;
- if ((pFile = fopen(strFile, "rt")) != NULL){
- fscanf (pFile, "%s%s", user, pass);
- printf("user = %s, pass = %s\n", user, pass);
- while(fscanf(pFile, "%s %s", user, pass) == 2){
- //creation of linked list
- pNew = malloc(sizeof(userType));
- strcpy(pNew -> username, user);
- strcpy(pNew -> password, pass);
- //name
- fgets(pNew -> info.name.first, 21, pFile);
- pNew -> info.name.first[strlen(pNew -> info.name.first) - 1] = '\0';
- //type
- ch = fgetc(pFile);
- pNew -> type = ch;
- fgetc(pFile);
- //credit limit
- fscanf(pFile, "%f%f", &pNew -> creditLimit, &pNew -> outstanding);
- fgetc(pFile); //extra new line
- //always insert at head of list
- pNew -> pNext = *pFirst;
- *pFirst = pNew;
- }
- fclose(pFile);
- }
- else printf("Error in Loading File...\n");
- }
- /* Function adminMainMenu allows the admin to view the admin menu
- */
- void adminMainMenu(){
- int choice;
- do{
- printf("\n\n ADMIN MENU: \n");
- printf("1 - Manage Accounts\n");
- printf("2 - Manage Stocks\n");
- printf("3 - Prepare Delivery Receipt\n");
- printf("4 - Shutdown\n");
- printf("5 - Log-Out\n");
- printf("Choice: "); scanf("%d", &choice); fflush(stdin);
- switch(choice){
- case 1: //manageAccount();
- break;
- case 2: //manageStocks();
- break;
- case 3: break;
- case 4: //*shutdown=1;
- break;
- case 5: break;
- }
- printf("\n");
- }while(choice != 4 && choice != 5);
- }
- /* Function shopperMainMenu allows the shopper to view the shopper menu
- */
- void shopperMainMenu(){
- int choice;
- do{
- printf("\n\nSHOPPER 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");
- printf("Choice: "); scanf("%d", &choice); fflush(stdin);
- switch(choice){
- case 1: //modifyInfo();
- break;
- case 2: //displayStocks();
- break;
- case 3: break;
- case 4: break;
- case 5: break;
- case 6: break;
- case 7: break;
- case 8: break;
- default: ;
- }
- printf("\n");
- }while(choice!=8);
- }
- int main(){
- ptrUser pUsers = NULL,
- pNew, pLast;
- int opt = 1, userInput = 0, LogIn;
- char cDump;
- ptrUser pRun, pTrail,
- pCurrentUser;
- string20 strFile;
- do{//Prints the main menu abd asks for the users input
- printf("\n Main Menu \n");
- printf("Please input the number of your selection: \n");
- printf("1 - Sign-Up \n");
- printf("2 - Log-In \n");
- printf("3 - Exit Program \n");
- printf("Your Option: "); scanf("%d%c", &userInput, &cDump);
- switch (userInput){
- case 1: pNew = malloc(sizeof(userType)); //Sign-Up Case
- pNew -> pNext = NULL;
- signUp(pNew, pUsers);
- if (pUsers == NULL) //first user
- pUsers = pNew;
- else if (strcmp(pUsers -> username, pNew -> username) > 0){ //new users
- pNew -> pNext = pUsers;
- pUsers = pNew; break;
- case 2: LogIn = userLogin(pUsers, &pCurrentUser);//Log-In Case
- if (LogIn == -1)
- printf("Failed to Login \n");
- else if (LogIn){
- printf("-Welcome Admin- \n \n");
- adminMainMenu();
- }
- else {
- printf("-Welcome Shopper- \n \n");
- shopperMainMenu();
- }break;
- case 3: printf("Shutting Down Program..........");//Shutdown Case (for convinience)
- //freeAll(&pUsers);
- opt = 0;
- }
- }
- } while (opt == 1);
- saveToTextFile(pUsers);
- printf("Enter Filename: "); scanf("%s", strFile);
- loadFromText(strFile, &pUsers);
- freeAll(&pUsers);
- displayAll(pUsers);
- pUsers = NULL;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement