Create an Interface source file that contains a command line interface (i.e. mai
ID: 3544457 • Letter: C
Question
Create an Interface source file that contains a command line interface (i.e. main function) that uses the functions and structures in Inventory.c and Inventory.h (my files are below).
///////////////////////////////////////////////////////////////Inventory.c//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "Inventory.h"
#include <string.h>
#include <stdlib.h>
int initializeItem(Item * pItem, char * strName, char * strDescription, int iQuantity, int iPrice)
{
if(!pItem)
return 1;
if(!strName)
return 2;
if(!strDescription)
return 3;
if(iQuantity<0)
return 4;
if(iPrice<0)
return 5;
if(strlen(strName)>49) return 6;
if(strlen(strDescription)>255) return 7;
strcpy(pItem->strName,strName);
strcpy(pItem->strDescription,strDescription);
pItem->iQuantity = iQuantity;
pItem->iPrice = iPrice;
return 0;
}
void printItem(Item item)
{
printf(" Item name is %s",item.strName);
printf(" Item Description is %s",item.strDescription);
printf(" Item Quantity is %d",item.iQuantity);
printf(" Item Price is %d",item.iPrice);
}
Item * findItem(Inventory * pInventory, char * strName)
{
int i;
for(i=0; i<50; i++)
{
if(strcmp(pInventory->items[i].strName,strName)==0)
{
return &(pInventory->items[i]);
} // end if loop
} // end for
return NULL;
}
int addItem(Inventory * pInventory, char * strName, char * strDescription, int iQuantity, int iPrice)
{
int i;
int error_code;
for(i=0; i<50; i++)
{
if(strcmp(pInventory->items[i].strName!=NULL)
continue;
else
break;
}//end for
if(i==50)
{
printf("NO Place to add Item ");
return 8;
}
Item local_item;
error_code = initializeItem(&local_item,strName,strDescription,iQuantity,iPrice);
if(error_code!=0)
return error_code;
strcpy(pInventory->tems[i].strName,strName);
strcpy(pInventory->tems[i].strDescription,strDescription);
pInventory->tems[i].iQuantity = iQuantity;
pInventory->tems[i].iPrice = iPrice;
return 0;
}
int removeItem(Inventory * pInventory, char * strName)
{
int i;
if(!strName)
return 2;
for(i=0; i<50; i++)
{
if(strcmp(pInventory->items[i].strName,strName)==0)
{
break;
} // end if loop
} // end for
if(i==50)
{
return 0;
}
for(i<50; i++)
{
if(strcmp(pInventory->items[i+1].strName!=NULL)
break;
else
{
pInventory->items[i] = pInventory->items[i+1];
}
}
pInventory->items[i].strName[0] = '';
pInventory->items[i].strDescription[0] = '';
pInventory->tems[i].iQuantity = 0.0;
pInventory->tems[i].iPrice = 0.0;
currentSize--;
return 0;
}
///////////////////////////////////////////////////////////////Inventory.h///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
struct Item
{
char strName[50];
char strDescription[256];
int iQuantity;
int iPrice;
};
struct Inventory
{
char strName[50];
int iCurrentSize;
int iMaxSize;
struct Item items[50];
};
int initializeItem(Item * pItem, char * strName, char * strDescription, int iQuantity, int iPrice);
void printItem(Item item);
int addItem(Inventory * pInventory, char * strName, char * strDescription, int iQuantity, int iPrice);
Item * findItem(Inventory * pInventory, char * strName);
int removeItem(Inventory * pInventory, char * strName);
Explanation / Answer
///////////////////////////////////////////////////////////////main.c///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include "Inventory.h"
void menu()
{
printf("1.Create Inventory");
printf("2.Print Inventory");
printf("3.Add Item");
printf("4.Find Item");
printf("5.Remove item");
printf("6.Help");
printf("7.Exit");
}
int select()
{
int choice;
printf("Please select choice :");
scanf("%d",&choice);
while(choice<1 || choice>7)
{
printf("Invalid Choice. Please select choice :");
scanf("%d",&choice);
}
return choice;
}
int main()
{
int choice,i,j;
int inventory_name_index= 0;
char Inventory_name[50];
char name[50];
char description[256];
int quantity,price;
Inventory inv[2];
Item* local_item = NULL;
menu();
choice = select();
switch(choice)
{
case 1:
{
printf("Enter Inventory Name :");
scanf("%s",Inventory_name);
if(inventory_name_index==2)
printf("Array is full cant add more items ");
else
strcpy(inv[inventory_name_index++].strName,Inventory_name);
}
break;
case 2:
{
printf("Enter Inventory Name :");
scanf("%s",Inventory_name);
for(i=0; i<2; i++)
{
if(strcmp(Inventory_name,inv[i].strName)==0)
{
for(j=0; j<inv[i].iCurrentSize; j++)
{
printItem(inv[i].items[j]);
}// end for
break;
} // end if
} // end for
if(i==2)
printf("Inventory Not found ");
}
break;
case 3:
{
printf("Enter Inventory Name :");
scanf("%s",Inventory_name);
for(i=0; i<2; i++)
{
if(strcmp(Inventory_name,inv[i].strName)==0)
{
printf("Enter Name of item ");
scanf("%s",name);
printf("Enter Description of item ");
scanf("%s",description);
printf("Enter Quantity of item ");
scanf("%d",quantity);
printf("Enter price of item ");
scanf("%d",price);
addItem(&inv[i],name,description,quantity,price);
break;
} // end if
} // end for
if(i==2)
printf("Inventory Not found ");
}
break;
case 4:
{
printf("Enter Inventory Name :");
scanf("%s",Inventory_name);
for(i=0; i<2; i++)
{
if(strcmp(Inventory_name,inv[i].strName)==0)
{
printf("Enter Name of item ");
scanf("%s",name);
local_item = findItem(&inv[i],name);
if(local_item!=NULL)
{
printf(" Item name is %s",local_item->strName);
printf(" Item Description is %s",local_item->strDescription);
printf(" Item Quantity is %d",local_item->iQuantity);
printf(" Item Price is %d",local_item->iPrice);
}
else
printf("Item was not found ");
break;
} // end if
} // end for
if(i==2)
printf("Inventory Not found ");
}
break;
case 5:
{
printf("Enter Inventory Name :");
scanf("%s",Inventory_name);
for(i=0; i<2; i++)
{
if(strcmp(Inventory_name,inv[i].strName)==0)
{
printf("Enter Name of item ");
scanf("%s",name);
if(removeItem(&inv[i],name)!=0)
printf("Item Cant be removed from Inventory because item not found ");
break;
} // end if
} // end for
if(i==2)
printf("Inventory Not found ");
}
break;
case 6: menu(); break;
case 7: return 0;
} //end switch
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.