Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define MAX 5
- #include <iomanip>
- #include <stdlib.h> //for exit() function
- using namespace std;
- 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;
- }
- };
- Reservation records[MAX];
- //Option 1: Display All sorted by...
- void displayAll();
- //Option 1: Search & Display...
- void searchNDisplay();
- int main(){
- 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;
- cout << "Please enter your selection" << endl <<
- "1. Display All Sorted by..." << endl <<
- "2. Search & Display..." << endl <<
- "3. Given...display all..." << endl <<
- "4. Exit" << endl;
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- system("CLS");
- switch (choice) {
- //Display All Sorted by...
- case '1':
- displayAll();
- //Search...
- case '2':
- searchNDisplay();
- //Given...display all
- case '3':
- case '4':
- exit(1);
- default:
- cout << choice << " is not valid choice" << endl;
- main();
- }
- return 0;
- }
- //Bubble sort by Name
- void bubbleSortName(Reservation A[]);
- //Bubble sort by Date
- void bubbleSortDate(Reservation A[]);
- //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
- case '1':
- bubbleSortName(records);
- cout<<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;
- for(int i = 0; i<MAX; i++){
- records[i].display();
- }
- break;
- //Display all sorted by date
- case '2':
- bubbleSortDate(records);
- cout<<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;
- for(int i = 0; i<MAX; i++){
- records[i].display();
- }
- break;
- case '3':
- main();
- default:
- cout << choice << " is not valid choice" << endl;
- displayAll();
- }
- cout << "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- }
- //Bubble sort by Name
- void bubbleSortName(Reservation A[]){
- int i,j;
- Reservation current;
- for(i=0; i<MAX;i++){
- for (j=0; j<MAX-1; j++){
- if(A[j].getName()>A[j+1].getName()){
- current = A[j];
- A[j] = A[j+1];
- A[j+1] = current;
- }
- }
- }
- }
- //Bubble sort by Date
- void bubbleSortDate(Reservation A[]){
- int i,j;
- Reservation current;
- for(i=0; i<MAX;i++){
- for (j=0; j<MAX-1; j++){
- if(A[j].getDate()>A[j+1].getDate()){
- current = A[j];
- A[j] = A[j+1];
- A[j+1] = current;
- }
- }
- }
- }
- //Bubble sort by Phone Number
- void bubbleSortPhoneNo(Reservation A[]){
- int i,j;
- Reservation current;
- for(i=0; i<MAX;i++){
- for (j=0; j<MAX-1; j++){
- if(A[j].getPhoneNo()>A[j+1].getPhoneNo()){
- current = A[j];
- A[j] = A[j+1];
- A[j+1] = current;
- }
- }
- }
- }
- //Search Phone No Record
- int binarySearch(Reservation A[], int key, int low, int high);
- //Option 2 : Search...
- void searchNDisplay(){
- char choice;
- int foundIndex;
- 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
- case '1':
- int phoneNo;
- cout<<"Enter phone number : ";
- cin>>phoneNo;
- bubbleSortPhoneNo(records);
- foundIndex = binarySearch(records, phoneNo, 0, MAX-1);
- cout<<foundIndex<<endl;
- //Search by Name
- case '2':
- case '3':
- main();
- default:
- cout << choice << " is not valid choice" << endl;
- displayAll();
- }
- cout << "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- }
- //Search Phone No Record
- int binarySearchPhoneNo(Reservation A[], int key, int low, int high){
- int middle; //the middle index number of the array
- while (low <= high ){
- middle = (low+high)/2;
- if(key==A[middle].getPhoneNo()){
- return middle;
- }else if(key<A[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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement