Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a project containing the files main.cpp, reserved.cpp, reserved.h, guest.cpp, guest.h, table.cpp, table.h. Split the functions from Part A into the appropriate modules as outlined

Create a project containing the files main.cpp, reserved.cpp, reserved.h, guest.cpp, guest.h, table.cpp, table.h. Split the functions from Part A into the appropriate modules as outlined in the general instructions. The program should compile and have the same input and output as Part A. In order to determine where a function belongs, look to see what functions it calls, and which functions it is called by. Functions which rely heavily on each other are good candidates for a module. Functions which are associated in purpose may also be candidates for a particular module.

main.cpp:

#include #include #include #include #include #include #include

using namespace std;

void swapReserveItems(int reservations[][5], int indexA, int indexB) { ///swaps 2 items in reservation by row index for(int j = 0; j<5; j++) { int temp = reservations[indexA][j]; reservations[indexA][j]=reservations[indexB][j]; reservations[indexB][j]=temp; } }

void readTable(std::istream& in, int row[], int numCols) { ///reads a single table row from a line in a stream /// /// FILL THIS FUNCTION for (int i=0 ; i < numCols ; i++) in>>row[i]; }

void sortReservations(int reservations[][5], int numItems) { ///sorts the reservations by reservation number(the first element in each row of the array) /// /// FILL THIS FUNCTION /// for(int i=0; i

}

}

}

void writeTable(std::ostream& out, int row[], int numCols) { /// writes a single table to a stream for(int i = 0; i< numCols; i++) { out<

}

void writeGuest(std::ostream& out, std::string guest[], int numCols) { /// writes a single guest to a stream /// /// FILL THIS FUNCTION /// for (int i=0; i< numCols;i++){ out<< guest[i]<<" "; } out<

void readGuest(std::istream& in, std::string guest[], int numCols) { ///reads a single guest from a line in a stream for(int i=0; i>guest[i]; } }

void loadGuests(std::istream& in, std::string guests[][5], int numGuests) { /// loads guests from a stream for(int i=0; i

void swapGuestItems(std::string guest[][5], int indexA, int indexB) { ///swaps 2 items in guest by row index /// /// FILL THIS FUNCTION /// for(int i=0 ;i<5; i++){ string temp = guest[indexA][i]; guest[indexA][i]= guest[indexB][i]; guest[indexB][i]=temp; } }

std::string getGuestPhone() { ///prompts the user for a guests phone number while(true) { std::string phone=""; std::cout<<"Enter phone(no punctuation): "; std::cin>>phone; if(!std::cin || phone.length()!=10 || !(phone.find_first_not_of( "0123456789" ) == string::npos)) /// checks phone number is a series of digits of the right length { std::cout<<" :::Invalid Entry!!!:::"; std::cin.clear(); std::cin.ignore(256,' '); } else { std::cin.clear(); std::cin.ignore(256,' ');

return phone; }

} return ""; }

void sortGuests(std::string guests[][5], int numItems) { ///sorts the guests by guest number for(int i=1; iatoi(guests[j+1][0].c_str())) { swapGuestItems(guests,j,j+1); } } }

}

void saveGuests(std::string guests[][5], int numGuests) { /// saves guest array to a file std::fstream outFile; outFile.open("guests.txt", std::ios::out); outFile<

void saveTables(int table[][9], int numRows) { ///saves tables in table array to a file std::fstream outFile; outFile.open("tables.txt", std::ios::out); outFile<

bool guestExists(std::string guests[][5], int numGuests, std::string Gname) { ///checks to see if a guest exists by their last name for( int i=0; i

}

void addGuest(std::string guests[][5], int numGuests, std::string row[], int numCols) { ///adds a guest held in row[] to a temp array and saves the new array to file std::string temp[numGuests+1][5]; for(int i = 0; i

}

void readReservation(std::istream& in, int reservation[], int numCols) { ///reads a single reservation from a stream and loads it into the row reservation[] for(int i=0; i>reservation[i]; } }

std::string genNewGuestID(std::string guests[][5], int numGuests) { ///finds either an empty ID to return or returns a new one bool found = false; int largest=0; int ID=0; sortGuests(guests,numGuests); ///looks for a gap in the sorted IDs for(int i=0; ilargest) { largest = atoi(guests[i][0].c_str()); } } ///if a gap was not found, set ID to largest + 1 if(!found) { std::stringstream ss; ss<

std::string GuestNameByGID(std::string guests[][5],int numGuests,int GID) { /// returns a guests name given the GID for(int i=0; i

int getGuestIndex(std::string guests[][5], int numGuests, std::string Gname) { /// returns the index of a guest given guests name for(int i=0; i

return 0; }

void writeReservation(std::ostream& out, int reservation[], int numCols) { /// writes a single reservation to a stream for(int i = 0; i< numCols; i++) { out<

void printAvailibility(std::ostream& out, int table[][9], int numRows, int party) { /// prints the availability of tables based on party size for(int i=0; i2&&party<=4) { if(table[i][4]==0) { out<<"\t"<<4< 4 && party <= 8) { if(table[i][8]==0) { out<<"\t"<<8<

}

bool insertReservedTable(int table[][9], int numRows, int time, int party, int RID) { /// adds a reservation to the table array for(int i=0; i2 && party <= 4) { if(table[i][4]==0) { table[i][4]=RID; std::cout<<" ::::Reservation Completed::::"<

} if(party>4 && party <=8) { if(table[i][8]==0) { table[i][8]=RID; std::cout<<" ::::Reservation Completed::::"<

std::cout<<" :::Table already Taken!!!:::"<

}

bool releaseTable(int table[][9], int numRows, int RID) { ///releases a table from the table array for(int i=0; i< numRows; i++) { for(int j=0; j<9; j++) { if(table[i][j]==RID) { table[i][j]=0; std::cout<<" ::::Reservation Removed::::"<

}

void loadTables(std::istream& in, int table[][9], int numRows) { ///loads tables from a stream for(int i=0; i

int genNewResID(int reservation[][5],int numRIDs) { ///finds either an empty ID to return or returns a new one bool found = false; int largest=0; int ID=0; sortReservations(reservation,numRIDs); ///looks for a gap in the sorted IDs for(int i=0; ilargest) { largest = reservation[i][0]; } } ///if a gap was not found, set ID to largest + 2 (plus one would set it equal to the last item in the list. The last item is never evaluated for largest because of the nature of the for loop used) if(!found) { return largest+2; } return ID; }

void loadReservations(std::istream& in, int reservations[][5], int numRIDs) { /// loads reservations to an array from a stream for(int i=0; i

}

void saveReservations(int reservations[][5], int numRIDs) { /// saves the array reservations to a file std::fstream outFile; outFile.open("reserved.txt"); outFile<

void deleteRes(int reservation[][5],int numRIDs, int index) { /// swaps a reservation with the end of the reservation array, then saves the array numRIDs-1 to file swapReserveItems(reservation,index,numRIDs-1); saveReservations(reservation,numRIDs-1);

}

void addReservation(int reservations[][5], int numRIDs, int row[], int numCols) { /// adds a reservation to the file int temp[numRIDs + 1][5]; for(int i=0; i

///********************************************************************************** ///**************ALL BELOW THIS LINE STAYS IN MAIN*********************************** ///**********************************************************************************

std::string getGuestExpiry() { time_t t = time(NULL); tm* timePtr = localtime(&t); while(true) { int MM=0; int YYYY=0; std::cout<<"Enter phone(MM YYYY): "; std::cin>>MM; std::cin>>YYYY;

///validates the expiration data against the current date if(!std::cin || ((MM<1||MM>12)||(YYYY < (timePtr->tm_year+1900) || YYYY > 9999 ))||(YYYY==(timePtr->tm_year + 1900) && MM < (timePtr->tm_mon + 1))) { std::cout<<" :::Invalid Date!!!:::"; std::cin.clear(); std::cin.ignore(256,' '); } else { std::cin.clear(); std::cin.ignore(256,' '); ///converts the year to 2 digits YYYY= YYYY%100; std::stringstream ss; ss<

}

return ""; }

std::string getGuestCC() { ///prompts the user for a guests credit card number while(true) { std::string CC=""; std::cout<<"Enter CC#(no punctuation): "; std::cin>>CC; if(!std::cin || CC.length()!=16 || !(CC.find_first_not_of( "0123456789" ) == string::npos)) { std::cout<<" :::Invalid Entry!!!:::"; std::cin.clear(); std::cin.ignore(256,' '); } else { std::cin.clear(); std::cin.ignore(256,' ');

return CC; }

}

return ""; }

void printAllRID(std::ostream& out, int reservations[][5], int numRIDs, std::string guests[][5], int numGuests) { /// Prints all of the reservations to a stream out<<" :::::::::::::::::::::Reservations:::::::::::::::::::::::::: "<

}

void cancelReservation(int reservations[][5], int numRIDs, std::string guests[][5], int numGuests, int tables[][9], int numRows) { /// removes reservation from the different arrays printAllRID(std::cout, reservations, numRIDs, guests,numGuests); int selection =0; while(true) { std::cout<<"Select reservation to cancel(0-"<>selection; if(!cin || (selection > (numRIDs) || selection< 1)) { std::cout<<":::::Invalid Selection:::::"; std::cin.clear(); std::cin.ignore(256,' '); } else { break; } } int RID = reservations[selection-1][0]; deleteRes(reservations, numRIDs, selection-1); releaseTable(tables, numRows, RID);

}

int getTime(int numRows) { /// prompts user for time of a reservation int time=0; while(true) { std::cout<<"Select a time (0-"<>time; if(!std::cin || (time<0 || time> (numRows-1))) { std::cout<<" :::::Invalid Selection:::::"; } else { std::cin.clear(); std::cin.ignore(256,' '); time = (time * 100)+1700; return time; } } return time; }

int getNumParty() { /// prompts the user for the number in a guests party while(true) { int party=0; std::cout<<"Enter size of party: "; std::cin>>party; if(!std::cin || party > 8 || party < 1) { std::cout<<" :::Invalid Entry!!!:::"; std::cin.clear(); std::cin.ignore(256,' '); } else { std::cin.clear(); std::cin.ignore(256,' '); return party; }

} return 0; }

int menu() { ///displays the menu and returns a users selection int selection=0; while(true) { std::cout<<" :::::::::::::::::::Main Menu:::::::::::::::::::: "<>selection; if(!std::cin || (selection < 1 || selection > 4)) { std::cout<<" :::::Invalid Selection:::::"; std::cin.clear(); std::cin.ignore(256,' '); continue; } else { std::cin.clear(); std::cin.ignore(256,' '); return selection; } } }

void NewReservation(int reservation[][5], int numRIDs, std::string guests[][5], int numGuests, int tables[][9], int numRows) { ///allows a user to enter a new reservation std::string Gname=""; int GID=0; std::cout<<"Enter the guests last name: "; std::cin>>Gname; std::cin.clear(); std::cin.ignore(256,' '); if(!guestExists(guests,numGuests,Gname)) { std::string Grow[5]; Grow[0]=genNewGuestID(guests,numGuests); Grow[1]=Gname; Grow[2]=getGuestPhone(); Grow[3]=getGuestCC(); Grow[4]=getGuestExpiry(); addGuest(guests,numGuests,Grow, 5); GID=atoi(Grow[0].c_str());

} else { std::cout<<":::Guest already Exists :):::"<

int time =0; while(true) { printAvailibility(std::cout,tables, numRows, Rrow[2]); time = getTime(numRows); if(!insertReservedTable(tables,numRows,time,Rrow[2],Rrow[0])) { std::cout<<":::Time not available:::"; } else { break; } } Rrow[3]=time; Rrow[4]=0; addReservation(reservation,numRIDs, Rrow, 5);

}

bool reservationCycle() { ///handles the menu and general function calls int numGuests=0; int numRIDs=0; int numRows=0; std::fstream inFile; inFile.open("reserved.txt",std::ios::in); inFile>>numRIDs; if(!inFile) { std::cout<<":::File \"reserved.txt\" missing or corrupted:::"<>numGuests; if(!inFile) { std::cout<<":::File \"guests.txt\" missing or corrupted:::"<>numRows; if(!inFile) { std::cout<<" "<

int main() { bool loop = true; while(loop) { loop = reservationCycle(); }

return 0; }

guests.txt:

4 1 whitlock 7577577575 5555555555555555 10/99 2 morris 4564564564 4444444444444444 12/99 3 carrington 2342342342 2222222222222222 01/99 4 clark 1231231231 1111111111111111 03/99

reserved.txt:

4 1 4 4 1700 0 2 3 8 2100 0 3 2 2 1800 0 4 1 2 1800 0 

tables.txt

6 1700 0 4 0 1 0 0 0 0 1800 3 0 0 0 0 0 0 0 1900 0 0 0 0 0 0 0 0 2000 0 0 0 0 0 0 0 0 2100 0 0 0 0 0 0 0 2 2200 0 0 0 0 0 0 0 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions