Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to implement the functions into the code below: #include \"inventory

ID: 3543643 • Letter: I

Question

I'm trying to implement the functions into the code below:


#include "inventory.h"
#include <string.h>
#include <stdlib.h>

int initializeItem(Item * pItem, char * strName, char * strDescription, int iQuantity, int iPrice){
return 0;
}

void printItem(Item item){

}

Item * findItem(Inventory * pInventory, char * strName){
return NULL;
}

int addItem(Inventory * pInventory, char * strName, char * strDescription, int iQuantity, int iPrice){
return 0;
}


int removeItem(Inventory * pInventory, char * strName){
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

#include "inventory.h"
#include <string.h>
#include <stdlib.h>
int initializeItem(Item * pItem, char * strName, char * strDescription, int iQuantity, int iPrice)
{
// pItem, strName, and strDescription cannot be NULL
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)
{
//means item doesnt exists in inventory...no need to remove.
return 0;
}
// here i holds the element to be removed; start moving one by one...
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);