Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class Toy {
  5. public:
  6.  
  7.     std::string get_name() {
  8.         return name;
  9.     }
  10.  
  11.     double get_coast() {
  12.         return coast;
  13.     }
  14.  
  15.     int get_age_from() {
  16.         return age_from;
  17.     }
  18.  
  19.     int get_age_to() {
  20.         return age_to;
  21.     }
  22.  
  23.     std::string to_string() {
  24.         return "Название: " + name + ", Стоимость: " + std::to_string(coast) +
  25.                 ", Возраст от: " + std::to_string(age_from) + ", Возраст до: " + std::to_string(age_to) + ".";
  26.     }
  27.  
  28.     Toy(std::string name, double coast, int age_from, int age_to) {
  29.         this->age_from = age_from;
  30.         this->age_to = age_to;
  31.         this->coast = coast;
  32.         this->name = name;
  33.     }
  34.  
  35. private:
  36.     std::string name;
  37.     double coast;
  38.     int age_from;
  39.     int age_to;
  40.  
  41. };
  42.  
  43.  
  44.  
  45.  
  46. class ToyRepository {
  47. public:
  48.      ToyRepository() {
  49.         toys = new std::vector<Toy*>();
  50.      }
  51.  
  52.     void add_toy(Toy* toy) {
  53.         toys->push_back(toy);
  54.     }
  55.  
  56.     std::string to_string() {
  57.         std::string str = "";
  58.         for (int i = 0; i < toys->size(); i ++) {
  59.             str += toys->at(i)->to_string() + "\n";
  60.         }
  61.     }
  62.  
  63.     std::string toys_to_string(std::vector<Toy*>* toys) {
  64.         std::string str = "";
  65.         for (int i = 0; i < toys->size(); i ++) {
  66.             str += toys->at(i)->to_string() + "\n";
  67.         }
  68.     }
  69.  
  70.     std::vector<Toy*>* find_toys(double max_coast, int age) {
  71.         std::vector<Toy*>* founded_toys = new std::vector<Toy*>();
  72.         for (int i = 0; i < toys->size(); i ++) {
  73.             if (toys->at(i)->get_coast() <= max_coast && toys->at(i)->get_age_from() <= age && toys->at(i)->get_age_to() >= age) {
  74.                 founded_toys->push_back(toys->at(i));
  75.             }
  76.         }
  77.         return founded_toys;
  78.     }
  79.  
  80.  
  81.  
  82. private:
  83.     std::vector<Toy*>* toys;
  84. };
  85.  
  86.  
  87. int show_menu() {
  88.     int choice;
  89.     std::cout << "1. Ввести данные" << std::endl;
  90.     std::cout << "2. Вывести все данные" << std::endl;
  91.     std::cout << "3. Найти все игрушки ниже указаннй цены подходящие детям определенного возраста" << std::endl;
  92.     std::cout << "4. Найти самуй дорогую введенную игрушку" << std::endl;
  93.     std::cin >> choice;
  94.     return choice;
  95. }
  96.  
  97. Toy* input_toy() {
  98.     std::string name;
  99.     std::cout << "Введите название игрушки" << std::endl;
  100.     std::cin >> name;
  101.     std::cout << std::endl;
  102.  
  103.     double coast;
  104.     std::cout << "Введите стоимость игрушки" << std::endl;
  105.     std::cin >> coast;
  106.     std::cout << std::endl;
  107.  
  108.     int age_from;
  109.     std::cout << "Введите начальный возраст для игрушки" << std::endl;
  110.     std::cin >> age_from;
  111.     std::cout << std::endl;
  112.  
  113.     int age_to;
  114.     std::cout << "Введите конечный возраст игрушки" << std::endl;
  115.     std::cin >> age_to;
  116.     std::cout << std::endl;
  117.  
  118.     return new Toy(name, coast, age_from, age_to);
  119. }
  120.  
  121. std::vector<Toy*>* get_toys_for_age_and_coast(ToyRepository* toyRepository) {
  122.     double coast;
  123.     std::cout << "Введите стоимость игрушки" << std::endl;
  124.     std::cin >> coast;
  125.     std::cout << std::endl;
  126.  
  127.     int age;
  128.     std::cout << "Введите возраст" << std::endl;
  129.     std::cin >> age;
  130.     std::cout << std::endl;
  131.  
  132.     return toyRepository->find_toys(coast, age);
  133. }
  134.  
  135. int main() {
  136.     ToyRepository* toyRepository = new ToyRepository();
  137.     while (true) {
  138.         auto choice = show_menu();
  139.         switch (choice) {
  140.             case 1: {
  141.                 Toy *toy = input_toy();
  142.                 toyRepository->add_toy(toy);
  143.                 break;
  144.             }
  145.             case 2: {
  146.                 std::string str = toyRepository->to_string();
  147.                 std::cout << st1r;
  148.                 break;
  149.             }
  150.             case 3: {
  151.                 std::cout << toyRepository->toys_to_string(get_toys_for_age_and_coast(toyRepository));
  152.                 break;
  153.             }
  154.             default: {
  155.                 std::cout << "Введите верный номер";
  156.                 break;
  157.             }
  158.         }
  159.     }
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement