Add private Controller helper method getRestaurantFromRestaurants()
This commit is contained in:
@@ -28,6 +28,44 @@ bool checkAccess(std::shared_ptr<User> user, const std::string& userWithAccess)
|
||||
return true;
|
||||
}
|
||||
|
||||
restaurants::iterator pickRestaurantFromRestaurants(RestaurantOwner& restaurantOwner)
|
||||
{
|
||||
util::clear();
|
||||
int restaurantIndex = 1, restaurantChoiceIndex;
|
||||
restaurants& ownerRestaurants = restaurantOwner.getRestaurants();
|
||||
if (ownerRestaurants.empty())
|
||||
{
|
||||
std::cout << "You do not own any Restaurants!\n";
|
||||
return ownerRestaurants.end();
|
||||
}
|
||||
std::cout << "Pick a Restaurant\n";
|
||||
std::cout << std::left << std::setw(8) << "Index"
|
||||
<< std::left << std::setw(5) << "ID"
|
||||
<< std::left << std::setw(25) << "Name"
|
||||
<< std::left << std::setw(10) << "Status"
|
||||
<< "\n";
|
||||
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())
|
||||
{
|
||||
return ownerRestaurants.end();
|
||||
}
|
||||
restaurants::iterator restaurantIterator = ownerRestaurants.begin();
|
||||
std::advance(restaurantIterator, restaurantChoiceIndex);
|
||||
return restaurantIterator;
|
||||
}
|
||||
|
||||
void FoodDeliveryController::run()
|
||||
{
|
||||
bool isMenuActive = true;
|
||||
@@ -230,40 +268,9 @@ void FoodDeliveryController::updateRestaurantStatus()
|
||||
return;
|
||||
}
|
||||
RestaurantOwner& restaurantOwner = *(std::dynamic_pointer_cast<RestaurantOwner>(m_authenticatedUser));
|
||||
restaurants& ownerRestaurants = restaurantOwner.getRestaurants();
|
||||
int restaurantChoiceIndex;
|
||||
if (ownerRestaurants.empty())
|
||||
restaurants::iterator restaurantIterator = pickRestaurantFromRestaurants(restaurantOwner);
|
||||
if (restaurantIterator != restaurantOwner.getRestaurants().end())
|
||||
{
|
||||
std::cout << "You do not own any Restaurants!\n";
|
||||
return;
|
||||
}
|
||||
std::cout << "Pick a Restaurant to Open/Close\n";
|
||||
std::cout << std::left << std::setw(8) << "Index"
|
||||
<< std::left << std::setw(5) << "ID"
|
||||
<< std::left << std::setw(25) << "Name"
|
||||
<< std::left << std::setw(10) << "Status"
|
||||
<< "\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);
|
||||
@@ -275,6 +282,12 @@ void FoodDeliveryController::updateRestaurantStatus()
|
||||
listRestaurants();
|
||||
std::cout << "Updated Restaurant " << restaurantIterator->second->getName() << " successfully\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Invalid Index. Cannot update Restaurant status!\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::cout << "Exception: " << e.what() << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user