CS201 Assignment No. 04 Solution

Assignment No. 04
Semester: Fall 2011
CS201: Introduction to Programming
Total Marks: 20
Due Date:16/01/2012

Instructions:

Please read the following instructions carefully before submitting assignment. It should be clear that your assignment will not get any credit if:
§         The assignment is submitted after due date.
§         The submitted assignment does not open or file is corrupt.
§         Assignment is copied(partial or full) from any source (websites, forums, students, etc)

Note: You have to upload only .cpp file. Assignment in any other format (extension) will not be accepted and will be awarded with zero marks. For example, if you submit code in .doc (Word document) or .txt files, no reward will be given in any case.


Objectives:

The objective of this assignment is to provide hands on experience of:

§         Classes
§         Member Functions
§         Dynamic Memory allocation in C/C++
§         String Manipulation Functions
§         File Handling


Guidelines:

§        Code should be properly indented and well commented.
§        Follow C/C++ rules while writing variable names, function names etc
§        Use only dev-C++ for this assignment.
§        Use appropriate C/C++ structure i.e. if-else; switch statement etc to get inputs from user where required (Marks will be deducted if inappropriate structure will be used).








Problem Statement:  Airline Schedule System

Write a program for Flight Schedule System which will check the flight schedule.  

Detailed Description:

In the program, you are required to make a class with name 'Schedule' and following are the data members of class schedule.

ñ     Trip
ñ     Date of departure
ñ     Date of return
ñ     Destination(Going to)
ñ     Origin (Leaving from)
ñ     Flight No
ñ     Departure time
ñ     Arrival time

Initialize all these data members with their default values by creating constructor for the class. Object creation of class should be done dynamically using dynamic memory allocation functions. Also class schedule should have a destructor.
Along with constructor and destructor of class, it is mandatory to used getter and setter functions for all above stated data members to properly assign their values.

Detail description of all data members are given below.

Class data members description:

Trip:
Class variables; trip should be off type Boolean; value of the variable should be taken from the user, if user sets it to true, then user should enter the required information for following data members and this trip will become a Round Trip:

  • Date of departure
  • Date of return
  • Destination
  • Origin

And if user set the variable values to false, prompt user to enter the required information for one-way trip:

  • Date of departure
  • Destination
  • Origin

Date of departure/Date of return:
Date of departure and date of return can be any date, entered by the user.


Flight No:
Flight numbers will be assigned randomly which can be any number from 0 to 200. 

Origins/Destinations:
The user can chose one of five destinations and origins:

§         Lahore
§         Karachi
§         Islamabad
§         Peshawar
§         Quetta

Departure Time and Arrival Time
Both these will be generated randomly and must have at least difference of two hours in-between departure time and arrival time.

When you are done with this, you are required to write a member function of class named WriteData().In the function WriteData(), you are required to create a new text file name “schedule.txt” in same folder/directory where you have saved your .cpp file. Open this file by using file handling functions and then write all Schedule information in that file in following format:

Flight No:
Origin:
Destination:
Date of Departure:
Date of Return:
Departure Time:
Arrival Time:


Points To Remember:

Following points should be kept in mind and handled accordingly, otherwise marks will be deducted. 

ñ     Reading and writing from text file, must be done with standard file handling functions provided in handouts. 
ñ     All data members must be declared and initialized with appropriate data type.
ñ     Exceptional cases must be kept in mind and handled accordingly while taking input from user.
ñ      User must be prompted if there is any error while:

       Creating a file.
       Opening a file for reading/ writing.










                              

SOLUTION



#include <iostream>
#include <fstream>
#include <string>
using namespace std;


class Schedule
{
public:
       enum Place
       {
            Lahore =0 ,
            Karachi,
            Islamabad,
            Peshawar,
            Quetta
        };
private:
    bool trip;
    string departureDate;   
    string returnDate;
    Place destination; //(Going to)
    Place origin;      //(Leaving from)
    int flightNo;
    int depatureTime;  //only hour (not full time like 12:56)
    int arrivalTime;


public:
       Schedule()   //default constructor
       {
           trip = false;
           departureDate = "";
           returnDate = "";
           destination = Lahore;
           origin = Lahore;
           flightNo = 0;
           depatureTime = 0;
           arrivalTime = 0;         
       };
       
       ~Schedule() //destructor
       {
           cout<<"Destructor ..."<<endl;       
       };
       
//get/set properties       
      bool getTrip() {return trip;};
      string  getDepartureDate(){return departureDate;};
      string  getReturnDate(){return returnDate;};   
      Place  getDestination(){return destination;};
      Place  getOrigin(){return origin;};
      int  getFlightNo(){return flightNo;};
      int  getDepatureTime(){return depatureTime;};
      int  getArrivalTime(){return arrivalTime;};
      
      void  setTrip(bool val) {trip = val;};
      void  setDepartureDate(string date){departureDate = date;};
      void  setReturnDate(string date){returnDate = date;};   
      void  setDestination(Place val){destination = val;};
      void  setOrigin(Place val){origin = val;};
      void  setFlightNo(int val){flightNo = val;};
      void  setDepatureTime(int aTime){depatureTime = aTime;};
      void  setArrivalTime(int aTime){arrivalTime = aTime;};


// method for get input 
      void getInputData()
      {
           int i;
    
           cout<<"\n==========================="<<endl;
           cout<<"Enter flight information :"<<endl;
           cout<<"Trip type (0: Oneway Trip | 1: Round Trip): ";
           cin>>trip;
         
           cout<<"Date of departure: ";
           cin>>departureDate;          


           
           returnDate = ""; 
           if(trip)//round trip
           {       
               cout<<"Date of return: ";
               cin>>returnDate;                         
           }
           
           do
           {           
               cout<<"Destination (0: Lahore, 1: Karachi, 2: Islamabad, 3: Peshawar, 4: Quetta): ";
               cin>>i;          
           }
           while (i<0 || i >4);
           destination = Place(i);
           do
           {           
               cout<<"Origin (0: Lahore, 1: Karachi, 2: Islamabad, 3: Peshawar, 4: Quetta): ";
               cin>>i;          
           }
           while (i<0 || i >4);
           origin = Place(i);
           
           /* initialize random seed: */
           srand ( time(NULL) );
           flightNo = rand() % 201;
           
           depatureTime = rand() % 24;
           arrivalTime = rand() % 24;
           if(abs(arrivalTime-depatureTime)<2) arrivalTime = (arrivalTime + 2)%24;           
       };    
       
// method for write to disk
       void writeData()
       {
          static char *places[] = {
                 "Lahore", "Karachi", "Islamabad", "Peshawar", "Quetta"
          };
          
          ofstream myfile ("schedule.txt");
          if (myfile.is_open())
          {
            myfile << "Flight No: "<<flightNo<<endl;
            myfile << "Origin: "<<places[origin]<<endl;
            myfile << "Destination: "<<places[destination]<<endl;
            myfile << "Date of Departure: "<<departureDate<<endl;
            if(trip)
            {
                myfile << "Date of Return: "<<returnDate<<endl;    
            }
            else
            {
                myfile << "Date of Return: N/A"<<endl;  
            }           
            myfile << "Departure Time: "<<depatureTime<<":00"<<endl;
            myfile << "Arrival Time: "<<arrivalTime<<":00"<<endl;
            myfile.close();
          }
          else cout << "Unable to open file";
       };   
};


int main()
{
    Schedule *mysdl = new Schedule();
    mysdl->getInputData();
    mysdl->writeData();
    
    system("pause");
    return 0;
}

         

No comments:

Post a Comment