Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. /* 04/06/2019 - Linked List Practice Problem for Programming 2
  2. ** Prepared by: Cris Lawrence Adrian G. Militante
  3. ** Answered by:
  4.  
  5. ** Given the structures and function prototypes below, write the code for all the functions defined.
  6. **
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. typedef struct ProductDetails{
  12. char prodID[9];
  13. char prodName[32];
  14. char shelf[5];
  15. }ProductDetails;
  16.  
  17. typedef struct Product{
  18. ProductDetails details;
  19. float price;
  20. int stocks;
  21. int itemsSold;
  22. }Product;
  23.  
  24. typedef struct ProdNode{
  25. Product prod;
  26. struct ProdNode* nextProd;
  27. }ProdNode, *ProductList;
  28.  
  29. typedef struct WarehouseDetails{
  30. char warehouseName[32];
  31. char warehouseID[3];
  32. char address[128];
  33. }WarehouseDetails;
  34.  
  35. typedef struct Warehouse{
  36. WarehouseDetails details;
  37. ProductList inventory;
  38. }Warehouse;
  39.  
  40. Warehouse initWH(void);
  41. int findProd(ProductList, char*);
  42. void insertProdUnique(ProductList*, Product);
  43. void deleteProd(ProductList*, char*);
  44. ProductList getShelfItems(ProductList, char*);
  45. void displayProduct(ProductList);
  46. void displayWarehouseInv(Warehouse);
  47.  
  48. //ProductList deleteShelfItems(ProductList* list, char shelf[]);
  49. //void insertSorted(ProductList list);
  50. //void insertLast(ProductList, Product);
  51.  
  52. /* Instructions
  53. ** 1) initWH: The function will initialize the inventory of a warehouse to be EMPTY, and will ask the user to input
  54. the warehouse details.
  55. Example: Input warehouse name: Blankedale
  56. Input warehouse ID: BL
  57. Input warehouse address: Germany
  58.  
  59. 2) findProd: The function will check the linked list and return 1 if the passed prodID matches any product in the list.
  60. Else, it will return 0.
  61.  
  62. 3) insertProdUnique: The function will insert after the last node of the linked list, the new Product if that product's ID
  63. does not yet exist in the list. Else, update the details and price of the product to be the same as the
  64. new Product. Also, update the stocks to be equal to the sum of the previous stock and the new product stock.
  65. itemsSold field should not be updated.
  66.  
  67. 4) deleteProd: The function will go through the list and delete the product with the prodID that has been passed
  68. ONLY IF the product to be deleted has zero stocks. Display relevant error messages when needed
  69. ("E.G. Cannot delete product. Product still has stocks.")
  70.  
  71. 5) getShelfItems: The function will go through the list of products and return a new ProductList that contains all of
  72. the items that have belong to the same shelf.
  73.  
  74. 6) displayWarehouseInv: The function will display the warehouse name and all of the products within the warehouse,
  75. together with their details.Format the display as you see fit.
  76. */
  77.  
  78. int main(void)
  79. {
  80. Warehouse blankdale;
  81. ProductList shelfItems=NULL;
  82. Product newProds[] = {{{"00000001", "APPLES", "FR01"}, 32.00, 10, 50},
  83. {{"00000002", "PINEAPPLES", "FR01"}, 66.00, 20, 50},
  84. {{"00000003", "ORANGES", "FR01"}, 21.00, 30, 50},
  85. {{"00000004", "MELONS", "FR02"}, 120.00, 0, 50},
  86. {{"00000005", "SQUASH", "FR02"}, 55.00, 35, 50},
  87. {{"00000006", "BANANA", "FR01"}, 40.00, 0, 50},
  88. {{"00000007", "GRAPES", "FR01"}, 5.00, 22, 50},
  89. {{"00000008", "MANGO", "FR01"}, 37.00, 32, 50},
  90. {{"00000009", "CHERRY", "FR02"}, 7.00, 45, 50},
  91. {{"00000001", "PEACH", "FR02"}, 22.00, 68, 50}
  92. };
  93. /* Make the necessary function calls to check if your functions were successfully written */
  94. blankdale = initWH();
  95. insertProdUnique(&blankdale.inventory,newProds[0]);
  96. insertProdUnique(&blankdale.inventory,newProds[1]);
  97. insertProdUnique(&blankdale.inventory,newProds[2]);
  98. insertProdUnique(&blankdale.inventory,newProds[3]);
  99. insertProdUnique(&blankdale.inventory,newProds[4]);
  100.  
  101. displayWarehouseInv(blankdale);
  102.  
  103. printf("%s",findProd(blankdale.inventory, "00000001") == 1 ? "Item Found" : "Item Not Found");
  104. getch();
  105. system("CLS");
  106. deleteProd(&blankdale.inventory, "00000002");
  107. displayWarehouseInv(blankdale);
  108. getch();
  109. system("CLS");
  110. shelfItems = getShelfItems(blankdale.inventory, "FR01" );
  111. displayProduct(shelfItems);
  112. return 0;
  113. }
  114.  
  115. Warehouse initWH(void)
  116. {
  117. Warehouse temp;
  118. puts("Enter warehouse name:");
  119. gets(temp.details.warehouseName);
  120. puts("Enter warehouse id:");
  121. gets(temp.details.warehouseID);
  122. puts("Enter warehouse address:");
  123. gets(temp.details.address);
  124. temp.inventory=NULL;
  125. return temp;
  126. }
  127.  
  128. int findProd(ProductList list, char id[])
  129. {
  130. ProductList trav;
  131. for(trav=list; trav!=NULL && strcmp(trav->prod.details.prodID, id) !=0; trav=trav->nextProd){}
  132. return trav == NULL? 0:1;
  133. }
  134.  
  135. void insertProdUnique(ProductList* list, Product newProd)
  136. {
  137. ProductList temp, *trav;
  138. for(trav=list; *trav!=NULL && strcmp(newProd.details.prodID, (*trav)->prod.details.prodID) !=0; trav= &(*trav)->nextProd){}
  139. if(*trav==NULL){
  140. temp=(ProductList)malloc(sizeof(ProdNode));
  141. if(temp!=NULL){
  142. temp->prod=newProd;
  143. temp->nextProd=*trav;
  144. *trav=temp;
  145. }
  146. }else{
  147. (*trav)->prod.details = newProd.details;
  148. (*trav)->prod.price = newProd.price;
  149. (*trav)->prod.stocks += newProd.stocks;
  150. puts("Item was found. Record were updated");
  151. }
  152. }
  153.  
  154. void deleteProd(ProductList* list, char id[])
  155. {
  156. ProductList temp, *trav;
  157. for(trav=list; trav!=NULL && strcmp(id, (*trav)->prod.details.prodID) !=0; trav= &(*trav)->nextProd){}
  158. if(*trav!=NULL){
  159. if((*trav)->prod.stocks==0){
  160. temp=*trav;
  161. *trav=temp->nextProd;
  162. free(temp);
  163. }else{
  164. puts("Cannot delete product. Still has stocks");
  165. }
  166. }
  167. }
  168.  
  169. ProductList getShelfItems(ProductList list, char* shelf)
  170. {
  171. ProductList shelfItems = NULL;
  172. ProductList temp, trav, *trav2;
  173. for(trav=list; trav!=NULL; trav=trav->nextProd){
  174. if(strcmp(shelf,trav->prod.details.shelf)==0){
  175. for(trav2=&shelfItems; *trav2!=NULL; trav2= &(*trav2)->nextProd){}
  176. temp= (ProductList) malloc (sizeof(ProdNode));
  177. if(temp!=NULL){
  178. temp->prod= trav->prod;
  179. temp->nextProd = *trav2;
  180. *trav2 = temp;
  181. }
  182. }
  183. }
  184. return shelfItems;
  185. }
  186.  
  187. void displayProduct(ProductList list)
  188. {
  189. for(;list!=NULL; list=list->nextProd){
  190. printf("Product: %s\n",list->prod.details.prodName);
  191. printf("ID: %s\n",list->prod.details.prodID);
  192. printf("Price: %.2f\n",list->prod.price);
  193. printf("Shelf: %s\n",list->prod.details.shelf);
  194. printf("Sold: %d\n",list->prod.itemsSold);
  195. printf("Stock: %d\n---------------\n",list->prod.stocks);
  196. }
  197. }
  198.  
  199. void displayWarehouseInv(Warehouse temp)
  200. {
  201. char buffer[200];
  202. sprintf(buffer,"Warehouse Name: %s\nWarehouse ID: %s\nAddress: %s",temp.details.warehouseName, temp.details.warehouseID, temp.details.address);
  203. puts(buffer);
  204. displayProduct(temp.inventory);
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement