Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- class Toy {
- public:
- std::string get_name() {
- return name;
- }
- double get_coast() {
- return coast;
- }
- int get_age_from() {
- return age_from;
- }
- int get_age_to() {
- return age_to;
- }
- std::string to_string() {
- return "Название: " + name + ", Стоимость: " + std::to_string(coast) +
- ", Возраст от: " + std::to_string(age_from) + ", Возраст до: " + std::to_string(age_to) + ".";
- }
- Toy(std::string name, double coast, int age_from, int age_to) {
- this->age_from = age_from;
- this->age_to = age_to;
- this->coast = coast;
- this->name = name;
- }
- private:
- std::string name;
- double coast;
- int age_from;
- int age_to;
- };
- class ToyRepository {
- public:
- ToyRepository() {
- toys = new std::vector<Toy*>();
- }
- void add_toy(Toy* toy) {
- toys->push_back(toy);
- }
- std::string to_string() {
- std::string str = "";
- for (int i = 0; i < toys->size(); i ++) {
- str = str + toys->at(i)->to_string() + "\n";
- }
- return str;
- }
- std::string toys_to_string(std::vector<Toy*>* toys) {
- std::string str = "";
- for (int i = 0; i < toys->size(); i ++) {
- str += toys->at(i)->to_string() + "\n";
- }
- return str;
- }
- std::vector<Toy*>* find_toys(double max_coast, int age) {
- std::vector<Toy*>* founded_toys = new std::vector<Toy*>();
- for (int i = 0; i < toys->size(); i ++) {
- if (toys->at(i)->get_coast() <= max_coast && toys->at(i)->get_age_from() <= age && toys->at(i)->get_age_to() >= age) {
- founded_toys->push_back(toys->at(i));
- }
- }
- return founded_toys;
- }
- Toy* get_most_expensive_toy(std::string name) {
- Toy* most_expensive_toy;
- for (int i = 0; i < toys->size(); i ++) {
- if (toys->at(i)->get_name() == name) {
- if (most_expensive_toy == nullptr) {
- most_expensive_toy = toys->at(i);
- } else if (toys->at(i)->get_coast() >= most_expensive_toy->get_coast()) {
- most_expensive_toy = toys->at(i);
- }
- }
- }
- return most_expensive_toy;
- }
- private:
- std::vector<Toy*>* toys;
- };
- int show_menu() {
- int choice;
- std::cout << "1. Ввести данные" << std::endl;
- std::cout << "2. Вывести все данные" << std::endl;
- std::cout << "3. Найти все игрушки ниже указаннй цены подходящие детям определенного возраста" << std::endl;
- std::cout << "4. Найти самуй дорогую введенную игрушку" << std::endl;
- std::cin >> choice;
- return choice;
- }
- Toy* input_toy() {
- std::string name;
- std::cout << "Введите название игрушки" << std::endl;
- std::cin >> name;
- std::cout << std::endl;
- double coast;
- std::cout << "Введите стоимость игрушки" << std::endl;
- std::cin >> coast;
- std::cout << std::endl;
- int age_from;
- std::cout << "Введите начальный возраст для игрушки" << std::endl;
- std::cin >> age_from;
- std::cout << std::endl;
- int age_to;
- std::cout << "Введите конечный возраст игрушки" << std::endl;
- std::cin >> age_to;
- std::cout << std::endl;
- return new Toy(name, coast, age_from, age_to);
- }
- std::vector<Toy*>* get_toys_for_age_and_coast(ToyRepository* toyRepository) {
- double coast;
- std::cout << "Введите стоимость игрушки" << std::endl;
- std::cin >> coast;
- std::cout << std::endl;
- int age;
- std::cout << "Введите возраст" << std::endl;
- std::cin >> age;
- std::cout << std::endl;
- return toyRepository->find_toys(coast, age);
- }
- Toy* get_most_expensive_toy(ToyRepository* toyRepository) {
- std::string name;
- std::cout << "Введите название игрушки" << std::endl;
- std::cin >> name;
- std::cout << std::endl;
- return toyRepository->get_most_expensive_toy(name);
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- ToyRepository* toyRepository = new ToyRepository();
- while (true) {
- auto choice = show_menu();
- switch (choice) {
- case 1: {
- Toy *toy = input_toy();
- toyRepository->add_toy(toy);
- break;
- }
- case 2: {
- std::string str = toyRepository->to_string();
- std::cout << str;
- break;
- }
- case 3: {
- std::string str = toyRepository->toys_to_string(get_toys_for_age_and_coast(toyRepository));
- std::cout << str;
- break;
- }
- case 4: {
- Toy* toy = get_most_expensive_toy(toyRepository);
- if (toy == nullptr) {
- std::cout << "Игрушка с данным названием не найдена" << std::endl;
- } else {
- std::cout << toy->to_string() << std::endl;
- }
- break;
- }
- default: {
- std::cout << "Введите верный номер";
- break;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement