1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//creating class
class FlightBooking
{
//creating public class members
public:
FlightBooking(int id, int capacity, int reserved);
FlightBooking();
void printStatus();
bool reserveSeats(int number_of_seats, int reserved);
bool cancelSeats(int number_of_seats, int reserved);
int getID(int id);
//creating private class members
private:
int id;
int capacity;
int reserved;
float percent;
};
//saves inputted flight information when creating a flight
FlightBooking::FlightBooking(int id, int capacity, int reserved)
{
this -> id = id;
this -> capacity = capacity;
this -> reserved = reserved;
}
FlightBooking::FlightBooking()
{
id = 0;
capacity = 0;
reserved = 0;
}
//returns id value
int FlightBooking::getID(int id)
{
this -> id = id;
return id;
}
//function prints status of flight
void FlightBooking::printStatus()
{
percent = (reserved * 100.0 / capacity); //calculate percent
cout << "\nFlight " << id << ": " << reserved << "/" << capacity << " " << setprecision(4) << percent << "% seats taken\n";
}
//function reserves inputted seats to specific flight
bool FlightBooking::reserveSeats(int number_of_seats, int reserved)
{
//if the reserved seats + added seats are less then or equal to limit, save the added seats
if(((reserved + number_of_seats) * 100.0 / capacity) <= 105) //proceeds to add seats if capacity does not exceed 105%
{
reserved += number_of_seats;
cout << " " << reserved << " "; //checking to see what value reserved gives before exiting function
this -> reserved = reserved;
return true;
}
cout << "You reached the 105% capacity of reserved seats on this flight";
return false;
}
//function cancels reserved seats on a specific flight
bool FlightBooking::cancelSeats(int number_of_seats, int reserved)
{
//if the deleted seats doesn't = to less than 0, save the current number of seats
if(reserved - number_of_seats >= 0)
{
reserved -= number_of_seats;
this -> reserved = reserved;
return true;
}
else
{
cout << "Cannot perform this operation; more seats being removed than initially registered"<< endl;
return false;
}
}
int main() {
int capacity, id, counter = 0;//sets variables
int reserved = 0;
string command;
//creates array of objects to store flight ids
FlightBooking booking[10];
//gives user prompts and what each prompt does
cout << "A. create \n creates empty flight [id] with [capacity]";
cout <<"\nB. delete \n deletes flight [id] \n";
cout << "C. add \n adds [n] reserved seats to flight [id]\n";
cout << "D. cancel \n cancels [n] reservations to flight [id] \nE. quit \n exits program" << endl;
do //do while loop that continues until user inputs quit
{
int seats = 0;
//prints every flight id info that has been inputted
for(int i = 0; i < counter ; i++)
booking[i].printStatus();
cout << "\nWhat would you like to do: "; //prompts user
cin >> command;
//user can create a flight until it reaches a limit of 10 ids
if(command == "create" && counter < 10)
{
do{ //loop repeats if either flight number or capacity is equal to 0
cout << "Provide flight number: ";
cin >> id;
cout << "Provide flight capacity: ";
cin >> capacity;
}while(capacity <= 0 || id <= 0);
booking[counter] = FlightBooking(id, capacity, reserved);
booking[counter].getID(id);
counter++;
}
//user is warned that it has reached the limit of creating flights
else if(command == "create" && counter >= 10)
cout << "You have reached the maximum capacity of 10 flights";
//adds seats to reserved
else if(command == "add")
{
cout << "Provide id number: ";
cin >> id;
cout << "Provide number of reserved seats: ";
cin >> seats;
//if the flight id is found
if(booking[counter].getID(id) > 0)
{
booking[counter] = FlightBooking(id, capacity, reserved);
booking[counter].reserveSeats(seats, reserved) ; //flight adds reserves seats
cout << reserved; //checking to see what value reserved gives after exiting function
}
else //if flight id not found, prompts user to try again
cout << "Flight ID not found; try again";
}
//cancel seats to reserved
else if(command == "cancel")
{
cout << "Provide id number: ";
cin >> id;
cout << "Provide number of cancelled seats: ";
cin >> seats;
//if the flight id is found
if(booking[counter].getID(id) > 0)
{
booking[counter] = FlightBooking(id, capacity, reserved);
booking[counter].cancelSeats(seats, reserved); //flight cancels reserves seats
}
else //if flight id not found, prompts user to try again
cout << "Flight ID not found; try again";
}
//deletes the flight id that the user had previously inputted
else if(command == "delete")
{
cout << "Provide id number: ";
cin >> id;
//if the flight id is found
if(booking[counter].getID(id) > 0)
{
booking[counter] = FlightBooking(id, capacity, reserved);
booking[counter] = FlightBooking(); //flight is deleted by setting all of its information to 0
counter--;
}
else //if flight id not found, prompts user to try again
cout << "Flight ID not found; try again";
}
else if(command != "quit") //if the command does not match any of the options, ask to try again
cout << "Invalid command; try again"<< endl;
}while(command != "quit");
return 0;
}
| |