00001
00002
00003
00004
00005
00006
00007
00008 #include "UserMenu.h"
00009 #include "Product.h"
00010 #include "Order.h"
00011
00012
00013 UserMenu::UserMenu()
00014 {
00015 currentUser = NULL;
00016 }
00017
00018 UserMenu::~UserMenu()
00019 {
00020 if (currentUser)
00021 delete currentUser;
00022 }
00023
00024 void UserMenu::wait()
00025 {
00026 cin.clear();
00027 cout << "\nPress Enter/Return to continue...\n";
00028 cin.ignore(1,0);
00029 }
00030
00031 void UserMenu::displayUnprivilegedMenu() throw (BadAuthException)
00032 {
00033 if (!currentUser)
00034 throw BadAuthException();
00035
00036 int choice;
00037
00038 do {
00039 system(CLEAR_SCREEN_CMD);
00040 cout << "USER MENU\n" <<
00041 "(1) Browse product catalog by category\n"
00042 "(2) View details of a chosen product\n"
00043 "(3) Browse all the configurations which include a given item\n"
00044 "(4) Browse user profile and all his order\n"
00045 "(5) Place a new order\n"
00046 "(0) LOGOUT\n\n";
00047
00048 cin >> choice;
00049 switch (choice) {
00050 case 1:
00054 browseProductCatalog();
00055 wait();
00056 break;
00057 case 2:
00062 browseProductCatalog();
00063 showProductDetail();
00064 wait();
00065 break;
00066 case 3:
00067 showConfigurationByProduct();
00068 wait();
00069 break;
00070 case 4:
00071 showUserProfile();
00072 wait();
00073 break;
00074 case 5:
00075 placeNewOrder();
00076 break;
00077 }
00078 } while (choice != 0);
00079
00080 delete currentUser, currentUser = NULL;
00081 }
00082
00083 void UserMenu::browseProductCatalog() throw (BadAuthException)
00084 {
00085 if (!currentUser)
00086 throw BadAuthException();
00087
00088 int cid;
00089 system(CLEAR_SCREEN_CMD);
00090
00091 cout << "CATEGORY LIST\n";
00092 vector<Category *> & vc = Category::catalog();
00093 if (&vc && vc.size()) {
00094 for (int i=0; i < (int)vc.size(); i++) {
00095 cout << *(vc[i]) << endl;
00096 }
00097 }
00098 cout << "\nEnter category ID [0 to browse all]: ";
00099 cin >> cid;
00100
00101 vector<ProductProxy *> & v = Product::catalog();
00102 if (&v && v.size()) {
00103 for (int i=0; i < (int)v.size(); i++) {
00104
00105
00106
00107
00108
00109
00110 ProductProxy *pp = v[i];
00111 cout << setw(4) << pp->uniqueID() << " | " << setw(30) << pp->getName() << endl;
00112 }
00113
00114 for_each(v.begin(), v.end(), deletePtr<ProductProxy>());
00115 delete &v;
00116 }
00117 }
00118
00119 void UserMenu::showProductDetail() throw (BadAuthException)
00120 {
00121 if (!currentUser)
00122 throw BadAuthException();
00123
00124 int pid;
00125
00126 cout << "\nEnter product ID: ";
00127 cin >> pid;
00128 ProductProxy pp(pid);
00129
00130 cout << endl << pp;
00131 }
00132
00133 void UserMenu::showConfigurationByProduct() throw (BadAuthException)
00134 {
00135 if (!currentUser)
00136 throw BadAuthException();
00137
00138 int pid;
00139
00140 cout << "BROWSE ALL CONFIGURAATIONS WHICH INCLUDE A GIVEN ITEM\n\n"
00141 "Choose a product from the following list\n\n";
00142
00143 vector<ProductProxy *> & v = Product::catalog();
00144 if (&v && v.size()) {
00145 for (int i=0; i < (int)v.size(); i++) {
00146 ProductProxy *pp = v[i];
00147 cout << setw(4) << pp->uniqueID() << " | " << setw(30) << pp->getName() << endl;
00148 }
00149
00150 for_each(v.begin(), v.end(), deletePtr<ProductProxy>());
00151 delete &v;
00152 }
00153 cout << "\nEnter product ID: ";
00154 cin >> pid;
00155 Product::showCompatibleProducts(pid);
00156 }
00157
00158 void UserMenu::showUserProfile() throw (BadAuthException)
00159 {
00160 if (!currentUser)
00161 throw BadAuthException();
00162
00163 cout << "USER DETAILS\n" << *currentUser << endl << endl;
00164
00165 vector<Order *> & orders = Order::ordersForUser(*currentUser);
00166 for (vector<Order *>::iterator it = orders.begin(); it != orders.end(); it++) {
00167 Order *ord = (Order *) *it;
00168 cout << "| " << *ord << endl;
00169 map<int, int> &products = ord->products();
00170 for (map<int,int>::iterator mit = products.begin(); mit != products.end(); mit++) {
00171 cout << "\tProduct " << (*mit).first << " Quantity: " << (*mit).second << endl;
00172 }
00173 delete &products;
00174 }
00175
00176 for_each(orders.begin(), orders.end(), deletePtr<Order>());
00177 delete &orders;
00178 }
00179
00180 void UserMenu::placeNewOrder() throw (BadAuthException)
00181 {
00182 if (!currentUser)
00183 throw BadAuthException();
00184
00185 bool addMoreProducts = true;
00186
00187 do {
00188 int aPid, aQty;
00189
00190 browseProductCatalog();
00191 cout << "\nEnter product ID [0 to end]: ";
00192 cin >> aPid;
00193 if (aPid == 0)
00194 addMoreProducts = false;
00195 else {
00196 cout << "How many pieces: ";
00197 cin >> aQty;
00198
00199 currentUser->getBasket()->addProduct(new ProductProxy(aPid), aQty);
00200 }
00201 } while (addMoreProducts);
00202
00203 if (currentUser->getBasket()->size() == 0) {
00204 cout << "No products were added to basket.\n";
00205 wait();
00206 return;
00207 }
00208 cout << "SUMMARY\n" << currentUser->getBasket() << endl;
00209 currentUser->placeOrder();
00210 }
00211
00212 void UserMenu::displayAdminMenu() throw (BadAuthException)
00213 {
00214 if (!currentUser)
00215 throw BadAuthException();
00216
00217 int choice;
00218
00219 do {
00220 system(CLEAR_SCREEN_CMD);
00221 cout << "ADMINISTRATION MENU\n" <<
00222 "(1) Add a new product category\n"
00223 "(2) Add a new product\n"
00224 "(3) Change product details\n"
00225 "(4) Disable an user\n"
00226 "(0) EXIT\n\n";
00227 cin >> choice;
00228 switch (choice) {
00229 case 1:
00230 addNewCategory();
00231 wait();
00232 break;
00233 case 2:
00234 addNewProduct();
00235 wait();
00236 break;
00237 case 3:
00238 changeProductDetail();
00239 wait();
00240 break;
00241 case 4:
00242 disableUser();
00243 wait();
00244 break;
00245 }
00246 } while (choice != 0);
00247 }
00248
00249 void UserMenu::addNewCategory() throw (BadAuthException)
00250 {
00251 if (!currentUser)
00252 throw BadAuthException();
00253
00254 Category *newCat;
00255 string name;
00256
00257 system(CLEAR_SCREEN_CMD);
00258 cout << "Enter the name of new category: ";
00259 cin >> name;
00260 newCat = Category::factory(name);
00261 newCat->store();
00262 }
00263
00264 void UserMenu::addNewProduct() throw (BadAuthException)
00265 {
00266 if (!currentUser)
00267 throw BadAuthException();
00268
00269 string name, descr = "";
00270 float price;
00271 int qty=0, cid;
00272
00273 system(CLEAR_SCREEN_CMD);
00274 cout << "ADD NEW PRODUCT\n\nChoose the category of new product from list\n";
00275
00276 vector<Category *> & v = Category::catalog();
00277 if (&v && v.size()) {
00278 for (int i=0; i < (int)v.size(); i++) {
00279 cout << *(v[i]) << endl;
00280 }
00281
00282 } else
00283 return;
00284
00285 cout << "Enter category ID: ";
00286 cin >> cid;
00287 cout << "Product name: ";
00288 cin >> name;
00289 cout << "Product description: ";
00290 cin >> descr;
00291 cout << "Price: ";
00292 cin >> price;
00293 cout << "Availability: ";
00294 cin >> qty;
00295
00296 Product *p = Product::factory(name, cid, price, descr, qty);
00297 p->store();
00298
00299 for_each(v.begin(), v.end(), deletePtr<Category>());
00300 delete &v;
00301 delete p;
00302 }
00303
00304 void UserMenu::changeProductDetail() throw (BadAuthException)
00305 {
00306 if (!currentUser)
00307 throw BadAuthException();
00308
00309 Product *p;
00310 int pid, attr;
00311 string attribute;
00312 float price;
00313
00314 browseProductCatalog();
00315 cout << "\nEnter product ID to change: ";
00316 cin >> pid;
00317 p = Product::productByID(pid);
00318 cout << "\nREVIEW PRODUCT DETAIL\n " << *p << endl;
00319
00320 do {
00321 cout << "\nCHOOSE WHICH ATTRIBUTE YOU WANT TO EDIT:\n"
00322 << "[1] Name\n[2] Description\n[3] Price\n[0] End\n\n"
00323 << "Enter attribute ID: ";
00324 cin >> attr;
00325 cout << "Enter new value: ";
00326 switch (attr) {
00327 case 1:
00328 cin >> attribute;
00329 p->setValueForKey(KEY_PRD_NAME, attribute);
00330 break;
00331 case 2:
00332 cin >> attribute;
00333 p->setValueForKey(KEY_PRD_DESCR, attribute);
00334 break;
00335 case 3:
00336 cin >> price;
00337 p->setFloatForKey(KEY_PRD_PRICE, price);
00338 break;
00339 default:
00340 attr = 0;
00341 break;
00342 }
00343 } while (attr != 0);
00344 p->update();
00345 }
00346
00352 void UserMenu::disableUser() throw (BadAuthException)
00353 {
00354 if (!currentUser)
00355 throw BadAuthException();
00356
00357 AdminUser *anAdmin = dynamic_cast<AdminUser *> (currentUser);
00358 assert(anAdmin);
00359 vector<User *> & uc = anAdmin->userList();
00360 int pid, i;
00361
00362 for (i=0; i<uc.size(); i++) {
00363 User *anUser = uc[i];
00364 cout << setw(4) << i+1 << " | " << anUser->fullName() << endl;
00365 }
00366 cout << "\nEnter user ID to disable: ";
00367 cin >> pid;
00368 if ((pid < 0) || (pid > uc.size())) {
00369 cerr << "Invalid selection" << endl;
00370 return;
00371 }
00372
00373 User *anUser = uc[i-1];
00374 anAdmin->changeUserPassword(*anUser, "*LK*");
00375
00376 for_each(uc.begin(), uc.end(), deletePtr<User>());
00377 delete &uc;
00378 }
00379
00380 void UserMenu::display() throw (BadAuthException)
00381 {
00382 if (!currentUser)
00383 throw BadAuthException();
00384
00385 if (currentUser->isAdmin())
00386 displayAdminMenu();
00387 else
00388 displayUnprivilegedMenu();
00389 }
00390
00391 bool UserMenu::login()
00392 {
00393 string username;
00394 string passwd;
00395
00396 system(CLEAR_SCREEN_CMD);
00397 cout << "USER LOGIN\nLogin [0 to quit] : ";
00398 cin >> username;
00399 if (username == "0")
00400 return false;
00401 cout << "Password: ";
00402 cin >> passwd;
00403
00404 if ((currentUser = User::login(username, passwd)))
00405 return true;
00406
00407 return false;
00408 }
00409
00410 void UserMenu::mainMenu()
00411 {
00412 int choice;
00413
00414 do {
00415 system(CLEAR_SCREEN_CMD);
00416 cout << "MAIN MENU\n\n(1) User login\n(2) New user registration\n"
00417 << "(0) Quit\n\nMake your selection: ";
00418 cin >> choice;
00419 if (choice == 1) {
00420 if (login())
00421 display();
00422 } else if (choice == 2)
00423 registerNewUser();
00424 } while (choice != 0);
00425 }
00426
00427 void UserMenu::registerNewUser()
00428 {
00429 string name, surname, address, city, login, passwd;
00430
00431 system(CLEAR_SCREEN_CMD);
00432 cout << "NEW USER REGISTRATION\n";
00433 cout << "Enter name : ";
00434 cin >> name;
00435 cout << "Enter surname : ";
00436 cin >> surname;
00437 cout << "Enter address : ";
00438 cin >> address;
00439 cout << "Enter city : ";
00440 cin >> city;
00441 cout << "Enter login : ";
00442 cin >> login;
00443 cout << "Enter password: ";
00444 cin >> passwd;
00445
00446 User::factory(name, surname, login, passwd, address, city);
00447 wait();
00448 }