Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip> //Reservation Class
- //Functions Class
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <stdlib.h> //for exit() function
- #define MAX 5 //Array Size/number of reservations allowed
- using namespace std;
- int found = 0; //check whether there is any imported text files
- string fileDirectory; //the .txt file directory
- class Reservation{
- private:
- int bookingNo;
- string name;
- int phoneNo;
- int paxNo;
- int time;
- int date;
- public:
- Reservation(){
- bookingNo = 1;
- name = "Tan Phit Huan";
- phoneNo = 0123456777;
- paxNo = 1;
- time = 1500;
- date = 20190101;
- }
- Reservation(int bNo, string na, int phNo, int pxNo, int t, int da){
- bookingNo = bNo;
- name = na;
- phoneNo = phNo;
- paxNo = pxNo;
- time = t;
- date = da;
- }
- void setBookingNo(int bNo){
- bookingNo = bNo;
- }
- int getBookingNo(){
- return bookingNo;
- }
- void setName(string na){
- name = na;
- }
- string getName(){
- return name;
- }
- void setPhoneNo(int phNo){
- phoneNo = phNo;
- }
- int getPhoneNo(){
- return phoneNo;
- }
- void setPaxNo(int pxNo){
- paxNo = pxNo;
- }
- int getPaxNo(){
- return paxNo;
- }
- void setTime(int t){
- time = t;
- }
- int getTime(){
- return time;
- }
- void setDate(long da){
- date = da;
- }
- int getDate(){
- return date;
- }
- void display(){
- int monthNYear = date/100;
- int year = date/10000;
- int month = monthNYear-(year*100);
- int day = date - (monthNYear*100);
- cout<<"| "<<setfill(' ')<<bookingNo<<" "
- <<left<<setw(30)<<name
- <<right<<setw(10)<<setfill('0')<<phoneNo
- <<setw(12)<<setfill(' ')<<paxNo<<setw(6)<<setfill(' ')<<" "
- <<setw(2)<<setfill('0')<< day <<"/"<<setw(2)<<setfill('0')<< month <<"/"<<setw(4)<<setfill('0')<< year<<setw(7)<<setfill(' ')<<" "
- <<setw(4)<<setfill('0')<< time <<" |"<<endl;
- }
- };
- 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;
- }
- //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;
- }
- found = 1;
- try{
- int i=0; //index number
- string tempString,line;
- //Reservation Information
- int bkNo, phoneNo, paxNo, day, month, year, date, time;
- string name;
- while(getline(myFile, line)){ //reads a line
- // cout<<i<<endl;
- // if(i >= MAX){
- // 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;
- found = 0;
- }
- 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;
- }
- }
- }
- }
- //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
- bubbleSortPhoneNo(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
- }
- //RECURSION FUNCTIONS
- //Given date, Display all with same date
- void givenDate (Reservation A[], int date, int high){
- if (date == A[high].getDate() ){
- A[high].display();
- return givenDate(A, date, high-1);
- }
- else if (high < 0){
- return;
- }
- else {
- return givenDate(A, date, high-1);
- }
- }
- //PART 6 FUNCTION
- //Give number of pax, Display all with at least given number of pax
- void givenPaxNo (Reservation A[], int pax, int high){
- if (pax <= A[high].getPaxNo() ){
- A[high].display();
- return givenPaxNo(A, pax, high-1);
- }
- if(pax>A[high].getPaxNo()){
- return;
- }
- else if (high < 0){
- return;
- }
- else {
- return givenPaxNo(A, pax, high-1);
- }
- }
- //ERROR CHECKING FUNCTIONS
- //for checking whether the name is empty
- bool checkName(string name){
- int counter = 0;
- bool found = 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(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV") != string::npos){
- found = true;
- }else{
- found = false;
- }
- //if no input or if got any non alphabetic character
- if(counter == 0 || found){
- 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;
- }
- };
- //Global Variables
- Functions stuff;
- Reservation records[MAX];
- //Option 1: Display All sorted by...
- void displayAll();
- //Option 2: Search & Display...
- void searchNDisplay();
- //Option 3: Given...display all
- void givenDisplayAll();
- int main(){
- //DEFAULT RESERVATIONS
- if(found == 0){
- 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));
- }
- }
- char choice;
- cout << "===============RESTAURANT RESERVATION SYSTEM II===============" << endl<<endl;
- if(found == 0){
- cout<<"< Currently using default reservation records >"<<endl<<endl;
- }else{
- cout<<"< Current Reservation Records : "<<fileDirectory<<" >"<<endl<<endl;
- }
- cout<< "Please enter your selection" << endl <<
- "1. Import Text File Data" << endl <<
- "2. Display All Sorted by..." << endl <<
- "3. Search & Display..." << endl <<
- "4. Given...display all..." << endl <<
- "5. Exit" << endl;
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- system("CLS");
- switch (choice) {
- //Display All Sorted by...
- case '1':
- cout<<"Enter 0 to return to main menu."<<endl<<endl;
- cout<<"NOTE : - Only the .txt file created from the 'Restaurant Reservation System I' can be used."<<endl
- <<" - Only "<<MAX<<" reservations are allowed."<<endl<<endl;
- cout<<"Please enter file directory of .txt file (e.g : /Users/TiffanyP/Desktop/Reservations.txt) : "<<endl;
- cin>>fileDirectory;
- if(fileDirectory != "0"){
- stuff.readTextFile(fileDirectory, records);
- }
- getchar();
- cout <<endl<< "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- //Display All Sorted by...
- case '2':
- displayAll();
- //Search...
- case '3':
- searchNDisplay();
- //Given...display all
- case '4':
- givenDisplayAll();
- case '5':
- exit(1);
- default:
- cout << choice << " is not valid choice" << endl;
- main();
- }
- return 0;
- }
- //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':
- stuff.bubbleSortName(records);
- stuff.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':
- stuff.bubbleSortDate(records);
- stuff.displayColumnNames();
- for(int i = 0; i<MAX; i++){
- records[i].display();
- }
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- break;
- case '3':
- main();
- default:
- cout << choice << " is not valid choice" << endl<<endl;
- displayAll();
- }
- cout <<endl<< "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- }
- //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 = stuff.binarySearchPhoneNo(records, phoneNo, 0, MAX-1);
- //If not found
- if(foundIndex==-1){
- cout<<"No such record with that phone number."<<endl;
- }else{
- //If found
- stuff.displayColumnNames();
- records[foundIndex].display();
- }
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- getchar();
- cout <<endl<< "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- //Search by Name (PART 4)
- case '2':
- do{
- cout<<"Enter name : ";
- getline(cin, name);
- }while(!stuff.checkName(name));
- foundIndex = stuff.binarySearchName(records, name, 0, MAX-1);
- //If not found
- if(foundIndex==-1){
- cout<<"No such record with that name."<<endl;
- }else{
- //If found
- stuff.displayColumnNames();
- records[foundIndex].display();
- }
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- cout <<endl<< "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- case '3':
- main();
- 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;
- //reset if a year before 2019 was entered then 2022 or later is entered
- }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(!stuff.checkDay(day, month, year));
- date = year*10000 + month*100 + day;
- stuff.displayColumnNames();
- //Call recursion function here
- stuff.givenDate(records, date, MAX-1);
- cout<<setfill('-')<<setw(87)<<"-"<<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;
- }
- }
- stuff.displayColumnNames();
- //Call recursion function here
- stuff.givenPaxNo(records, paxNo, MAX-1);
- cout<<setfill('-')<<setw(87)<<"-"<<endl;
- break;
- case '3':
- main();
- default:
- cout << choice << " is not valid choice" << endl;
- displayAll();
- }
- getchar();
- cout <<endl<< "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement