Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef FUNC_H
- #define FUNC_H
- #include <iostream>
- #include <iomanip>
- //libraries for importing text files
- #include <fstream> //allow read from text file
- #include <sstream> //for string stream class
- #include <string> //for npos, find_first_not_of, find_last_not_of, substr
- #include "ReservationClass.h" //inherit Reservation class
- //Array Size/number of reservations allowed
- #define MAX 5
- Reservation records[MAX];
- using namespace std;
- //GLOBAL VARIABLES
- int foundTextFile = 0; //check whether there is any imported text files
- int foundRecord = 0;
- string fileDirectory; //the .txt file directory
- class Functions{
- public:
- //DISPLAY TABLE COLUMN NAMES
- void displayColumnNames(){
- cout<<setfill('_')<<setw(87)<<"_"<<endl
- <<setfill(' ')<<setw(5)<<"| No. "
- <<left<<setw(30)<<"Name"
- <<setw(5)<<"Phone No."
- <<right<<setw(16)<<"Pax No."
- <<setw(10)<<"Date"
- <<setw(16)<<"Time |"<<endl
- <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
- }
- //SET DEFAULT RECORDS
- void defaultRecords(Reservation records[]){
- for(int i = 0; i<MAX; i++){
- records[i].setBookingNo(i+1);
- }
- records[0].setName("Tan Phit Huan");
- records[1].setName("Khong Ziv Hale");
- records[2].setName("Rex Luah");
- records[3].setName("Lee Yeu Gor");
- records[4].setName("Tiffany Pragasam");
- for(int i = 0; i<MAX; i++){
- records[i].setPhoneNo(123456777+i);
- }
- records[0].setPaxNo(2);
- records[1].setPaxNo(5);
- records[2].setPaxNo(7);
- records[3].setPaxNo(4);
- records[4].setPaxNo(10);
- records[0].setDate(20191212);
- records[1].setDate(20191111);
- records[2].setDate(20191111);
- records[3].setDate(20191212);
- records[4].setDate(20191211);
- for(int i = 0; i<MAX; i++){
- records[i].setTime(1200+(i*100));
- }
- }
- //TRIM FUNCTION
- string trim(const string& str){
- size_t first = str.find_first_not_of(' ');
- if (string::npos == first)
- {
- return str;
- }
- size_t last = str.find_last_not_of(' ');
- return str.substr(first, (last - first + 1));
- }
- //READ TEXT FILE FUNCTION
- void readTextFile(string fileDirectory, Reservation records[]){
- ifstream myFile(fileDirectory);
- if(!myFile.is_open()){
- cout<<endl<<"ERROR! File failed to open."<<endl;
- return;
- }
- foundTextFile = 1;
- try{
- int i=0; //index number
- int checkLinesNo = 0; //check number of lines in the text file
- string tempString,line;
- //Reservation Information
- int bkNo, phoneNo, paxNo, day, month, year, date, time;
- string name;
- while(getline(myFile, line)){ //reads a line
- checkLinesNo++;
- if(checkLinesNo > MAX+2){ //if text file exceeds 7 lines (5 lines of records + 1 column names and 1 divide line)
- cout<<endl<<"Maximum of "<<MAX<<" reservations can be stored."<<endl
- <<"Some data will be lost."<<endl;
- break;
- }
- stringstream ss(line); //using stringstream to manipulate the line
- getline(ss, tempString, ' '); //store the string in
- if(tempString != "|=====================================================================================|"){
- getline(ss, tempString, ' ');
- if(tempString != "No."){
- bkNo=stoi(tempString); //convert tempString to integer
- getline(ss, tempString, '|');
- //gets substring from character in position 2 onwards, 30 characters(where name column occupies)
- //Then trim to get rid of empty spaces
- name = (trim(tempString.substr(2,30)));
- phoneNo = stoi(tempString.substr(32,10));
- //need to trim because there can be 1 or 2 digits
- paxNo = stoi(trim(tempString.substr(52,2)));
- day = stoi(tempString.substr(60,2));
- month = stoi(tempString.substr(63,2));
- year = stoi(tempString.substr(66,4));
- time = stoi(tempString.substr(77,4));
- date = year*10000 + month*100 + day;
- //Overriding the default/previous records with the new records
- records[i].setBookingNo(bkNo);
- records[i].setName(name);
- records[i].setPhoneNo(phoneNo);
- records[i].setPaxNo(paxNo);
- records[i].setDate(date);
- records[i].setTime(time);
- i++;
- }
- }
- }
- }catch(...){
- cout<<endl<<"ERROR! Import process cancelled."<<endl
- <<"Please check whether it is the correct text file or directory."<<endl;
- foundTextFile = 0;
- }
- if(foundTextFile == 1){
- cout<<endl<<"Import Successful."<<endl;
- }
- myFile.close();
- }
- //BUBBLE SORT FUNCTIONS
- //Sort by Name
- void bubbleSortName(Reservation records[]){
- int i,j;
- Reservation current;
- for(i=0; i<MAX;i++){
- for (j=0; j<MAX-1; j++){
- if(records[j].getName()>records[j+1].getName()){
- current = records[j];
- records[j] = records[j+1];
- records[j+1] = current;
- }
- }
- }
- }
- //Sort by Date
- void bubbleSortDate(Reservation records[]){
- int i,j;
- Reservation current;
- for(i=0; i<MAX;i++){
- for (j=0; j<MAX-1; j++){
- if(records[j].getDate()>records[j+1].getDate()){
- current = records[j];
- records[j] = records[j+1];
- records[j+1] = current;
- }
- }
- }
- }
- //Sort by Phone Number
- void bubbleSortPhoneNo(Reservation records[]){
- int i,j;
- Reservation current;
- for(i=0; i<MAX;i++){
- for (j=0; j<MAX-1; j++){
- if(records[j].getPhoneNo()>records[j+1].getPhoneNo()){
- current = records[j];
- records[j] = records[j+1];
- records[j+1] = current;
- }
- }
- }
- }
- //Sort by number of Pax
- void bubbleSortPaxNo(Reservation records[]){
- int i,j;
- Reservation current;
- for(i=0; i<MAX;i++){
- for (j=0; j<MAX-1; j++){
- if(records[j].getPaxNo()>records[j+1].getPaxNo()){
- current = records[j];
- records[j] = records[j+1];
- records[j+1] = current;
- }
- }
- }
- }
- //BINARY SEARCH FUNCTIONS
- //Search Phone No Record
- int binarySearchPhoneNo(Reservation records[], int key, int low, int high){
- int middle; //the middle index number of the array
- bubbleSortPhoneNo(records);
- while (low <= high ){
- middle = (low+high)/2;
- if(key==records[middle].getPhoneNo()){
- return middle;
- }else if(key<records[middle].getPhoneNo()){
- high = middle-1; //reset high index to left side
- }else{
- low = middle+1; //reset low index to right side
- }
- }
- return -1; //key not found
- }
- //Search Name Record
- int binarySearchName(Reservation records[], string key, int low, int high){
- int middle; //the middle index number of the array
- bubbleSortName(records);
- while (low <= high ){
- middle = (low+high)/2;
- if(key==records[middle].getName()){
- return middle;
- }else if(key<records[middle].getName()){
- high = middle-1; //reset high index to left side
- }else{
- low = middle+1; //reset low index to right side
- }
- }
- return -1; //key not found
- }
- //Search Same Date Record
- int binarySearchDate(Reservation records[], int key, int low, int high){
- int middle; //the middle index number of the array
- bubbleSortDate(records);
- while (low <= high ){
- middle = (low+high)/2;
- if(key==records[middle].getDate()){
- return middle;
- }else if(key<records[middle].getDate()){
- high = middle-1; //reset high index to left side
- }else{
- low = middle+1; //reset low index to right side
- }
- }
- return -1; //key not found
- }
- //Search Phone No Record
- int binarySearchPaxNo(Reservation records[], int key, int low, int high){
- int middle; //the middle index number of the array
- bubbleSortPaxNo(records);
- while (low <= high ){
- middle = (low+high)/2;
- if(key<=records[middle].getPaxNo()){
- return 1;
- }else{
- low = middle+1; //reset low index to right side
- }
- }
- return -1; //key not found
- }
- //RECURSION FUNCTIONS
- //Given date, Display all with same date
- void givenDate (Reservation records[], int date, int high){
- if (high < 0){
- return;
- }
- else if (date == records[high].getDate() ){
- records[high].display();
- return givenDate(records, date, high-1);
- }
- else {
- return givenDate(records, date, high-1);
- }
- }
- //PART 6 FUNCTION
- //Give number of pax, Display all with at least given number of pax
- void givenPaxNo (Reservation records[], int pax, int high){
- if(high < 0){
- return;
- }
- else if (pax <= records[high].getPaxNo() ){
- records[high].display();
- return givenPaxNo(records, pax, high-1);
- }
- if(pax>records[high].getPaxNo()){
- return;
- }
- else {
- return givenPaxNo(records, pax, high-1);
- }
- }
- //ERROR CHECKING FUNCTIONS
- //for checking whether the name is empty
- bool checkName(string name){
- int counter = 0;
- bool foundTextFile = true;
- //if found any characters other than space
- if(name.find_first_not_of(" ") != string::npos){
- counter++;
- }
- //if found any characters other than the alphabets and a space
- if(name.find_first_not_of(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") != string::npos){
- foundTextFile = true;
- }else{
- foundTextFile = false;
- }
- //if no input or if got any non alphabetic character
- if(counter == 0 || foundTextFile){
- cout<<"Invalid Input! Need a name!"<<endl;
- return false;
- }else{
- return true;
- }
- }
- //Check whether that day exists
- bool checkDay(int day, int month, int year){
- //even months
- if(day <= 0){
- cout<<"Invalid Input! Enter positive numbers!"<<endl;
- }else if(month%2 == 0){
- // if the month is february
- if(month == 2){
- //february in common year that contain 28 days
- if(year%4 != 0 && day>28){
- cout << "Please key in between the day from 1 to 28"<< endl;
- }
- // february in leap year that contain 29 days
- else if(year%4 == 0 && day>29){
- cout << "Please key in between the day from 1 to 29"<< endl;
- }
- else{
- return true;
- }
- }
- // month that are even with 31 days
- else if(month > 7 && day > 31){
- cout << "Please key in between the day from 1 to 31"<< endl;
- }
- // month that are even with 30 days
- else if(month < 7 && day > 30){
- cout << "Please key in between the day from 1 to 30"<< endl;
- }
- else{
- return true;
- }
- // month is odd
- }else if(month%2 != 0){
- // month that are odd with 30 days
- if(month>8 && day > 30){
- cout << "Please key in between the day from 1 to 30"<< endl;
- // month that are odd with 31 days
- }else if(month<8 && day > 31){
- cout << "Please key in between the day from 1 to 31"<< endl;
- }else{
- return true;
- }
- }
- else{
- return true;
- }
- return false;
- }
- ///////////////////////////////////////////////////////////////////////////////////
- //SELECTION FUNCTIONS//
- //Option 1 : Display All sorted by...
- void displayAll(){
- char choice;
- cout << "-----------------=DISPLAY ALL=------------------" << endl;
- cout << "Display all sorted by..." << endl <<
- "1. Name" << endl <<
- "2. Date" << endl<<
- "3. Back to Main Menu"<< endl;
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- system("CLS");
- switch (choice) {
- //Display all sorted by name (PART 1)
- case '1':
- bubbleSortName(records);
- displayColumnNames();
- for(int i = 0; i<MAX; i++){
- records[i].display();
- }
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- break;
- //Display all sorted by date (PART 2)
- case '2':
- bubbleSortDate(records);
- displayColumnNames();
- for(int i = 0; i<MAX; i++){
- records[i].display();
- }
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- break;
- case '3':
- return;
- default:
- cout << choice << " is not valid choice" << endl<<endl;
- displayAll();
- }
- cout <<endl<< "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- return;
- }
- //Option 2 : Search...
- void searchNDisplay(){
- char choice;
- int foundIndex, phoneNo, correct;
- string name;
- cout << "-----------------=SEARCH & DISPLAY=------------------" << endl;
- cout << "Search..." << endl <<
- "1. Phone Number" << endl <<
- "2. Name" << endl<<
- "3. Back to Main Menu"<< endl;
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- system("CLS");
- switch (choice) {
- //Search by Phone Number (PART 3)
- case '1':
- cout<<endl<<"Enter phone number (e.g : 0123456789) : ";
- cin>>phoneNo;
- correct = 1;
- while (correct == 1){
- if (cin.fail() || phoneNo<0){
- cin.clear();
- cin.ignore();
- cout << "Invalid input! Enter positive numbers!" << endl<<endl;
- cout<< "Enter phone number (e.g : 0123456789) : ";
- cin >> phoneNo;
- }else{
- correct = 0;
- }
- }
- foundIndex = binarySearchPhoneNo(records, phoneNo, 0, MAX-1);
- //If not found
- if(foundIndex==-1){
- cout<<"No such record with that phone number."<<endl;
- }else{
- //If found
- displayColumnNames();
- records[foundIndex].display();
- }
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- getchar();
- cout <<endl<< "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- return;
- //Search by Name (PART 4)
- case '2':
- do{
- cout<<"Enter name : ";
- getline(cin, name);
- }while(!checkName(name));
- foundIndex = binarySearchName(records, name, 0, MAX-1);
- //If not found
- if(foundIndex==-1){
- cout<<"No such record with that name."<<endl;
- }else{
- //If found
- displayColumnNames();
- records[foundIndex].display();
- }
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- cout <<endl<< "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- return;
- case '3':
- return;
- default:
- cout << choice << " is not valid choice" << endl<<endl;
- searchNDisplay();
- }
- }
- //Option 3 : Given...display all
- void givenDisplayAll(){
- char choice;
- int year, month, day, date, paxNo, correct;
- cout << "-----------------=GIVEN DISPLAY ALL=------------------" << endl;
- cout << "Given...display all records with..." << endl <<
- "1. Date, the same date" << endl <<
- "2. Number of Pax, at least the given number of pax" << endl<<
- "3. Back to Main Menu"<< endl;
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- system("CLS");
- switch (choice) {
- //Display by Date (PART 5)
- case '1':
- correct = 1;
- cout<<endl<<"Enter year (e.g : 2019): ";
- cin>>year;
- while (correct == 1){
- if (cin.fail() || year<0){
- cin.clear();
- cin.ignore();
- cout << "Invalid input! Enter positive numbers!" << endl<<endl;
- cout<< "Enter year (e.g : 2019) : ";
- cin >> year;
- }else{
- correct = 0;
- }
- }
- correct = 1;
- cout<<endl<<"Enter month (e.g : January = 1) : ";
- cin>>month;
- while (correct == 1){
- if (cin.fail() || month<1 || month>12){
- cin.clear();
- cin.ignore();
- cout << "Invalid input! Only numbers from 1 to 12!" << endl<<endl;
- cout<< "Enter month (e.g : January = 1) : ";
- cin >> month;
- }else{
- correct = 0;
- }
- }
- do{
- correct = 1;
- cout<<endl<<"Enter day : ";
- cin>>day;
- while (correct == 1){
- if (cin.fail()){
- cin.clear();
- cin.ignore();
- cout << "Invalid input! Enter numbers!" << endl<<endl;
- cout<< "Enter day : ";
- cin >> day;
- }else{
- correct = 0;
- }
- }
- }while(!checkDay(day, month, year));
- date = year*10000 + month*100 + day;
- if(binarySearchDate(records, date, 0, MAX-1) != -1){
- displayColumnNames();
- //Call recursion function here
- givenDate(records, date, MAX-1);
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- }else{
- cout<<endl<<"No such record with that date."<<endl;
- }
- break;
- //Display by Pax Number (PART 6)
- case '2':
- correct = 1;
- cout<<endl<<"Enter number of pax : ";
- cin>>paxNo;
- while (correct == 1){
- if (cin.fail() || paxNo<=0){
- cin.clear();
- cin.ignore();
- cout << "Invalid input! Enter positive numbers (mininimum : 1)!" << endl<<endl;
- cout<< "Enter number of pax : ";
- cin >> paxNo;
- }else{
- correct = 0;
- }
- }
- if(binarySearchPaxNo(records, paxNo, 0, MAX-1) != -1){
- displayColumnNames();
- //Call recursion function here
- givenPaxNo(records, paxNo, MAX-1);
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- }else{
- cout<<endl<<"No such record with the same number of pax or more."<<endl;
- }
- break;
- case '3':
- return;
- default:
- cout << choice << " is not valid choice" << endl;
- displayAll();
- }
- getchar();
- cout <<endl<< "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- return;
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment