Advertisement
serd2011

HotelC++

Aug 23rd, 2020
1,605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. //main.cpp
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. #include "Hotel.h"
  8.  
  9. template<typename T>
  10. T getNumberFromCin(std::string,
  11.     T min = std::numeric_limits<T>().min(),
  12.     T max = std::numeric_limits<T>().max()
  13. );
  14.  
  15. int main()
  16. {
  17.     std::vector<Hotel> hotels;
  18.     int count = 3;
  19.  
  20.     for (int i = 0; i < count; i++)
  21.     {
  22.         std::string name;
  23.         int cost;
  24.  
  25.         std::cout << "Enter Hotel name >>> ";
  26.         std::getline(std::cin, name);
  27.         cost = getNumberFromCin("Enter Hotel cost >>> ", 0, 25000);
  28.         hotels.push_back(Hotel(name, cost));
  29.     }
  30.  
  31.     for (auto hotel : hotels) {
  32.         std::cout << hotel << std::endl;
  33.     }
  34.  
  35.     system("pause");
  36.     return 0;
  37. }
  38.  
  39.  
  40. template<typename T>
  41. T getNumberFromCin(std::string str, T min, T max) {
  42.     T num;
  43.     std::cout << str;
  44.     std::cin >> num;
  45.     while (num < min || num > max || std::cin.fail())
  46.     {
  47.         std::cout << "Invalid input!" << std::endl << str;
  48.         if (std::cin.fail())
  49.         {
  50.             std::cin.clear();
  51.             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  52.         }
  53.         std::cin >> num;
  54.     };
  55.     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  56.     return num;
  57. }
  58.  
  59.  
  60. //Hotel.h
  61.  
  62. #pragma once
  63. #include <iostream>
  64.  
  65. class Hotel {
  66.  
  67. public:
  68.     Hotel(std::string name, int cost);
  69.  
  70.     friend std::ostream& operator<< (std::ostream&, Hotel&);
  71.  
  72.     std::string getName();
  73.     int getCost();
  74.  
  75.     void setCost(int);
  76.  
  77. private:
  78.     Hotel();
  79.     std::string _name;
  80.     int _cost;
  81. };
  82.  
  83. //Hotel.cpp
  84.  
  85. #include "Hotel.h"
  86.  
  87. std::ostream& operator<< (std::ostream& stream, Hotel& obj) {
  88.     return stream << obj._name << " : " << obj._cost;
  89. }
  90.  
  91. Hotel::Hotel(std::string name, int cost): _name(name){
  92.     if (cost >= 25000)
  93.         throw new std::invalid_argument("Cost must be lower than 25000");
  94.     _cost = cost;
  95. }
  96.  
  97. std::string Hotel::getName() {
  98.     return  _name;
  99. }
  100.  
  101. int Hotel::getCost() {
  102.     return _cost;
  103. }
  104.  
  105. void Hotel::setCost(int newCost) {
  106.     _cost = newCost;
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement