Code:
#define INV_SIZE 20
#define SIZE 30
#define INITIAL_SIZE 5
#include <stdio.h>
#include <string.h>
typedef struct
{
char book_name[SIZE];
char publisher[SIZE];
int in_stock;
int isbn;
double cost;
double srp;
}vbook;
void Initialize(vbook inventory[]);
int Menu();
void AddNewItem(vbook inventory[], int *inv_size);
void DeleteItem(vbook inventory[], int *inv_size);
void DisplayRecords(vbook inventory[], int *inv_size);
void InventoryReport(vbook inventory[], int *inv_size, FILE *fp);
void ClearInventory(int *inv_size);
int Exit();
int main()
{
vbook inventory[INV_SIZE] = {0};
int selection = 0;
int *inv_size;
int exit = 0;
*inv_size = 6;
FILE *fp;
fp = fopen("report.txt", "w");
Initialize(inventory);//Initializes the first five sections of the inventory array
*inv_size = 6;
do{
selection = Menu();
switch (selection)
{
case 1:
AddNewItem(inventory, inv_size);
break;
case 2:
DeleteItem(inventory, inv_size);
break;
case 3:
DisplayRecords(inventory, inv_size);
break;
case 4:
InventoryReport(inventory, inv_size, fp);
break;
case 5:
ClearInventory(inv_size);
break;
case 6:
exit = Exit();
break;
}
}while(exit != -1);
return(0);
}
void Initialize(vbook inventory[])
{
int i;
strcpy(inventory[0].book_name, "Twilight");
strcpy(inventory[0].publisher, "Little_Readers");
inventory[0].isbn = 000;
inventory[0].in_stock = 99;
inventory[0].cost = 4.20;
inventory[0].srp = 19.99;
strcpy(inventory[1].book_name, "New_Moon");
strcpy(inventory[1].publisher, "Little_Readers");
inventory[1].isbn = 111;
inventory[1].in_stock = 16;
inventory[1].cost = 5.30;
inventory[1].srp = 24.99;
strcpy(inventory[2].book_name, "Eclipse");
strcpy(inventory[2].publisher, "Little_Readers");
inventory[2].isbn = 222;
inventory[2].in_stock = 20;
inventory[2].cost = 6.20;
inventory[2].srp = 26.99;
strcpy(inventory[3].book_name, "Brave_New_World");
strcpy(inventory[3].publisher, "Penguin_Classics");
inventory[3].isbn = 333;
inventory[3].in_stock = 43;
inventory[3].cost = 13.28;
inventory[3].srp = 35.99;
strcpy(inventory[4].book_name, "Erotix");
strcpy(inventory[4].publisher, "Adam_and_Eve");
inventory[4].isbn = 444;
inventory[4].in_stock = 13;
inventory[4].cost = 1.99;
inventory[4].srp = 10.99;
strcpy(inventory[5].book_name, "Harry_Potter");
strcpy(inventory[5].publisher, "Wizard");
inventory[5].isbn = 5555;
inventory[5].in_stock = 17;
inventory[5].cost = 1.99;
inventory[5].srp = 10.99;
for(i = 6;i <= INV_SIZE;i++)
{
inventory[i].isbn = -1;
}
}
int Menu()
{
int selection;
printf("Enter 1 to add a new item to inventory.\n");
printf("Enter 2 to delete an item from inventory.\n");
printf("Enter 3 to display all inventory records.\n");
printf("Enter 4 to create a current inventory report.\n");
printf("Enter 5 to clear entire inventory.\n");
printf("Enter 6 to Exit.\n");
scanf("%d", &selection);
return(selection);
}
void AddNewItem(vbook inventory[], int *inv_size)
{
int i = 0;
int exit = 0;
do{
if(i > INV_SIZE)
{
printf("Your inventory size has surpassed available memory.\n");
printf("Please delete some items from inventory or increase inventory size.\n");
exit = -1;
}
if(inventory[i].isbn == -1 && exit == 0)
{
printf("Please enter the name of the book.\n");
scanf("%s", inventory[i].book_name);
printf("Please enter the publisher of the book.\n");
scanf("%s", inventory[i].publisher);
printf("Please enter the number of books in stock.\n");
scanf("%d", &inventory[i].in_stock);
printf("Please enter the ISBN number of the book.\n");
scanf("%d", &inventory[i].isbn);
printf("Please enter the suppliers cost for one book.\n");
scanf("%lf", &inventory[i].cost);
printf("Please enter the retail price for the book.\n");
scanf("%lf", &inventory[i].srp);
exit = -1;
printf("i is %d", i);
printf("inv size is %d", *inv_size);
*inv_size = (*inv_size + 1);
printf("inv size is %d", *inv_size);
}
else
i++;
}while(exit == 0 && i < INV_SIZE);
}
void DeleteItem(vbook inventory[], int *inv_size)
{
char temp_title[SIZE];
int temp_isbn;
int i;
printf("Please enter the name of the book you would like to delete.\n");
scanf("%s", temp_title);
printf("Please enter the ISBN number of the item you want to delete.\n");
scanf("%d", temp_isbn);
for(i = 0; i < *inv_size; i++)
{
if(temp_isbn == inventory[i].isbn)
{
inventory[i] = inventory[*(inv_size-1)];
}
else
printf("That item could not be found in the inventory.\n");
}
}
void DisplayRecords(vbook inventory[], int *inv_size)
{
int i;
for(i = 0; i < *inv_size; i++)
{
printf("Book Name: %s\n", inventory[i].book_name);
printf("Publisher Name: % s\n", inventory[i].publisher);
printf("ISBN: %d\n", inventory[i].isbn);
printf("Number of items in stock: %d\n", inventory[i].in_stock);
printf("Supplier Price: %.2f\n", inventory[i].cost);
printf("Suggested Retail Price: %.2f\n\n", inventory[i].srp);
}
}
void InventoryReport(vbook inventory[], int *inv_size, FILE *fp)
{
int i;
for(i = 0; i < *inv_size; i++)
{
fprintf(fp,"Book Name: %s\n", inventory[i].book_name);
fprintf(fp,"Publisher Name: % s\n", inventory[i].publisher);
fprintf(fp,"ISBN: %d\n", inventory[i].isbn);
fprintf(fp,"Number of items in stock: %d\n", inventory[i].in_stock);
fprintf(fp,"Supplier Price: %.2f\n", inventory[i].cost);
fprintf(fp,"Suggested Retail Price: %.2f\n\n", inventory[i].srp);
}
}
void ClearInventory(int *inv_size)
{
*inv_size = 0;
}
int Exit()
{
return(-1);
}