#include #include using namespace std; // cargo class class Cargo { string label; // variable to store the Cargo's label int height; // variable to store the Cargo's height int width; // variable to store the Cargo's width int length; // variable to store the Cargo's length double weight; // variable to store the Cargo's weight public: // default constructor to initialize a Cargo Object Cargo() { label = ""; height = 0; width = 0; length = 0; weight = 0.0; } // parameterized constructor to initialize a Cargo object with passed arguments Cargo(string _label, int _height, int _width, int _length, double _weight) { label = _label; height = _height; width = _width; length = _length; weight = _weight; } // function to return the Volume of the Cargo int getVolume() { return height * width * length; } // function to return the weight of the Cargo double getWeight() { return weight; } // function to return the label of the Cargo string getLabel() { return label; } // function to print cargo details void print() { cout << "\n"; cout << left << setw(30) << "Label: " << label << "\n"; cout << left << setw(30) << "volume: " << getVolume() << "\n"; cout << left << setw(30) << "Weight: " << weight << "\n\n"; } }; // CargoPlane class class CargoPlane { // variable to store the current city location of the plane string currentCity; // specified by the constructor // variable to store the total distance(in miles) flown by the plane int milesFlown; // initally 0 // variable to store the total duration(in hours) flown by the plane int hoursFlown; // initally 0 // variable to store the fuel consumed in gallons per hour by the plane int fuelRate; // specified by the constructor // variable to store the total fuel capacity of the plane int fuelCapacity; // specified by the constructor // variable to store the current amount of fuel consumed by the plane int fuelConsumed; // initally 0 // variable to store the maximum weight capacity of the plane double weightLimit; // specified by the constructor // variable to store the current weight of cargo loaded on the plane double weightLoaded; // initally 0 // variable to store the maximum volume capacity of the plane int volumeLimit; // specified by the constructor // variable to store the current volume of cargo loaded on the plane int volumeLoaded; // initally 0 // variable to store the details of Cargo objects loaded on the plane Cargo cargos[50]; // this signifies that a cargo plane can carry at most 50 cargo containers int cargoCount; // keeps track of how many cargo containers are loaded onto the plane; initally 0 public: // default constructor to initialize a CargoPlane Object CargoPlane() { weightLimit = 0; volumeLimit = 0; fuelCapacity = 0; fuelRate = 0; currentCity = "home"; milesFlown = 0; hoursFlown = 0; fuelConsumed = 0; weightLoaded = 0; volumeLoaded = 0; cargoCount = 0; } // parameterized constructor to initialize a CargoPlane object with passed arguments CargoPlane(double maxWeight, int maxVolume, int fuelcapacity, int fuelrate, const string &city) { weightLimit = maxWeight; volumeLimit = maxVolume; fuelCapacity = fuelcapacity; fuelRate = fuelrate; currentCity = city; milesFlown = 0; hoursFlown = 0; fuelConsumed = 0; weightLoaded = 0; volumeLoaded = 0; cargoCount = 0; } // function to load a cargo object onto the plane after multiple checks int loadCargo(Cargo cargo) { // if volume and weight both are out of bounds, return -3 if (cargo.getVolume() + volumeLoaded > volumeLimit && cargo.getWeight() + weightLoaded > weightLimit) return -3; // if volume is out of bounds, return -2 if (cargo.getVolume() + volumeLoaded > volumeLimit) return -2; // if weight is out of bounds, return -1 if (cargo.getWeight() + weightLoaded > weightLimit) return -1; // if both are in bounds, update the weight and volume loaded, assign the cargo variable, increment the cargo count and return 1 weightLoaded += cargo.getWeight(); volumeLoaded += cargo.getVolume(); cargos[cargoCount] = cargo; cargoCount++; return 1; } // function to remove a cargo from the plane, if present. void unLoadCargo(string label) { // foundIndex stores the index of the cargo, if found int foundIndex = -1; for (int i = 0; i < cargoCount; i++) { if (label == cargos[i].getLabel()) { foundIndex = i; // if the label matches the cargo label, break out of the loop after assigning the foundIndex break; } } if (foundIndex != -1) //if foundIndex is not -1 then the cargo label was found { // delete that cargo from the list for (int i = foundIndex; i < cargoCount - 1; i++) { cargos[i] = cargos[i + 1]; } // update the volume and weight loaded volumeLoaded -= cargos[foundIndex].getVolume(); weightLoaded -= cargos[foundIndex].getWeight(); // decrease the cargocount cargoCount--; } } // function to fly the plane from the initial city to the final city if there is enough fuel bool fly(string city, int hours, int miles) { // assign the city and update the hours and miles flown currentCity = city; hoursFlown += hours; milesFlown += miles; int fuelRequired = fuelRate * hours; // check if fuelRequired is within bounds if (fuelRequired + fuelConsumed <= fuelCapacity) { // in this case, the plane had enough fuel to travel, so the flight was successful // Now, we add the fuelRequired to the fuelConsumed fuelConsumed += fuelRequired; return true; } // otherwise, more fuel was reuired than the amount available and the plane crashed return false; } // function to print the CargoPlane details void print() { cout << left << setw(15) << "Current City:" << currentCity << "\n"; cout << left << setw(15) << "Mile Flown:" << milesFlown << "\n"; cout << left << setw(15) << "Hours Flown:" << hoursFlown << "\n"; cout << left << setw(15) << "Fuel Usage:" << fuelRate << " gallons per hour\n"; cout << left << setw(15) << "Fuel Capacity:" << fuelCapacity << "\t"; cout << left << setw(15) << "Consumed:" << fuelConsumed << "\t"; cout << left << setw(15) << "Remaining:" << fuelCapacity - fuelConsumed << "\n"; cout << left << setw(15) << "Weight Limit:" << weightLimit << "\t"; cout << left << setw(15) << "Consumed:" << weightLoaded << "\t"; cout << left << setw(15) << "Remaining:" << weightLimit - weightLoaded << "\n"; cout << left << setw(15) << "Volume Limit:" << volumeLimit << "\t"; cout << left << setw(15) << "Consumed:" << volumeLoaded << "\t"; cout << left << setw(15) << "Remaining:" << volumeLimit - volumeLoaded << "\n\n"; cout << "Current Cargo\n"; if (cargoCount == 0) cout << "None"; else { for (int i = 0; i < cargoCount; i++) { cargos[i].print(); } } } }; // driver function int main() { string command; // variable to store the command chosen by the user CargoPlane plane(40000, 30000, 60000, 450, "home"); // creating a CargoPlane object while (true) // the loop runs until the player quits { cout << "Please choose one of the following commands\n"; cout << "FLY, LOAD, UNLOAD, PRINT, QUIT\n"; cout << "Choice: "; cin >> command; // etting user's command choice if (command == "FLY") { string city; // variable to store the destinatuon city int hours, miles; // variables to store the tavel hours and distance cout << "\nEnter the destination city name (without spaces): "; cin >> city; cout << "\nEnter travel time in hours: "; cin >> hours; cout << "\nEnter travel distance in miles: "; cin >> miles; // calling the fly function for the CaroPlane object bool result = plane.fly(city, hours, miles); if (result) cout << "\nThe flight was successful \n"; else cout << "\nThe flight crashed because it ran out of fuel\n"; } else if (command == "LOAD") { string label; // variable to store the cargo label int height, length, width; // variables to store the height, length, width of the cargo double weight; // variable to store the cargo weight cout << "\nEnter Cargo label: "; cin.ignore(); getline(cin, label); cout << "\nEnter Cargo height: "; cin >> height; cout << "\nEnter Cargo width: "; cin >> width; cout << "\nEnter Cargo length: "; cin >> length; cout << "\nEnter Cargo weight: "; cin >> weight; // create a cargo object with the data entered Cargo c(label, height, width, length, weight); // call loadCargo function to load cargo c onto the plane int result = plane.loadCargo(c); switch (result) { case -3: cout << "\nPlane\'s Volume and Weight limit exceeded! Cannot load this cargo conatiner!\n"; break; case -2: cout << "\nPlane\'s Volume limit exceeded! Cannot load this cargo conatiner!\n"; break; case -1: cout << "Plane\'s Weight limit exceeded! Cannot load this cargo conatiner!\n"; break; case 1: cout << "\nCargo container with label: " << c.getLabel() << " was successfully loaded!\n"; break; } } else if (command == "UNLOAD") { string label; // variable to store the cargo label cout << "\nEnter the label of the container to be unloaded: "; cin >> label; // call unloadCargo function to unload cargo with label from the plane plane.unLoadCargo(label); } else if (command == "PRINT") { // call print function to print the palne details plane.print(); } else if (command == "QUIT") { break; } cout << "\n\n\n\n\n"; } cout << "\n\nThank You\n\n"; return 0; }