I\'m trying to implement the functions below so that it compiles and works : her
ID: 3543618 • Letter: I
Question
I'm trying to implement the functions below so that it compiles and works:
here's the code (Inventory.h, TestInventory.c, Inventory.c:
/*File Inventory.h*/
#include<stdio.h>
typedef struct Item{
char strName[50];
char strDescription[256];
int iQuantity;
int iPrice;
}Item;
typedef struct Inventory{
char strName[50];
int iCurrentSize;
int iMaxSize;
struct Item items[50];
}Inventory;
int initializeItem(Item * pItem, char * strName, char * strDescription, int iQuantity, int iPrice);
void printItem(Item item);
Item * findItem(Inventory * pInventory, char * strName);
int addItem(Inventory * pInventory, char * strName, char * strDescription, int iQuantity, int iPrice);
int removeItem(Inventory * pInventory, char * strName);
/* File Inventory.c consisting of function stubs only*/
#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;
}
/*File TestInventory.c implements all functions and test them*/
#include "inventory.h"
#include <string.h>
#include <stdlib.h>
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;
}
}
int main(){
Item item1;
Inventory inv;
inv.iMaxSize=10;
printf("Testing "initialise" and "printItem" function ");
initializeItem(&item1,"IamaItem","thats my description", 30,100);
printItem(item1);
addItem(&inv, "IamaItem1","thats my description1", 30,100);
addItem(&inv, "IamaItem2","thats my description2", 40,50);
findItem(&inv, "IamaItem1");
int i;
// Printing contents of Inventory array (which contains Item1 and Item2)
printf("Testing "addItem" and "printItem" function ");
for(i=0;i<inv.iCurrentSize;i++){
printItem(inv.items[i]);
}
// CurrentSize should be 2
printf("%d ",inv.iCurrentSize);
removeItem(&inv,"IamaItem1");
// CurrentSize should be 1 since as Item1 is removed
printf("%d ",inv.iCurrentSize);
printf("Testing "removeItem" and "printItem" function ");
// Printing contents of Inventory array (which contains now contains Item2 only)
for(i=0;i<inv.iCurrentSize;i++){
printItem(inv.items[i]);
}
return 0;
}
Explanation / Answer
Just put the files in the same folder and compile and run TestInventory.c
I am using c-free compiler for that. Its working perfectly