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.
- Kyle Christian Ramon L. Santiago, DLSU ID# 11608722
- **********************************************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define MAXCHAR 16
- #define MAXADD 51
- #define MAXNAME 21
- #define MAXCODE 9
- #define MAXCART 100
- typedef char Char [MAXCHAR];
- typedef char Name [MAXNAME];
- typedef char Add [MAXADD];
- typedef char Code [MAXCODE];
- typedef char Adcd [MAXCODE];
- typedef struct {
- Name First;
- Name Second;
- Name Last;
- }sName;
- typedef struct {
- sName Userinfo;
- Add Address;
- }Userinfo;
- typedef struct {
- Code Prodcode;
- int qty;
- }ProductTag;
- typedef ProductTag arrBought[MAXCART];
- typedef struct Usertag {
- Char username;
- Char password;
- Userinfo info;
- char type;
- float credlimit;
- float outstanding;
- arrBought cart;
- int nItems;
- int count;
- struct Usertag *pNext;
- };
- typedef struct Usertag Usertype;
- typedef Usertype * ptrUser;
- typedef struct {
- Char supplier;
- Char product;
- int quantity;
- float purchase;
- float unit;
- float discount;
- int sold;
- Code code;
- }Productlist;
- typedef struct {
- Char category;
- Productlist list;
- }Stock;
- typedef Stock Stocks[10];
- void getUserinfo (Userinfo *uInfo){
- printf("Enter your First Name: ");
- fgets(uInfo->Userinfo.First ,MAXNAME,stdin);
- printf("Enter your Middle Name: ");
- fgets(uInfo->Userinfo.Second,MAXNAME,stdin);
- printf("Enter your Last Name: ");
- fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
- printf("Enter your Address: ");
- fgets(uInfo->Address,MAXADD,stdin);
- uInfo->Userinfo.Last[strlen(uInfo->Userinfo.Last)-1]='\0';
- uInfo->Userinfo.First[strlen(uInfo->Userinfo.First)-1]='\0';
- uInfo->Userinfo.Second[strlen(uInfo->Userinfo.Second)-1]='\0';
- if (uInfo->Userinfo.First[0] >= 97 && uInfo->Userinfo.First[0] <=122)
- uInfo->Userinfo.First[0]-=32;
- if (uInfo->Userinfo.Second[0] >= 97 && uInfo->Userinfo.Second[0] <=122)
- uInfo->Userinfo.Second[0]-=32;
- if (uInfo->Userinfo.Last[0] >= 97 && uInfo->Userinfo.Last[0] <=122)
- uInfo->Userinfo.Last[0]-=32;
- printf("Your Name is: ");
- printf("%s, %s %s\n",uInfo->Userinfo.Last,uInfo->Userinfo.First,uInfo->Userinfo.Second);
- printf("Your Address is: ");
- printf("%s\n",uInfo->Address);
- }
- int passcheck (Usertype *user){
- int i,count=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'))
- count++;
- if (count==0)
- printf("You must have at least 1 non-letter character\n");
- return count;
- }
- int length (Usertype *user){
- if (strlen (user->password) < 6){
- printf("password is too short \n");
- return 0;
- }
- else if (strlen (user->password) > 15){
- printf("password is too long \n");
- return 0;
- }
- else return 1;
- }
- int Useravailable (Usertype *user, char input[]) {
- ptrUser pTemp;
- while (user != NULL) {
- pTemp = user;
- user = user->pNext;
- if (strcmp (input,pTemp->username)==0){
- printf("Your username is taken. Enter another \n");
- return 1;
- }
- }
- return 0;
- }
- void Signup (Usertype *user, Usertype *list){
- char cDump;
- Adcd Admincode;
- Char input;
- do{
- printf("Username must be 3-15 characters long\n");
- printf("Enter Username: ");
- scanf("%s%c",&input,&cDump);
- } while (Useravailable (list,input) || (strlen (input) < 3|| strlen(input) > 15));
- strcpy(user->username,input);
- do{
- printf("Password must be 6-15 characters long and have 1 non letter character\n");
- printf("Enter Password: ");
- scanf("%s%c",user->password,&cDump);
- } while (length (user)==0 || passcheck(user)==0);
- getUserinfo (&user->info);
- printf("Enter account type [S]hopper or [A]dmin : ");
- scanf("%c",&user->type);
- if (user->type=='s'|| user->type=='S'){
- user->credlimit = 5000.00;
- user->outstanding = 0.00;
- user->nItems = 0;
- user->type = 'S';
- printf("Credit limit: %0.2f \n",user->credlimit);
- printf("Outstanding balance: %0.2f \n",user->outstanding);
- printf("Your cart is empty \n");
- }
- else if (user->type == 'a' || user->type == 'A'){
- printf("Enter Admin Code: \n");
- do{
- scanf("%s",Admincode);
- if (strcmp(Admincode,"DLSU2017")==0)
- user->type = 'A';
- else printf("Wrong code,try again \n");
- } while (strcmp(Admincode,"DLSU2017")!=0);
- }
- user->count=0;
- }
- int login (ptrUser user, ptrUser *pAccount) {
- Char input;
- ptrUser pTemp,pFirst=user;
- if (user == NULL){
- printf("No Users, please sign up\n");
- return 0;
- }
- do {
- printf("Enter Username: ");
- scanf("%s",&input);
- while (user != NULL) {
- pTemp = user;
- user = user->pNext;
- if (strcmp (pTemp->username, input )==0){
- printf("USERNAME FOUND: %s \n",pTemp->username);
- if (pTemp->count < 3){
- do {
- printf("Enter Password: ");
- scanf("%s",&input);
- if (strcmp (pTemp->password, input)!=0){
- printf("Try again, %d tries left\n",3-pTemp->count);
- pTemp->count++;
- }
- } while (pTemp->count < 4 && strcmp (pTemp->password,input) != 0);
- }
- printf("SUCCESFUL %s \n",pTemp->username);
- if (strcmp(pTemp->password, input) == 0){
- *pAccount = pTemp;
- return 1;
- }
- else if (pTemp->count > 3) {
- printf("Your account has been locked, contact an admin \n");
- return 0;
- }
- }
- }
- user=pFirst;
- } while (strcmp (user->username,input));
- return 0;
- }
- void displayAll (ptrUser pUser){
- while (pUser != NULL) {
- printf("%s\n",pUser->username);
- pUser=pUser->pNext;
- }
- }
- void Displaylock (Usertype *user){
- printf("Locked Accounts \n");
- while (user != NULL) {
- if (user->count > 3)
- printf("%s\n",user->username);
- user = user->pNext;
- }
- }
- ptrUser search (ptrUser pFirst, Char username){
- ptrUser pRun;
- pRun = pFirst;
- while (pRun != NULL && strcmp(username,pRun->username) != 0)
- pRun = pRun->pNext;
- return pRun;
- }
- void Unlockaccount (Usertype *user) {
- Char input;
- ptrUser pTemp;
- printf("Enter Account to Unlock \n");
- scanf("%s",&input);
- pTemp=search (user,input);
- pTemp->count = 0;
- printf("Account of %s has been unlocked \n",pTemp->username);
- }
- void Unlockall (Usertype *user) {
- while (user != NULL) {
- if (user->count > 3)
- user->count = 0;
- user = user->pNext;
- }
- printf("All Accounts Unlocked \n");
- }
- void Viewaccounts (Usertype *user) {
- while (user != NULL) {
- if (user->outstanding)
- printf("%s has an outstanding balance of %0.2f \n",user->username,user->outstanding);
- user = user->pNext;
- }
- }
- void Manageaccounts (Usertype *user) {
- int exit=1,input;
- do {
- printf("Manage Accounts \n");
- printf("Choose an option \n");
- printf("View Locked Accounts : 1 \n");
- printf("Unlock Specific Account : 2 \n");
- printf("Unlock All Locked Accounts : 3 \n");
- printf("View Accounts With Outstanding Balance : 4 \n");
- printf("Return to Admin Menu : 5 \n");
- scanf("%d",&input);
- switch (input) {
- case 1: Displaylock (user); break;
- case 2: Unlockaccount (user); break;
- case 3: Unlockall (user); break;
- case 4: Viewaccounts (user); break;
- case 5: exit = 0; break;
- }
- }while (exit);
- }
- void Viewallstock (Stocks *stock, int *nCat) {
- int i,n;
- printf("Viewing All Stock\n");
- printf(" Category | Supplier | Product Code | Product | Available | Sold | Purchase | Selling | Discount\n");
- for (i=0;i<(*nCat);i++){
- printf(" %s ",stock[i]->category);
- for (n=0;n<(15-strlen(stock[i]->category));n++)
- printf(" ");
- printf("%s ",stock[i]->list.supplier);
- for (n=0;n<(15-strlen(stock[i]->list.supplier));n++)
- printf(" ");
- printf("%s ",stock[i]->list.code);
- printf("%s",stock[i]->list.product);
- for (n=0;n<(15-strlen(stock[i]->list.product));n++)
- printf(" ");
- if (stock[i]->list.quantity>999)
- printf("%d ",stock[i]->list.quantity);
- else if (stock[i]->list.quantity>99)
- printf(" %d ",stock[i]->list.quantity);
- else if (stock[i]->list.quantity>9)
- printf(" %d ",stock[i]->list.quantity);
- else
- printf(" %d ",stock[i]->list.quantity);
- if (stock[i]->list.sold>999)
- printf("%d ",stock[i]->list.sold);
- else if (stock[i]->list.sold>99)
- printf(" %d ",stock[i]->list.sold);
- else if (stock[i]->list.sold>9)
- printf(" %d ",stock[i]->list.sold);
- else
- printf(" %d ",stock[i]->list.sold);
- printf("\t");
- printf("%0.2f ",stock[i]->list.purchase); printf("\t");
- printf("%0.2f ",stock[i]->list.unit); printf("\t");
- printf("%0.1f \n",stock[i]->list.discount);
- }
- }
- void Addstock (Stocks *stock,int *nCat) {
- int i,valid=1,n;
- Char input;
- do {
- printf("Enter Category : ");
- fgets (input,16,stdin);
- fgets (input,16,stdin);
- } while (strlen(input)< 3 || strlen(input)> 15);
- strcpy(stock[*nCat]->category,input);
- printf("Enter Supplier : ");
- fgets (stock[*nCat]->list.supplier,16,stdin);
- printf("Enter Product : ");
- fgets (stock[*nCat]->list.product,16,stdin);
- printf("Enter Quantity Available : ");
- scanf("%d",&stock[*nCat]->list.quantity);
- printf("Enter Purchase Price : ");
- scanf("%f",&stock[*nCat]->list.purchase);
- printf("Enter Unit Selling Price : ");
- scanf("%f",&stock[*nCat]->list.unit);
- printf("Enter Discount Rate : ");
- scanf("%f",&stock[*nCat]->list.discount);
- stock[*nCat]->list.sold = 0;
- stock[*nCat]->list.code[0] = stock[*nCat]->category[0];
- stock[*nCat]->list.code[1] = stock[*nCat]->list.supplier[0];
- stock[*nCat]->list.code[2] = stock[*nCat]->list.product[0];
- for (i=3;i<8;i++)
- stock[*nCat]->list.code[i] = rand() % 10 + '0';
- stock[*nCat]->category[strlen(stock[*nCat]->category)-1] = '\0';
- stock[*nCat]->list.supplier[strlen(stock[*nCat]->list.supplier)-1] ='\0';
- stock[*nCat]->list.product [strlen(stock[*nCat]->list.product)-1] = '\0';
- stock[*nCat]->list.code[8] = '\0';
- printf("Stock Added\n");
- (*nCat)++;
- }
- void Modifystock (Stocks *stock, int *nCat) {
- int exit =1, input, i,n,y;
- Code code;
- printf("Modify Stock");
- Viewallstock (stock,nCat);
- printf("Enter product code: ");
- scanf("%s",&code);
- for (i=0;i<(*nCat);i++){
- if (strcmp(stock[i]->list.code,code)==0){
- do {
- printf("Choose an option \n");
- printf("Modify Category : 1\n");
- printf("Modify Supplier : 2\n");
- printf("Modify Product : 3\n");
- printf("Modify Purchase Price : 4\n");
- printf("Modify Selling Price : 5\n");
- printf("Modify Discount : 6\n");
- printf("Finish Mod of product %s : 7\n",stock[i]->list.code);
- scanf("%d",&input);
- switch (input) {
- case 1: printf("Enter New Category : ");
- fgets (stock[i]->category,16,stdin);
- fgets (stock[i]->category,16,stdin);
- printf("New Category is %s\n",stock[i]->category); break;
- case 2: printf("Enter New Supplier : ");
- fgets (stock[i]->list.supplier,16,stdin);
- fgets (stock[i]->list.supplier,16,stdin);
- printf("New Supplier is %s\n",stock[i]->list.supplier); break;
- case 3: printf("Enter New Product Name : ");
- fgets (stock[i]->list.product,16,stdin);
- fgets (stock[i]->list.product,16,stdin);
- printf("New Product Name is %s\n",stock[i]->list.product); break;
- case 4: printf("Enter New Purchase Price : ");
- scanf("%f",&stock[i]->list.purchase);
- printf("New Purchase Price is %0.2f\n",stock[i]->list.purchase); break;
- case 5: printf("Enter New Selling Price : ");
- scanf("%f",&stock[i]->list.unit);
- printf("New Selling Price is %0.2f\n",stock[i]->list.unit); break;
- case 6: printf("Enter New Discount : ");
- scanf("%f",&stock[i]->list.discount);
- printf("New Discount Rate is %0.1f\n",stock[i]->list.discount); break;
- case 7: if (stock[i]->list.code[0]!=stock[i]->category[0])
- stock[i]->list.code[0]=stock[i]->category[0];
- if (stock[i]->list.code[1]!=stock[i]->list.supplier[0])
- stock[i]->list.code[1]=stock[i]->list.supplier[0];
- if (stock[i]->list.code[2]!=stock[i]->list.product[0])
- stock[i]->list.code[2]=stock[i]->list.product[0];
- for (n=0;n<(*nCat);n++){
- if (n!=i)
- if (strcmp (stock[i]->list.code,stock[n]->list.code)==0)
- for (y=3;y<9;y++)
- stock[i]->list.code[y]= rand()%10+'0';
- }
- printf("Product Code is %s \n",stock[i]->list.code);
- exit = 0; break;
- }
- } while (exit);
- }
- }
- }
- void Restock (Stocks *stock,int *nCat){
- int exit = 1, input, i,change;
- Code code;
- do {
- change = 0;
- Viewallstock (stock,nCat);
- printf("Enter Product Code : ");
- scanf("%s",code);
- printf("Enter Quantity to be added : ");
- scanf("%d",&input);
- for (i=0;i<(*nCat);i++)
- if (strcmp (stock[i]->list.code,code)==0){
- stock[i]->list.quantity += input;
- change = 1;
- }
- if (i = (*nCat) && change==0)
- exit = 0;
- } while (exit);
- }
- void Managestocks (Stocks *stock, int *nCat) {
- int exit=1,input;
- do {
- printf("Manage Stocks \n");
- printf("Choose an option \n");
- printf("Add new stock : 1 \n");
- printf("View all stocks : 2 \n");
- printf("View stocks by category : 3 \n");
- printf("View stocks to reorder : 4 \n");
- printf("Modify stock info : 5 \n");
- printf("Restock : 6 \n");
- printf("Save inventory : 7 \n");
- printf("Update inventory from file : 8 \n");
- printf("Return to Admin Menu : 9 \n");
- scanf("%d",&input);
- switch (input) {
- case 1: Addstock (stock,nCat); break;
- case 2: Viewallstock (stock,nCat); break;
- // case 3: Viewstockcat (stock); break;
- // case 4: Viewreorder (stock); break;
- case 5: Modifystock (stock,nCat); break;
- case 6: Restock (stock,nCat); break;
- // case 7: Saveinventory (stock); break;
- // case 8: Updateinventory (stock); break;
- case 9: exit = 0;
- }
- } while (exit);
- }
- void Adminmenu (Usertype *user,Usertype *All,Stocks *stock, int *esc, int *nCat){
- int exit=1,input;
- do {
- printf("ADMIN MENU \n");
- printf("Choose an option \n");
- printf("Manage Accounts: 1 \n");
- printf("Manage Stocks: 2 \n");
- printf("Prepare Delivery Report: 3 \n");
- printf("Shutdown Kioski: 4 \n");
- printf("Log out: 5 \n");
- scanf("%d",&input);
- switch (input) {
- case 1: Manageaccounts (All); break;
- case 2: Managestocks (stock,nCat); break;
- // case 3: Deliveryreport (user);
- case 4: *esc = 0;
- case 5: exit = 0;
- }
- } while (exit);
- }
- void Changename (Userinfo *uInfo){
- printf("Enter your First Name: ");
- fgets(uInfo->Userinfo.First,MAXNAME,stdin);
- fgets(uInfo->Userinfo.First,MAXNAME,stdin);
- printf("Enter your Middle Name: ");
- fgets(uInfo->Userinfo.Second,MAXNAME,stdin);
- printf("Enter your Last Name: ");
- fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
- uInfo->Userinfo.Last[strlen(uInfo->Userinfo.Last)-1]='\0';
- uInfo->Userinfo.First[strlen(uInfo->Userinfo.First)-1]='\0';
- uInfo->Userinfo.Second[strlen(uInfo->Userinfo.Second)-1]='\0';
- if (uInfo->Userinfo.First[0] >= 97 && uInfo->Userinfo.First[0] <=122)
- uInfo->Userinfo.First[0]-=32;
- if (uInfo->Userinfo.Second[0] >= 97 && uInfo->Userinfo.Second[0] <=122)
- uInfo->Userinfo.Second[0]-=32;
- if (uInfo->Userinfo.Last[0] >= 97 && uInfo->Userinfo.Last[0] <=122)
- uInfo->Userinfo.Last[0]-=32;
- printf("Your New Name is: ");
- printf("%s, %s %s\n",uInfo->Userinfo.Last,uInfo->Userinfo.First,uInfo->Userinfo.Second);
- }
- void Changeadd (Userinfo *uInfo) {
- printf("Enter your Address: ");
- fgets(uInfo->Address,MAXADD,stdin);
- fgets(uInfo->Address,MAXADD,stdin);
- printf("Your New Address is: ");
- printf("%s\n",uInfo->Address);
- }
- void Changepass (Usertype *user) {
- char cDump;
- do{
- printf("Enter Password: ");
- scanf("%s%c",user->password,&cDump);
- } while (length (user)==0 || passcheck(user)==0);
- printf("Your new password is: ");
- printf("%s\n", user->password);
- }
- void Modifyinfo (Usertype *user) {
- int exit = 1,input;
- do {
- printf("Modify User Menu \n");
- printf("Choose an option \n");
- printf("Change Name: 1 \n");
- printf("Change Address: 2 \n");
- printf("Change Password: 3 \n");
- printf("Return to Shopper Menu: 4 \n");
- scanf("%d",&input);
- switch (input) {
- case 1: Changename (&user->info); break;
- case 2: Changeadd (&user->info); break;
- case 3: Changepass (user); break;
- case 4: exit = 0;
- }
- } while (exit);
- }
- void Settlebalance (Usertype *user) {
- int card,security,paid=1;
- float payment;
- printf("Your outstanding balance is %.2f \n",user->outstanding);
- printf("Enter your credit card number (without spaces): ");
- scanf("%d",&card);
- printf("Enter your security code: ");
- scanf("%d",&security);
- do {
- printf("Enter amount: ");
- scanf("%f",&payment);
- if (payment <= user->outstanding){
- user->outstanding -= payment;
- paid=0;
- }
- else
- printf("You entered too much!");
- } while (paid);
- printf("Your new balance is %.2f\n",user->outstanding);
- }
- void Viewallprod (Stocks *stock, int *nCat){
- int i,n;
- printf("Viewing All Stock\n");
- printf(" Category | Brand | Product Code | Product | Available | Sold | Price | Discount\n");
- for (i=0;i<(*nCat);i++){
- if (stock[i]->list.quantity > 0) {
- printf(" %s ",stock[i]->category);
- for (n=0;n<(15-strlen(stock[i]->category));n++)
- printf(" ");
- printf("%s ",stock[i]->list.supplier);
- for (n=0;n<(15-strlen(stock[i]->list.supplier));n++)
- printf(" ");
- printf("%s\t",stock[i]->list.code);
- printf("%s ",stock[i]->list.product);
- for (n=0;n<(15-strlen(stock[i]->list.product));n++)
- printf(" ");
- if (stock[i]->list.quantity>999)
- printf("%d ",stock[i]->list.quantity);
- else if (stock[i]->list.quantity>99)
- printf(" %d ",stock[i]->list.quantity);
- else if (stock[i]->list.quantity>9)
- printf(" %d ",stock[i]->list.quantity);
- else
- printf(" %d ",stock[i]->list.quantity);
- if (stock[i]->list.sold>999)
- printf("%d ",stock[i]->list.sold);
- else if (stock[i]->list.sold>99)
- printf(" %d ",stock[i]->list.sold);
- else if (stock[i]->list.sold>9)
- printf(" %d ",stock[i]->list.sold);
- else
- printf(" %d ",stock[i]->list.sold);
- printf("\t");
- printf("%0.2f ",stock[i]->list.unit); printf("\t");
- printf("%0.1f \n",stock[i]->list.discount);
- }
- }
- }
- void Shoppermenu (Usertype *user,Stocks *stock, int nCat){
- int exit = 1,input;
- do {
- printf("Shopper Menu \n");
- printf("Hello %s\n", user->info.Userinfo.First);
- printf("Choose an option \n");
- printf("Modify User Info: 1 \n");
- printf("Browse All Products: 2 \n");
- printf("Browse Products by Category: 3 \n");
- printf("Browse Products on Sale: 4 \n");
- printf("Add to Cart: 5 \n");
- printf("View Cart: 6 \n");
- if (user->outstanding != 0.00)
- printf("Settle Outstanding Balance: 7 \n");
- printf("Log out: 8 \n");
- scanf ("%d",&input);
- switch (input) {
- case 1: Modifyinfo (user); break;
- case 2: Viewallprod (stock,&nCat); break;
- case 3:
- case 4:
- case 5:
- case 6:
- case 7: Settlebalance (user); break;
- case 8: exit = 0;
- }
- } while (exit);
- }
- void freeAll (ptrUser *pUser) {
- ptrUser pTemp;
- while ((*pUser) != NULL){
- pTemp= *pUser;
- *pUser = (*pUser)->pNext;
- free(pTemp);
- }
- }
- int main () {
- int exit=1,input, nCat = 0;
- Usertype user;
- ptrUser pUsers = NULL, pNew, pLast, pAccount, pRun, pTrail;
- Stocks stock;
- do {
- printf("Main Menu \n");
- printf("Choose an option: \n");
- printf("Log-in : 1\n");
- printf("Sign-up : 2\n");
- scanf("%d",&input);
- switch (input){
- case 1: if (login (pUsers,&pAccount)){
- if (pAccount->type == 'S')
- Shoppermenu (pAccount,&stock,nCat);
- if (pAccount->type == 'A')
- Adminmenu (pAccount,pUsers,&stock,&exit,&nCat);
- } break;
- case 2: pNew = malloc(sizeof(Usertype));
- Signup (pNew,pUsers);
- pNew->pNext = NULL ;
- if (pUsers == NULL)
- pUsers = pNew;
- else if (strcmp (pUsers->username, pNew->username)>0){
- pNew->pNext = pUsers;
- pUsers = pNew;
- }
- else {
- 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: displayAll (pUsers); break;
- }
- } while (exit);
- freeAll(&pUsers);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement