Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 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 = str + toys->at(i)->to_string() + "\n";
  60.         }
  61.         return str;
  62.     }
  63.  
  64.     std::string toys_to_string(std::vector<Toy*>* toys) {
  65.         std::string str = "";
  66.         for (int i = 0; i < toys->size(); i ++) {
  67.             str += toys->at(i)->to_string() + "\n";
  68.         }
  69.         return str;
  70.     }
  71.  
  72.     std::vector<Toy*>* find_toys(double max_coast, int age) {
  73.         std::vector<Toy*>* founded_toys = new std::vector<Toy*>();
  74.         for (int i = 0; i < toys->size(); i ++) {
  75.             if (toys->at(i)->get_coast() <= max_coast && toys->at(i)->get_age_from() <= age && toys->at(i)->get_age_to() >= age) {
  76.                 founded_toys->push_back(toys->at(i));
  77.             }
  78.         }
  79.         return founded_toys;
  80.     }
  81.  
  82.     Toy* get_most_expensive_toy(std::string name) {
  83.         Toy* most_expensive_toy;
  84.         for (int i = 0; i < toys->size(); i ++) {
  85.             if (toys->at(i)->get_name() == name) {
  86.                 if (most_expensive_toy == nullptr) {
  87.                     most_expensive_toy = toys->at(i);
  88.                 } else if (toys->at(i)->get_coast() >= most_expensive_toy->get_coast()) {
  89.                     most_expensive_toy = toys->at(i);
  90.                 }
  91.             }
  92.         }
  93.  
  94.         return most_expensive_toy;
  95.     }
  96.  
  97.  
  98. private:
  99.     std::vector<Toy*>* toys;
  100. };
  101.  
  102.  
  103. int show_menu() {
  104.     int choice;
  105.     std::cout << "1. Ввести данные" << std::endl;
  106.     std::cout << "2. Вывести все данные" << std::endl;
  107.     std::cout << "3. Найти все игрушки ниже указаннй цены подходящие детям определенного возраста" << std::endl;
  108.     std::cout << "4. Найти самуй дорогую введенную игрушку" << std::endl;
  109.     std::cin >> choice;
  110.     return choice;
  111. }
  112.  
  113. Toy* input_toy() {
  114.     std::string name;
  115.     std::cout << "Введите название игрушки" << std::endl;
  116.     std::cin >> name;
  117.     std::cout << std::endl;
  118.  
  119.     double coast;
  120.     std::cout << "Введите стоимость игрушки" << std::endl;
  121.     std::cin >> coast;
  122.     std::cout << std::endl;
  123.  
  124.     int age_from;
  125.     std::cout << "Введите начальный возраст для игрушки" << std::endl;
  126.     std::cin >> age_from;
  127.     std::cout << std::endl;
  128.  
  129.     int age_to;
  130.     std::cout << "Введите конечный возраст игрушки" << std::endl;
  131.     std::cin >> age_to;
  132.     std::cout << std::endl;
  133.  
  134.     return new Toy(name, coast, age_from, age_to);
  135. }
  136.  
  137. std::vector<Toy*>* get_toys_for_age_and_coast(ToyRepository* toyRepository) {
  138.     double coast;
  139.     std::cout << "Введите стоимость игрушки" << std::endl;
  140.     std::cin >> coast;
  141.     std::cout << std::endl;
  142.  
  143.     int age;
  144.     std::cout << "Введите возраст" << std::endl;
  145.     std::cin >> age;
  146.     std::cout << std::endl;
  147.  
  148.     return toyRepository->find_toys(coast, age);
  149. }
  150.  
  151. Toy* get_most_expensive_toy(ToyRepository* toyRepository) {
  152.     std::string name;
  153.     std::cout << "Введите название игрушки" << std::endl;
  154.     std::cin >> name;
  155.     std::cout << std::endl;
  156.  
  157.     return toyRepository->get_most_expensive_toy(name);
  158. }
  159.  
  160. int main() {
  161.     setlocale(LC_ALL, "Russian");
  162.     ToyRepository* toyRepository = new ToyRepository();
  163.     while (true) {
  164.         auto choice = show_menu();
  165.         switch (choice) {
  166.             case 1: {
  167.                 Toy *toy = input_toy();
  168.                 toyRepository->add_toy(toy);
  169.                 break;
  170.             }
  171.             case 2: {
  172.                 std::string str = toyRepository->to_string();
  173.                 std::cout << str;
  174.                 break;
  175.             }
  176.             case 3: {
  177.                 std::string str = toyRepository->toys_to_string(get_toys_for_age_and_coast(toyRepository));
  178.                 std::cout << str;
  179.                 break;
  180.             }
  181.             case 4: {
  182.                 Toy* toy = get_most_expensive_toy(toyRepository);
  183.                 if (toy == nullptr) {
  184.                     std::cout << "Игрушка с данным названием не найдена" << std::endl;
  185.                 } else {
  186.                     std::cout << toy->to_string() << std::endl;
  187.                 }
  188.                 break;
  189.             }
  190.             default: {
  191.                 std::cout << "Введите верный номер";
  192.                 break;
  193.             }
  194.         }
  195.     }
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement