Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <stdlib.h> //for exit() function
- #include <fstream> //for creating text file
- using namespace std;
- int total = 0;
- class Reservation{
- private:
- string name;
- int phoneNo;
- int paxNo;
- int day;
- int month;
- int year;
- int time;
- int date;
- long dateAndTime;
- public:
- Reservation(){
- name = "Tan Phit Huan";
- phoneNo = 0123456777;
- paxNo = 1;
- day = 1;
- month = 1;
- year = 2019;
- time = 1500;
- date = 20190101;
- dateAndTime = 201910201649;
- }
- Reservation(string na, int phNo, int pxNo, int d, int mon, int y, int t,int da, long dNt){
- name = na;
- phoneNo = phNo;
- paxNo = pxNo;
- day = d;
- month = mon;
- year = y;
- time = t;
- date = da;
- dateAndTime =dNt;
- }
- 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 setDay(int d){
- day = d;
- }
- int getDay(){
- return day;
- }
- void setMonth(int m){
- month = m;
- }
- int getMonth(){
- return month;
- }
- void setYear(int y){
- year = y;
- }
- int getYear(){
- return year;
- }
- void setTime(int t){
- time = t;
- }
- int getTime(){
- return time;
- }
- void setDate(int da){
- date = da;
- }
- int getDate(){
- return date;
- }
- void setDateAndTime(long dNt){
- dateAndTime = dNt;
- }
- long getDateAndTime(){
- return dateAndTime;
- }
- };
- class linklist{
- private:
- struct node{
- Reservation info;
- node *link;
- }*head;
- public:
- linklist(){
- head=NULL;
- }
- void InsNewNode(string na, int phNo, int pxNo, int d, int mon, int y, int t, long dNt){
- node*newNode = new node;
- node*p,*q = head;
- newNode -> info.setName(na);
- newNode -> info.setPhoneNo(phNo);
- newNode -> info.setPaxNo(pxNo);
- newNode -> info.setDay(d);
- newNode -> info.setMonth(mon);
- newNode -> info.setYear(y);
- newNode -> info.setTime(t);
- newNode -> info.setDateAndTime(dNt);
- //If no records
- if (head == NULL){
- head = newNode;
- return;
- }
- //Insert at the start of list
- if (head -> info.getDateAndTime() > newNode -> info.getDateAndTime()){
- newNode -> link = head;
- head = newNode;
- return;
- }
- while(q->info.getDateAndTime() < newNode -> info.getDateAndTime() && q->link != NULL){
- p = q;
- q = q->link;
- }
- //Insert at the end of the list
- if(q == NULL){
- q = newNode;
- newNode-> link = NULL;
- return;
- //Insert in the middle of the list
- }else{
- p = q;
- q = q->link;
- p->link = newNode;
- newNode-> link = q;
- return;
- }
- }
- void deleteNode(int num){
- //Delete first node
- if(num == 1){
- node*temp = head;
- head = head -> link;
- delete temp;
- }
- //Delete last node
- else if(num == total && total != 1){
- node*p, *q = head;
- while(q -> link != NULL){
- p = q;
- q = q -> link;
- }
- p->link = NULL;
- delete q;
- }
- //Delete nodes in the middle
- else{
- node*p, *q = head;
- for(int i = 1; i<num; i++){
- p = q;
- q = q -> link;
- }
- p -> link = q ->link;
- delete q;
- }
- total--;
- }
- void PrintList(){
- node *current;
- current = head;
- int count = 0;
- 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;
- while (current != NULL){
- count++;
- cout<<"| "<<setfill(' ')<<count<<" "
- <<left<<setw(30)<< current->info.getName()
- <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
- <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
- <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
- <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
- current = current->link;
- }
- cout<<endl;
- }
- void PrintNameRecords(string name){
- node *current;
- current = head;
- int count = 0;
- 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;
- while (current != NULL){
- //If there is any reservation with the same name
- if(name == current->info.getName()){
- count++;
- cout<<"| "<<setfill(' ')<<count<<" "
- <<left<<setw(30)<< current->info.getName()
- <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
- <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
- <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
- <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
- }
- current = current->link;
- }
- cout<<endl;
- }
- void PrintDateRecords(int date){
- node *current;
- current = head;
- int count = 0;
- 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;
- while (current != NULL){
- //If there is any reservation with the same date
- if(date == current->info.getDate()){
- count++;
- cout<<"| "<<setfill(' ')<<count<<" "
- <<left<<setw(30)<< current->info.getName()
- <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
- <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
- <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
- <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
- }
- current = current->link;
- }
- cout<<endl;
- }
- void PrintListTextFile(){
- string fileName;
- int count = 0;
- cout << "---------------=CREATE TEXT FILE=---------------" << endl;
- //Create file with the file name user entered
- cout << "Enter file name: ";
- cin >> fileName;
- cout << "Your .txt file can be found in the same folder where your .cpp file is saved." << endl;
- fileName = fileName + ".txt";
- //output to .txt file
- ofstream file(fileName.c_str());
- if (file.is_open()) {
- //output all the reservation records
- node *current;
- current = head;
- file<<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;
- while (current != NULL){
- count++;
- file<<"| "<<setfill(' ')<<count<<" "
- <<left<<setw(30)<< current->info.getName()
- <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
- <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
- <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
- <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
- current = current->link;
- }
- file.close(); //close file
- }
- }
- // ~linklist(){
- // node *q;
- // if(head == NULL){
- // return;
- // }
- // while( head != NULL ){
- // q = head->link;
- // delete head;
- // head = q;
- // }
- // }
- };
- linklist RList;
- void addRecord();
- void updateRecord();
- void deleteRecord();
- void viewRecord(); //1. select by name. 2. Display all
- void createTextFile();
- int main() {
- int choice;
- cout << "===============RESTAURANT RESERVATION SYSTEM===============" << endl;
- cout << "Please enter your selection" << endl <<
- "1. Add Reservation Records" << endl <<
- "2. Update Reservation Records" << endl <<
- "3. Delete Reservation Records" << endl <<
- "4. View Reservation Records " << endl <<
- "5. Create text file" << endl <<
- "6. Exit" << endl;
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- system("CLS");
- switch (choice) {
- //Add Records
- case 1:
- addRecord();
- break;
- //Update Records
- case 2:
- if (total == 0) {
- cout << "No reservation records available." << endl;
- main();
- }
- updateRecord();
- break;
- //Delete Records
- case 3:
- if (total == 0) {
- cout << "No reservation records available." << endl;
- main();
- }
- deleteRecord();
- break;
- //View Reservation Records
- case 4:
- if (total == 0) {
- cout << "No reservation records available." << endl;
- main();
- }
- viewRecord();
- break;
- //Create Text File
- case 5:
- if (total == 0) {
- cout << "No reservation records available." << endl;
- main();
- }
- createTextFile();
- break;
- case 6:
- exit(1);
- break;
- default:
- cout << choice << " is not valid choice" << endl;
- main();
- }
- return 0;
- }
- //OPTION 1 : Add Reservation Records
- void addRecord(){
- string name;
- int phoneNo, paxNo, day, month, year, time;
- long dateAndTime;
- cout << "-----------------=ADD RECORD=-------------------" << endl;
- cout<<"Enter name : ";
- getline(cin, name);
- cout<<"Enter phone No. (e.g : 0123456789) : ";
- cin>>phoneNo;
- cout<<"Enter pax No. : ";
- cin>>paxNo;
- cout<<"Enter year (e.g : 2019) : ";
- cin>>year;
- cout<<"Enter month (e.g : January = 1) : ";
- cin>>month;
- cout<<"Enter day : ";
- cin>>day;
- cout<<"Enter time in 24hr format (e.g : 3.30pm = 1530) : ";
- cin>>time;
- //for inserting the node in order
- dateAndTime = year*100000000 + month*1000000 + day*10000 + time;
- RList.InsNewNode(name, phoneNo, paxNo, day, month, year, time, dateAndTime);
- total++;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Press enter to continue . . .";
- getchar();
- system("CLS");
- main();
- }
- //OPTION 2 : Update Reservation Records
- void updateRecord(){
- cout << "----------------=UPDATE RECORD=-----------------" << endl;
- }
- //OPTION 3 : Delete Reservation Records
- void deleteRecord(){
- int num;
- bool correct;
- cout << "----------------=DELETE RECORD=-----------------" << endl;
- RList.PrintList();
- do{
- correct = true;
- cout<<"(Enter 0 to go back to Main Menu)"<<endl
- <<"Enter the No. you wish to delete : ";
- cin>>num;
- //if more than total reservations or negative number
- if(num > total || num < 0){
- cout<<endl
- <<"INVALID INPUT!"<<endl
- <<"Total Reservations : "<<total<<endl;
- correct = false;
- }else if(num == 0){
- main();
- }else{
- RList.deleteNode(num);
- }
- }while(!correct);
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Press enter to continue . . .";
- getchar();
- system("CLS");
- main();
- }
- //OPTION 4 : View Reservation Records
- void viewRecord(){
- int choice;
- string name;
- int year, month, day, date;
- cout << "-----------------=VIEW RECORD=------------------" << endl;
- cout << "Please enter your selection" << endl <<
- "1. View All Reservations" << endl <<
- "2. Search by Name" << endl<<
- "3. Search by Date"<<endl<<
- "4. Back to Main Menu"<<endl;
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- system("CLS");
- switch (choice) {
- //View All Reservations
- case 1:
- RList.PrintList();
- break;
- //Search by Name
- case 2:
- cout<<"Enter Name : ";
- getline(cin, name);
- RList.PrintNameRecords(name);
- break;
- case 3:
- cout<<"Enter year (e.g : 2019) : ";
- cin>>year;
- cout<<"Enter month (e.g : January = 1) : ";
- cin>>month;
- cout<<"Enter day : ";
- cin>>day;
- date = year*10000 + month*100 + day;
- break;
- case 4:
- main();
- break;
- default:
- cout << choice << " is not valid choice" << endl;
- main();
- }
- cout << "Press enter to continue . . .";
- getchar();
- system("CLS");
- main();
- }
- //OPTION 5 : Create Text File
- void createTextFile(){
- RList.PrintListTextFile();
- getchar();
- cout << "-----------------------------------------------------" << endl;
- cout << "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement