Advertisement
Guest User

mew

a guest
Jun 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.69 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // function prototyping
  4.  
  5. void menu();
  6. void addItem();
  7. void showAllItems();
  8. void buyItem();
  9. void sell(int quantity,float total);
  10.  
  11. // count holds the value for total items in the inventory
  12. int count = 0;
  13. // totalPrice initiazlied to 0
  14. float totalPrice = 0;
  15. struct inventory
  16. {
  17.     int items;
  18.     float price;
  19. } ;
  20.  
  21. // define inventory as array
  22. // in this case, products is an array of struct (named inventory),defined above
  23.  
  24. struct inventory products[10];
  25.  
  26. int main()
  27. {
  28.     // userChoice holds the value of the input after displaying the menu
  29.     int userChoice;
  30.    
  31.     menu();
  32.  
  33.     while (1){
  34.  
  35.         scanf("%d",&userChoice);
  36.  
  37.         switch (userChoice) {
  38.             case 1:
  39.                 showAllItems();
  40.                 break;
  41.             case 2:
  42.                 addItem();
  43.                 break;
  44.             case 3:
  45.                 buyItem();
  46.                 break;
  47.             case 4:
  48.                 printf("===============================\n");
  49.                 return 0;
  50.                 break;
  51.             default:
  52.                 printf("Invalid Choice. \n");
  53.                 continue;
  54.         }
  55.         menu();
  56.     }
  57.  
  58.     return 0;
  59.  
  60. }
  61. // prints the available options
  62. void menu()
  63. {
  64.     printf("Choose your Option \n");
  65.     printf("1: Show All Records\n");
  66.     printf("2: Add Items \n");
  67.     printf("3: Buy Items \n");
  68.     printf("4: Exit \n");
  69.     printf("====================================\n");
  70. }
  71.  
  72. // add items to the inventory
  73. void addItem()
  74. {  
  75.     printf(" Number of screw drivers \n");
  76.     scanf("%d",&products[count].items);
  77.  
  78.     printf(" Unit Price \n");
  79.     scanf("%f",&products[count].price);
  80.  
  81.     count++;
  82.     printf("Product Added\n");
  83. }
  84.  
  85. //display all items in the inventory
  86. void showAllItems()
  87. {
  88.     // if count,which is the total number of items in the inventory is zero,no need to loop
  89.     if(count == 0) {
  90.  
  91.         printf("List is empty\n");
  92.         return;
  93.     }
  94.     // else loop through the list of items in the products[] inventory
  95.     for ( int i =0; i < count ; i++) {
  96.  
  97.         printf(" %d SCREWDRIVERS : UNIT PRICE %.2f \n", products[i].items , products[i].price);
  98.     }
  99.  
  100. }
  101.  
  102. // buy an item from inventory
  103. void buyItem()
  104. {
  105.     // check if inventory is empty
  106.     if (count <= 0) {
  107.  
  108.         printf("THe list is empty!\n");
  109.         return;
  110.     }
  111.  
  112.     // quantity holds the value for how many screwdrivers needs (according to input)
  113.     int quantity;
  114.  
  115.     printf("\n How many SCREWDRIVERS? \n");
  116.     scanf("%d",&quantity);
  117.    
  118.     // calling sell function
  119.     sell(quantity,0);  
  120.     printf("TOTAL FOR THIS SALE: RM%.2f\n",totalPrice );
  121.  
  122. }
  123.  
  124.  
  125.  
  126. // sell is a recursive function which checks the followings
  127. /*
  128.     1. First it checks if the quantity is less than the items last entry in the products struct array
  129.     2. If yes,then it will sell the amount (quantity) and update the items number in the products struct array,Add the price to totalPrice
  130.     3. If the items are equal to the items of last entry of products array,Add the price to totalPrice
  131.     4. If yes,then sell the amount (quantity) and remove the last entry (row) from the array.Because there won't be any left.also the items of the last entry's item is reduced to 0 which can be used for further validation
  132.     5. If the amount (quantity) is greater than the items of the last entry of the array,then first sell the amount (quantity),then remove the last entry from the array cause all the items have been sold and there are some more needed.So as we need iterate over the same logic,we'll be calling the function again (recursion).Add the price to totalPrice
  133.    
  134. */ 
  135. void sell(int quantity,float total)
  136. {
  137.  
  138.  
  139.     if (quantity < products[count - 1].items ) {
  140.  
  141.  
  142.             products[count - 1].items = products[count - 1].items - quantity;
  143.             totalPrice += products[count -1].price * quantity;
  144.            
  145.             printf("\n %d  SCREWDRIVERS SOLD AT RM %.2f .  - SALE: RM%.2f\n",quantity,products[count - 1].price, totalPrice);
  146.      
  147.         } else if (quantity == products[count - 1].items ) {
  148.  
  149.  
  150.             products[count - 1].items = products[count - 1].items - quantity;
  151.             totalPrice += products[count -1].price * quantity;
  152.  
  153.             printf("\n %d  SCREWDRIVERS SOLD AT RM %.2f .  - SALE: RM%.2f\n",quantity,products[count - 1].price,totalPrice);
  154.            
  155.             products[count -1].items = 0;
  156.  
  157.             count--;
  158.  
  159.         } else {
  160.            
  161.        
  162.             int leftOverItems = quantity - products[count - 1 ].items; // 50 - 30 = 20
  163.             totalPrice = products[count -1].price * quantity;
  164.             printf("\n %d  SCREWDRIVERS SOLD AT RM %.2f .  - SALE: RM%.2f\n",products[count-1].items,products[count - 1].price, total);
  165.  
  166.             products[count - 1].items = 0;
  167.  
  168.             count--;
  169.  
  170.             if(count == 0) {   
  171.  
  172.                 printf("%d SCREWDRIVERS NOT AVAILABLE FOR SALE.\n", leftOverItems);
  173.                 return;
  174.             }
  175.             // calling this function again
  176.             sell(leftOverItems,totalPrice);
  177.  
  178.         }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement