Advertisement
1cutcut1

Untitled

May 11th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.42 KB | None | 0 0
  1. //1vata zadacha d i
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Room {
  7. public:
  8.     Room() { }
  9.  
  10.     virtual void displayRoomInfo() = 0;
  11.  
  12.     virtual void displayRoomPrice() = 0;
  13.  
  14.     virtual ~Room() { }
  15. };
  16.  
  17. class StandardRoom : public Room {
  18.     bool hasBathroom;
  19.  
  20. public:
  21.     StandardRoom() {
  22.         this->hasBathroom = false;
  23.     }
  24.  
  25.     StandardRoom(bool hasBathroom) {
  26.         this->hasBathroom = hasBathroom;
  27.     }
  28.  
  29.     void displayRoomInfo() {
  30.         cout << "This is a standard room with a queen-sized bed";
  31.         if(hasBathroom) cout << " and a bathroom";
  32.         cout << ".\n";
  33.     }
  34.  
  35.     void displayRoomPrice() {
  36.         cout << "The price for a standard room is $";
  37.         if(hasBathroom) cout << "100";
  38.         else cout << "80";
  39.         cout << " per night.\n";
  40.     }
  41.  
  42.     ~StandardRoom() { }
  43. };
  44.  
  45. class DeluxeRoom : public Room {
  46.     bool hasBalcony;
  47.  
  48. public:
  49.     DeluxeRoom() { this->hasBalcony = false; }
  50.  
  51.     DeluxeRoom(bool hasBalcony) { this->hasBalcony = hasBalcony; }
  52.  
  53.     void displayRoomInfo() {
  54.         cout << "This is a deluxe room with a king-sized bed, a bathroom, a mini-fridge";
  55.         if(hasBalcony) cout << " and a balcony";
  56.         cout << ".\n";
  57.     }
  58.  
  59.     void displayRoomPrice() {
  60.         cout << "The price for a deluxe room is $";
  61.         if(hasBalcony) cout << "200";
  62.         else cout << "160";
  63.         cout << " per night.\n";
  64.     }
  65.  
  66.     ~DeluxeRoom() { }
  67. };
  68.  
  69. int main() {
  70.     Room* rooms[5];
  71.  
  72.     int testCase;
  73.  
  74.     cin >> testCase;
  75.  
  76.     if (testCase == 1) {
  77.         cout << "TEST CASE 1: TESTING STANDARD ROOM CLASS" << endl;
  78.  
  79.         for (int i = 0; i < 5; i++) {
  80.             rooms[i] = new StandardRoom();
  81.             rooms[i]->displayRoomInfo();
  82.             rooms[i]->displayRoomPrice();
  83.         }
  84.     } else if (testCase == 2) {
  85.         cout << "TEST CASE 2: TESTING DELUXE ROOM CLASS" << endl;
  86.         for (int i = 0; i < 5; i++) {
  87.             rooms[i] = new DeluxeRoom();
  88.             rooms[i]->displayRoomInfo();
  89.             rooms[i]->displayRoomPrice();
  90.         }
  91.     } else {
  92.         cout << "TEST CASE 3: TESTING BOTH CLASSES" << endl;
  93.         for (int i = 0; i < 5; i++) {
  94.             if (i % 2) {
  95.                 bool hasBalcony;
  96.                 cin >> hasBalcony;
  97.                 rooms[i] = new DeluxeRoom(hasBalcony);
  98.             }
  99.             else {
  100.                 bool hasBathroom;
  101.                 cin >> hasBathroom;
  102.                 rooms[i] = new StandardRoom(hasBathroom);
  103.             }
  104.             rooms[i]->displayRoomInfo();
  105.             rooms[i]->displayRoomPrice();
  106.         }
  107.     }
  108.  
  109.     return 0;
  110. ---------------------------------------------------------------------------------------------------------------------------------------
  111. //2ra
  112. #include <iostream>
  113. #include <cstring>
  114. using namespace std;
  115.  
  116. class FoodItem {
  117. protected:
  118.     char* tip;
  119.     int n;
  120.  
  121. public:
  122.     FoodItem() {
  123.         this->tip = new char[8];
  124.         strcpy(this->tip, "empty");
  125.         this->n = 0;
  126.     }
  127.  
  128.     FoodItem(char* type, int n) {
  129.         this->tip = new char[strlen(type) + 1];
  130.         strcpy(this->tip, type);
  131.         this->n = n;
  132.     }
  133.  
  134.     char* getType() { return tip; }
  135.  
  136.     virtual int getPrice() = 0;
  137.  
  138.     virtual int getTime() = 0;
  139.  
  140.     virtual ~FoodItem() {
  141.         delete [] tip;
  142.     }
  143. };
  144.  
  145. class Pizza : public FoodItem {
  146.     char* dough;
  147.  
  148. public:
  149.     Pizza() : FoodItem() {
  150.         this->dough = new char[12];
  151.         strcpy(this->dough, "wholeWheat");
  152.     }
  153.  
  154.     Pizza(char* tip, int n, char* dough) : FoodItem(tip, n) {
  155.         this->dough = new char[strlen(dough) + 1];
  156.         strcpy(this->dough, dough);
  157.     }
  158.  
  159.     int getPrice() {
  160.         if(strcmp(this->dough, "wholeWheat") == 0) {
  161.             return 250 * n;
  162.         }
  163.         else if(strcmp(this->dough, "glutenFree") == 0) {
  164.             return 350 * n;
  165.         } else {
  166.             return 0;
  167.         }
  168.     }
  169.  
  170.     int getTime() { return 25; }
  171.  
  172.     ~Pizza() {
  173.         delete [] dough;
  174.     }
  175. };
  176.  
  177. class Steak : public FoodItem {
  178.     bool cooked;
  179.  
  180. public:
  181.     Steak() : FoodItem() {
  182.         this->cooked = false;
  183.     }
  184.  
  185.     Steak(char* tip, int n, bool cooked) : FoodItem(tip, n) {
  186.         this->cooked = cooked;
  187.     }
  188.  
  189.     int getPrice() { return 1300 * n; }
  190.  
  191.     int getTime() {
  192.         if(cooked) return 20;
  193.         else if(!cooked) return 15;
  194.         else return 0;
  195.     }
  196.  
  197.     ~Steak() { }
  198. };
  199.  
  200. FoodItem* getMaxFoodItem(FoodItem* pItem[], int n) {
  201.     FoodItem* item = pItem[0];
  202.     int max_food_price = pItem[0]->getPrice();
  203.  
  204.     for(int i = 0; i < n; i++) {
  205.         if(max_food_price < pItem[i]->getPrice()) {
  206.             max_food_price = pItem[i]->getPrice();
  207.             item = pItem[i];
  208.         }
  209.     }
  210.  
  211.     return item;
  212. }
  213.  
  214. int main() {
  215.     FoodItem *p;
  216.     int n;
  217.     cin>>n;
  218.  
  219.     char type[30];
  220.     char dough[30];
  221.     bool cooked;
  222.     int size;
  223.  
  224.     FoodItem *items[n];
  225.  
  226.     for (int i = 0; i <n; ++i) {
  227.         cin>>type;
  228.         cin>>size;
  229.  
  230.  
  231.         if(strcmp(type, "pizza")==0 ) {
  232.             cin>>dough;
  233.             items[i] = new Pizza(type, size, dough);
  234.         }else{
  235.             cin>>cooked;
  236.             items[i] = new Steak(type, size, cooked);
  237.         }
  238.  
  239.  
  240.     }
  241.  
  242.     FoodItem *it = getMaxFoodItem(items, n);
  243.     cout<<"Type: "<<it->getType()<<endl;
  244.     cout<<"The max price is: "<<it->getPrice()<<endl;
  245.     cout<<"Time to cook: "<<it->getTime();
  246.     return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement