Next Day Cargo Company needs an integrated application withtheir system that sorts the cargo with respect to their weight anddistributes the weight evenly (if possible) between the givenvehicles. Note that each vehicle has a weight limit, and cannotexceed it. But each vehicle can be used more than once during theseoperations. And do not forget, they want a fast solution becausewaiting a long time for the algorithm to complete reports will risktheir motto, which is “Next Day Cargoâ€. They require a report whichwill be used as a work order for workers and truck drivers as itshows the order of load for each vehicle. The report/order willhave the following four columns: Number (which will be the loadingsequence), Barcode Number, Weight, Postal Code. Our team has workedon API architecture and concluded in the solution given in Figure2.4. In this API there are two main steps, which are explained asfollows; 1. Sort the shipments: We are planning to use merge sortalgorithm. Sorting must be from heaviest to lightest. 2. Distributethe sorted shipments to vehicles: To distribute shipments toavailable vehicles there are some restriction/request as follows; oThere is a list of available vehicles, in which a vehicle can beused more than once if needed. o Total weight of assigned shipmentsto a vehicle cannot exceed its weight capacity. o Order ofassignments must be from heaviest to lightest which will be used asa loading order. For the distribution of cargo to vehicles thefollowing steps are considered; • Take each cargo in the givenorder, • Take a vehicle and calculate the total weight of the takencargo assigned to the taken vehicle, • If the capacity of thevehicle is not exceed, assign the cargo to the vehicle, • If not,select another vehicle until a suitable vehicle is found. Vehicleand Shipment classes are completed in the API.LoadsOfVehicle andSort classes need implementations. A sample report is provided inthe file SampleReport.txt.
In order to sort the shipments by weight, you will need toimplement the sortShipmentsByWeight method in the Sort.java file.To implement the sortShipmentsByWeight you will need to implementthe following two methods mergeSortShipmentsByWeight and mergemethod. mergeSortShipmentsByWeight will take three arguments, anarray of shipments, a start index and an end index. This methodshould recursively call itself until the shipment has been sorted.The merge method should merge two halves of the sorted shipment.Develop the sortShipmentsByWeight() method in the Sort.java file tosort cargo from heaviest to lightest. The shipments now need to bedistributed evenly among the vehicles. Each vehicle can do multipletransfers and can be used more than once if needed, but the weightlimit of each vehicle cannot be exceeded. The first method toimplement is distributeShipmentsToVehicles which will return themaximum weight per load for each vehicle. Once the maximum capacityper vehicle has been calculated, the shipments need to be loadedonto the vehicle using the loadTheVehicles method. Develop thedistributeShipmentsToVehicles method to evenly distribute weightbetween transportation vehicles but do not exceed the weight limitof each vehicle. (Each vehicle can do multiple transfers/can beused more than once if needed) Implment the loadTheVehicles methodto load the shipments into the vehicles, the total capacity of thevehicles must be greater than the total weight of the shipment.Implement the getLoadReport method in the LoadsOfVehicle.java fileto generate a report. Since the load reports will be read by humansand computers alike, it is important that the report format matchesthe sample report, including spaces.
LoadsOfVehicles.java
import java.util.ArrayList;
/**
* @author
*
*/
public class LoadsOfVehicle {
private Vehicle vehicle;
private ArrayList cargoList;
public LoadsOfVehicle(Vehicle vehicle) {
super();
this.vehicle = vehicle;
this.cargoList = new ArrayList<>();
}
public ArrayList getCargoList() {
return cargoList;
}
public void addCargo(Shipment shipment) {
cargoList.add(shipment);
}
public double getLoadedWeight() {
double totalWeight = 0;
for (int i = 0; i < cargoList.size(); i++) {
totalWeight += cargoList.get(i).getWeight();
}
return totalWeight;
}
public double getVehicleCapacity() {
return vehicle.getWeightCapacity();
}
public String getVehiclePlate() {
return vehicle.getPlate();
}
public String getVehicleDriver() {
return vehicle.getDriverName();
}
/**This methods generates report for the vehicle and the
* the shipments in it wrt weight (from heaviest to lightest)
*
* @return generated report
*/
public String getLoadReport() {
return null;
}
/**This method takes vehicles and their loadings as input and
* generates report for each vehicle
*
* @param loadsOfVehicles vehicle and loadings array
* @return a report for each vehicle showing loading sequence
*/
public static String[] getReports(LoadsOfVehicle[] loadsOfVehicles){
return null;
}
}
SampleReport.txt
Vehicle Plate : KZ66 ZYT
Weight Limit : 10.00
Driver : John Locke
Loading Sequence Barcode Number Weight Delivery Address PostalCode
---------------- -------------- ----------------------------------
1 2147253625 2.000 34547
2 2147043627 0.750 34543
---------------- -------------- ----------------------------------
2 -------------- 2.750 ----------------------------
Vehicle Plate : SBG 984
Weight Limit : 20.00
Driver : Down Brown
Loading Sequence Barcode Number Weight Delivery Address PostalCode
---------------- -------------- ----------------------------------
1 2147013624 20.000 34546
---------------- -------------- ----------------------------------
1 -------------- 20.000 ----------------------------
Vehicle Plate : EKI6 LLO
Weight Limit : 50.00
Driver : Adam Smith
Loading Sequence Barcode Number Weight Delivery Address PostalCode
---------------- -------------- ----------------------------------
1 2147313623 15.000 34543
2 2147013628 1.250 34543
3 2147014626 0.500 34542
---------------- -------------- ----------------------------------
3 -------------- 16.750 ----------------------------
Shipment.java
public class Shipment {
private double weight;
private long barcodeNumber;
private int fromPostalCode;
private int toPostalCode;
public Shipment(double weight, long barcodeNumber, intfromPostalCode, int toPostalCode) {
super();
this.weight = weight;
this.barcodeNumber = barcodeNumber;
this.fromPostalCode = fromPostalCode;
this.toPostalCode = toPostalCode;
}
public int getFromPostalCode() {
return fromPostalCode;
}
public double getWeight() {
return weight;
}
public long getBarcodeNumber() {
return barcodeNumber;
}
public int getToPostalCode() {
return toPostalCode;
}
}
Sort.java
public class Sort {
/**
* @param shipments
* @param vehicles
* @return
*/
public static LoadsOfVehicle[] getLoadingArray(Shipment[]shipments, Vehicle[] vehicles) {
return null;
}
/**
* @param shipmentsToSort
*/
public static void sortShipmentsByWeight(Shipment[]shipmentsToSort) {
}
/**
* @param shipmentsToSort
* @param start
* @param end
*/
private static void mergeSortShipmentsByWeight(Shipment[]shipmentsToSort, int start, int end) {
}
/**
* @param array
* @param start
* @param middle
* @param end
*/
private static void merge(Shipment[] array, int start, int middle,int end) {
}
/**
* @param sortedShipments
* @param vehicles
* @return
*/
public static LoadsOfVehicle[] distributeShipmentsToVehicles(
Shipment[] sortedShipments, Vehicle[] vehicles) {
//calculate total weight of shipments
//calculate total weight capacity of vehicles
//if capacity of vehicles is not enough add more turns forvehicles
//define return array with extra vehicle turns
//use modulus to add vehicles more than once if needed
//distribute weight to vehicles
return null;
}
/**
* @param vehicleListToLoad
* @param sortedShipments
*/
public static void loadTheVehicles(LoadsOfVehicle[]vehicleListToLoad, Shipment[] sortedShipments) {
//while there is more cargo to load
//distribute weight over vehicles
//calculate total weight if current weight is loaded
//there is a cargo to load and vehicle has the capacity
//load cargo to vehicle
//If no more cargo to load break while loop
}
public static void main(String[] args) {
/*
* This main method is a stub.
* It does nothing.
* Feel free to write your own code to test yourimplementation.
* In this case, we have nothing actionable in here, just thiscomment block, so the JVM should rapidly lose interest and move onto the rest of your code.
*/
}
}
Vehicle.java
public class Vehicle {
private double weightCapacity;
private String plate;
private String driverName;
public Vehicle(double weightCapacity, String plate, StringdriverName) {
super();
this.weightCapacity = weightCapacity;
this.plate = plate;
this.driverName = driverName;
}
public double getWeightCapacity() {
return weightCapacity;
}
public String getPlate() {
return plate;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
}