1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
//main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "Restaurant.h"
#include "FTime.h"
int ReadMenuChoice();
void DisplayMenu();
void ProcessChoice(int choice, Restaurant info[], int& count);
void AddRestaurant(Restaurant info[], int& count);
void WriteLine(char ch, int size);
void PrintAll(const Restaurant info[], int count);
void ReadRating(Restaurant& r);
void ReadName(istream& in, Restaurant& n);
void ReadTime(FTime& t, string prompt);
using namespace std;
const int MAX = 100;
enum{ADD = 1, PRINT, EXIT};
template <class T>
bool Read(T& val);
int main()
{
Restaurant info[MAX];
int count = 0, choice;
while( EXIT != (choice = ReadMenuChoice() ) )
{
ProcessChoice(choice, info, count);
}
return 0;
}
int ReadMenuChoice()
{
int choice;
bool valid;
DisplayMenu();
do
{
cout << "Enter 1-3 : ";
valid = Read(choice);
if(!valid)
{
cout << "Invalid Format" << endl;
}
else if(valid)
{
if (choice < ADD || choice > EXIT)
{
cout << "Invalid Entry! Try again." << endl;
valid = false;
}
else
{
valid = true;
}
}
}while(!valid);
return choice;
}
void DisplayMenu()
{
WriteLine('=', 60);
cout << "MENU" << endl;
WriteLine('-', 60);
cout << "1. Add Restaurant" << endl;
cout << "2. Print All " << endl;
cout << "3. Exit" << endl;
WriteLine('-', 60);
}
void ProcessChoice(int choice, Restaurant info[], int& count)
{
FTime openHour, closeHour;
switch(choice)
{
case ADD:
AddRestaurant(info, count);
break;
case PRINT:
PrintAll(info, count);
break;
}
}
void PrintAll(const Restaurant info[], int count)
{
cout << endl;
cout << "RESTAURANTS:" << endl;
WriteLine('=', 60);
cout << left << setw(20) << "Name:" << setw(15) << "Rating" << setw(15) << "Open" << setw(15)
<< "Close" << endl;
WriteLine('-', 60);
for(int i = 0; i < count; i++)
cout << left << setw(20) << info[i].GetName() << setw(15) << info[i].GetRating() << setw(15)
<< info[i].GetTime() << setw(15)
<< info[i].GetTime() << endl;
WriteLine('-', 60);
}
void AddRestaurant(Restaurant info[], int& count)
{
FTime open, close;
Restaurant n , r;
int rating;
if(count < MAX)
{
ReadName(cin, n);
ReadRating(r);
ReadTime(open, "Opening Hour:");
ReadTime(close, "Closing Hour:");
count++;
}
else
cout << "FULL" << endl;
}
void ReadName(istream& in, Restaurant& n)
{
bool valid;
do
{
try
{
cout << "Name: ";
getline(cin, n);
valid = true;
}
catch(runtime_error e)
{
cout << e.what() << endl;
valid = false;
cin.clear();
cin.ignore(1000, '\n');
}
}while(!valid);
}
void ReadRating(Restaurant& r)
{
bool valid;
do
{
try
{
cout << "Rating: ";
cin >> r;
valid = true;
}
catch(runtime_error e)
{
cout << e.what() << endl;
valid = false;
cin.clear();
cin.ignore(1000, '\n');
}
}while(!valid);
}
void WriteLine(char ch, int size)
{
cout << setfill(ch) << setw(size) << "" << endl;
cout << setfill(' ');
}
void ReadTime(FTime& t, string prompt)
{
bool valid;
do
{
try
{
cout << prompt << " (HH:MM): ";
cin >> t;
valid = true;
}
catch(runtime_error e)
{
cout << e.what() << endl;
valid = false;
cin.clear();
cin.ignore(1000, '\n');
}
}while(!valid);
}
template <class T>
bool Read(T& val)
{
bool valid;
cin >> val;
if( cin.fail() )
{
cin.clear();
valid = false;
}
else
{
valid = true;
}
cin.ignore(1000, '\n');
return valid;
}
|