Question

In: Computer Science

C++ please You have been challenged with a menu program before, but this one is a...

C++ please

You have been challenged with a menu program before, but this one is a little more complex. You will use loops and you will use an output file for a receipt.

Using class notes, write an algorithm for a program which uses a loop system to allow customers to select items for purchase from a list on the screen. (List at least 8 items to choose from on your menu. You choose the items to be listed.).

When the user is finished choosing items, display the amount due and then accept the customer's payment. If the customer pays enough, display change due on the screen.

If the customer does not pay enough, use another loop system to get additional payment from the customer until the total due has been paid. When finished, have an output file that lists all items purchased along with prices, the subtotal, the tax amount, the grand total and total paid.

Solutions

Expert Solution

// Some standard headers
#include <iostream> // std::cin, std::cout
#include <fstream> // std::ofstream
#include <cstdio> // stricmp()
#include <iomanip> // std::setw(), std::setfill()
#include <string> // std::string
#include <cctype> // isdigit()
#include <vector> // std::vector
#include <cstdlib> // system(), itoa(), rand(), srand()
#include <conio.h> // getchr()
#include <ctime> // time()
#include <sstream> // std::stringstream
#include <cstring> // stricmp()
#include <windows.h> // GetStdHandle(), SetConsoleTextAttribute()
#include <algorithm> // std::reverse()

using namespace std;

int stricmp(const char * pStr1, const char *pStr2)
{
char c1, c2;
int v;

do {
c1 = *pStr1++;
c2 = *pStr2++;
/* The casts are necessary when pStr1 is shorter & char is signed */
v = (unsigned int) tolower(c1) - (unsigned int) tolower(c2);
} while ((v == 0) && (c1 != '\0') && (c2 != '\0') );

return v;
}

char *itoa(int i, char *buffer, int n = 10)
{
std::string str;
do
{
str += (char)('0' + (i % 10)); i /= 10;
}while(i != 0);
  
std::reverse(str.begin(), str.end());

strcpy(buffer, str.c_str());
return buffer;
}

// This code works on Windows only. In case if the program needs to be run on another platform, we can easily change the function and that's it.

enum
{
   Color_White = 7,
   Color_Yellow = 14,
   Color_Purple = 13,
   Color_Green = 10,
   Color_Bright_White = 15,
   Color_Light_Blue = 11,
   Color_Pink_Blue_Background = 23,
   Color_Red = 12
};

static std::ostream& setConsoleTextColor(int myColor)
{
   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole,myColor);

   return cout;
}

// Some constants
const int no_of_items = 12;
const int ZERO_FOR_CAL = 0; // Zero for cal(calculation)
const int ONE_FOR_CAL = 1;
const int TWO_FOR_CAL = 2;
const int THREE_FOR_CAL = 3;
const int FOUR_FOR_CAL = 4;

const int MINUS_ONE_FOR_CAL = -1;

// General item structure for all hotes
struct Item_Raw
{
   char name[100];
   char description[500];
   double price;  
};

// Global hotel Appetizer Items
enum
{
   Item_Shev_puri = 0,
   Item_Bhel_puri,
   Item_dabeli
};

// Global hotel Main Dishes Items
enum
{
   Item_Naan = Item_dabeli + 1,
   Item_kebabs,
   Item_Hakka_noodles,
};

// Global hotel Beverages Items
enum
{
   Item_Mirinda = Item_Hakka_noodles + 1,
   Item_Sprite,
   Item_Nimbu_Pani
};

// Global hotel Desserts Items
enum
{
   Item_Ice_cream = Item_Nimbu_Pani + 1,
   Item_Gulab_jamun,
   Item_Gajar_ka_halwa
};


// Quick-initialization can be done if we use a char array instead of a std::string
Item_Raw globalhotelItems[no_of_items] = {
// hotel Appetizer
   {
       "Shev puri",
       6.50
   },
   {
       "Bhel puri",
       5.50
   },
   {
       "dabeli",
       6.50
   },

// hotel Main Dishes
   {
       "Naan",
       10.50
   },
   {
       "kebabs",
       7.50
   },
   {
       "Hakka noodles",
       14.50
   },

// hotel Beverages
   {
       "Mirinda",
       4.50
   },
   {
       "Sprite",
       2.50
   },
   {
       "Nimbu Pani",
       3.50
   },

// hotel Desserts
   {
       "Ice cream",
       4.50
   },
   {
       "Gulab jamun",
       4.50
   },
   {
       "Gajar ka halwa",
       4.50
   }
};


// Some constant
const double tax_of_the_hotel = 0.0825;

// An item structure for an user-defined hotel
class Item
{
private :
   std::string name;
   std::string description;
   double price;

public :
   void importItem(const Item_Raw &item);

   void setName(std::string _name);

   void setDescription(std::string _description);

   void setPrice(double _price);

   void setItem(std::string _name, std::string _description, double _price);

   std::string getName() const;

   std::string getDescription() const;

   double getPrice() const;
};

// User-defined hotel items
enum hotelAppetizer
{
   hotel_Appetizer_1,
   hotel_Appetizer_2,
   hotel_Appetizer_3
};

enum hotelMainDishes
{
   hotel_Main_Dishes_1,
   hotel_Main_Dishes_2,
   hotel_Main_Dishes_3
};

enum hotelBeverages
{
   hotel_Beverages_1,
   hotel_Beverages_2,
   hotel_Beverages_3
};

enum hotelDesserts
{
   hotel_Desserts_1,
   hotel_Desserts_2,
   hotel_Desserts_3
};

// User-defined hotel constants
const int hotel_APPETIZER_ITEMS = 3;
const int hotel_MAIN_DISHES_ITEMS = 3;
const int hotel_BEVERAGES_ITEMS = 3;
const int hotel_DESSERTS_ITEMS = 3;

class hotel
{
private :
   Item hotelAppetizerItems[hotel_APPETIZER_ITEMS];
   Item hotelMainDishesItems[hotel_MAIN_DISHES_ITEMS];
   Item hotelBeveragesItems[hotel_BEVERAGES_ITEMS];
   Item hotelDessertsItems[hotel_DESSERTS_ITEMS];
      
   public :
      
   // The recpt structure for the user-defined hotel
   struct recpt
   {
       recpt()
       {
           totalPrice = 0;
           taxPrice = 0;
           gratuity = 0;          
       }

       vector<Item> itemsOrd;

       double totalPrice;
       double taxPrice;
       double gratuity;
   };

   // Default constructor
   hotel();

   // Default constructor will also call this function
   void initialize();

   // Show hotel menu (If bHelp is toggled, the hotel will show the customer how food items are Ord)
   void showMenu(bool bHelp = false);

   // Show hotel help
   void showHelp();

   // Validate the customer's choice
   // Returns -1 if fails, 0 if no action should be taken, and 1 if successful
   int getInput(Item &theItem);

   // Formatting function
   static std::string printName(const std::string &s, int len = 0);

   // Generates a Html recpt
   static std::string generaterecptHtml(const recpt &myrecpt);

   // A customer has entered the hotel. Run the hotel
   void runhotel();

private :

   // A helper function for hotel::getInput()
   // Return 0 if successful, 1 to proceed to order, -1 if fails
   int getDescription(unsigned int category, unsigned int item_id);

   // A helper function for hotel::getInput()
   // Return 1 if successful, -1 if fails
   int getPrice(unsigned int category, unsigned int item_id, Item &theItem);

   // A helper function for hotel::getInput()
   // Return false (0) if no action should be taken, and return true (1) otherwise
   static int descriptionOrder(const Item &item);

   // A helper function for hotel::getInput() and hotel::getPrice()
   // This indicates that an order is completed.
   static void completeOrder(const Item &item, Item &theItem);
};

   void Item::importItem(const Item_Raw &item)
   {
       setName(item.name);
       setDescription(item.description);
       setPrice(item.price);
   }

   void Item::setName(std::string _name)
   {
       name = _name;
   }

   void Item::setDescription(std::string _description)
   {
       description = _description;
   }

   void Item::setPrice(double _price)
   {
       price = _price;
   }

   void Item::setItem(std::string _name, std::string _description, double _price)
   {
       setName(_name);
       setDescription(_description);
       setPrice(_price);
   }

   std::string Item::getName() const
   {
       return name;
   }

   std::string Item::getDescription() const
   {
       return description;
   }

   double Item::getPrice() const
   {
       return price;
   }

   hotel::hotel() {initialize();}

   void hotel::initialize()
   {
       hotelAppetizerItems[hotel_Appetizer_1].importItem(globalhotelItems[Item_Shev_puri]);
       hotelAppetizerItems[hotel_Appetizer_2].importItem(globalhotelItems[Item_Bhel_puri]);
       hotelAppetizerItems[hotel_Appetizer_3].importItem(globalhotelItems[Item_dabeli]);

       hotelMainDishesItems[hotel_Main_Dishes_1].importItem(globalhotelItems[Item_Naan]);
       hotelMainDishesItems[hotel_Main_Dishes_2].importItem(globalhotelItems[Item_kebabs]);
       hotelMainDishesItems[hotel_Main_Dishes_3].importItem(globalhotelItems[Item_Hakka_noodles]);

       hotelBeveragesItems[hotel_Beverages_1].importItem(globalhotelItems[Item_Mirinda]);
       hotelBeveragesItems[hotel_Beverages_2].importItem(globalhotelItems[Item_Sprite]);
       hotelBeveragesItems[hotel_Beverages_3].importItem(globalhotelItems[Item_Nimbu_Pani]);

       hotelDessertsItems[hotel_Desserts_1].importItem(globalhotelItems[Item_Ice_cream]);
       hotelDessertsItems[hotel_Desserts_2].importItem(globalhotelItems[Item_Gulab_jamun]);
       hotelDessertsItems[hotel_Desserts_3].importItem(globalhotelItems[Item_Gajar_ka_halwa]);
   }

   // This function guarantees all the strings will have the same length, making it easier to format the output
   std::string hotel::printName(const std::string &s, int len)
   {
       int i;
       std::string str = s;

       len -= s.size();
       for(i = 0; i < len; i++) str += ' ';
      
       return str;
   }

   void hotel::showMenu(bool bHelp)
   {
       int i;
       int len;
       const int len_max = 65;

       system("cls");
       cout << setfill(' ');
       setConsoleTextColor(Color_Bright_White) << setw((len_max + 10) / 2 + (len_max + 10) / 4) << "Sabor Intenso hotel Menu : " << endl;
       setConsoleTextColor(Color_White) << endl;

       cout.setf(ios::fixed);
       cout.precision(2);

       len = hotelAppetizerItems[0].getName().size();
       for(i = 1; i < hotel_APPETIZER_ITEMS; i++)
       {
           if(len < hotelAppetizerItems[i].getName().size()) len = hotelAppetizerItems[i].getName().size();
       }
       len += 1;
      
       cout << setfill('-');

       setConsoleTextColor(Color_Yellow) << "A";
       setConsoleTextColor(Color_White) << ". ";
       setConsoleTextColor(Color_Pink_Blue_Background) << "Appretizer";
       setConsoleTextColor(Color_White) << " : " << endl;

       for(i = 0; i < hotel_APPETIZER_ITEMS; i++)
       {
           setConsoleTextColor(Color_Purple) << " " << i + 1 << ". ";
           setConsoleTextColor(Color_White) << printName(hotelAppetizerItems[i].getName(), len) << setw(len_max - len);
           setConsoleTextColor(Color_Yellow) << "$ " << hotelAppetizerItems[i].getPrice() << endl;
       }
       cout << endl;

       len = hotelMainDishesItems[0].getName().size();
       for(i = 1; i < hotel_MAIN_DISHES_ITEMS; i++)
       {
           if(len < hotelMainDishesItems[i].getName().size()) len = hotelMainDishesItems[i].getName().size();
       }
       len += 1;
      
       cout << setfill('-');

       setConsoleTextColor(Color_Yellow) << "B";
       setConsoleTextColor(Color_White) << ". ";
       setConsoleTextColor(Color_Pink_Blue_Background) << "Main Dishes";
       setConsoleTextColor(Color_White) << " : " << endl;

       for(i = 0; i < hotel_MAIN_DISHES_ITEMS; i++)
       {
           setConsoleTextColor(Color_Purple) << " " << i + 1 << ". ";
           setConsoleTextColor(Color_White) << printName(hotelMainDishesItems[i].getName(), len) << setw(len_max - len);
           setConsoleTextColor(Color_Green) << "$ " << hotelMainDishesItems[i].getPrice() << endl;
       }
       cout << endl;

       len = hotelBeveragesItems[0].getName().size();
       for(i = 1; i < hotel_BEVERAGES_ITEMS; i++)
       {
           if(len < hotelBeveragesItems[i].getName().size()) len = hotelBeveragesItems[i].getName().size();
       }
       len += 1;
      
       cout << setfill('-');

       setConsoleTextColor(Color_Yellow) << "C";
       setConsoleTextColor(Color_White) << ". ";
       setConsoleTextColor(Color_Pink_Blue_Background) << "Beverages";
       setConsoleTextColor(Color_White) << " : " << endl;

       for(i = 0; i < hotel_BEVERAGES_ITEMS; i++)
       {
           setConsoleTextColor(Color_Purple) << " " << i + 1 << ". ";
           setConsoleTextColor(Color_White) << printName(hotelBeveragesItems[i].getName(), len) << setw(len_max - len);
           setConsoleTextColor(Color_Yellow) << "$ " << hotelBeveragesItems[i].getPrice() << endl;
       }
       cout << endl;

       len = hotelDessertsItems[0].getName().size();
       for(i = 1; i < hotel_DESSERTS_ITEMS; i++)
       {
           if(len < hotelDessertsItems[i].getName().size()) len = hotelDessertsItems[i].getName().size();
       }
       len += 1;
      
       cout << setfill('-');

       setConsoleTextColor(Color_Yellow) << "D";
       setConsoleTextColor(Color_White) << ". ";
       setConsoleTextColor(Color_Pink_Blue_Background) << "Desserts";
       setConsoleTextColor(Color_White) << " : " << endl;

       for(i = 0; i < hotel_DESSERTS_ITEMS; i++)
       {
           setConsoleTextColor(Color_Purple) << " " << i + 1 << ". ";
           setConsoleTextColor(Color_White) << printName(hotelDessertsItems[i].getName(), len) << setw(len_max - len);
           setConsoleTextColor(Color_Green) << "$ " << hotelDessertsItems[i].getPrice() << endl;
       }
       cout << endl;

       setConsoleTextColor(Color_White);


       if(bHelp == true) showHelp();
   }

   void hotel::showHelp()
   {
       cout << "How to order an item : \n";
       cout << "+ ";
       setConsoleTextColor(Color_Purple) << 'A';
       setConsoleTextColor(Color_White) << ", ";
       setConsoleTextColor(Color_Purple) << 'B';
       setConsoleTextColor(Color_White) << ", ";
       setConsoleTextColor(Color_Purple) << 'C';
       setConsoleTextColor(Color_White) << ", or ";
       setConsoleTextColor(Color_Purple) << "D ";
       setConsoleTextColor(Color_White) << "to select a category, and ";
      
       setConsoleTextColor(Color_Purple) << '1';
       setConsoleTextColor(Color_White) << ", ";
       setConsoleTextColor(Color_Purple) << '2';
       setConsoleTextColor(Color_White) << ", or ";
       setConsoleTextColor(Color_Purple) << "3 ";
       setConsoleTextColor(Color_White) << "to order the desired item\n";

       switch(rand() % 2)
       {
           case 0 :
           {
               cout << "+ E.g : If you choose ";
               setConsoleTextColor(Color_Bright_White) << "C2";
               setConsoleTextColor(Color_White) << ", you are ordering the '";

               setConsoleTextColor(Color_Green) << "Sprite";
               setConsoleTextColor(Color_White) << "' item\n\n";
           }
           break;
           default :
           {
               cout << "+ E.g : If you choose ";
               setConsoleTextColor(Color_Bright_White) << "B2";
               setConsoleTextColor(Color_White) << ", you are ordering the '";

               setConsoleTextColor(Color_Green) << "kebabs";
               setConsoleTextColor(Color_White) << "' item\n\n";
           }
           break;
       }

       cout << "How to get food description : \n";
       cout << "+ Append a '";

       setConsoleTextColor(Color_Purple) << 'H';
       setConsoleTextColor(Color_White) << "' character at the end of your choice to get description\n";
       cout << "+ For example, to get the food description of the item '";

       setConsoleTextColor(Color_Green) << "dabeli";
       setConsoleTextColor(Color_White) << "', type '";
       setConsoleTextColor(Color_Bright_White) << "A3H";
       setConsoleTextColor(Color_White) << "'\n\n";  
   }

   void hotel::completeOrder(const Item &item, Item &theItem)
   {

       cout << "+ You have successfully Ord : '";
       setConsoleTextColor(Color_Green) << item.getName();
       setConsoleTextColor(Color_White) << "'" << endl;
       setConsoleTextColor(Color_White) << "+ Your order's price : ";
       setConsoleTextColor(Color_Light_Blue) << item.getPrice() << "$";
       setConsoleTextColor(Color_White) << endl;

       theItem = item;
   }

   // Return false (0) if no action should be taken, and return true (1) otherwise
   int hotel::descriptionOrder(const Item &item)
   {
       std::string choice;

       cout << "+ The item : '";
       setConsoleTextColor(Color_Green) << item.getName();
       setConsoleTextColor(Color_White) << "'" << endl;
       cout << " " << item.getDescription() << endl;      
       cout << "+ Item price : ";
       setConsoleTextColor(Color_Light_Blue) << item.getPrice() << "$";
       setConsoleTextColor(Color_White) << endl << endl;

       // Random response may be a good feature
       if(rand() % 100 >= 50)
       {
           cout << "Do you want to order this one? (";
           setConsoleTextColor(Color_Yellow) << "Y";
           setConsoleTextColor(Color_White) << " to proceed) : ";
          
           setConsoleTextColor(Color_Yellow);
           cin >> choice;
           setConsoleTextColor(Color_White);
       }
       else
       {
           cout << "Would you like to order this one? (";
           setConsoleTextColor(Color_Yellow) << "Y";
           setConsoleTextColor(Color_White) << " to proceed) : ";
          
           setConsoleTextColor(Color_Yellow);
           cin >> choice;
           setConsoleTextColor(Color_White);
       }

       if(stricmp(choice.c_str(), "Y") == ZERO_FOR_CAL || stricmp(choice.c_str(), "Yes") == ZERO_FOR_CAL)
       {
           // Proceed to order
           return ONE_FOR_CAL;
       }
       else if(stricmp(choice.c_str(), "N") == ZERO_FOR_CAL || stricmp(choice.c_str(), "No") == ZERO_FOR_CAL)
       {
           cout << endl;
           cout << "+ You chose not to order the '";
           setConsoleTextColor(Color_Green) << item.getName();
           setConsoleTextColor(Color_White) << "' item" << endl;
           cout << "+ Please press any key to resume your ordering. . ."; getch();
       }

       // Don't want to order yet
       return ZERO_FOR_CAL;
   }


   // Return 0 if successful, 1 to proceed to order, -1 if fails
   int hotel::getDescription(unsigned int category, unsigned int item_id)
   {
       switch(category)
       {
           case ZERO_FOR_CAL :
           {
               if(item_id >= hotel_APPETIZER_ITEMS)
               {
                   cout << "+ You have selected the wrong food item. Please try again." << endl;
                   cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
               }
               return descriptionOrder(hotelAppetizerItems[item_id]);
           }
           break;
           case ONE_FOR_CAL :
           {
               if(item_id >= hotel_MAIN_DISHES_ITEMS)
               {
                   cout << "+ You have selected the wrong food item. Please try again." << endl;
                   cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
               }
               return descriptionOrder(hotelMainDishesItems[item_id]);
           }
           break;
           case TWO_FOR_CAL :
           {
               if(item_id >= hotel_BEVERAGES_ITEMS)
               {
                   cout << "+ You have selected the wrong food item. Please try again." << endl;
                   cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
               }
               return descriptionOrder(hotelBeveragesItems[item_id]);
           }
           break;
           case THREE_FOR_CAL :
           {
               if(item_id >= hotel_DESSERTS_ITEMS)
               {
                   cout << "+ You have selected the wrong food item. Please try again." << endl;
                   cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
               }
               return descriptionOrder(hotelDessertsItems[item_id]);
           }
           break;
       }

       return 0;
   }

   // Return 1 if successful, -1 if fails
   int hotel::getPrice(unsigned int category, unsigned int item_id, Item &theItem)
   {
       switch(category)
       {
           case ZERO_FOR_CAL :
           {
               if(item_id >= hotel_APPETIZER_ITEMS)
               {
                   cout << "+ You have selected the wrong food item. Please try again." << endl;
                   cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
               }
               completeOrder(hotelAppetizerItems[item_id], theItem);
           }
           break;
           case ONE_FOR_CAL :
           {
               if(item_id >= hotel_MAIN_DISHES_ITEMS)
               {
                   cout << "+ You have selected the wrong food item. Please try again." << endl;
                   cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
               }
               completeOrder(hotelMainDishesItems[item_id], theItem);
           }
           break;
           case TWO_FOR_CAL :
           {
               if(item_id >= hotel_BEVERAGES_ITEMS)
               {
                   cout << "+ You have selected the wrong food item. Please try again." << endl;
                   cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
               }
               completeOrder(hotelBeveragesItems[item_id], theItem);
           }
           break;
           case THREE_FOR_CAL :
           {
               if(item_id >= hotel_DESSERTS_ITEMS)
               {
                   cout << "+ You have selected the wrong food item. Please try again." << endl;
                   cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
               }
               completeOrder(hotelDessertsItems[item_id], theItem);
           }
           break;
       }

       return 1;
   }

   // Returns -1 if fails, 0 if no action is taken, and 1 if successful
   int hotel::getInput(Item &theItem)
   {
       int retValue;
       unsigned int item_id = ZERO_FOR_CAL;
       std::string choice;

       cout << "What do you want to order : ";
       switch(rand() % 4)
       {
           case 0 : setConsoleTextColor(Color_Bright_White); break;
           case 1 : setConsoleTextColor(Color_Yellow); break;
           case 2 : setConsoleTextColor(Color_Light_Blue); break;
           default : setConsoleTextColor(Color_Purple); break;
       }
       cin >> choice;

       setConsoleTextColor(Color_White);

       if(choice.size() > THREE_FOR_CAL)
       {
           cout << "+ You have made an invald choice. Please try again." << endl;
           cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
       }

       if(isdigit(choice[ONE_FOR_CAL]))
       {
           item_id = (choice[ONE_FOR_CAL] - '0');
       }
       else
       {
           cout << "+ You have made an invald choice. Please try again." << endl;
           cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
       }

       if(choice.size() == THREE_FOR_CAL)
       {
           if(toupper(choice[TWO_FOR_CAL]) != 'H')
           {
               cout << "+ You have made an invald choice. Please try again." << endl;
               cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;  
           }
           else
           {
               switch(toupper(choice[ZERO_FOR_CAL]))
               {
                   case 'A' : retValue = getDescription(ZERO_FOR_CAL, item_id - 1); break;
                   case 'B' : retValue = getDescription(ONE_FOR_CAL, item_id - 1); break;
                   case 'C' : retValue = getDescription(TWO_FOR_CAL, item_id - 1); break;
                   case 'D' : retValue = getDescription(THREE_FOR_CAL, item_id - 1); break;
                   default :
                       cout << "+ You have made an invalid choice. Please try again." << endl;
                       cout << "+ Please press any key to resume your ordering. . ."; getch(); retValue = MINUS_ONE_FOR_CAL;
                   break;
               }
               if(retValue <= ZERO_FOR_CAL) return retValue;
           }
       }


       switch(toupper(choice[ZERO_FOR_CAL]))
       {
           case 'A' : return getPrice(ZERO_FOR_CAL, item_id - 1, theItem); break;
           case 'B' : return getPrice(ONE_FOR_CAL, item_id - 1, theItem); break;
           case 'C' : return getPrice(TWO_FOR_CAL, item_id - 1, theItem); break;
           case 'D' : return getPrice(THREE_FOR_CAL, item_id - 1, theItem); break;
           default :
               cout << "+ You have made an invalid choice. Please try again." << endl;
               cout << "+ Please press any key to resume your ordering. . ."; getch(); return MINUS_ONE_FOR_CAL;
           break;
       }
       return ONE_FOR_CAL;
   }

   std::string hotel::generaterecptHtml(const recpt &myrecpt)
   {
       int i;
       char buffer[300];
       stringstream ss;
       std::string fileName = "recpt_ID";

       int file_id = 1000 + rand() % 9000;

       fileName += (itoa(file_id, buffer, 10), buffer);
       fileName += ".html";

       ofstream myFile(fileName.c_str());

       myFile << "<html>\n";
       myFile << "<head>\n";

       myFile << "<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\" />\n";
       myFile << "<style type=\"text/css\">\n";
       myFile << "td, th {border: 1px solid black;}\n";
       myFile << "</style>\n";

       myFile << "<title>" << "Sabor Intenso hotel recpt" << "</title>\n";
       myFile << "</head>\n";

       myFile << "<body background= \"bg_001.jpg\">\n";
       myFile << "<font face=\"verdana\" size=\"6\">" << "<center>" << "Sabor Intenso hotel recpt" << "</center>" << "</font>" ;

       myFile << "<br>\n";

       myFile << "<font face=\"verdana\" size=\"4\">";

       myFile << "<table style=\"width: 100%\">\n";
       myFile << "<tr bgcolor=\"#FFFF00\">\n";
       myFile << "<th>" << "Id" << "</th>\n";
       myFile << "<th>" << "Item name" << "</th>\n";
       myFile << "<th>" << "Item price" << "</th>\n";
       myFile << "<th>" << "Total" << "</th>\n";
       myFile << "</tr>\n";

       for(i = 0; i < myrecpt.itemsOrd.size(); i++)
       {
           if(i % 2 == 0)
           myFile << "<tr bgcolor=\"#FFBBBB\">\n";
           else
           myFile << "<tr bgcolor=\"#BBFFBB\">\n";

           myFile << "<td>" << "<center>" << itoa(i + 1, buffer, 10) << "</center>" << "</td>\n";
           myFile << "<td>" << "<center>" << myrecpt.itemsOrd[i].getName().c_str() << "</center>" << "</td>\n";

           ss.str("");
           ss.clear();
  
           ss.setf(ios::fixed);
           ss.precision(2);

           ss << "$" << myrecpt.itemsOrd[i].getPrice();
           myFile << "<td>" << "<center>" << ss.str().c_str() << "</center>" << "</td>\n";
           myFile << "<td>" << "<center>" << "---" << "</center>" << "</td>\n";

           myFile << "</tr>\n";
       }


           myFile << "<tr bgcolor=\"#CCCCFF\">\n";
           myFile << "<td>" << "<center>" << "---" << "</center>" << "</td>\n";
           myFile << "<td>" << "<center>" << "<b>" << "Subtotal" << "</b>" << "</center>" << "</td>\n";

           ss.str("");
           ss.clear();

           ss << "$" << myrecpt.totalPrice;
           myFile << "<td>" << "<center>" << "---" << "</center>" << "</td>\n";
           myFile << "<td>" << "<center>" << "<b>" << ss.str().c_str() << "</b>" << "</center>" << "</td>\n";

           myFile << "</tr>\n";

           myFile << "<tr bgcolor=\"#CCCCFF\">\n";

           myFile << "<td>" << "<center>" << "---" << "</center>" << "</td>\n";
           myFile << "<td>" << "<center>" << "<b>" << "Tax" << "</b>" << "</center>" << "</td>\n";

           ss.str("");
           ss.clear();

           ss << "$" << myrecpt.taxPrice;
           myFile << "<td>" << "<center>" << "---" << "</center>" << "</td>\n";
           myFile << "<td>" << "<center>" << "<b>" << ss.str().c_str() << "</b>" << "</center>" << "</td>\n";

           myFile << "</tr>\n";

           if(myrecpt.gratuity > 0)
           {
               myFile << "<tr bgcolor=\"#CCCCFF\">\n";

               myFile << "<td>" << "<center>" << "---" << "</center>" << "</td>\n";
               myFile << "<td>" << "<center>" << "<b>" << "Gratuity" << "</b>" << "</center>" << "</td>\n";

               ss.str("");
               ss.clear();

               ss << "$" << myrecpt.gratuity;
               myFile << "<td>" << "<center>" << "---" << "</center>" << "</td>\n";
               myFile << "<td>" << "<center>" << "<b>" << ss.str().c_str() << "</b>" << "</center>" << "</td>\n";

               myFile << "</tr>\n";
           }

       myFile << "<tr bgcolor=\"#BBBBFF\">\n";

       myFile << "<td>" << "<center>" << "---" << "</center>" << "</td>\n";
       myFile << "<td>" << "<center>" << "<b>" << "Total price" << "</b>" << "</center>" << "</td>\n";

       ss.str("");
       ss.clear();

       ss << "$" << (myrecpt.totalPrice + myrecpt.taxPrice + myrecpt.gratuity);
       myFile << "<td>" << "<center>" << "---" << "</center>" << "</td>\n";
       myFile << "<td>" << "<center>" << "<b>" << "<font face=\"verdana\" size=\"4\" color=\"#454500\">" << ss.str().c_str() << "</font>" << "</b>" << "</center>" << "</td>\n";

       myFile << "</tr>\n";
       myFile << "</table>\n";

       myFile << "<h4>" << "<center>" << "-- -- --Thank you for your business-- -- --" << "</h4>" << "</center>\n";

       myFile << "</font>";

       myFile << "</body>\n";
       myFile << "</html>\n";

       myFile.close();
       return fileName;
   }

   void hotel::runhotel()
   {
       int i, k, len;
       hotel &myhotel = (*this); // An useful trick, since the code is moved from function main()
       hotel::recpt myrecpt;

       const int len_max = 65;
       const int len2 = 4;
      
       int retValue;

       Item theItem;
       std::string choice;

       srand(time(NULL));

       bool bHelpNeeded = true;
       bool bFinishedOrdering = false;
       while(bFinishedOrdering == false)
       {
           myhotel.showMenu(bHelpNeeded);

           k = (int)myrecpt.itemsOrd.size() - 1;

           if(k >= 0)
           {
               cout << "Your previous order : ";
               setConsoleTextColor(Color_Green) << myrecpt.itemsOrd[k].getName();
               setConsoleTextColor(Color_White) << " (";
               setConsoleTextColor(Color_Light_Blue) << myrecpt.itemsOrd[k].getPrice() << "$";
               setConsoleTextColor(Color_White) << ")" << endl;
           }

           if((retValue = myhotel.getInput(theItem)) > ZERO_FOR_CAL)
           {
               bHelpNeeded = false;
               myrecpt.itemsOrd.push_back(theItem);
               myrecpt.totalPrice += theItem.getPrice();

               cout << "+ Total items you have Ord : ";
               setConsoleTextColor(Color_Yellow) << myrecpt.itemsOrd.size() << endl;
               setConsoleTextColor(Color_White) << "+ Total price (without tax) : ";
               setConsoleTextColor(Color_Yellow) << myrecpt.totalPrice << "$";
               setConsoleTextColor(Color_White) << endl;

               cout << endl;
               cout << "+ Do you want to order more (";
               setConsoleTextColor(Color_Yellow) << "Y";
               setConsoleTextColor(Color_White) << " to continue) : ";
              
               setConsoleTextColor(Color_Yellow);
               cin >> choice;
               setConsoleTextColor(Color_White);

               // If the customer chooses "Y", the program will continue to prompt the customer for order.
               // Otherwise, the program will simply interpret the user response as "No" and stop the ordering
               if(stricmp(choice.c_str(), "Y") == ZERO_FOR_CAL || stricmp(choice.c_str(), "Yes") == ZERO_FOR_CAL)
               {
                   bFinishedOrdering = false;
               }
               else
               {
                   bFinishedOrdering = true;          
               }
           }
           else
           {
               if(retValue < ZERO_FOR_CAL)
               {
                   // In case a customer made an invalid choice, the hotel will provide the user with additional information
                   // so that the customer makes a valid choice next time
                   bHelpNeeded = true;
               }
               else
               {
                   bHelpNeeded = false;
               }
           }
       }

       myrecpt.taxPrice = (myrecpt.totalPrice * tax_of_the_hotel);

       cout << endl;
       setConsoleTextColor(Color_Bright_White) << "You have finished ordering ";
       setConsoleTextColor(Color_Yellow) << myrecpt.itemsOrd.size();
       setConsoleTextColor(Color_Bright_White) << " items";
       setConsoleTextColor(Color_White) << endl;

       // Handling Gratuity (If any)
       double gratuity_temp;
       if(rand() % 100 >= 55)
       {
           if(rand() % 100 >= 50)
           myrecpt.gratuity = myrecpt.totalPrice * 0.20;
           else
           myrecpt.gratuity = myrecpt.totalPrice * 0.15;

           // When it comes to gratuity, customers can only afford to "pay" with their own money notes
           // We have to round the (gratuity) of course, and round to the nearest exact unit (0.25$ per unit each)
           for(gratuity_temp = 0.25; gratuity_temp < myrecpt.gratuity; gratuity_temp += 0.25)
           {
               if(gratuity_temp + 0.25 > myrecpt.gratuity) break;
           }

           myrecpt.gratuity = gratuity_temp;
           setConsoleTextColor(Color_Bright_White) << "Your gratuity : ";
           setConsoleTextColor(Color_Green) << "$" << myrecpt.gratuity << endl;
       }

       len = myrecpt.itemsOrd[0].getName().size();
       for(i = 1; i < myrecpt.itemsOrd.size(); i++)
       {
           if(len < myrecpt.itemsOrd[i].getName().size()) len = myrecpt.itemsOrd[i].getName().size();
       }
       len += 1;

       cout << setfill('-');
       for(i = 0; i < myrecpt.itemsOrd.size(); i++)
       {
           setConsoleTextColor(Color_Yellow) << " " << i + 1 << ". ";
           setConsoleTextColor(Color_White) << hotel::printName(myrecpt.itemsOrd[i].getName(), len) << setw(len_max - len);
           setConsoleTextColor(Color_Light_Blue) << "$" << myrecpt.itemsOrd[i].getPrice() << endl;
       }
       cout << endl;

       setConsoleTextColor(Color_Bright_White) << hotel::printName(" Subtotal : ", len) << setw(len_max - len + len2);
      
       setConsoleTextColor(Color_Yellow);
       cout << "$" << myrecpt.totalPrice << endl;
       setConsoleTextColor(Color_White);

       setConsoleTextColor(Color_Bright_White) << hotel::printName(" Tax : ", len) << setw(len_max - len + len2);

       setConsoleTextColor(Color_Yellow);
   &nbs


Related Solutions

Please C++ create a program that will do one of two functions using a menu, like...
Please C++ create a program that will do one of two functions using a menu, like so: 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 1 Enter Catalan number to calculate: 3 Catalan number at 3 is 5 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 2 Enter Fibonacci number to calculate: 6 Fibonacci number 6 is 8 Create a function of catalan that will take a parameter and return...
Write a C/C++ program that simulate a menu based binary numbercalculator. This calculate shall have...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities:Covert a binary string to corresponding positive integersConvert a positive integer to its binary representationAdd two binary numbers, both numbers are represented as a string of 0s and 1sTo reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the following three functions:int binary_to_decimal(string...
Write a menu program to have the above options for the polynomials. Your menu program should...
Write a menu program to have the above options for the polynomials. Your menu program should not use global data; data should be allowed to be read in and stored dynamically. Test your output with the data below. Poly #1: {{2, 1/1}, {1, 3/4}, {0, 5/12}} Poly #2: {{4, 1/1}, {2, -3/7}, {1, 4/9}, {0, 2/11}} provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1....
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
Confused about going through this C program. Thank you Program Specifications: *********************************************** ** MAIN MENU **...
Confused about going through this C program. Thank you Program Specifications: *********************************************** ** MAIN MENU ** *********************************************** A) Enter game results B) Current Record (# of wins and # of losses and # of ties) C) Display ALL results from all games WON D) Display ALL results ordered by opponent score from low to high. E) Quit Your program will have a menu similar to the example above. The game results will simply be the score by your team and...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
Using a vector of integers that you define. Write a C++ program to run a menu...
Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 2. Write a function called getValidAge that allows a user to...
C++ Write a menu based program for the pet rescue. There should be 2 menu options...
C++ Write a menu based program for the pet rescue. There should be 2 menu options -Add a pet -View pets -If the user chooses option 1, you will open a data file for writing without erasing the current contents, and write a new pet record to it. The file can be formatted any way that you choose but should include the pet's name, species, breed, and color. You my also include any additional information you think is appropriate. -If...
in c++ please In this program you are going to have several files to turn in...
in c++ please In this program you are going to have several files to turn in (NOT JUST ONE!!) hangman.h – this is your header file – I will give you a partially complete header file to start with. hangman.cpp – this is your source file that contains your main function functions.cpp – this is your source file that contains all your other functions wordBank.txt – this is the file with words for the game to use.  You should put 10...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT