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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <limits>
#include <algorithm>
// ********************* Person Class *************************
class Person
{
public:
Person( std::string name = "", int age = -1 , char sex = 'u')
: m_name{name},m_age{age},m_sex{sex}
{}
void name( const std::string & n ) { m_name = n; }
std::string name() const { return m_name; }
std::string & name() { return m_name; }
void age( int a ) { m_age = a; }
int age() const { return m_age; }
int & age() { return m_age; }
void sex( char s ) { m_sex = s; }
char sex() const { return m_sex; }
char & sex() { return m_sex; }
void setData( std::istream &, std::ostream &);
private:
std::string m_name;
int m_age;
char m_sex;
};
void Person::setData( std::istream & is = std::cin, std::ostream & os = std::cout )
{
os << "Enter name: ";
std::getline( is, m_name );
os << "Enter age: ";
is >> m_age;
os << "Enter sex (m/w/u): ";
is >> m_sex;
}
std::ostream & operator<<( std::ostream & os, const Person & p)
{
os <<"Name: "<<p.name() << ", Age: "<<p.age()<<", Sex: ";
if( p.sex() == 'm' ) os << "male";
else if( p.sex() == 'w') os << "female";
else os << "unspecified";
return os;
}
std::istream & operator>>( std::istream & is, Person & p)
{
std::string tmp_name;
std::getline( is, tmp_name );
p.name( tmp_name );
int tmp_age;
is >> tmp_age;
p.age( tmp_age );
char tmp_sex;
is >> tmp_sex;
p.sex( tmp_sex );
return is;
}
// ********************* Seat Class *************************
class Seat
{
public:
Seat(const std::string & number = "A1")
: m_number{number}, m_person{Person()}
{}
bool isReserved() const { return m_person.name().size(); }
Person person() const { return m_person; }
Person & person() { return m_person; }
void person( const Person & p ) { m_person = p; }
std::string number() const { return m_number; }
std::string & number() { return m_number; }
void number( const std::string & n ) { m_number = n; }
private:
std::string m_number;
Person m_person;
};
std::ostream & operator<<( std::ostream & os, const Seat & seat)
{
os << "number: "<<seat.number()<<"\n";
if( seat.person().name().size() )
os << seat.person()<<"\n";
else
os << "free\n";
return os;
}
// ********************* Plane Class *************************
class Plane
{
public:
Plane( int noOfSeats );
Seat seat( const std::string & number );
void availableSeats() const;
bool isSeatAvailable( const std::string & seatNum) const;
int freeSeats() const;
bool bookSeat(const Person &, const std::string & seatNum);
bool cancelSeat(const Person &);
Seat findSeat( const std::string & name );
private:
friend std::ostream & operator<<( std::ostream &, const Plane &);
std::vector<Seat> m_seats;
int m_freeSeats;
};
Plane::Plane( int noOfSeats )
: m_freeSeats{noOfSeats}
{
for( int i = 0; i < noOfSeats; ++i )
{
char row = i / 4 + 'A';
m_seats.push_back( Seat(
std::string("") + row
+ std::to_string( i % 4 + 1)
) );
}
}
Seat Plane::seat( const std::string & num )
{
for( auto & seat : m_seats )
{
if( seat.number() == num )
return seat;
}
throw std::runtime_error("Wrong seat number!");
}
void Plane::availableSeats() const
{
if( !m_freeSeats ) std::cout << "No free seats.";
for( const auto & seat : m_seats )
{
if( !seat.person().name().size() )
std::cout << seat.number() << ' ';
else
std::cout << "* ";
}
}
bool Plane::isSeatAvailable( const std::string & number) const
{
for( const auto & seat : m_seats )
if( seat.number() == number && seat.person().name() == "" )
return true;
return false;
}
int Plane::freeSeats() const { return m_freeSeats; }
bool Plane::bookSeat( const Person & p, const std::string & seatNum)
{
auto itr = std::find_if( m_seats.begin(), m_seats.end(),
[&] (const Seat & seat) { return seat.number() == seatNum; }
);
if( itr == m_seats.end() ) return false;
itr->person( p );
-- m_freeSeats;
return true;
}
bool Plane::cancelSeat( const Person & person )
{
for( int i = 0; i < m_seats.size(); ++i )
{
if( m_seats[i].person().name() == person.name() )
{
m_seats[i].person().name( "" );
++ m_freeSeats;
return true;
}
}
return false;
}
Seat Plane::findSeat( const std::string & name )
{
for( auto & seat : m_seats )
{
if( seat.person().name() == name )
return seat;
}
throw std::runtime_error( "Could not find seat!" );
}
std::ostream & operator<< ( std::ostream & os, const Plane & plane )
{
for( const auto & seat : plane.m_seats )
{
os << seat.number() << ": ";
if( seat.person().name().size() )
os << seat.person();
else
os << "free";
os << "\n";
}
return os;
}
// **************** freestanding functions ******************
void book( Plane & plane )
{
using namespace std;
cout << "Enter seat: ";
string seatNum;
cin >> seatNum;
cin.ignore(999, '\n' );
if( !plane.isSeatAvailable(seatNum) )
{
cout << "Seat " << seatNum
<< " is not available.\n";
return;
}
// enter passenger details
Person passenger;
passenger.setData();
cout << '\n';
if( !plane.bookSeat(passenger, seatNum) )
{
cout << "Booking for " << passenger.name() << " failed!\n";
} else {
cout << "Booked seat:\n"
<< plane.findSeat(passenger.name()) << "\n";
}
}
// ********************* Main ********************************
int main()
{
using namespace std;
Plane plane( 100 );
cout << "Welcome to Airline Booking\n\n";
cout << "Please select from the following choices: \n\n";
int choice;
do
{
cout << "\n\n";
cout << "1) View available seats: \n";
cout << "2) Make a booking: \n";
cout << "3) exit: \n\n";
cin >> choice;
cin.ignore(999, '\n' );
switch (choice)
{
case 1:
{
cout << "Available seats: ";
plane.availableSeats();
break;
}
case 2:
{
// booking a passenger
book( plane );
break;
}
case 3:
{
cout << "exit.\n\n";
break;
}
default:
{
cout << "Sorry try again. \n\n ";
break;
}
}
} while (choice != 3);
return 0;
}
|