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
- #define MAXCAT 10
- 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;
- int checkout;
- struct Usertag *pNext;
- struct Usertag *pPrev;
- };
- typedef struct Usertag Usertype;
- typedef Usertype * ptrUser;
- typedef struct Productlist{
- Char supplier;
- Char product;
- int quantity;
- float purchase;
- float unit;
- float discount;
- int sold;
- Code code;
- struct Productlist *pNext;
- };
- typedef struct Productlist Prodlist;
- typedef struct {
- Char category;
- Prodlist *list;
- int count;
- }Stock;
- typedef Stock Stocks[MAXCAT];
- void getUserinfo (Userinfo *uInfo){ /* Gets user's information */
- 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){ /* Checks if there is a non-letter character */
- 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){ /* Checks the length of password */
- 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[]) { /* Checks if user name is available */
- 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){ /* Signup */
- 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->checkout = 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) { /* login */
- 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;
- }
- }
- }
- printf("USERNAME NOT FOUND \n");
- user=pFirst;
- } while (strcmp (user->username,input));
- return 0;
- }
- void displayAll (ptrUser pUser){ /* Displays all accounts */
- while (pUser != NULL) {
- printf("%s\n",pUser->username);
- pUser=pUser->pNext;
- }
- }
- void Displaylock (Usertype *user){ /* Displays all locekd accounts */
- 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){ /* Searches for specific user */
- ptrUser pRun;
- pRun = pFirst;
- while (pRun != NULL && strcmp(username,pRun->username) != 0)
- pRun = pRun->pNext;
- return pRun;
- }
- void Unlockaccount (Usertype *user) { /* Unlocks a specific 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) { /* Unlocks all users */
- while (user != NULL) {
- if (user->count > 3)
- user->count = 0;
- user = user->pNext;
- }
- printf("All Accounts Unlocked \n");
- }
- void Viewaccounts (Usertype *user) { /* Displays all users with outstanding balance */
- 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) { /* Manage accounts menu */
- 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 n) { /* Displays all stocks */
- int i,count=0;
- char cDump;
- Prodlist *temp;
- printf("Viewing All Stock\n");
- if (n==0){
- for (i=0;i<(*nCat);i++){
- temp = stock[i]->list;
- while (temp!=NULL){
- if (count == 0)
- printf(" Category Supplier Product Code Product Available Sold Purchase Selling Discount\n");
- printf("%15s ",stock[i]->category);
- printf("%15s ",temp->supplier);
- printf("%14s ",temp->code);
- printf("%15s ",temp->product);
- printf("%9d ",temp->quantity);
- printf("%4d ",temp->sold);
- printf("%7.2f ",temp->purchase);
- printf("%7.2f ",temp->unit);
- printf("%7.2f \n",temp->discount);
- count++;
- if (count > 20) {
- printf("NEXT PAGE ");
- scanf("%c",&cDump);
- scanf("%c",&cDump);
- count = 0;
- }
- temp=temp->pNext;
- }
- }
- }
- else {
- for (i=0;i<(*nCat);i++){
- temp = stock[i]->list;
- while (temp!=NULL){
- if (count == 0)
- printf(" Category Brand Product Code Product Available Sold Unit Discount\n");
- printf("%15s ",stock[i]->category);
- printf("%15s ",temp->supplier);
- printf("%14s ",temp->code);
- printf("%15s ",temp->product);
- printf("%9d ",temp->quantity);
- printf("%4d ",temp->sold);
- printf("%7.2f ",temp->unit);
- printf("%7.2f \n",temp->discount);
- count++;
- if (count > 20) {
- printf("NEXT PAGE ");
- scanf("%c",&cDump);
- scanf("%c",&cDump);
- count = 0;
- }
- temp=temp->pNext;
- }
- }
- }
- }
- int Getcat (Stocks *stock, Char input, int nCat) {
- int i;
- if (nCat==0)
- return 0;
- else {
- for (i=0;i<nCat;i++)
- if (strcmp(input,stock[i]->category)==0)
- return i;
- return nCat;
- }
- }
- void Alphabeticalindex (Stocks *stock, int nCat) {
- int swap, i;
- Char sTemp;
- Prodlist *pTemp;
- do{
- swap = 0;
- for(i=0; i<(nCat)-1; i++)
- if(strcmp(stock[i] -> category, stock[i+1] -> category)>0){
- strcpy(sTemp, stock[i] -> category);
- strcpy(stock[i] -> category, stock[i+1] -> category);
- strcpy(stock[i+1] -> category, sTemp);
- pTemp = stock[i] -> list;
- stock[i] -> list = stock[i+1] -> list;
- stock[i+1] -> list = pTemp;
- swap=1;
- }
- }while(swap);
- }
- void Getprodinfo (Prodlist *pNew,Stocks *stock,int n){
- int i;
- printf("Enter Supplier : ");
- fgets (pNew->supplier,16,stdin);
- printf("Enter Product : ");
- fgets (pNew->product,16,stdin);
- printf("Enter Quantity Available : ");
- scanf("%d",&pNew->quantity);
- printf("Enter Purchase Price : ");
- scanf("%f",&pNew->purchase);
- printf("Enter Unit Selling Price : ");
- scanf("%f",&pNew->unit);
- printf("Enter Discount Rate : ");
- scanf("%f",&pNew->discount);
- pNew->sold = 0;
- pNew->code[0] = stock[n]->category[0];
- pNew->code[1] = pNew->supplier[0];
- pNew->code[2] = pNew->product[0];
- for (i=3;i<8;i++)
- pNew->code[i] = rand() % 10 + '0';
- pNew->supplier[strlen(pNew->supplier)-1] ='\0';
- pNew->product [strlen(pNew->product)-1] = '\0';
- pNew->code[8] = '\0';
- }
- void Addstock (Stocks *stock,int *nCat) { /* Adds Stock */
- int i,valid=1,n=-1;
- Prodlist *pNew=NULL,*pRun=NULL,*pTrail=NULL;
- Char input;
- char cDump;
- do {
- printf("Enter Category : ");
- scanf("%c",&cDump);
- fgets (input,16,stdin);
- input[strlen(input)-1]='\0';
- n = Getcat (stock,input,*nCat);
- } while (n==-1);
- strcpy(stock[n]->category,input);
- if (n==*nCat && n !=0)
- stock[n]->list=NULL;
- if (stock[n]->list==NULL)
- (*nCat)++;
- pNew=malloc(sizeof(Prodlist));
- Getprodinfo (pNew,stock,n);
- if (stock[n]->list == NULL){
- stock[n]->list = pNew;
- stock[n]->list->pNext=NULL;
- }
- else if (strcmp (stock[n]->list->supplier, pNew->supplier)>0){
- pNew->pNext = stock[n]->list;
- stock[n]->list = pNew;
- }
- else if (strcmp (stock[n]->list->supplier, pNew->supplier)==0){
- if (strcmp(stock[n]->list->code,pNew->code) > 0){
- pNew->pNext = stock[n]->list;
- stock[n]->list = pNew;
- }
- else{
- pRun = stock[n]->list;
- while (pRun != NULL && strcmp(pRun-> code, pNew->code) <0){
- pTrail = pRun;
- pRun = pRun->pNext;
- }
- pTrail->pNext = pNew;
- pNew->pNext = pRun;
- }
- }
- else {
- pRun = stock[n]->list;
- while (pRun != NULL && strcmp(pRun->supplier, pNew->supplier) < 0) {
- pTrail = pRun;
- pRun = pRun->pNext;
- }
- pTrail->pNext = pNew;
- pNew->pNext=pRun;
- }
- printf("Stock Added\n");
- }
- int Getcatindex (Stocks *stock, Char input, int nCat){
- int i;
- if (nCat==0)
- return -2;
- else {
- for (i=0;i<nCat;i++)
- if (strcmp(input,stock[i]->category)==0)
- return i;
- return -1;
- }
- }
- void Modifystock (Stocks *stock, int *nCat) { /* Modifys stock information */
- int exit =1, input, i,n,y,k;
- Code code;
- Char newcat;
- Prodlist *temp,*pRun,*pTrail;
- printf("Modify Stock");
- Viewallstock (stock,nCat,0);
- printf("Enter product code: ");
- scanf("%s",&code);
- for (i=0;i<(*nCat);i++){
- temp = stock[i]->list;
- while (temp!=NULL){
- if (strcmp(temp->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",temp->code);
- scanf("%d",&input);
- switch (input) {
- case 1: printf("Enter New Category : ");
- fgets (newcat,16,stdin);
- fgets (newcat,16,stdin);
- newcat[strlen(newcat)-1]='\0';
- if (Getcatindex (stock,newcat,*nCat)==-1){
- strcpy(stock[*nCat]->category,newcat);
- if (stock[*nCat]->list == NULL){
- stock[*nCat]->list = temp;
- stock[*nCat]->list->pNext=NULL;
- }
- printf("New Category is %s\n",stock[*nCat]->category);
- k=1;
- (*nCat)++;
- }
- else {
- printf("NOPE IT WENT HERE \n");
- if (stock[Getcatindex (stock,newcat,*nCat)]->list == NULL){
- stock[Getcatindex (stock,newcat,*nCat)]->list = temp;
- stock[Getcatindex (stock,newcat,*nCat)]->list->pNext=NULL;
- }
- else if (strcmp (stock[Getcatindex (stock,newcat,*nCat)]->list->code, temp->code)>0){
- temp->pNext = stock[Getcatindex (stock,newcat,*nCat)]->list;
- stock[Getcatindex (stock,newcat,*nCat)]->list = temp;
- }
- else {
- pRun = stock[Getcatindex (stock,newcat,*nCat)]->list;
- while (pRun != NULL && strcmp(pRun->code, temp->code) < 0) {
- pTrail = pRun;
- pRun = pRun->pNext;
- }
- pTrail->pNext = temp;
- temp->pNext=pRun;
- }
- printf("New Category is %s\n",stock[Getcatindex(stock,newcat,*nCat)]->category);
- k=2;
- }
- break;
- case 2: printf("Enter New Supplier : ");
- fgets (temp->supplier,16,stdin);
- fgets (temp->supplier,16,stdin);
- temp->supplier[strlen(temp->supplier)-1]='\0';
- printf("New Supplier is %s\n",temp->supplier); break;
- case 3: printf("Enter New Product Name : ");
- fgets (temp->product,16,stdin);
- fgets (temp->product,16,stdin);
- temp->product[strlen(temp->product)-1]='\0';
- printf("New Product Name is %s\n",temp->product); break;
- case 4: printf("Enter New Purchase Price : ");
- scanf("%f",&temp->purchase);
- printf("New Purchase Price is %0.2f\n",temp->purchase); break;
- case 5: printf("Enter New Selling Price : ");
- scanf("%f",&temp->unit);
- printf("New Selling Price is %0.2f\n",temp->unit); break;
- case 6: printf("Enter New Discount : ");
- scanf("%f",&temp->discount);
- printf("New Discount Rate is %0.1f\n",temp->discount); break;
- case 7: if (k==1){
- if (temp->code[0]!=stock[*nCat]->category[0])
- temp->code[0]=stock[*nCat]->category[0];
- if (temp->code[1]!=temp->supplier[0])
- temp->code[1]=temp->supplier[0];
- if (temp->code[2]!=temp->product[0])
- temp->code[2]=temp->product[0];
- for (n=0;n<(*nCat);n++){
- if (n!=i)
- if (strcmp (temp->code,stock[n]->list->code)==0)
- for (y=3;y<8;y++)
- temp->code[y]= rand()%10+'0';
- }
- }
- if (temp->code[0]!=stock[i]->category[0])
- temp->code[0]=stock[i]->category[0];
- if (temp->code[1]!=temp->supplier[0])
- temp->code[1]=temp->supplier[0];
- if (temp->code[2]!=temp->product[0])
- temp->code[2]=temp->product[0];
- for (n=0;n<(*nCat);n++){
- if (n!=i)
- if (strcmp (temp->code,stock[n]->list->code)==0)
- for (y=3;y<8;y++)
- temp->code[y]= rand()%10+'0';
- }
- printf("New Product Code is %s \n",temp->code);
- exit = 0; break;
- }
- } while (exit);
- }
- temp=temp->pNext;
- }
- }
- }
- void Restock (Stocks *stock,int *nCat){ /* Restocks products */
- int exit = 1, input, i,change;
- Code code;
- Prodlist *temp;
- do {
- change = 0;
- Viewallstock (stock,nCat,0);
- printf("Enter Product Code : ");
- scanf("%s",code);
- printf("Enter Quantity to be added : ");
- scanf("%d",&input);
- for (i=0;i<(*nCat);i++){
- temp = stock[i]->list;
- while (temp!=NULL){
- if (strcmp (temp->code,code)==0){
- temp->quantity += input;
- change = 1;
- }
- temp=temp->pNext;
- }
- }
- if (change==0){
- printf("Last input product Code not found: Not restocked \n");
- exit = 0;
- }
- } while (exit);
- }
- void Saveinventory (Stocks *stock, int nCat){
- int i;
- FILE * pText;
- Char Filename;
- Prodlist *temp;
- if (stock[0]==NULL)
- printf("Nothing to save \n");
- else {
- printf("Enter Filename : ");
- scanf("%s",Filename);
- pText=fopen(Filename,"wt");
- if(pText != NULL){
- for (i=0;i<nCat;i++){
- temp = stock[i]->list;
- while(temp != NULL){
- fprintf(pText, "%s %s %s \n%d %7.2f %s \n%7.2f&%7.2f %d \n",stock[i]->category, temp->code, temp->product, temp->quantity, temp->purchase, temp->supplier, temp->unit, temp->discount, temp->sold);
- if (i!=(nCat-1))
- fprintf(pText,"\n");
- temp = temp -> pNext;
- }
- }
- fclose(pText);
- }else printf("Error creating file\n");
- }
- }
- void Showbycat (Stocks *stock, int i,int n){
- Prodlist *temp;
- int count = 0;
- char cDump;
- if (n){
- temp = stock[i]->list;
- while (temp!=NULL){
- if (count == 0)
- printf(" Category Supplier Product Code Product Available Sold Purchase Selling Discount\n");
- printf("%15s ",stock[i]->category);
- printf("%15s ",temp->supplier);
- printf("%14s ",temp->code);
- printf("%15s ",temp->product);
- printf("%9d ",temp->quantity);
- printf("%4d ",temp->sold);
- printf("%7.2f ",temp->purchase);
- printf("%7.2f ",temp->unit);
- printf("%7.2f \n",temp->discount);
- count++;
- if (count > 20) {
- printf("NEXT PAGE ");
- scanf("%c",&cDump);
- scanf("%c",&cDump);
- count = 0;
- }
- temp=temp->pNext;
- }
- }
- else
- temp = stock[i]->list;
- while (temp!=NULL){
- if (count == 0)
- printf(" Category Brand Product Code Product Available Sold Unit Discount\n");
- printf("%15s ",stock[i]->category);
- printf("%15s ",temp->supplier);
- printf("%14s ",temp->code);
- printf("%15s ",temp->product);
- printf("%9d ",temp->quantity);
- printf("%4d ",temp->sold);
- printf("%7.2f ",temp->unit);
- printf("%7.2f \n",temp->discount);
- count++;
- if (count > 20) {
- printf("NEXT PAGE ");
- scanf("%c",&cDump);
- scanf("%c",&cDump);
- count = 0;
- }
- temp=temp->pNext;
- }
- }
- void Viewstockcat (Stocks *stock, int nCat,int i){
- int exit = 1;
- Char input;
- do{
- printf("Enter a category : ");
- fgets (input,16,stdin);
- fgets (input,16,stdin);
- input[strlen(input)-1]='\0';
- if (Getcatindex (stock,input,nCat)==-2){
- printf("No stocks to show \n");
- exit=0;
- }
- else if (Getcatindex (stock,input,nCat)==-1)
- printf("No Category");
- else {
- Showbycat (stock,Getcatindex (stock,input,nCat),i);
- exit=0;
- }
- } while (exit);
- }
- void Updateinventory (Stocks *stock, int *nCat){
- FILE * pFile;
- int i=0,valid=1,n=-1;
- Prodlist *temp;
- Char input,filename;
- char cDump;
- printf("Enter file name : ");
- scanf("%s",filename);
- if ((pFile = fopen (filename,"rt"))!=NULL){
- while (fscanf(pFile, "%s %s %s %d %7.2f %s %7.2f %7.2f %d",input, temp->code, temp->product, temp->quantity, temp->purchase, temp->supplier, temp->unit, temp->discount, temp->sold)==9){
- strcpy(stock[i]->category,input);
- stock[i]->list = temp;
- printf("IT SHOULD WORK \n");
- }
- }
- }
- void Viewreorder (Stocks *stock, int nCat){
- int i,count=0,print=1;
- Prodlist *temp;
- char cDump;
- for (i=0;i<(nCat);i++){
- temp = stock[i]->list;
- while (temp!=NULL){
- if (count == 0&&print)
- printf(" Category Supplier Product Code Product Available Sold Purchase Selling Discount\n");
- if (temp->quantity <= 10){
- printf("%15s",stock[i]->category);
- printf("%15s",temp->supplier);
- printf("%14s",temp->code);
- printf("%15s",temp->product);
- printf("%9d ",temp->quantity);
- printf("%4d ",temp->sold);
- printf("%7.2f ",temp->purchase);
- printf("%7.2f ",temp->unit);
- printf("%7.2f \n",temp->discount);
- print=1;
- count++;
- }
- else print=0;
- if (count > 20) {
- printf("NEXT PAGE ");
- scanf("%c",&cDump);
- scanf("%c",&cDump);
- count = 0;
- }
- temp=temp->pNext;
- }
- }
- }
- void Managestocks (Stocks *stock, int *nCat) { /* Manage stocks menu */
- int exit=1,input,i;
- 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,0); break;
- case 3: Viewstockcat (stock,*nCat,1); break;
- case 4: Viewreorder (stock,*nCat); break;
- case 5: Modifystock (stock,nCat); break;
- case 6: Restock (stock,nCat); break;
- case 7: Alphabeticalindex (stock,*nCat);
- Saveinventory (stock, *nCat); break;
- case 8: Updateinventory (stock, nCat); break;
- case 9: exit = 0;
- }
- } while (exit);
- }
- void Initializemysoul (Usertype *user){
- Usertype *pTrail=NULL;
- while (user!=NULL){
- if (user->type =='S'){
- user->pPrev = pTrail;
- pTrail = user;
- }
- user=user->pNext;
- }
- }
- void Updateoutstanding (Usertype *user,Stocks *stock, int nCat){
- int i,n;
- Prodlist *temp;
- float amount=0;
- while (user!=NULL){
- if (user->type=='S'&&user->nItems>0){
- for (n=0;n<user->nItems;n++){
- for (i=0;i<nCat;i++){
- temp = stock[i]->list;
- while (temp != NULL){
- if (strcmp (temp->code,user->cart[n].Prodcode)==0)
- amount += temp->unit-((temp->discount/100)*temp->unit);
- temp=temp->pNext;
- }
- }
- }
- user->outstanding += amount;
- user->nItems = 0;
- user->checkout = 0;
- }
- user=user->pNext;
- }
- }
- void Deliveryreport (Usertype *user, Stocks *stock,int nCat){
- int i,n,total;
- char input,cDump;
- Prodlist *temp,*temporary;
- FILE * pFile;
- Usertype *pFirst=user;
- float disc,amount,Outstanding;
- do {
- disc=0;
- amount=0;
- if (user->type=='S'&&user->nItems>0){
- printf("User ID : %s \n",user->username);
- printf("Customer Name : %s, %s %s \n",user->info.Userinfo.Last,user->info.Userinfo.First,user->info.Userinfo.Second);
- printf("Delivery Address : %s \n",user->info.Address);
- printf(" Product Code Product Quantity Unit Total Subtotal \n");
- for (n=0;n<user->nItems;n++){
- for(i=0;i<nCat;i++){
- temp = stock[i]->list;
- while (temp != NULL){
- if (strcmp (temp->code,user->cart[n].Prodcode)==0){
- printf("%14s ", temp->code);
- printf("%15s ",temp->product);
- printf("%10d ",user->cart[n].qty);
- printf("%7.2f ",temp->unit);
- printf("%7.2f ",temp->unit*user->cart[n].qty);
- printf("-%7.2f ",temp->discount);
- total += user->cart[n].qty;
- disc += (temp->discount/100)*temp->unit;
- amount += temp->unit-((temp->discount/100)*temp->unit);
- printf("%7.2f \n",temp->unit-((temp->discount/100)*temp->unit));
- }
- temp=temp->pNext;
- }
- }
- }
- Outstanding = amount + user->outstanding;
- printf("Number of Items : %d \n",total);
- printf("Total Discount : %7.2f \n",disc);
- printf("Bill Amount : %7.2f \n",amount);
- printf("Total Outstanding : %7.2f \n",Outstanding);
- if (user->pNext != NULL&& user->pNext->type=='S')
- printf("Next : N\n");
- if (user->pPrev != NULL)
- printf("Back : B\n");
- printf("Exit : X\n");
- scanf("%c",&cDump);
- scanf("%c",&input);
- if (input >= 97 && input <=122)
- input-=32;
- if (input == 'N')
- user=user->pNext;
- if (input == 'B')
- user=user->pPrev;
- }
- else
- user=user->pNext;
- } while (input != 'X'&&user!=NULL);
- pFile =fopen ("Delivery Recipts.txt","wt");
- if (pFile != NULL){
- while (pFirst!=NULL){
- if (pFirst->type=='S'&&pFirst->nItems>0){
- fprintf(pFile,"%s %d\n",pFirst->username,pFirst->nItems);
- for (n=0;n<pFirst->nItems;n++){
- for(i=0;i<nCat;i++){
- temporary = stock[i]->list;
- while (temporary != NULL){
- if (strcmp (temporary->code,pFirst->cart[n].Prodcode)==0)
- fprintf(pFile,"%d %s %s\n@%7.2f&%7.2f\n",pFirst->cart[n].qty,temporary->code,temporary->product,temporary->unit,temporary->discount);
- temporary = temporary->pNext;
- }
- }
- }
- fprintf(pFile,"\n");
- }
- pFirst=pFirst->pNext;
- }
- }
- fclose(pFile);
- Updateoutstanding (user,stock,nCat);
- }
- void Adminmenu (Usertype *user,Usertype *All,Stocks *stock, int *esc, int *nCat){ // Admin Menu
- int exit=1,input,i;
- 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: Initializemysoul (user);
- Deliveryreport (All,stock,*nCat); break;
- case 4: *esc = 0;
- case 5: exit = 0;
- }
- } while (exit);
- }
- void Changename (Userinfo *uInfo){/* Changes user information */
- int input;
- printf("Choose an option : \n");
- printf("Edit first name : 1 \n");
- printf("Edit middle name : 2\n");
- printf("Edit last name : 3\n");
- scanf("%d",&input);
- switch (input) {
- case 1: printf("Enter your First Name: ");
- fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
- fgets(uInfo->Userinfo.First,MAXNAME,stdin);
- if (uInfo->Userinfo.First[0] >= 97 && uInfo->Userinfo.First[0] <=122)
- uInfo->Userinfo.First[0]-=32;
- uInfo->Userinfo.First[strlen(uInfo->Userinfo.First)-1]='\0';
- printf("Your New Name is: ");
- printf("%s",uInfo->Userinfo.First);
- break;
- case 2: printf("Enter your Middle Name: ");
- fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
- fgets(uInfo->Userinfo.Second,MAXNAME,stdin);
- if (uInfo->Userinfo.Second[0] >= 97 && uInfo->Userinfo.Second[0] <=122)
- uInfo->Userinfo.Second[0]-=32;
- uInfo->Userinfo.Second[strlen(uInfo->Userinfo.Second)-1]='\0';
- printf("Your New Name is: ");
- printf("%s",uInfo->Userinfo.Second);
- break;
- case 3: printf("Enter your Last Name: ");
- fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
- fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
- if (uInfo->Userinfo.Last[0] >= 97 && uInfo->Userinfo.Last[0] <=122)
- uInfo->Userinfo.Last[0]-=32;
- uInfo->Userinfo.Last[strlen(uInfo->Userinfo.Last)-1]='\0';
- printf("Your New Name is: ");
- printf("%s",uInfo->Userinfo.Last);
- }
- }
- void Changeadd (Userinfo *uInfo) { /* Changes username */
- 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) { /* Changes password */
- 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) { /* Modifys user info */
- 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) { /* Pays for outstanding balance */
- 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);
- }
- int Checkitem (Usertype *user,Code code){
- int i;
- if (user->nItems==0)
- return 0;
- for (i=0;i<user->nItems;i++)
- if (strcmp (user->cart[i].Prodcode,code)==0)
- return i;
- return user->nItems;
- }
- void Addtocart (Usertype *user, Stocks *stock,int nCat){
- int exit = 1,input,quant,i,n;
- Code code;
- Prodlist *temp;
- Viewallstock (stock,&nCat,1);
- do {
- printf("Enter product code : ");
- scanf("%s",code);
- printf("Enter quantity : ");
- scanf("%d",&quant);
- for (i=0;i<(nCat);i++){
- temp = stock[i]->list;
- while (temp!=NULL){
- if (strcmp (temp->code,code)==0){
- if (quant<=temp->quantity){
- n = Checkitem (user,code);
- user->cart[n].qty+=quant;
- strcpy(user->cart[n].Prodcode,code);
- if (n==user->nItems)
- user->nItems++;
- }
- }
- temp=temp->pNext;
- }
- }
- printf("Add another product to cart? (1 = Yes or 0 = No) : ");
- scanf("%d",&input);
- if (!input)
- exit = 0;
- } while (exit);
- }
- void Checkout (Usertype *user, Stocks *stock,int nCat){
- int n,i;
- Prodlist *temp;
- for (n=0;n<user->nItems;n++){
- for(i=0;i<nCat;i++){
- temp = stock[i]->list;
- while (temp != NULL){
- if (strcmp (temp->code,user->cart[n].Prodcode)==0){
- temp->quantity -= user->cart[n].qty;
- temp->sold += user->cart[n].qty;
- }
- temp = temp -> pNext;
- }
- }
- }
- user->checkout=1;
- }
- int getindex (Usertype *user,Code code){
- int i;
- for (i=0;i<user->nItems;i++)
- if (strcmp (user->cart[i].Prodcode,code)==0)
- return i;
- }
- void Removeitem (Usertype *user){
- int i;
- Code code;
- printf("Enter product code : ");
- scanf("%s",code);
- for (i=getindex(user,code);i<user->nItems;i++)
- user->cart[i]=user->cart[i+1];
- user->nItems--;
- }
- void Removeitem2 (Usertype *user, Code code){
- int i;
- for (i=getindex(user,code);i<user->nItems;i++)
- user->cart[i]=user->cart[i+1];
- user->nItems--;
- }
- void Updatequantity (Usertype *user, Stocks *stock, int nCat){
- int i,quant,n;
- Code code;
- Prodlist *temp;
- printf("Enter product code : ");
- scanf("%s",code);
- printf("Enter quantity : ");
- scanf("%d",&quant);
- for (n=0;n<user->nItems;n++){
- if (strcmp(user->cart[n].Prodcode,code)==0){
- for (i=0;i<nCat;i++){
- temp=stock[i]->list;
- while (temp != NULL){
- if (strcmp (temp->code,user->cart[n].Prodcode)==0){
- if (quant == 0)
- Removeitem2 (user,code);
- else if (quant<=temp->quantity)
- user->cart[n].qty+=quant;
- else
- printf("Not enough \n");
- }
- temp=temp->pNext;
- }
- }
- }
- }
- }
- void Editcart (Usertype *user,Stocks *stock,int nCat){
- int exit =1, input;
- do{
- printf("Choose an option : \n");
- printf("Remove an item : 1\n");
- printf("Update Quantity : 2\n");
- printf("Return : 3\n");
- scanf("%d",&input);
- switch (input){
- case 1: Removeitem(user);
- case 2: Updatequantity(user,stock,nCat);
- case 3: exit = 0; break;
- }
- } while (exit);
- }
- void Viewcart (Usertype *user, Stocks *stock, int nCat){
- int i,total=0,n,input;
- Prodlist *temp;
- float disc=0,amount=0;
- printf(" Product Code Product Quantity Unit Total Subtotal \n");
- for (n=0;n<user->nItems;n++){
- for(i=0;i<nCat;i++){
- temp = stock[i]->list;
- while (temp != NULL){
- if (strcmp (temp->code,user->cart[n].Prodcode)==0){
- printf("%14s ", temp->code);
- printf("%15s ",temp->product);
- printf("%10d ",user->cart[n].qty);
- printf("%7.2f ",temp->unit);
- printf("%7.2f ",temp->unit*user->cart[n].qty);
- printf("-%7.2f ",temp->discount);
- total += user->cart[n].qty;
- disc += (temp->discount/100)*temp->unit;
- amount += temp->unit-((temp->discount/100)*temp->unit);
- printf("%7.2f \n",temp->unit-((temp->discount/100)*temp->unit));
- }
- temp=temp->pNext;
- }
- }
- }
- printf("Number of Items : %d \n",total);
- printf("Total Discount : %.2f\n",disc);
- printf("Cart Amount : %.2f\n",amount);
- printf("\n");
- printf("Choose an option \n");
- printf("Check out : 1\n");
- printf("Edit cart items : 2\n");
- printf("Back to shopper menu : 3\n");
- scanf("%d",&input);
- switch (input) {
- case 1: Checkout (user,stock,nCat); break;
- case 2: Editcart (user,stock,nCat);
- case 3: break;
- }
- }
- void Viewstocksale (Stocks *stock, int nCat){
- int i,count=0,print=1;
- Prodlist *temp;
- char cDump;
- for (i=0;i<(nCat);i++){
- temp = stock[i]->list;
- while (temp!=NULL){
- if (count == 0&&print)
- printf(" Category Brand Product Code Product Available Sold Unit Discount\n");
- if (temp->discount>0){
- printf("%15s ",stock[i]->category);
- printf("%15s ",temp->supplier);
- printf("%14s ",temp->code);
- printf("%15s ",temp->product);
- printf("%9d ",temp->quantity);
- printf("%4d ",temp->sold);
- printf("%7.2f ",temp->unit);
- printf("%7.2f \n",temp->discount);
- print=1;
- count++;
- }
- else print=0;
- if (count > 20) {
- printf("NEXT PAGE ");
- scanf("%c",&cDump);
- scanf("%c",&cDump);
- count = 0;
- }
- temp=temp->pNext;
- }
- }
- }
- void Shoppermenu (Usertype *user,Stocks *stock, int nCat){ /* Shopper Menu */
- 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");
- if (user->outstanding<user->credlimit&&user->checkout==0)
- printf("Add to Cart: 5 \n");
- if (user->nItems>0)
- 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: Viewallstock (stock,&nCat,1); break;
- case 3: Viewstockcat (stock,nCat,0); break;
- case 4: Viewstocksale (stock,nCat); break;
- case 5: Addtocart (user,stock,nCat); break;
- case 6: Viewcart (user,stock,nCat); break;
- case 7: Settlebalance (user); break;
- case 8: exit = 0;
- }
- } while (exit);
- }
- void freeAll (ptrUser *pUser,Stocks *stock,int nCat) { /* Frees all data */
- int i;
- ptrUser pTemp;
- Prodlist *temp,*Free;
- while ((*pUser) != NULL){
- pTemp= *pUser;
- *pUser = (*pUser)->pNext;
- free(pTemp);
- }
- for (i=0;i<nCat;i++){
- temp=stock[i]->list;
- while (temp!=NULL){
- Free = temp;
- temp=temp->pNext;
- free(Free);
- }
- }
- }
- int main () {
- int exit=1,input, nCat = 0,i,checkout=1;
- Usertype user;
- ptrUser pUsers = NULL, pNew, pLast, pAccount, pRun, pTrail;
- Stocks stock;
- stock[0].list=NULL;
- 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;
- pNew->pPrev =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: Managestocks (&stock,&nCat); break;
- }
- } while (exit);
- freeAll(&pUsers,&stock,nCat);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement