Advertisement
vencinachev

Shelter-header

Jun 18th, 2021
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.45 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct Animal
  6. {
  7.     string kind;
  8.     string breed;
  9.     int age{ 0 };
  10.     double price{0};
  11. };
  12.  
  13. struct Node
  14. {
  15.     Animal data;
  16.     Node* next{nullptr};
  17. };
  18.  
  19. struct Customer
  20. {
  21.     string kind;
  22.     string breed;
  23.     int age;
  24.     double price;
  25. };
  26.  
  27. class Shelter
  28. {
  29.     Node* Animals{ nullptr };
  30.     double balance();
  31.     unsigned freePlases { 0 };
  32. public:
  33.     void InsertAnimalInList(Animal x);
  34.     void removeAnimalFromList(Animal x);
  35.     void decreaseFreePlases();
  36.     void increaseFreePlases();
  37.     int countOfAnimals() const;
  38.     int countOfCats() const;
  39.     int countOfDogs() const;
  40.     double costForDay();
  41.     double profitForDay() const;
  42.     double Sale(Animal a);
  43.     void printShelter() const;
  44.     bool IsFound(Customer C) const;
  45.     Animal GetAnimal(Customer C) const;
  46.     double findSumCats() const;
  47.     double findSumDogs() const;
  48.     bool hasFreePlace() const;
  49.     void shelterInfo() const;
  50. };
  51. double Shelter::costForDay()
  52. {
  53.     double cost = 0;
  54.     Node* current = Animals;
  55.     for (int i = 0; i < countOfAnimals(); i++)
  56.     {
  57.         cost += (current->data.price * 0.02);
  58.     };
  59.     return cost;
  60. }
  61.  
  62. //double Shelter::profitForDay() {}
  63. Animal readAnimal() {
  64.     Animal animal;
  65.     cout << "Enter data for new animal:"<<endl;
  66.     cout << "Kind: ";
  67.     cin >> animal.kind;
  68.     cout << "Breed: ";
  69.     cin.ignore();
  70.     getline(cin, animal.breed);
  71.     cout << "Age: ";
  72.     cin >> animal.age;
  73.     cout << "Price: ";
  74.     cin >> animal.price;
  75.     return animal;
  76. }
  77.  
  78. void printAnimal(Animal animal)
  79. {
  80.     cout << "Kind: " << animal.kind << endl;
  81.     cout << "Breed: "<< animal.breed << endl;
  82.     cout << "Age: "<< animal.age << endl;
  83.     cout << "Price: " << animal.price << endl;
  84.     cout << endl;
  85. }
  86.  
  87. void Shelter::printShelter() const
  88. {
  89.     cout << "Animals in the shelter are: " << endl;
  90.     Node* current = Animals;
  91.     while (current != nullptr)
  92.     {
  93.         printAnimal(current->data);
  94.         current = current->next;
  95.     }
  96.     cout << endl;
  97. }
  98.  
  99. void Shelter::InsertAnimalInList(Animal x)
  100. {
  101.     Node* temp = new Node;
  102.     temp->data = x;
  103.     temp->next = nullptr;
  104.     if (Animals == nullptr)
  105.     {
  106.         Animals = temp;
  107.     }
  108.     else
  109.     {
  110.         Node* temp1 = Animals;
  111.         while (temp1->next != nullptr)
  112.         {
  113.             temp1 = temp1->next;
  114.         }
  115.         temp1->next = temp;
  116.     }
  117. }
  118.  
  119. void Shelter::removeAnimalFromList(Animal x)
  120. {
  121.     if (Animals == nullptr)
  122.     {
  123.             return;
  124.     }
  125.     Node* current = Animals;
  126.     Node* prev = Animals;
  127.     while (current != nullptr)
  128.     {
  129.         if (x.age == current->data.age &&
  130.             x.kind == current->data.kind &&
  131.             x.breed == current->data.breed &&
  132.             x.price == current->data.price)
  133.         {
  134.             if (current == Animals)
  135.             {
  136.                 Animals = current->next;
  137.                 delete current;
  138.                 current = nullptr;
  139.             }
  140.             else
  141.             {
  142.                 prev->next = current->next;
  143.                 delete current;
  144.                 current = nullptr;
  145.             }
  146.         }
  147.         else
  148.         {
  149.             if (current != Animals) prev = current;
  150.             current=current->next;
  151.         }
  152.     }
  153. }
  154.  
  155. void Shelter::decreaseFreePlases()
  156. {
  157.     freePlases--;
  158. }
  159.  
  160. void Shelter::increaseFreePlases()
  161. {
  162.     freePlases++;
  163. }
  164.  
  165. int Shelter::countOfAnimals() const
  166. {
  167.     return countOfCats() + countOfDogs();
  168. }
  169.  
  170. int Shelter::countOfCats() const
  171. {
  172.     int count = 0;
  173.     Node* current = Animals;
  174.     while (current != nullptr)
  175.     {
  176.         if (current->data.kind == "cat")
  177.         {
  178.             ++count;
  179.         }
  180.         current = current->next;
  181.     }
  182.     return count;
  183. }
  184.  
  185. int Shelter::countOfDogs() const
  186. {
  187.     int count = 0;
  188.  
  189.     Node* current = Animals;
  190.     while (current != nullptr)
  191.     {
  192.         if (current->data.kind == "dog")
  193.         {
  194.             ++count;
  195.         }
  196.         current = current->next;
  197.     }
  198.     return count;
  199. }
  200.  
  201. double Shelter::findSumCats() const
  202. {
  203.     double sum = 0;
  204.     Node* current = Animals;
  205.     while (current != nullptr)
  206.     {
  207.         if (current->data.kind == "cat")
  208.         {
  209.             sum += current->data.price;
  210.         }
  211.         current = current->next;
  212.     }
  213.     return sum;
  214. }
  215.  
  216. double Shelter::findSumDogs() const
  217. {
  218.     double sum = 0;
  219.     Node* current = Animals;
  220.     while (current != nullptr)
  221.     {
  222.         if (current->data.kind == "dog")
  223.         {
  224.             sum += current->data.price;
  225.         }
  226.         current = current->next;
  227.     }
  228.     return sum;
  229. }
  230.  
  231. double InsertAnimal(Animal newAnimal, Shelter shelter)
  232. {
  233.     shelter.InsertAnimalInList(newAnimal);
  234.     shelter.decreaseFreePlases();
  235.     double cost = 0;
  236.     if (newAnimal.kind == "cat")
  237.     {
  238.         cost =double (shelter.findSumCats() / shelter.countOfCats());
  239.     }
  240.     if (newAnimal.kind == "dog")
  241.     {
  242.         cost = double(shelter.findSumDogs() / shelter.countOfDogs());
  243.     }
  244.     return cost;
  245. }
  246.  
  247. double Shelter::Sale(Animal a)
  248. {
  249.     removeAnimalFromList(a);
  250.     increaseFreePlases();
  251.     return a.price;
  252. }
  253.  
  254. Customer Requirements()
  255. {
  256.     Customer C;
  257.     cout << "Enter kind: ";
  258.     cin.ignore();
  259.     getline(cin,C.kind);
  260.     if (C.kind != "cat" && C.kind != "dog")
  261.     {
  262.         cout << "Invalid kind!";
  263.         exit(1);
  264.     }
  265.     string s;
  266.     cout << "Do you want to enter your requirements manual or automatic? "<<endl;
  267.     cin >> s;
  268.     if (s == "automatic")
  269.     {
  270.  
  271.     }
  272.     else if (s == "manual")
  273.     {
  274.         cout << "Enter breed: ";
  275.         cin.ignore();
  276.         getline(cin,C.breed);
  277.         cout << "Enter age: ";
  278.         cin >> C.age;
  279.         cout << "Enter limit of price: ";
  280.         cin >> C.price;
  281.     }
  282.     else
  283.     {
  284.         cout << "Invalid input!"; exit(1);
  285.     }
  286.     return C;
  287. }
  288.  
  289. bool Shelter::IsFound(Customer C) const
  290. {
  291.     if (Animals == nullptr)
  292.     {
  293.         return false;
  294.     }
  295.     Node* current = Animals;
  296.     while (current != nullptr)
  297.     {
  298.         if (current->data.kind == C.kind &&
  299.             current->data.breed == C.breed &&
  300.             current->data.age == C.age &&
  301.             current->data.price <= C.price)
  302.         {
  303.             return true;
  304.         }
  305.         current = current->next;
  306.     }
  307.     return false;
  308. }
  309.  
  310. Animal Shelter::GetAnimal(Customer C) const
  311. {
  312.     if (Animals == nullptr)
  313.     {
  314.         exit(1);
  315.     }
  316.     Node* current = Animals;
  317.     while (current != nullptr)
  318.     {
  319.         if (current->data.kind == C.kind &&
  320.             current->data.breed == C.breed &&
  321.             current->data.age == C.age &&
  322.             current->data.price <= C.price)
  323.         {
  324.             return current->data;
  325.         }
  326.         current = current->next;
  327.     }
  328.     exit(1);
  329. }
  330.  
  331. bool Shelter::hasFreePlace() const
  332. {
  333.     return freePlases != 0;
  334. }
  335.  
  336. void Shelter::shelterInfo() const
  337. {
  338.     cout << "Free places: " << freePlases << endl;
  339.     cout << "Animals count " << countOfAnimals() << endl;
  340.     cout << "Cats count " << countOfCats() << endl;
  341.     cout << "Dogs count " << countOfDogs() << endl;
  342.     cout << "Cats sum: " << findSumCats() << endl;
  343.     cout << "Dogs sum: " << findSumDogs() << endl;
  344. }
  345.  
  346. /*
  347. double Shelter::balance() {
  348.     return profitForDay() - costForDay();
  349. }*/
  350.  
  351.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement