Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // function prototyping
- void menu();
- void addItem();
- void showAllItems();
- void buyItem();
- void sell(int quantity,float total);
- // count holds the value for total items in the inventory
- int count = 0;
- // totalPrice initiazlied to 0
- float totalPrice = 0;
- struct inventory
- {
- int items;
- float price;
- } ;
- // define inventory as array
- // in this case, products is an array of struct (named inventory),defined above
- struct inventory products[10];
- int main()
- {
- // userChoice holds the value of the input after displaying the menu
- int userChoice;
- menu();
- while (1){
- scanf("%d",&userChoice);
- switch (userChoice) {
- case 1:
- showAllItems();
- break;
- case 2:
- addItem();
- break;
- case 3:
- buyItem();
- break;
- case 4:
- printf("===============================\n");
- return 0;
- break;
- default:
- printf("Invalid Choice. \n");
- continue;
- }
- menu();
- }
- return 0;
- }
- // prints the available options
- void menu()
- {
- printf("Choose your Option \n");
- printf("1: Show All Records\n");
- printf("2: Add Items \n");
- printf("3: Buy Items \n");
- printf("4: Exit \n");
- printf("====================================\n");
- }
- // add items to the inventory
- void addItem()
- {
- printf(" Number of screw drivers \n");
- scanf("%d",&products[count].items);
- printf(" Unit Price \n");
- scanf("%f",&products[count].price);
- count++;
- printf("Product Added\n");
- }
- //display all items in the inventory
- void showAllItems()
- {
- // if count,which is the total number of items in the inventory is zero,no need to loop
- if(count == 0) {
- printf("List is empty\n");
- return;
- }
- // else loop through the list of items in the products[] inventory
- for ( int i =0; i < count ; i++) {
- printf(" %d SCREWDRIVERS : UNIT PRICE %.2f \n", products[i].items , products[i].price);
- }
- }
- // buy an item from inventory
- void buyItem()
- {
- // check if inventory is empty
- if (count <= 0) {
- printf("THe list is empty!\n");
- return;
- }
- // quantity holds the value for how many screwdrivers needs (according to input)
- int quantity;
- printf("\n How many SCREWDRIVERS? \n");
- scanf("%d",&quantity);
- // calling sell function
- sell(quantity,0);
- printf("TOTAL FOR THIS SALE: RM%.2f\n",totalPrice );
- }
- // sell is a recursive function which checks the followings
- /*
- 1. First it checks if the quantity is less than the items last entry in the products struct array
- 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
- 3. If the items are equal to the items of last entry of products array,Add the price to totalPrice
- 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
- 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
- */
- void sell(int quantity,float total)
- {
- if (quantity < products[count - 1].items ) {
- products[count - 1].items = products[count - 1].items - quantity;
- totalPrice += products[count -1].price * quantity;
- printf("\n %d SCREWDRIVERS SOLD AT RM %.2f . - SALE: RM%.2f\n",quantity,products[count - 1].price, totalPrice);
- } else if (quantity == products[count - 1].items ) {
- products[count - 1].items = products[count - 1].items - quantity;
- totalPrice += products[count -1].price * quantity;
- printf("\n %d SCREWDRIVERS SOLD AT RM %.2f . - SALE: RM%.2f\n",quantity,products[count - 1].price,totalPrice);
- products[count -1].items = 0;
- count--;
- } else {
- int leftOverItems = quantity - products[count - 1 ].items; // 50 - 30 = 20
- totalPrice = products[count -1].price * quantity;
- printf("\n %d SCREWDRIVERS SOLD AT RM %.2f . - SALE: RM%.2f\n",products[count-1].items,products[count - 1].price, total);
- products[count - 1].items = 0;
- count--;
- if(count == 0) {
- printf("%d SCREWDRIVERS NOT AVAILABLE FOR SALE.\n", leftOverItems);
- return;
- }
- // calling this function again
- sell(leftOverItems,totalPrice);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement