Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
1.
CODE:
#include <iostream>
enum class LandingGearState {
RETRACTED,
EXTENDED
};
class Airplane {
private:
LandingGearState landingGearState;
public:
Airplane() : landingGearState(LandingGearState::RETRACTED) {}
void retractLandingGear() {
if (landingGearState == LandingGearState::EXTENDED) {
std::cout << "Retracting landing gear..." << std::endl;
landingGearState = LandingGearState::RETRACTED;
} else {
std::cout << "Landing gear is already retracted." << std::endl;
}
}
void extendLandingGear() {
if (landingGearState == LandingGearState::RETRACTED) {
std::cout << "Extending landing gear..." << std::endl;
landingGearState = LandingGearState::EXTENDED;
} else {
std::cout << "Landing gear is already extended." << std::endl;
}
}
void printLandingGearStatus() {
std::cout << "Landing Gear Status: ";
if (landingGearState == LandingGearState::RETRACTED) {
std::cout << "Retracted" << std::endl;
} else {
std::cout << "Extended" << std::endl;
}
}
};
int main() {
Airplane airplane;
int choice;
do {
std::cout << "Airplane Landing Gear Control" << std::endl;
std::cout << "1. Retract Landing Gear" << std::endl;
std::cout << "2. Extend Landing Gear" << std::endl;
std::cout << "3. Check Landing Gear Status" << std::endl;
std::cout << "4. Exit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
airplane.retractLandingGear();
break;
case 2:
airplane.extendLandingGear();
break;
case 3:
airplane.printLandingGearStatus();
break;
case 4:
std::cout << "Exiting program." << std::endl;
break;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
}
} while (choice != 4);
return 0;
}
OUTPUT:
Airplane Landing Gear Control
1. Retract Landing Gear
2. Extend Landing Gear
3. Check Landing Gear Status
4. Exit
Enter your choice: 1
Landing gear is already retracted.
Airplane Landing Gear Control
1. Retract Landing Gear
2. Extend Landing Gear
3. Check Landing Gear Status
4. Exit
Enter your choice: 2
Extending landing gear...
Airplane Landing Gear Control
1. Retract Landing Gear
2. Extend Landing Gear
3. Check Landing Gear Status
4. Exit
Enter your choice: 3
Landing Gear Status: Extended
Airplane Landing Gear Control
1. Retract Landing Gear
2. Extend Landing Gear
3. Check Landing Gear Status
4. Exit
Enter your choice: 4
Exiting program.