Sei sulla pagina 1di 5

#define _CRT_SECURE_NO_DEPRECATE

#include <conio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <ctype.h>
#define FIELD_SIZE 25
#define length(array) ( sizeof (array) / sizeof (array)[i] )
struct Product
{
char id[20];
char name[20];
int backupStock;
int inStock;
int amountsold;
int reorder;
};
struct usernames
{
char id[20];
char password[20];
};
struct usernames username[20];
int Menu(void);
int mainmenu(void);
int reorder(int, int, int);
int is_authorized(const char, const char);
void get_password(char, int);

int main(void)
{
char uid[FIELD_SIZE];
char pwd[FIELD_SIZE];
strcpy(username[0].id, "knightly");
strcpy(username[0].password, "password");
strcpy(username[1].id, "jack1");
strcpy(username[1].password, "love3");
strcpy(username[2].id, "kim3");
strcpy(username[2].password, "lime2");
***

printf("
*****WELCOME TO THE KNIGHTLY'S INVENTORY SYSTEM**
\n
");
printf("
Where shopping is at its best!!
\n
");
printf("\n
Please enter your log-in information
\n
");
printf("\n User ID: ");
fflush(stdout);
if (fgets(uid, sizeof uid, stdin) != NULL)
{

char *newline = strchr(uid, '\n');


/* Trim the newline if it's present */
if (newline != NULL)
{
*newline = '\0';
printf("\n Password: ");
fflush(stdout);
get_password(pwd, sizeof pwd);
}
if (is_authorized(uid, pwd))
{
printf("\n \n Login Successful \n");
Menu();
}
else
{
printf("\n \n Incorrect Login \n ");
Sleep(1000);
}
//while (k == 0)
}
return 0;
}
int Menu()
{
struct Product products[100];
int i, fa, choice;
FILE *fpMI;
char search[10], *sptr;
int x;
int order;
int answer;
//int reorder;
int backupStock;
int inStock;
int amountsold, c;
int found = 0;
int a = 0;
int done = 0;
char ch;
long int count = 0;
while ((choice = mainmenu()) != 4)
{
switch (choice)
{
case 1:
printf("
**************UPDATE INFORMAT
ION************ \n
");
fpMI = fopen("knightlystuff.txt", "a+");
printf("\n File opened successfully through fopen()\n");
printf("How many products are you entering today\n");
scanf("%d", &fa);
for (i = 0; i < fa; i++)
{
printf("\n Enter Product ID: \n");
scanf("%s", &products[i].id);
fprintf(fpMI, "%-10s", products[i].id);

fflush(stdin);
printf("Enter Product Name: \n");
scanf("%s", &products[i].name);
fprintf(fpMI, "%-10s", products[i].name);
fflush(stdin);
printf("Enter amount put in for Saftey: \n");
scanf("%d", &products[i].backupStock);
fprintf(fpMI, "%-10d", products[i].backupStock);
printf("Enter Product Sold: \n");
scanf("%d", &products[i].amountsold);
fprintf(fpMI, "%-10d", products[i].amountsold);
printf("Enter amount in stock: \n");
scanf("%d", &products[i].inStock);
products[i].reorder = reorder(products[i].amount
sold, products[i].inStock, products[i].backupStock);
fprintf(fpMI, "%-10d\n", products[i].reorder);
printf("\n \n The amount to reorder is = \n %d",
products[i].reorder);
}
fclose(fpMI);
break;
case 2:
printf("
**************PRODUCT SEARCH*
*********** \n
");
system("cls");
printf("Enter the product you would like to search for.\
n");
scanf("%s", search);
fpMI = fopen("knightlystuff.txt", "r");
if (fpMI == NULL)
{
printf("Couldn't open knightlystuff.txt for read
ing.\n");
}
while (a <= 10)
{
fscanf(fpMI, "%s %s %d %d %d", products[a].id, p
roducts[a].name, &products[a].backupStock, &products[a].inStock, &products[a].re
order);
a = a + 1;
}
fclose(fpMI);
for (a = 0; a <= 10; a++)
{
if (strcmp(&products[a].name, &search) == 0)
{
printf("%s %s %d %d %d", products[a].id,
products[a].name, products[a].backupStock, products[a].inStock, products[a].reo
rder);
done += 1;
found = 1;
}
}
while ((a <= 10) && (done != 2));
if (found != 1)
{

printf("The product is not found.\n ");


}
break;
case 3:
fpMI = fopen("knightlystuff.txt", "r");
if (fpMI == NULL)
{
printf("Couldn't open knightlystuff.txt for read
ing.\n");
}
a = 0;
fscanf(fpMI, "%s %s %d %d %d", products[a].id, products[
a].name, &products[a].backupStock, &products[a].inStock, &products[a].reorder);
a += 1;
while (!feof(fpMI))
{
fscanf(fpMI, "%s %s %d %d %d", products[a].id, p
roducts[a].name, &products[a].backupStock, &products[a].inStock, &products[a].re
order);
a += 1;
}
c = a - 1;
printf("
**************DISPLAY INFORMA
TION************ \n
");
printf(" \n Id, Product Name, BackupStock, Instock and R
eorder are displayed. ");
for (a = 0; a < c; a++)
{
printf("\n%6s %6s: %d %d %d\n", products[a]
.id, products[a].name, products[a].backupStock, products[a].inStock, products[a]
.reorder);
}
/* close the file */
fclose(fpMI);
break;
case 4:
printf("Quitting............ \n");
exit(0);
Sleep(1000);
break;
}
getch();
}
}
int mainmenu(void)
{
int op;
system("cls");
printf("\n
**MAINMENU**
\n
");
printf("\n1 - Update Information \n");
printf("2 - Search for a product \n");
printf("3 - Display Information \n");
printf("4 - Exit the system \n");
printf("Choose: ");
scanf("\n %d", &op);
return op;
}

int is_authorized(const char *uid, const char *pwd)


{
int i;
for (i = 0; i < length(username); i++)
{
if (strcmp(uid, username[i].id) == 0 &&
strcmp(pwd, username[i].password) == 0)
{
return 1;
}
}
return 0;
}
void get_password(char *pwd, int size)
{
int i = 0;
int ch;
while (i < size - 1 && (ch = getch()) != '\r')
{
if (ch == '\b')
{
/* Don't run off the front end of the array */
if (i != 0)
{
printf("\b%c\b", ' ');
--i;
}
}
else
{
putchar('*');
pwd[i++] = (char)ch;
}
}
pwd[i] = '\0';
}
int reorder(int amountsold, int inStock, int backupStock)
{
int reorder;
reorder = ((inStock - amountsold) + backupStock);
return reorder;
}

Potrebbero piacerti anche