Function Stubs Functions int initializeItem(Item * pItem, char * strName, char *
ID: 3543049 • Letter: F
Question
- Function Stubs
- Functions
-
- int initializeItem(Item * pItem, char * strName, char * strDescription, int iQuantity, int iPrice)
-
- Fills the given item with the given values.
- Constraints:
-
- pItem, strName, and strDescription cannot be NULL
- iQuantity and iPrice must be >= 0
- Return 0 on success or an error code if there is a problem (i.e. a constraint is not met).
- void printItem(Item item)
-
- Prints the fields in the given item, one field per line.
- int addItem(Inventory * pInventory, char * strName, char * strDescription, int iQuantity, int iPrice)
-
- Adds a new Item with the given values to the end of the Inventory's items array.
- Constraints:
-
- The new Item should have the same constraints as discussed in the initializeItem function.
- pInventory cannot be NULL.
- If the inventory is full, do not add the item and return a error code.
- If an item with the same name exists in the Inventory, do not add the item and return an error code.
- Make sure that you update the iCurrentSize.
- Return 0 on success or an error code if there is a problem (i.e. a constraint is not met).
- Item * findItem(Inventory * pInventory, char * strName)
-
- Searches the Inventory for an item with the given name and returns a pointer to that item.
- Return a pointer to the found Item or a NULL pointer if it was not found (or there was an error).
- int removeItem(Inventory * pInventory, char * strName)
-
- Removes the Item with the given name from the given inventory.
- If the Item is not at the end of the array, shift all Items after it down to fill the empty space.
- Make sure that you update the iCurrentSize.
- Return 0 if the Item was successfully removed or if it does not exist in the Inventory.
- Return an error code if there was a problem .
Explanation / Answer
/*Functions required*/
int initializeItem(Item * pItem, char * strName, char * strDescription, int iQuantity, int iPrice){
if(pItem==NULL ||strName==NULL || strDescription==NULL){
printf("strName, and strDescription cannot be NULL "); return;
}
if(iQuantity<0 || iPrice<0){
printf("iQuantity and iPrice must be >= 0 "); return;
}
//assigning values to pItem attributes
strcpy((*pItem).strName,strName);
strcpy((*pItem).strDescription,strDescription);
(*pItem).iQuantity=iQuantity;
(*pItem).iPrice=iPrice;
return 0;
}
void printItem(Item item){
if(&item!=NULL){
printf("strName = %s strDescription= %s iQuantity = %d iPrice = %d ",item.strName,item.strDescription,item.iQuantity,item.iPrice);
}
}
Item * findItem(Inventory * pInventory, char * strName){
int i;
for(i=0;i<(*pInventory).iCurrentSize;i++){
if(strcmp((*pInventory).items[i].strName,strName)==0){
Item *x;
x = (Item*)malloc( sizeof( Item ) );
x = &(*pInventory).items[i];
return x;
}
}
return NULL;
}
int addItem(Inventory * pInventory, char * strName, char * strDescription, int iQuantity, int iPrice){
if(strName==NULL || strDescription==NULL){
printf("strName, and strDescription cannot be NULL "); return;
}
if(iQuantity<0 || iPrice<0){
printf("iQuantity and iPrice must be >= 0 "); return;
}
if(pInventory==NULL){
printf("pInventory cannot be NULL "); return;
}
if((*pInventory).iCurrentSize==(*pInventory).iMaxSize){
printf("Inventory is full, can't add item to it "); return;
}
if( findItem(pInventory,strName)!=NULL ){
printf("Item already exist in the inventory "); return;
}
strcpy((*pInventory).items[(*pInventory).iCurrentSize].strName,strName);
strcpy((*pInventory).items[(*pInventory).iCurrentSize].strDescription,strDescription);
(*pInventory).items[(*pInventory).iCurrentSize].iQuantity=iQuantity;
(*pInventory).items[(*pInventory).iCurrentSize].iPrice=iPrice;
(*pInventory).iCurrentSize++;
return 0;
}
int removeItem(Inventory * pInventory, char * strName){
int i,j;
int found=0;
for(i=0;i<(*pInventory).iCurrentSize;i++){
if(strcmp((*pInventory).items[i].strName,strName)==0){
found=1;break;
}
}
if(found==0){
printf("Item doesn't exist in the inventory "); return 0;
}
else{
printf("%d ",i);
// shifting all the elements ahead of removed element towards left
for(j=i;j<(*pInventory).iCurrentSize;j++){
(*pInventory).items[j]=(*pInventory).items[j+1];
}
(*pInventory).iCurrentSize--;
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.