//        Assignment 4: SECJ 1013 Section 8
//------------------------------------------------------------
//   Group 5: - Teh Jing Ling (A20EC0228)
//		      - Wong Hui Shi (A20EC0169)
//            - Eunice Lim Xian Ni (A20EC0034)
//   Title: Calculation on Airplane Ticket (case study 3) 
//------------------------------------------------------------ 
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#define MAX 100
using namespace std;

//Function prototype
void dates(int, int, int);  
int Dest(string);
void price(int , int , int , int );
void data(string[],int &,int &,int &,string &,string [],string &,int);

ifstream infile("CS3.txt");        //Read information from file CS3.txt
ofstream outfile ("OutCS3.txt");   //Write on OutCS3.txt file

int main()  //Main function
{
	int year, month, date,i;
	int numDestination;
	string *user, user1, *day;
	string destination;
	 
	
	if (!infile)  //Check whether file exist           
    {
       cout << "ERROR: FILE IS INVALID" << endl;
       exit(1);
    }
	
	
	//Title display on first line of output.txt file
	outfile << left << setw(20) << "User" << setw(30) << "Destination" 
	     << setw(10) << "Day" << setw(20) << "Date" << "Ticket Price" << endl << endl;

   	user=new string[MAX];
   	day=new string[MAX];
   	
	for(i=0;i<10;i++)  //Read user's name and day
	{
	infile >> user[i] >> day[i];

    //Determine the day
    data(day,year,month,date,user1,user,destination,i);

    //Function calls
    dates(date,month,year);  // Dates
    numDestination = Dest(destination);	 // destination code
	price(date, month, year, numDestination); // price
	
    outfile << endl;  //Insert a new line for the next information
    };
    
   //Close the files
   infile.close();   
   outfile.close();
   
   delete [] user;
   delete [] day;
    
   return 0;
	
}


void dates(int DATE,int MONTH,int YEAR)   //Function dates
{
	
	if (DATE==1)   //Adding suffixes to the date
	outfile << DATE << "st ";
	else if (DATE==2)
	outfile << DATE << "nd ";
	else if (DATE==3)
	outfile << DATE << "rd ";
	else if((DATE>3) && (DATE<31) && (DATE!=29))
	outfile << DATE << "th ";
	else
	outfile << "INVALID";
	
	
	switch (MONTH)   //Determine the month
	{
	    case 1 : outfile << "January";
	         break;
	    case 2 : outfile << "February";
	         break;
	    case 3 : outfile << "March";
	         break;
	    case 4 : outfile << "April";
	         break;
	    case 5 : outfile << "May";
	         break;
	    case 6 : outfile << "June";
	         break;
	    case 7 : outfile << "July";
	         break;
	    case 8 : outfile << "August";
	         break;
	    case 9 : outfile << "September";
		     break;
	    case 10 : outfile << "October";
	          break;
	    case 11 : outfile << "November";
	          break;
	    case 12 : outfile << "December";
		      break;
        default : outfile << "INVALID";
              break;
    }
    
    //Determine the year so that it is between 2020 and 2022, inclusively
    if((YEAR>=2020)&&(YEAR<=2022))  
    outfile << YEAR;
    else
    outfile << "INVALID";
}


int Dest(string d)  //Function destination
{
	int num;
	if (d[1]=='B')
    num=1;
    else if (d[1] =='M')
    num=2;
    else if (d[1]=='P') 
	num=3;
	else if (d[1] =='S')
	num=4;
	else if (d[1]=='H')
	num=5;
	else
	num=0;
	
	return num;  //Return value of num to main function
}


void price(int dt, int mth, int yr, int numdest)  //Function price
{
	int p;  //Variable p represents the ticket price
	outfile << right << setw(5);
			
			//If the date is less than one week from 5th November 2020, then the price will be double.
			if ((dt<=11)&&(dt>=5)&&(yr==2020)&&(mth==11))
			{
			     switch (numdest)
		         {
		             case 1 : p=2*1499;
		                      outfile << "RM" << p;
		                      break;
		             case 2 : p=2*1899;
		                      outfile << "RM" << p;
		                      break;
		             case 3 : p=2*2099;
		                      outfile << "RM" << p;
		                      break;
		             case 4 : p=2*1909;
		                      outfile << "RM" << p;
		                      break;
		             case 5 : p=2*1129;
		                      outfile << "RM" << p;
		                      break;
		             default : outfile << "INVALID";
		                      break;
		         }

	     	}
		    else
		    {
		    	//If the date is NOT less than one week from 5th November 2020, then it will be normal price.
		    	switch (numdest)
		       {
		         case 1 : p=1499;
		                  outfile << "RM" << p;
		                  break;
		         case 2 : p=1899;
		                  outfile << "RM" << p;
		                  break;
		         case 3 : p=2099;
		                  outfile << "RM" << p;
		                  break;
		         case 4 : p=1909;
		                  outfile << "RM" << p;
		                  break;
		         case 5 : p=1129;
		                  outfile << "RM" << p;
		                  break;
		         default : outfile << "INVALID";
		                  break;
		         }
		    }
}

void data(string day[], int &year, int &month, int &date,string &user1,string user[],string &destination,int i)
{
	if((day[i]=="Sunday")||(day[i]=="Monday")||(day[i]=="Tuesday")||(day[i]=="Wednesday")||(day[i]=="Thursday")||(day[i]=="Friday")||(day[i]=="Saturday"))
	{
		//This statement will be selected if there are no whitespace between users' names
		infile >>  year >>  month >>  date ;   //Read year, month and date 
		getline(infile,destination);           //Read destination
		
    	outfile << left << setw(19) << user[i] << setw(31) << destination << setw(10) << day[i] ;  //Display user's name, destination and day
    	
    }
    else
    {
    	//This statement will be selected if in between the users' names contain whitespace
    	user1 = day[i];     //User's name for second word
    	infile >> day[i];   //Read day
       //Determine the day
    	if((day[i]=="Sunday")||(day[i]=="Monday")||(day[i]=="Tuesday")||(day[i]=="Wednesday")||(day[i]=="Thursday")||(day[i]=="Friday")||(day[i]=="Saturday"))
    	{
    		infile >>  year >>  month >>  date ;   //Read year, month and date 
		    getline(infile,destination);           //Read destination
		    string space = " ";	   	               //Add empty space between the user's first name and second name
		    
		    user[i] = user[i]+space+user1;  
		    outfile << left << setw(19) << user[i] << setw(31) << destination << setw(10) << day[i];  //Display user's name, destination and day	    
    	}
		else
		outfile << "Invalid";
    }
}

