Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <deque>
  4. #include <set>
  5. #include <test_runner.h>
  6. using namespace std;
  7. long long time_ =0;
  8. class abc_time {
  9. public:
  10. abc_time(const long long& t): time(t) {}
  11. long long time;
  12. };
  13. class clients_time: public abc_time {
  14. public:
  15. explicit clients_time(const long long& t, const int& i ): abc_time(t), id(i) {}
  16. int id;
  17. };
  18. class rooms_time: public abc_time {
  19. public:
  20. explicit rooms_time(const long long& t, const int& r) : abc_time(t), room(r) {}
  21. int room;
  22. };
  23.  
  24. class Hotel {
  25. public:
  26. Hotel(const string& name): name_(name), reserved_rooms_(0), last_clients_(0) {}
  27. void Add(const long long& time, const int& client_id,
  28. const int& room_count) {
  29. time_ = time;
  30. // придумать как избавиться от цикла
  31. /*for(int i=0; i<room_count; i++) {
  32. rooms_.push_back(rooms_time(time_, 1));
  33. }*/
  34. rooms_.push_back(rooms_time(time_, room_count));
  35.  
  36. reserved_rooms_ += room_count;
  37.  
  38. // удалить из очереди комнат неактуальные
  39. // обновить данные по комнатам за сутки
  40. Update_rooms();
  41.  
  42.  
  43. clients_.push_back(clients_time(time_, client_id));
  44. // удалить неактуальных клиентов
  45. // оюновить данные по клиентам за сутки
  46. // проблема с уник клиентами
  47. /*if (uniq_clients_.lower_bound(client_id)==uniq_clients_.end()) {
  48. uniq_clients_.insert(client_id);
  49. }*/
  50. Update_clients();
  51. //last_clients_ = uniq_clients_.size();
  52.  
  53. }
  54. void Update_rooms() {
  55. for(auto& i : rooms_) {
  56. // если время идет по нарастанию, то получается нет смысла всю очередь проверять
  57. if (time_ - i.time >= day_sec ) {
  58. reserved_rooms_ -= i.room;
  59. rooms_.pop_front();
  60. }
  61. else break;
  62. }
  63. //reserved_rooms_ = rooms_.size();
  64. }
  65. void Update_clients() {
  66. for(auto& i:clients_) {
  67. if (time_ - i.time >= day_sec ) {
  68. //int name = i.id;
  69. clients_.pop_front();
  70.  
  71. //uniq_clients_.erase(name);
  72. /*if (uniq_clients_.lower_bound(name)==uniq_clients_.end()) {
  73. last_clients_--;
  74. }*/
  75. }
  76. else break;
  77. }
  78. uniq_clients_.clear();
  79. for(auto& x : clients_) {
  80. uniq_clients_.insert(x.id);
  81. }
  82. last_clients_ = uniq_clients_.size();
  83. }
  84. int GetRooms() const {
  85. return reserved_rooms_;
  86. }
  87. int GetClients() const {
  88. return last_clients_;
  89. }
  90. private:
  91. static const int day_sec = 86400;
  92. const string name_;
  93.  
  94. deque <clients_time> clients_;
  95. deque <rooms_time> rooms_;
  96. set<int> uniq_clients_;
  97.  
  98. int reserved_rooms_;
  99. int last_clients_;
  100. //long long time_;
  101. };
  102. void Test() {
  103. Hotel Marriott("Marriott");
  104. /*Marriott.Add(10, 1, 1);
  105. ASSERT_EQUAL(Marriott.GetClients(), 1);
  106. ASSERT_EQUAL(Marriott.GetRooms(), 1);
  107. Marriott.Add(20, 2, 2);
  108. ASSERT_EQUAL(Marriott.GetClients(), 2);
  109. ASSERT_EQUAL(Marriott.GetRooms(), 3);
  110. Marriott.Add(86415, 2, 1);
  111. ASSERT_EQUAL(Marriott.GetClients(), 1);
  112. ASSERT_EQUAL(Marriott.GetRooms(), 3);
  113. cout << "OK" << endl;*/
  114. //ASSERT_EQUAL(Marriott.GetClients(), 0);
  115. //ASSERT_EQUAL(Marriott.GetRooms(), 0);
  116. Hotel FourSeasons("FourSeasons");
  117. FourSeasons.Add(10, 1, 2);
  118. ASSERT_EQUAL(FourSeasons.GetClients(), 1);
  119. ASSERT_EQUAL(FourSeasons.GetRooms(), 2);
  120. //Marriott.Add(10, 1, 1);
  121. FourSeasons.Add(86409, 2, 1);
  122. ASSERT_EQUAL(FourSeasons.GetClients(), 2);
  123. ASSERT_EQUAL(FourSeasons.GetRooms(), 3);
  124. //ASSERT_EQUAL(Marriott.GetClients(), 1);
  125. Marriott.Add(86410, 2, 10);
  126.  
  127. // почему не изменяются значения постояльцев и кол-во комнат ????
  128. ASSERT_EQUAL(FourSeasons.GetClients(), 1);
  129. ASSERT_EQUAL(FourSeasons.GetRooms(), 1);
  130. //ASSERT_EQUAL(Marriott.GetRooms(), 10);
  131. };
  132. int main()
  133. {
  134. Test();
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement