# Inventory and Delivery using C assignment solution

## Brewery Delivery Shipping Assignment

Your assignment is to create a C program that will ship brewery inventory to each of the five warehouses to meet their needs. Your goal is to create a program that will accomplish this task in the minimum amount of time.

The YumYum brewery does not know what method of shipping to the warehouses will be the fastest. What this means is that they want to run some experiments. Your program has to be able to track the amount of time required to perform the following delivery strategies:

1. Take care of all of a warehouse's inventory needs one by one starting with the furthest away warehouse (5) and then working your way back to the closest warehouse (1).

2. Do the same thing, but this time deliver to the closest warehouse first (1) and work your way to the furthest away warehouse (5).

3. Meet the warehouse's needs for kegs first, then boxes of bottles, then cases of cans. Start with the furthest away warehouse (5) and work towards the closest warehouse (1). Note that you have three trucks and each one of them can be loaded. You will need to travel from one

warehouse to the next as you unload your product. If the three trucks cannot hold all of the requested inventory, they will have to return to the brewery to get refilled before going back to service the remaining warehouses.

4. Meet ½ of each warehouse's inventory needs and then come back and provide them with the second half of what they have requested.

When you display the amount of time required to make the deliveries to all of the warehouses, display the time in hours and minutes.

When you want to write your C assignment program, use a number of functions to implement what the program does.

You are going to want to be able to experiment with multiple strategies to discover what approach allows you to minimize your delivery time.

Solution:

``` #include < stdio.h> #include < stdlib.h> #include < stdbool.h> #include < time.h> #include < string.h> #define WORLD 0 #define largeTruckMaxLoad 80000 #define mediumTruckMaxLoad 20000 #define smallTruckMaxLoad 2000 #define kegWeight 140 #define boxOfBottlesWeight 36 #define caseOfCansWeight 20 #define loadTime 60 #define unloadTime 30 struct inventory { int warehouseID; int kegs; int boxsOfBottles; int casesOfCans; }; struct truckContents { int numKegs; int numBoxs; int numCases; }; int shippingTimes = {0,5,10,15,20,25, 5,0,5,10,15,20, 10,5,0,5,10,15, 15,10,5,0,5,10, 20,15,10,5,0,5, 25,20,15,10,5,0}; #include "HW3ShipingRoutines.h" /* Define Functions */ struct truckContents loadTruck (int truckHaulingCapacity,int kegOrder, int bottleOrder, int canOrder); int shipBeer(storeID); int shipProduct(storeID); // INPUT: file_name (the text file name) // OUTPUT: total brewery inventory and inventories of each warehouse void readData (const char* file_name, int brewery_inventory, struct inventory inventories) { FILE* file = fopen (file_name, "r"); int i = 0; int j=0; for(j=0;j<3;j++) { fscanf (file, "%d", &i); brewery_inventory[j] = i; } int wid, kegs, bob, coc; for(j=0;j<5;j++) { fscanf (file, "%d %d %d %d", &wid, &kegs, &bob, &coc); inventories[j].warehouseID=wid; inventories[j].kegs=kegs; inventories[j].boxsOfBottles=bob; inventories[j].casesOfCans=coc; } fclose (file); } // get the maximum time from 3 trucks int maximumOfTime(int time1, int time2, int time3){ if((time1 >= time2) && (time1 >= time3)) { return time1; } else if((time2 >= time1) && (time2 >= time3)) { return time2; } else { return time3; } } // strategy 1 (from warehouse 5 to 1) int strategy1(struct inventory inventories_,int brewery_inventory ) { // copy inventory to a new variable struct inventory inventories; int i; for (i = 0; i < 5; ++i) { inventories[i] = inventories_[i]; } // initialize array of trucks struct truckContents truck; // initial time for each trucks int shipTimeTruck={0,0,0}; // initialize the total packet that should be delivered int totalKegsRemaining = brewery_inventory; int totalBoxsRemaining = brewery_inventory; int totalCasesRemaining = brewery_inventory; // initialize the total packet of each keg.box.case in each truck int totalKegsInTruck={0,0,0}; int totalBoxsInTruck={0,0,0}; int totalCasesInTruck={0,0,0}; // initialize maximum load default int truckMaxLoadDefault = {80000,20000,2000}; // initialize maximum load (used if a truck has a remaining item) int truckMaxLoad; int t=0; for(i=4;i>=0;i--) { while(true) { // if the inventory warehouse still need a product and the product in the truck is empty if((totalKegsInTruck[t] == 0 && inventories[i].kegs > 0) || (totalBoxsInTruck[t] == 0 && inventories[i].boxsOfBottles > 0) || (totalCasesInTruck[t] == 0 && inventories[i].casesOfCans > 0)) { // update the max load of truck truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]-boxOfBottlesWeight*totalBoxsInTruck[t]-caseOfCansWeight*totalCasesInTruck[t]; // priority load for next warehouse truck[t] = loadTruck(truckMaxLoad[t],inventories[i].kegs,inventories[i].boxsOfBottles,inventories[i].casesOfCans); // update the amount of product in the truck totalKegsInTruck[t] += inventories[i].kegs-truck[t].numKegs; totalBoxsInTruck[t] += inventories[i].boxsOfBottles-truck[t].numBoxs; totalCasesInTruck[t] += inventories[i].casesOfCans-truck[t].numCases; // update the max load of truck truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]-boxOfBottlesWeight*totalBoxsInTruck[t]-caseOfCansWeight*totalCasesInTruck[t]; // load for the remaining space truck[t] = loadTruck(truckMaxLoad[t],totalKegsRemaining,totalBoxsRemaining,totalCasesRemaining); // update the amount of product in the truck totalKegsInTruck[t] += totalKegsRemaining-truck[t].numKegs; totalBoxsInTruck[t] += totalBoxsRemaining-truck[t].numBoxs; totalCasesInTruck[t] += totalCasesRemaining-truck[t].numCases; // update the reamining product that should be delivered totalKegsRemaining = truck[t].numKegs; totalBoxsRemaining = truck[t].numBoxs; totalCasesRemaining = truck[t].numCases; // calculate the ship time for loading a product shipTimeTruck[t] += shipBeer(i); } else { // calculate the ship time navigating an unload the product shipTimeTruck[t] += shipProduct(i); } // update amount of kegs in inventory and in the truck after unloaded if(inventories[i].kegs >= totalKegsInTruck[t]){ inventories[i].kegs -= totalKegsInTruck[t]; totalKegsInTruck[t] = 0; } else{ totalKegsInTruck[t] -= inventories[i].kegs; inventories[i].kegs = 0 ; } // update amount of boxs in inventory and in the truck after unloaded if(inventories[i].boxsOfBottles >= totalBoxsInTruck[t]){ inventories[i].boxsOfBottles -= totalBoxsInTruck[t]; totalBoxsInTruck[t] = 0; } else{ totalBoxsInTruck[t] -= inventories[i].boxsOfBottles; inventories[i].boxsOfBottles = 0 ; } // update amount of cases in inventory and in the truck after unloaded if(inventories[i].casesOfCans >= totalCasesInTruck[t]){ inventories[i].casesOfCans -= totalCasesInTruck[t]; totalCasesInTruck[t] = 0; } else{ totalCasesInTruck[t] -= inventories[i].casesOfCans; inventories[i].casesOfCans = 0 ; } // change truck t++; if(t == 3) // limit to only 3 trucks t=0; // if all of the product are delivered in each inventory in a warehouse ID, then change to another warehouse if((inventories[i].kegs <=0) && (inventories[i].boxsOfBottles <=0) && (inventories[i].casesOfCans <=0)) { break; } } } return maximumOfTime(shipTimeTruck[t],shipTimeTruck[t],shipTimeTruck[t]); } // strategy 2 (from warehouse 1 to 5) int strategy2(struct inventory inventories_,int brewery_inventory ) { struct inventory inventories; int i; for (i = 0; i < 5; ++i) { inventories[i] = inventories_[i]; } struct truckContents truck; int shipTimeTruck={0,0,0}; int totalKegsRemaining = brewery_inventory; int totalBoxsRemaining = brewery_inventory; int totalCasesRemaining = brewery_inventory; int totalKegsInTruck={0,0,0}; int totalBoxsInTruck={0,0,0}; int totalCasesInTruck={0,0,0}; int truckMaxLoadDefault = {80000,20000,2000}; int truckMaxLoad; int t=0; for(i=0;i<5;i++) { while(true) { if((totalKegsInTruck[t] == 0 && inventories[i].kegs > 0) || (totalBoxsInTruck[t] == 0 && inventories[i].boxsOfBottles > 0) || (totalCasesInTruck[t] == 0 && inventories[i].casesOfCans > 0)) { truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]-boxOfBottlesWeight*totalBoxsInTruck[t]-caseOfCansWeight*totalCasesInTruck[t]; // priority load for next warehouse truck[t] = loadTruck(truckMaxLoad[t],inventories[i].kegs,inventories[i].boxsOfBottles,inventories[i].casesOfCans); totalKegsInTruck[t] += inventories[i].kegs-truck[t].numKegs; totalBoxsInTruck[t] += inventories[i].boxsOfBottles-truck[t].numBoxs; totalCasesInTruck[t] += inventories[i].casesOfCans-truck[t].numCases; // load for the remaining space truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]-boxOfBottlesWeight*totalBoxsInTruck[t]-caseOfCansWeight*totalCasesInTruck[t]; truck[t] = loadTruck(truckMaxLoad[t],totalKegsRemaining,totalBoxsRemaining,totalCasesRemaining); totalKegsInTruck[t] += totalKegsRemaining-truck[t].numKegs; totalBoxsInTruck[t] += totalBoxsRemaining-truck[t].numBoxs; totalCasesInTruck[t] += totalCasesRemaining-truck[t].numCases; totalKegsRemaining = truck[t].numKegs; totalBoxsRemaining = truck[t].numBoxs; totalCasesRemaining = truck[t].numCases; shipTimeTruck[t] += shipBeer(i); } else { shipTimeTruck[t] += shipProduct(i); } if(inventories[i].kegs >= totalKegsInTruck[t]){ inventories[i].kegs -= totalKegsInTruck[t]; totalKegsInTruck[t] = 0; } else{ totalKegsInTruck[t] -= inventories[i].kegs; inventories[i].kegs = 0 ; } if(inventories[i].boxsOfBottles >= totalBoxsInTruck[t]){ inventories[i].boxsOfBottles -= totalBoxsInTruck[t]; totalBoxsInTruck[t] = 0; } else{ totalBoxsInTruck[t] -= inventories[i].boxsOfBottles; inventories[i].boxsOfBottles = 0 ; } if(inventories[i].casesOfCans >= totalCasesInTruck[t]){ inventories[i].casesOfCans -= totalCasesInTruck[t]; totalCasesInTruck[t] = 0; } else{ totalCasesInTruck[t] -= inventories[i].casesOfCans; inventories[i].casesOfCans = 0 ; } t++; if(t == 3) t=0; if((inventories[i].kegs <=0) && (inventories[i].boxsOfBottles <=0) && (inventories[i].casesOfCans <=0)) { break; } } } return maximumOfTime(shipTimeTruck[t],shipTimeTruck[t],shipTimeTruck[t]); } // strategy 3 (from warehouse 5 to 1 but prioirty for kegs -> boxs, then cases) int strategy3(struct inventory inventories_,int brewery_inventory ) { struct inventory inventories; int i; for (i = 0; i < 5; ++i) { inventories[i] = inventories_[i]; } struct truckContents truck; int shipTimeTruck={0,0,0}; printf("SEND TO INVENTORY %d\n",i+1); int totalKegsRemaining = brewery_inventory; int totalBoxsRemaining = brewery_inventory; int totalCasesRemaining = brewery_inventory; int totalKegsInTruck={0,0,0}; int totalBoxsInTruck={0,0,0}; int totalCasesInTruck={0,0,0}; int truckMaxLoadDefault = {80000,20000,2000}; int truckMaxLoad; int t=0; for(i=4;i>=0;i--) { while(true) { printf("TRUCK: %d\n",t); if((totalKegsInTruck[t] == 0 && inventories[i].kegs > 0)) { printf("LOAD TRUCK\n"); truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]; // priority load for next warehouse truck[t] = loadTruck(truckMaxLoad[t],inventories[i].kegs,0,0); totalKegsInTruck[t] += inventories[i].kegs-truck[t].numKegs; printf("Total in truck: %d \n",totalKegsInTruck[t]); // load for the remaining space truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]; truck[t] = loadTruck(truckMaxLoad[t],totalKegsRemaining,0,0); totalKegsInTruck[t] += totalKegsRemaining-truck[t].numKegs; printf("Total in truck: %d \n",totalKegsInTruck[t]); totalKegsRemaining = truck[t].numKegs; shipTimeTruck[t] += shipBeer(i); } else { shipTimeTruck[t] += shipProduct(i); } if(inventories[i].kegs >= totalKegsInTruck[t]){ inventories[i].kegs -= totalKegsInTruck[t]; totalKegsInTruck[t] = 0; } else{ totalKegsInTruck[t] -= inventories[i].kegs; inventories[i].kegs = 0 ; } t++; if(t == 3) t=0; if((inventories[i].kegs <=0)) { break; } } } for(i=4;i>=0;i--) { while(true) { printf("TRUCK: %d\n",t); if((totalBoxsInTruck[t] == 0 && inventories[i].boxsOfBottles > 0)) { printf("LOAD TRUCK\n"); truckMaxLoad[t] = truckMaxLoadDefault[t]-boxOfBottlesWeight*totalBoxsInTruck[t]; // priority load for next warehouse truck[t] = loadTruck(truckMaxLoad[t],0,inventories[i].boxsOfBottles,0); totalBoxsInTruck[t] += inventories[i].boxsOfBottles-truck[t].numBoxs; printf("Total in truck: %d\n",totalBoxsInTruck[t]); // load for the remaining space truckMaxLoad[t] = truckMaxLoadDefault[t]-boxOfBottlesWeight*totalBoxsInTruck[t]; truck[t] = loadTruck(truckMaxLoad[t],0,totalBoxsRemaining,0); totalBoxsInTruck[t] += totalBoxsRemaining-truck[t].numBoxs; printf("Total in truck: %d\n",totalBoxsInTruck[t]); totalBoxsRemaining = truck[t].numBoxs; shipTimeTruck[t] += shipBeer(i); } else { shipTimeTruck[t] += shipProduct(i); } if(inventories[i].boxsOfBottles >= totalBoxsInTruck[t]){ inventories[i].boxsOfBottles -= totalBoxsInTruck[t]; totalBoxsInTruck[t] = 0; } else{ totalBoxsInTruck[t] -= inventories[i].boxsOfBottles; inventories[i].boxsOfBottles = 0 ; } t++; if(t == 3) t=0; if((inventories[i].boxsOfBottles <=0)) { break; } } } for(i=4;i>=0;i--) { while(true) { if((totalCasesInTruck[t] == 0 && inventories[i].casesOfCans > 0)) { truckMaxLoad[t] = truckMaxLoadDefault[t]-caseOfCansWeight*totalCasesInTruck[t]; // priority load for next warehouse truck[t] = loadTruck(truckMaxLoad[t],0,0,inventories[i].casesOfCans); totalCasesInTruck[t] += inventories[i].casesOfCans-truck[t].numCases; // load for the remaining space truckMaxLoad[t] = truckMaxLoadDefault[t]-caseOfCansWeight*totalCasesInTruck[t]; truck[t] = loadTruck(truckMaxLoad[t],0,0,totalCasesRemaining); totalCasesInTruck[t] += totalCasesRemaining-truck[t].numCases; totalCasesRemaining = truck[t].numCases; shipTimeTruck[t] += shipBeer(i); } else { shipTimeTruck[t] += shipProduct(i); } if(inventories[i].casesOfCans >= totalCasesInTruck[t]){ inventories[i].casesOfCans -= totalCasesInTruck[t]; totalCasesInTruck[t] = 0; } else{ totalCasesInTruck[t] -= inventories[i].casesOfCans; inventories[i].casesOfCans = 0 ; } t++; if(t == 3) t=0; if((inventories[i].casesOfCans <=0)) { break; } } } return maximumOfTime(shipTimeTruck[t],shipTimeTruck[t],shipTimeTruck[t]); } // strategy 4 (from warehouse 5 to 1 but delivered half by half) int strategy4(struct inventory inventories_,int brewery_inventory ) { struct inventory inventories; int i; for (i = 0; i < 5; ++i) { inventories[i] = inventories_[i]; } struct truckContents truck; int shipTimeTruck={0,0,0}; int totalKegsRemaining = brewery_inventory/2; int totalBoxsRemaining = brewery_inventory/2; int totalCasesRemaining = brewery_inventory/2; int totalKegsInTruck={0,0,0}; int totalBoxsInTruck={0,0,0}; int totalCasesInTruck={0,0,0}; int truckMaxLoadDefault = {80000,20000,2000}; int truckMaxLoad; int t=0; for(i=0;i<5;i++) { while(true) { if((totalKegsInTruck[t] == 0 && inventories[i].kegs > 0) || (totalBoxsInTruck[t] == 0 && inventories[i].boxsOfBottles > 0) || (totalCasesInTruck[t] == 0 && inventories[i].casesOfCans > 0)) { truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]-boxOfBottlesWeight*totalBoxsInTruck[t]-caseOfCansWeight*totalCasesInTruck[t]; // priority load for next warehouse truck[t] = loadTruck(truckMaxLoad[t],inventories[i].kegs,inventories[i].boxsOfBottles,inventories[i].casesOfCans); totalKegsInTruck[t] += inventories[i].kegs-truck[t].numKegs; totalBoxsInTruck[t] += inventories[i].boxsOfBottles-truck[t].numBoxs; totalCasesInTruck[t] += inventories[i].casesOfCans-truck[t].numCases; // load for the remaining space truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]-boxOfBottlesWeight*totalBoxsInTruck[t]-caseOfCansWeight*totalCasesInTruck[t]; truck[t] = loadTruck(truckMaxLoad[t],totalKegsRemaining,totalBoxsRemaining,totalCasesRemaining); totalKegsInTruck[t] += totalKegsRemaining-truck[t].numKegs; totalBoxsInTruck[t] += totalBoxsRemaining-truck[t].numBoxs; totalCasesInTruck[t] += totalCasesRemaining-truck[t].numCases; totalKegsRemaining = truck[t].numKegs; totalBoxsRemaining = truck[t].numBoxs; totalCasesRemaining = truck[t].numCases; shipTimeTruck[t] += shipBeer(i); } else { shipTimeTruck[t] += shipProduct(i); } if(inventories[i].kegs >= totalKegsInTruck[t]){ inventories[i].kegs -= totalKegsInTruck[t]; totalKegsInTruck[t] = 0; } else{ totalKegsInTruck[t] -= inventories[i].kegs; inventories[i].kegs = 0 ; } if(inventories[i].boxsOfBottles >= totalBoxsInTruck[t]){ inventories[i].boxsOfBottles -= totalBoxsInTruck[t]; totalBoxsInTruck[t] = 0; } else{ totalBoxsInTruck[t] -= inventories[i].boxsOfBottles; inventories[i].boxsOfBottles = 0 ; } if(inventories[i].casesOfCans >= totalCasesInTruck[t]){ inventories[i].casesOfCans -= totalCasesInTruck[t]; totalCasesInTruck[t] = 0; } else{ totalCasesInTruck[t] -= inventories[i].casesOfCans; inventories[i].casesOfCans = 0 ; } t++; if(t == 3) t=0; if((inventories[i].kegs <=0) && (inventories[i].boxsOfBottles <=0) && (inventories[i].casesOfCans <=0)) { break; } } } totalKegsRemaining = brewery_inventory/2; totalBoxsRemaining = brewery_inventory/2; totalCasesRemaining = brewery_inventory/2; for(i=0;i<5;i++) { while(true) { if((totalKegsInTruck[t] == 0 && inventories[i].kegs > 0) || (totalBoxsInTruck[t] == 0 && inventories[i].boxsOfBottles > 0) || (totalCasesInTruck[t] == 0 && inventories[i].casesOfCans > 0)) { truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]-boxOfBottlesWeight*totalBoxsInTruck[t]-caseOfCansWeight*totalCasesInTruck[t]; // priority load for next warehouse truck[t] = loadTruck(truckMaxLoad[t],inventories[i].kegs,inventories[i].boxsOfBottles,inventories[i].casesOfCans); totalKegsInTruck[t] += inventories[i].kegs-truck[t].numKegs; totalBoxsInTruck[t] += inventories[i].boxsOfBottles-truck[t].numBoxs; totalCasesInTruck[t] += inventories[i].casesOfCans-truck[t].numCases; // load for the remaining space truckMaxLoad[t] = truckMaxLoadDefault[t]-kegWeight*totalKegsInTruck[t]-boxOfBottlesWeight*totalBoxsInTruck[t]-caseOfCansWeight*totalCasesInTruck[t]; truck[t] = loadTruck(truckMaxLoad[t],totalKegsRemaining,totalBoxsRemaining,totalCasesRemaining); totalKegsInTruck[t] += totalKegsRemaining-truck[t].numKegs; totalBoxsInTruck[t] += totalBoxsRemaining-truck[t].numBoxs; totalCasesInTruck[t] += totalCasesRemaining-truck[t].numCases; totalKegsRemaining = truck[t].numKegs; totalBoxsRemaining = truck[t].numBoxs; totalCasesRemaining = truck[t].numCases; shipTimeTruck[t] += shipBeer(i); } else { shipTimeTruck[t] += shipProduct(i); } if(inventories[i].kegs >= totalKegsInTruck[t]){ inventories[i].kegs -= totalKegsInTruck[t]; totalKegsInTruck[t] = 0; } else{ totalKegsInTruck[t] -= inventories[i].kegs; inventories[i].kegs = 0 ; } if(inventories[i].boxsOfBottles >= totalBoxsInTruck[t]){ inventories[i].boxsOfBottles -= totalBoxsInTruck[t]; totalBoxsInTruck[t] = 0; } else{ totalBoxsInTruck[t] -= inventories[i].boxsOfBottles; inventories[i].boxsOfBottles = 0 ; } if(inventories[i].casesOfCans >= totalCasesInTruck[t]){ inventories[i].casesOfCans -= totalCasesInTruck[t]; totalCasesInTruck[t] = 0; } else{ totalCasesInTruck[t] -= inventories[i].casesOfCans; inventories[i].casesOfCans = 0 ; } t++; if(t == 3) t=0; if((inventories[i].kegs <=0) && (inventories[i].boxsOfBottles <=0) && (inventories[i].casesOfCans <=0)) { break; } } } return maximumOfTime(shipTimeTruck[t],shipTimeTruck[t],shipTimeTruck[t]); } int main() { // struct truckContents trucks; struct inventory inventories; int brewery_inventory; readData("HW #3 Data.txt", brewery_inventory, inventories); int i=0; for(i=0;i<3;i++) { printf("%d\n", brewery_inventory[i]); } for(i=0;i<5;i++) { printf("inv: %d %d %d %d\n", inventories[i].warehouseID, inventories[i].kegs, inventories[i].boxsOfBottles, inventories[i].casesOfCans); } int s1 = strategy1(inventories,brewery_inventory); int s2 = strategy2(inventories,brewery_inventory); int s3 = strategy3(inventories,brewery_inventory); int s4 = strategy4(inventories,brewery_inventory); printf("Strategy 1 time: %d:%d\n", s1/60,s1%60); printf("Strategy 2 time: %d:%d\n", s2/60,s2%60); printf("Strategy 3 time: %d:%d\n", s3/60,s3%60); printf("Strategy 4 time: %d:%d\n", s4/60,s4%60); return 0; } ```