chore: Do exception handling to prevent appliation crash
This commit is contained in:
@@ -14,20 +14,28 @@ void CustomerMenu::showMenu()
|
|||||||
bool isMenuActive = true;
|
bool isMenuActive = true;
|
||||||
while (isMenuActive)
|
while (isMenuActive)
|
||||||
{
|
{
|
||||||
int choice;
|
try
|
||||||
util::clear();
|
|
||||||
std::cout << "Welcome " << m_userFullName << "\n";
|
|
||||||
std::cout << "Customer Menu\n"
|
|
||||||
"1. View My Orders\n"
|
|
||||||
"2. Place Order\n"
|
|
||||||
"3. Cancel Order\n"
|
|
||||||
"4. View My Profile\n"
|
|
||||||
"5. Logout\n"
|
|
||||||
"Choice?: ";
|
|
||||||
util::readValue<int>(choice);
|
|
||||||
if (!handleOperation(choice))
|
|
||||||
{
|
{
|
||||||
isMenuActive = false;
|
int choice;
|
||||||
|
util::clear();
|
||||||
|
std::cout << "Welcome " << m_userFullName << "\n";
|
||||||
|
std::cout << "Customer Menu\n"
|
||||||
|
"1. View My Orders\n"
|
||||||
|
"2. Place Order\n"
|
||||||
|
"3. Cancel Order\n"
|
||||||
|
"4. View My Profile\n"
|
||||||
|
"5. Logout\n"
|
||||||
|
"Choice?: ";
|
||||||
|
util::readValue<int>(choice);
|
||||||
|
if (!handleOperation(choice))
|
||||||
|
{
|
||||||
|
isMenuActive = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "Exception: " << e.what() << std::endl;
|
||||||
|
util::pressEnter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,21 +14,30 @@ void DeliveryPartnerMenu::showMenu()
|
|||||||
bool isMenuActive = true;
|
bool isMenuActive = true;
|
||||||
while (isMenuActive)
|
while (isMenuActive)
|
||||||
{
|
{
|
||||||
int choice;
|
try
|
||||||
util::clear();
|
|
||||||
std::cout << "Welcome " << m_userFullName << "\n";
|
|
||||||
std::cout << "Delivery Partner Menu\n"
|
|
||||||
"1. View My Delivery Jobs\n"
|
|
||||||
"2. Accept Delivery Job\n"
|
|
||||||
"3. Confirm Delivery\n"
|
|
||||||
"4. View My Profile\n"
|
|
||||||
"5. Logout\n"
|
|
||||||
"Choice?: ";
|
|
||||||
util::readValue<int>(choice);
|
|
||||||
if (!handleOperation(choice))
|
|
||||||
{
|
{
|
||||||
isMenuActive = false;
|
int choice;
|
||||||
|
util::clear();
|
||||||
|
std::cout << "Welcome " << m_userFullName << "\n";
|
||||||
|
std::cout << "Delivery Partner Menu\n"
|
||||||
|
"1. View My Delivery Jobs\n"
|
||||||
|
"2. Accept Delivery Job\n"
|
||||||
|
"3. Confirm Delivery\n"
|
||||||
|
"4. View My Profile\n"
|
||||||
|
"5. Logout\n"
|
||||||
|
"Choice?: ";
|
||||||
|
util::readValue<int>(choice);
|
||||||
|
if (!handleOperation(choice))
|
||||||
|
{
|
||||||
|
isMenuActive = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "Exception: " << e.what() << std::endl;
|
||||||
|
util::pressEnter();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,31 +33,39 @@ void FoodDeliveryController::run()
|
|||||||
bool isMenuActive = true;
|
bool isMenuActive = true;
|
||||||
while (isMenuActive)
|
while (isMenuActive)
|
||||||
{
|
{
|
||||||
char choice;
|
int choice;
|
||||||
util::clear();
|
util::clear();
|
||||||
std::cout << "Food Delivery App\n"
|
std::cout << "Food Delivery App\n"
|
||||||
"1. Login\n"
|
"1. Login\n"
|
||||||
"2. Register\n"
|
"2. Register\n"
|
||||||
"3. Exit\n"
|
"3. Exit\n"
|
||||||
"Choice?: ";
|
"Choice?: ";
|
||||||
util::readValue<char>(choice);
|
try
|
||||||
if (choice == '1')
|
|
||||||
{
|
{
|
||||||
login();
|
util::readValue<int>(choice);
|
||||||
}
|
if (choice == 1)
|
||||||
else if (choice == '2')
|
{
|
||||||
|
login();
|
||||||
|
}
|
||||||
|
else if (choice == 2)
|
||||||
|
{
|
||||||
|
registerUser();
|
||||||
|
}
|
||||||
|
else if (choice == 3)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Invalid Choice!\n";
|
||||||
|
}
|
||||||
|
util::pressEnter();
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
registerUser();
|
std::cout << "Exception: " << e.what() << std::endl;
|
||||||
|
util::pressEnter();
|
||||||
}
|
}
|
||||||
else if (choice == '3')
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << "Invalid Choice!\n";
|
|
||||||
}
|
|
||||||
util::pressEnter();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,54 +117,61 @@ void FoodDeliveryController::login()
|
|||||||
|
|
||||||
void FoodDeliveryController::registerUser()
|
void FoodDeliveryController::registerUser()
|
||||||
{
|
{
|
||||||
std::string username, password, phone, name, email;
|
try
|
||||||
util::clear();
|
|
||||||
std::cout << "Enter Username: ";
|
|
||||||
util::readString(username);
|
|
||||||
if (m_users.find(username) != m_users.end())
|
|
||||||
{
|
{
|
||||||
std::cout << "User Already Exists!\n";
|
std::string username, password, phone, name, email;
|
||||||
return;
|
util::clear();
|
||||||
}
|
std::cout << "Enter Username: ";
|
||||||
std::cout << "Enter Password: ";
|
util::readString(username);
|
||||||
util::readString(password);
|
if (m_users.find(username) != m_users.end())
|
||||||
std::cout << "Enter Full Name: ";
|
|
||||||
util::readString(name);
|
|
||||||
std::cout << "Enter Phone: ";
|
|
||||||
util::readString(phone);
|
|
||||||
std::cout << "Enter Email: ";
|
|
||||||
util::readString(email);
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
int choice;
|
|
||||||
std::cout << "Select User Type\n"
|
|
||||||
"1. Restaurant Owner\n"
|
|
||||||
"2. Customer\n"
|
|
||||||
"3. Delivery Partner\n"
|
|
||||||
"4. Cancel\n"
|
|
||||||
"Choice?: ";
|
|
||||||
util::readValue<int>(choice);
|
|
||||||
switch (choice)
|
|
||||||
{
|
{
|
||||||
case 1:
|
std::cout << "User Already Exists!\n";
|
||||||
m_users[username] = std::make_shared<RestaurantOwner>(username, name, phone, password, email);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
m_users[username] = std::make_shared<Customer>(username, name, phone, password, email);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
m_users[username] = std::make_shared<DeliveryPartner>(username, name, phone, password, email);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
return;
|
return;
|
||||||
default:
|
|
||||||
util::clear();
|
|
||||||
std::cout << "Invalid Choice! Try Again\n";
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
break;
|
std::cout << "Enter Password: ";
|
||||||
}
|
util::readString(password);
|
||||||
std::cout << "User Registration Successful\n";
|
std::cout << "Enter Full Name: ";
|
||||||
|
util::readString(name);
|
||||||
|
std::cout << "Enter Phone: ";
|
||||||
|
util::readString(phone);
|
||||||
|
std::cout << "Enter Email: ";
|
||||||
|
util::readString(email);
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int choice;
|
||||||
|
std::cout << "Select User Type\n"
|
||||||
|
"1. Restaurant Owner\n"
|
||||||
|
"2. Customer\n"
|
||||||
|
"3. Delivery Partner\n"
|
||||||
|
"4. Cancel\n"
|
||||||
|
"Choice?: ";
|
||||||
|
util::readValue<int>(choice);
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
m_users[username] = std::make_shared<RestaurantOwner>(username, name, phone, password, email);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
m_users[username] = std::make_shared<Customer>(username, name, phone, password, email);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
m_users[username] = std::make_shared<DeliveryPartner>(username, name, phone, password, email);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
util::clear();
|
||||||
|
std::cout << "Invalid Choice! Try Again\n";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "User Registration Successful\n";
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "Exception: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FoodDeliveryController::listRestaurants()
|
void FoodDeliveryController::listRestaurants()
|
||||||
@@ -207,56 +222,63 @@ void FoodDeliveryController::addNewRestaurant()
|
|||||||
|
|
||||||
void FoodDeliveryController::updateRestaurantStatus()
|
void FoodDeliveryController::updateRestaurantStatus()
|
||||||
{
|
{
|
||||||
util::clear();
|
try
|
||||||
if (!checkAccess(m_authenticatedUser, "RestaurantOwner"))
|
|
||||||
{
|
{
|
||||||
return;
|
util::clear();
|
||||||
}
|
if (!checkAccess(m_authenticatedUser, "RestaurantOwner"))
|
||||||
RestaurantOwner& restaurantOwner = *(std::dynamic_pointer_cast<RestaurantOwner>(m_authenticatedUser));
|
{
|
||||||
restaurants& ownerRestaurants = restaurantOwner.getRestaurants();
|
return;
|
||||||
int restaurantChoiceIndex;
|
}
|
||||||
if (ownerRestaurants.empty())
|
RestaurantOwner& restaurantOwner = *(std::dynamic_pointer_cast<RestaurantOwner>(m_authenticatedUser));
|
||||||
{
|
restaurants& ownerRestaurants = restaurantOwner.getRestaurants();
|
||||||
std::cout << "You do not own any Restaurants!\n";
|
int restaurantChoiceIndex;
|
||||||
return;
|
if (ownerRestaurants.empty())
|
||||||
}
|
{
|
||||||
std::cout << "Pick a Restaurant to Open/Close\n";
|
std::cout << "You do not own any Restaurants!\n";
|
||||||
std::cout << std::left << std::setw(8) << "Index"
|
return;
|
||||||
<< std::left << std::setw(5) << "ID"
|
}
|
||||||
<< std::left << std::setw(25) << "Name"
|
std::cout << "Pick a Restaurant to Open/Close\n";
|
||||||
<< std::left << std::setw(10) << "Status"
|
std::cout << std::left << std::setw(8) << "Index"
|
||||||
<< "\n";
|
<< std::left << std::setw(5) << "ID"
|
||||||
int restaurantIndex = 1;
|
<< std::left << std::setw(25) << "Name"
|
||||||
for (restaurants::iterator restaurantIterator = ownerRestaurants.begin(); restaurantIterator != ownerRestaurants.end(); restaurantIterator++, restaurantIndex++)
|
<< std::left << std::setw(10) << "Status"
|
||||||
{
|
|
||||||
auto& restaurant = *(restaurantIterator->second);
|
|
||||||
std::cout
|
|
||||||
<< std::left << std::setw(8) << restaurantIndex
|
|
||||||
<< std::left << std::setw(5) << restaurant.getId()
|
|
||||||
<< std::left << std::setw(25) << restaurant.getName()
|
|
||||||
<< std::left << std::setw(10) << (restaurant.getStatus() ? "Open" : "Closed")
|
|
||||||
<< "\n";
|
<< "\n";
|
||||||
|
int restaurantIndex = 1;
|
||||||
|
for (restaurants::iterator restaurantIterator = ownerRestaurants.begin(); restaurantIterator != ownerRestaurants.end(); restaurantIterator++, restaurantIndex++)
|
||||||
|
{
|
||||||
|
auto& restaurant = *(restaurantIterator->second);
|
||||||
|
std::cout
|
||||||
|
<< std::left << std::setw(8) << restaurantIndex
|
||||||
|
<< std::left << std::setw(5) << restaurant.getId()
|
||||||
|
<< std::left << std::setw(25) << restaurant.getName()
|
||||||
|
<< std::left << std::setw(10) << (restaurant.getStatus() ? "Open" : "Closed")
|
||||||
|
<< "\n";
|
||||||
|
}
|
||||||
|
std::cout << "\nSelect Index: ";
|
||||||
|
util::readValue<int>(restaurantChoiceIndex);
|
||||||
|
restaurantChoiceIndex--;
|
||||||
|
if (restaurantChoiceIndex < 0 || restaurantChoiceIndex >= ownerRestaurants.size())
|
||||||
|
{
|
||||||
|
std::cout << "Invalid Index. Cannot update Restaurant status!\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
restaurants::iterator restaurantIterator = ownerRestaurants.begin();
|
||||||
|
std::advance(restaurantIterator, restaurantChoiceIndex);
|
||||||
|
if (restaurantIterator->second->getStatus() == true)
|
||||||
|
{
|
||||||
|
restaurantIterator->second->setStatus(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
restaurantIterator->second->setStatus(true);
|
||||||
|
}
|
||||||
|
listRestaurants();
|
||||||
|
std::cout << "Updated Restaurant " << restaurantIterator->second->getName() << " successfully\n";
|
||||||
}
|
}
|
||||||
std::cout << "\nSelect Index: ";
|
catch (const std::exception& e)
|
||||||
util::readValue<int>(restaurantChoiceIndex);
|
|
||||||
restaurantChoiceIndex--;
|
|
||||||
if (restaurantChoiceIndex < 0 || restaurantChoiceIndex >= ownerRestaurants.size())
|
|
||||||
{
|
{
|
||||||
std::cout << "Invalid Index. Cannot update Restaurant status!\n";
|
std::cout << "Exception: " << e.what() << std::endl;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
restaurants::iterator restaurantIterator = ownerRestaurants.begin();
|
|
||||||
std::advance(restaurantIterator, restaurantChoiceIndex);
|
|
||||||
if (restaurantIterator->second->getStatus() == true)
|
|
||||||
{
|
|
||||||
restaurantIterator->second->setStatus(false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
restaurantIterator->second->setStatus(true);
|
|
||||||
}
|
|
||||||
listRestaurants();
|
|
||||||
std::cout << "Updated Restaurant " << restaurantIterator->second->getName() << " successfully\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FoodDeliveryController::listRestaurantOrders()
|
void FoodDeliveryController::listRestaurantOrders()
|
||||||
|
|||||||
@@ -14,25 +14,33 @@ void RestaurantOwnerMenu::showMenu()
|
|||||||
bool isMenuActive = true;
|
bool isMenuActive = true;
|
||||||
while (isMenuActive)
|
while (isMenuActive)
|
||||||
{
|
{
|
||||||
int choice;
|
try
|
||||||
util::clear();
|
|
||||||
std::cout << "Welcome " << m_userFullName << "\n";
|
|
||||||
std::cout << "Restaurant Owner Menu\n"
|
|
||||||
"1. List My Restaurants\n"
|
|
||||||
"2. Add New Restaurant\n"
|
|
||||||
"3. Update Restaurant Status\n"
|
|
||||||
"4. List Orders\n"
|
|
||||||
"5. Mark Order Ready for Delivery\n"
|
|
||||||
"6. List Menu Items\n"
|
|
||||||
"7. Add New Item to Menu\n"
|
|
||||||
"8. Remove Item from Menu\n"
|
|
||||||
"9. View My Profile\n"
|
|
||||||
"10. Logout\n"
|
|
||||||
"Choice?: ";
|
|
||||||
util::readValue<int>(choice);
|
|
||||||
if (!handleOperation(choice))
|
|
||||||
{
|
{
|
||||||
isMenuActive = false;
|
int choice;
|
||||||
|
util::clear();
|
||||||
|
std::cout << "Welcome " << m_userFullName << "\n";
|
||||||
|
std::cout << "Restaurant Owner Menu\n"
|
||||||
|
"1. List My Restaurants\n"
|
||||||
|
"2. Add New Restaurant\n"
|
||||||
|
"3. Update Restaurant Status\n"
|
||||||
|
"4. List Orders\n"
|
||||||
|
"5. Mark Order Ready for Delivery\n"
|
||||||
|
"6. List Menu Items\n"
|
||||||
|
"7. Add New Item to Menu\n"
|
||||||
|
"8. Remove Item from Menu\n"
|
||||||
|
"9. View My Profile\n"
|
||||||
|
"10. Logout\n"
|
||||||
|
"Choice?: ";
|
||||||
|
util::readValue<int>(choice);
|
||||||
|
if (!handleOperation(choice))
|
||||||
|
{
|
||||||
|
isMenuActive = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "Exception: " << e.what() << std::endl;
|
||||||
|
util::pressEnter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user