Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#define MAXNAME 50
#define MAXCODE 100
#define MAXPRICE 50
#define MAXITEMS 1000
struct video {
char name[MAXNAME];
char code[MAXCODE];
char price[MAXPRICE];
int quantity;
};
typedef struct video stk;
stk items[MAXITEMS];
int NumItems;
int Size = sizeof(stk); //display size for the test
int addItems(void);
int idelete(void);
int edit(void);
int fetch(char *buff);
void menu(void);
void view(void);
void sortRecords(void);
int main(void) {
char buff[214];
int i; //number of entries
FILE * fp_in;
if((fp_in = fopen("stockdb.txt", "r")) == NULL) {
fputs("Error! Can't find or open stockdb.txt\n", stderr);
return 1;
}
i=0;
while((fgets(buff, sizeof(buff), fp_in))!=NULL) {
buff[strlen(buff)-1]='\0';
sscanf(buff, "%[^,], %[^,], %[^,], %d ", items[i].name,items[i].code,items[i].price,&items[i].quantity);
if(++i==MAXITEMS) {
printf("Error! The array to hold data, is too small. Please enlarge it or delete stock record\n\n");
getchar();
break;
}
}
fclose(fp_in);
NumItems=i;
sortRecords();
//view();
menu();
printf(" \n");
printf(" \r");
return 0;
}
/* this function is 90% done */
int addItems(void) {
int i,j=0,ok=0,len;
char buff[80]={'\0'};
char ch='y';
stk stk1; //variable only for this function
while(ch=='y' || ch=='Y') {
printf(" \n");
printf(" Name: ");
fgets(buff, MAXNAME, stdin);
len=strlen(buff)-1;
buff[len]='\0';
strcpy(stk1.name, buff);
printf(" *\n");
printf(" Bar code: *");
fgets(buff, MAXCODE, stdin);
len=strlen(buff)-1;
buff[len]='\0';
strcpy(stk1.code, buff);
printf(" *\n");
printf(" Price: *");
fgets(buff, MAXPRICE, stdin);
len=strlen(buff)-1;
buff[len]='\0';
strcpy(stk1.price, buff);
printf(" *\n");
printf(" Quantity: * ");
fgets(buff, sizeof(items[0].quantity), stdin);
sscanf(buff, "%d", &stk1.quantity);
for(j=0;j<MAXITEMS;j++) {
if(!(items[j].name[0])) {
items[j] = stk1;
ok=1;
++NumItems;
break;
}
}
if(!ok) {
printf("\nArray is full, couldn't add that record");
--NumItems;
break;
}
else {
printf("\nAdd another item to the stock? [y/n]: ");
scanf("%1c", &ch);
getchar();
}
}
//clean up screen
for(i=12;i<22;i+=2) {
printf(" *\n");
}
return ok; //successful add
}
int edit(void) {
char buff[80];
int i=0;
printf("\nEnter the name of the item you want to edit: ");
fgets(buff, MAXNAME, stdin);
if(buff[strlen(buff)-1]=='\n')
buff[strlen(buff)-1]='\0';
i = fetch(buff); //in the list?
if(!i) //no
return 0;
//video was found, i is it's index in the array
printf("\n\t Name: %s\n\t Bar_code: %s\n\t Quantity: %2d\n",
items[i].name, items[i].code, items[i].quantity);
printf("\n\t Enter new name [hit <enter> to leave it unchanged]: ");
fgets(buff, MAXNAME, stdin);
if(strlen(buff) >1) {
if(buff[strlen(buff)-1]=='\n') {
buff[strlen(buff)-1]='\0';
strcpy(items[i].name, buff);
}
}
printf("\n\t Enter new barcode [<enter> to leave it unchanged]: ");
fgets(buff, MAXCODE, stdin);
if(strlen(buff) >1) {
if(buff[strlen(buff)-1]=='\n') {
buff[strlen(buff)-1]='\0';
strcpy(items[i].code, buff);
}
}
printf("\n\t Enter new quantity [<enter> to leave it unchanged]: ");
fgets(buff, sizeof(items[0].quantity), stdin);
if(strlen(buff) >1) {
sscanf(buff, "%d", &items[i].quantity);
}
sortRecords();
return i;
}
int fetch(char *buff) {
int lo, hi, mid;
lo=0;
hi = NumItems-1;
while(lo <= hi) {
mid=(lo + hi)/2;
if((strcmp(items[mid].name, buff)) <0) {
lo=mid+1;
}
else if((strcmp(items[mid].name, buff)) >0) {
hi=mid-1;
}
else
return mid;
}
return -1;
}
int idelete(void) {
char buff[80];
int i;
printf("\nEnter the name of the item in stock you want to delete: ");
fgets(buff, MAXNAME, stdin);
buff[strlen(buff)-1]='\0';
i = fetch(buff); //get buff's index in the array
if(i) {
printf("Delete this record [y/n] ? ");
printf("\n\t Name: %s \n\t Bar code: %s \n\t Quantity: %d",items[i].name,\
items[i].code, items[i].quantity);
fgets(buff, 2, stdin);
if(buff[0]=='y' || buff[0]=='Y') {
printf("\nItem has been deleted");
getchar();
getchar();
//*vids[i].name = "";
//*vids[i].code = "";
//*vids[i].rating = 0;
}
return i;
}
else
return 0;
}
void menu(void) {
char ch;
char buff[80];
int i,n, index, ok=0;
do {
//clear the menu area of the display
//for(i=0;i<3;i++) {
//printf(" *\n");
//}
printf(" \n");
printf(" <<<< Welcome to the Main Menu >>>> \n");
printf(" \n");
printf(" [a] Add an item [d] Delete an item \n");
printf(" [e] Edit an item [s] Search for an item \n");
printf(" [v] View all items \n");// not ready yet
printf(" *\n");
printf(" Your Choice, or Q to quit: \n");
printf(" *\n");
//i=_wherey();
fgets(buff, sizeof(buff), stdin);
sscanf(&buff[0],"%c",&ch);
switch (ch) {
case 'a':
printf("\n Would you like to add a new item? [y/n]: ");
ch = getchar();
getchar();
if(ch=='y' || ch=='Y') {
n=addItems();
if(n) { //a successful add
sortRecords();
printf(" One item has been added. %d items in all now. **\n",NumItems);
getchar();
}
}
printf(" *\n");
break;
case 'd':
ok=idelete();
if(ok)
--NumItems;
break;
case 'e': edit();
break;
case 's':
for(i=0;i<10;i++) {
printf(" *\n");
}
printf("Enter item's \n Name: ");
fgets(buff, sizeof(items[0]), stdin);
buff[strlen(buff)-1]='\0';
index = fetch(buff);
//printf("Index: %d\n",index); getchar();
if(index>-1) {
printf(" #%d) title: %s\n bar_code: %s\n price: %s\n quantity: %d\n\n",
index+1,items[index].name,items[index].code,items[index].price,items[index].quantity);
printf(" *\n");
printf(" *\n");
printf(" *\n");
printf(" *\n");
getchar();
}
break;
//case 'v': view(); break;
//case 'w': writeAll(); break;
case 'Q': ch='q';
case 'q': break;
default: printf("\n\t\t Please enter a valid selection\n");
};
}while(ch!='q');
}
void sortRecords(void) {
int i, j;
stk testy;
for(i=1;i<NumItems;i++)
{
testy = items[i];
j = i-1;
while((strcmp(items[j].name, testy.name)) > 0)
{
items[j + 1] = items[j];
--j;
if(j<0) break;
}
items[j+1] = testy;
}
}