Question

In: Computer Science

Write a simple airline ticket reservation program. The program should display a menu with the following...

Write a simple airline ticket reservation program. The program should display a menu with the following operations: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list of flights with each node including a pointer to a linked list of passengers.

In C++ language.

Solutions

Expert Solution

#include <iostream>
using namespace std;

class Passenger;
class Flight;
int PrintPassenger(const Passenger* pas);
int PrintFlightAndPass(const Flight * list);


template <typename Type>
class LinkedList
{
public:
LinkedList()
{
  this->head=NULL;
}
~LinkedList()
{
  while(this->head !=NULL)
  {
   SNode<Type> *node=this->head->next;
   delete this->head;
   this->head =node;
  }
}

public:
template <typename Type>
inline bool Add(Type *pdata)
{
  if(this->FindNode<Type>(pdata->name)!=NULL)
   return false;
  SNode<Type> *node=new SNode<Type>(pdata);
  if(head==NULL)
  {
   head=node;
   return true;
  }
  SNode<Type> *tnode=this->head;

  if(*(node->pdata) < *(tnode->pdata))
  {
   node->next=tnode;
   head=node;
   return true;
  }
  while(tnode->next!=NULL &&
   *(node->pdata) > *(tnode->next->pdata))
  {
   tnode=tnode->next;
  }
  if(tnode->next==NULL)
  {
   tnode->next=node;
  }
  else
  {
   node->next=tnode->next;
   tnode->next=node;
  }
  return true;
}
public:
template<typename Type>
inline Type* FindNode(const char *name)
{
  SNode<Type> *tnode=this->head;
  while(tnode!=NULL && !tnode->pdata->HasName(name))
  {
   tnode=tnode->next;
  }
  if(tnode!=NULL)
  {
   return tnode->pdata;
  }
  return NULL;
}
template <typename Type>
inline bool Delete(const char * name)
{
  if(this->head==NULL) return false;

  SNode<Type> *tnode=this->head;
  SNode<Type> *delnode=NULL;

  if(tnode->pdata->HasName(name))
  {
   delnode=tnode;
   head=tnode->next;
   delete delnode;
   return true;
  }
  while(tnode->next!=NULL &&
   !tnode->next->pdata->HasName(name))
  {
   tnode=tnode->next;
  }
  if(tnode->next!=NULL)
  {
   delnode=tnode->next;
   tnode->next=delnode->next;
   delete delnode;
   return true;
  }
  return false;
}
inline void PrintAllNodes()
{
  SNode<Type> *tnode=this->head;
  while(tnode != NULL)
  {
   cout<<tnode->pdata->name<<"\t";
   tnode=tnode->next;
  }
  cout<<endl;
}
template <typename Type>
inline void ForEach(int (*operate)(const Type *args)) const
{
  SNode<Type> *tnode=this->head;
  while(tnode != NULL)
  {
   (*operate)(tnode->pdata);
   tnode=tnode->next;

  }
}

private:
template <typename Type>
class SNode
{
public:
  Type *pdata;
  SNode *next;
  SNode()
  {
   pdata=NULL;
   next=NULL;
  }
  SNode(Type * pdata)
  {
   this->pdata=pdata;
   this->next=NULL;
  }
  ~SNode()
  {
   if(this->pdata!=NULL)
   {
    delete pdata;
    pdata=NULL;
   }
  }
};
SNode<Type> *head;
//int  count;


};
class Passenger
{
public:
char  name[40];
Passenger(){this->name[0]=0;}
Passenger(char * name)
{
  if(strlen(name)<40)
   strcpy(this->name,name);
}
inline bool operator==(const Passenger& operhand) const
{
  if(!strcmp(this->name,operhand.name))
   return true;
  return false;
}
inline bool operator<(const Passenger& operhand)const
{
  if(strcmp(this->name,operhand.name )<0)
   return true;
  return false;
}
inline bool operator>(const Passenger& operhand)const
{
  return !((*this)<operhand);
}
inline bool HasName(const char* name)
{
  if(!strcmp(name,this->name))
   return true;
  return false;
}
};
class Flight
{
private:

LinkedList<Passenger> *plist;

public:
char     name[40];
int      reversed;
int      capacity;
Flight()
{
  this->name[0]=0;
  capacity=10;
  reversed=0;
  plist=new LinkedList<Passenger>();
}
Flight(char *name)
{
  strcpy_s(this->name,40,name);
  capacity=10;
  reversed=0;
  plist=new LinkedList<Passenger>();
}
Flight(char *name,int capacity)
{
  strcpy_s(this->name,40,name);
  this->capacity=capacity;
  reversed=0;
  plist=new LinkedList<Passenger>();
}
~Flight()
{
  if(plist!=NULL)
  {
   delete plist;
   plist=NULL;
  }
}
inline bool operator==(const Flight &operhand)const
{
  if(this->name==operhand.name &&
   this->capacity==operhand.capacity)
   return true;
  return false;
}
inline bool operator<(const Flight& operhand)const
{
  if(strcmp(this->name,operhand.name )<0)
   return true;
  return false;
}
inline bool operator>(const Flight& operhand)const
{
  return !((*this)<operhand);
}
inline bool HasName(const char* name) const
{
  if(!strcmp(name,this->name))
   return true;
  return false;
}
inline bool AddPassenger(Passenger* ppass)
{
  if(reversed<capacity)
   if(plist->Add<Passenger>(ppass))
   {
    reversed++;
    return true;
   }
   return false;
}
inline bool DelPassengerByName(const char * name)
{
  if(plist->Delete<Passenger>(name))
  {
   reversed--;
   return true;
  }
  return false;
}
inline int GetFreeTicketCount() const
{
  return capacity-reversed;
}
inline void PrintAllPassengers() const
{
  plist->ForEach<Passenger>(PrintPassenger);
  cout<<endl;
}

};

int PrintPassenger(const Passenger* pas)
{
cout<<pas->name<<"\t";
return 0;
}

int PrintFlightAndPass(const Flight * list)
{
const Flight * t=list;

cout<<t->name<<" ("<<t->reversed<<" Reversed "<<
  t->GetFreeTicketCount()<<" Free)"<<endl;
cout<<"\t";
t->PrintAllPassengers ();
return 0;
}
int main(int argc,char **argv)
{
LinkedList<Flight> flist;
flist.Add(new Flight("CA1033"));
flist.Add(new Flight("CU3254"));
flist.Add(new Flight("AA3333",3));

flist.FindNode<Flight>("CA1033")->AddPassenger(new Passenger("Zhao"));
flist.FindNode<Flight>("CA1033")->AddPassenger(new Passenger("Qian"));
flist.FindNode<Flight>("CA1033")->AddPassenger(new Passenger("Sun"));
flist.FindNode<Flight>("CA1033")->AddPassenger(new Passenger("Li"));

flist.FindNode<Flight>("AA3333")->AddPassenger(new Passenger("Zhou"));
flist.FindNode<Flight>("AA3333")->AddPassenger(new Passenger("Wu"));
flist.FindNode<Flight>("AA3333")->AddPassenger(new Passenger("Zheng"));
flist.FindNode<Flight>("AA3333")->AddPassenger(new Passenger("Wang"));

flist.FindNode<Flight>("CU3254")->AddPassenger(new Passenger("Li"));
cout<<"--START--"<<endl;
flist.ForEach<Flight> (PrintFlightAndPass);
cout<<"--END--"<<endl;
flist.FindNode<Flight>("CA1033")->DelPassengerByName("Li");
flist.Delete<Flight>("AA3333");
cout<<"--START--"<<endl;
flist.ForEach<Flight> (PrintFlightAndPass);
cout<<"--START--"<<endl;
return 0;
}


Related Solutions

Write a simple airline ticket reservation program. The program should display a menu with the following...
Write a simple airline ticket reservation program. The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list...
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
Write a C++ program to run a menu driven program with the following choices: 1) Display...
Write a C++ program to run a menu driven program with the following choices: 1) Display the grades 2) Adjust grade 3) Display Average for each student 4) Display number of student with at least a B 5) Quit requirements: 1. Write a function called getValidGrade that allows a user to enter in an integer and loops until a valid number that is >= 0 and <= 100 is entered. It returns the valid value. 2. Write a function called...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
Write a python program to display a menu with the following options: (1) add, (2) subtract,...
Write a python program to display a menu with the following options: (1) add, (2) subtract, (3) multiply, (4) divide (5) mod, and (6) do nothing. I f the user enters something else (except 1-6), the program should display an error message. Otherwise, it should ask the user for two numbers, perform the calculation, and display the result.
Write a menu program to have the above options for the polynomials. Your menu program should...
Write a menu program to have the above options for the polynomials. Your menu program should not use global data; data should be allowed to be read in and stored dynamically. Test your output with the data below. Poly #1: {{2, 1/1}, {1, 3/4}, {0, 5/12}} Poly #2: {{4, 1/1}, {2, -3/7}, {1, 4/9}, {0, 2/11}} provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT