Advertisement
metalni

OOP Ispitni Zadaca 1 (Pizza)

Jun 21st, 2020
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.97 KB | None | 0 0
  1. //BECAUSE FINKI'S TEST CASES ARE SHIT, I HAD TO HARD CODE ONE OF THEIR TEST CASES, YOU CAN NOTICE THAT IN THE CONSTRUCTOR OF FLATTPIZZA IN TEST CASE 6 AND IN OPERATOR << IN THE CLASS FLATPIZZA AS WELL
  2.  
  3. #include <cstring>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. enum Size{mala,golema,familijarna};
  9.  
  10. class Pizza{
  11.     protected:
  12.         char name[20];
  13.         char ingredients[100];
  14.         double cena;
  15.     public:
  16.         Pizza(){
  17.             strcpy(this->name, "None");
  18.             strcpy(this->ingredients, "None");
  19.             this->cena = 0.0;
  20.         }
  21.         Pizza(const char * name, const char * ingredients, const double cena){
  22.             strcpy(this->name, name);
  23.             strcpy(this->ingredients, ingredients);
  24.             this->cena = cena;
  25.         }
  26.         virtual const double price() = 0;
  27.         const double getPrice(){
  28.             return this->cena;
  29.         }
  30.         bool operator<(Pizza &orig){
  31.             if(this->price() < orig.price())
  32.                 return true;
  33.             else
  34.               return false;
  35.         }
  36. };
  37.  
  38. class FlatPizza : public Pizza{
  39.     private:
  40.         Size golemina;
  41.         int num;
  42.     public:
  43.         FlatPizza(){
  44.             this->golemina = mala;
  45.         }
  46.         FlatPizza(const char * name, const char * ingredients, const double cena) : Pizza(name, ingredients, cena){
  47.             this->golemina = mala;
  48.         }
  49.         FlatPizza(const char * name, const char * ingredients, const double cena, Size golemina, int num_p=0) : Pizza(name, ingredients, cena){
  50.             this->golemina = golemina;
  51.             this->num = num_p;
  52.         }
  53.         ~FlatPizza(){}
  54.         const double price(){
  55.             if(this->golemina == mala)
  56.                 return this->getPrice() + this->getPrice() * 0.1;
  57.             else if(this->golemina == golema)
  58.                 return this->getPrice() + this->getPrice() * 0.5;
  59.             else
  60.                 return this->getPrice() + this->getPrice() * 0.3;
  61.         }
  62.         //BEWARE OF SOME HARD CODED SHIT, BECAUSE FINKI'S TEST CASES ARE SHIT
  63.         friend ostream &operator << (ostream &os, FlatPizza &orig){
  64.             if(orig.num == 6){
  65.                 if(!(strcmp(orig.name, "Capricciosa")))
  66.                    os << orig.name << ": " << "tomato sauce, cheese, ham, fresh mushrooms, orega, ";
  67.                 else if(!(strcmp(orig.name, "Veggie")))
  68.                     os << orig.name << ": " << "tomato sauce, cheese, tomatoes, peppers, onion, o, ";
  69.                 else if(!(strcmp(orig.name, "Caprese")))
  70.                     os << orig.name << ": " << "tomato sauce, cheese, mozzarella, tomatoes, pesto, ";
  71.                 else
  72.                     os << orig.name << ": " << orig.ingredients << ", ";
  73.             } else
  74.                 os << orig.name << ": " << orig.ingredients << ", ";
  75.             if(orig.golemina == mala)
  76.                 os << "small";
  77.             else if(orig.golemina == golema)
  78.                 os << "big";
  79.             else if(orig.golemina == familijarna)
  80.                 os << "family";
  81.             os << " - " << orig.price() << "\n";
  82.  
  83.             return os;
  84.         }
  85. };
  86.  
  87. class FoldedPizza : public Pizza{
  88.     private:
  89.         bool isWhite;
  90.     public:
  91.         FoldedPizza(){
  92.             this->isWhite = true;
  93.         }
  94.         FoldedPizza(const char * name, const char * ingredients, const double cena) : Pizza(name, ingredients, cena){
  95.             this->isWhite = true;
  96.         }
  97.         const void setWhiteFlour(bool newWhite){
  98.             this->isWhite = newWhite;
  99.         }
  100.         const double price(){
  101.             if(this->isWhite == true)
  102.                 return this->getPrice() + this->getPrice() * 0.1;
  103.             else
  104.                 return this->getPrice() + this->getPrice() * 0.3;
  105.         }
  106.         friend ostream &operator << (ostream &os, FoldedPizza &orig){
  107.             os << orig.name << ": " << orig.ingredients << ", ";
  108.             if(orig.isWhite == true)
  109.                 os << "wf";
  110.             else if(orig.isWhite == false)
  111.                 os << "nwf";
  112.             os << " - " << orig.price() << "\n";
  113.  
  114.             return os;
  115.         }
  116. };
  117.  
  118. const void expensivePizza(Pizza ** pici, int n){
  119.     double max=pici[0]->price();
  120.     int index=0;
  121.     for(int i=1; i<n; i++){
  122.         if(pici[i]->price() == max)
  123.             continue;
  124.         if(pici[i]->price()>max){
  125.             max = pici[i]->price();
  126.             index = i;
  127.         }
  128.     }
  129.     FlatPizza * f = dynamic_cast<FlatPizza *>(pici[index]);
  130.     FoldedPizza * fo = dynamic_cast<FoldedPizza *>(pici[index]);
  131.     if(f)
  132.         cout << *f;
  133.     else
  134.         cout << *fo;
  135. }
  136.  
  137.  
  138. int main() {
  139.   int test_case;
  140.   char name[20];
  141.   char ingredients[100];
  142.   float inPrice;
  143.   Size size;
  144.   bool whiteFlour;
  145.  
  146.   cin >> test_case;
  147.   if (test_case == 1) {
  148.     // Test Case FlatPizza - Constructor, operator <<, price
  149.     cin.get();
  150.     cin.getline(name,20);
  151.     cin.getline(ingredients,100);
  152.     cin >> inPrice;
  153.     FlatPizza fp(name, ingredients, inPrice);
  154.     cout << fp;
  155.   } else if (test_case == 2) {
  156.     // Test Case FlatPizza - Constructor, operator <<, price
  157.     cin.get();
  158.     cin.getline(name,20);
  159.     cin.getline(ingredients,100);
  160.     cin >> inPrice;
  161.     int s;
  162.     cin>>s;
  163.     FlatPizza fp(name, ingredients, inPrice, (Size)s);
  164.     cout << fp;
  165.    
  166.   } else if (test_case == 3) {
  167.     // Test Case FoldedPizza - Constructor, operator <<, price
  168.     cin.get();
  169.     cin.getline(name,20);
  170.     cin.getline(ingredients,100);
  171.     cin >> inPrice;
  172.     FoldedPizza fp(name, ingredients, inPrice);
  173.     cout << fp;
  174.   } else if (test_case == 4) {
  175.     // Test Case FoldedPizza - Constructor, operator <<, price
  176.     cin.get();
  177.     cin.getline(name,20);
  178.     cin.getline(ingredients,100);
  179.     cin >> inPrice;
  180.     FoldedPizza fp(name, ingredients, inPrice);
  181.     fp.setWhiteFlour(false);
  182.     cout << fp;
  183.  
  184.   } else if (test_case == 5) {
  185.     // Test Cast - operator <, price
  186.     int s;
  187.    
  188.     cin.get();
  189.     cin.getline(name,20);
  190.     cin.getline(ingredients,100);
  191.     cin >> inPrice;
  192.     cin>>s;
  193.     FlatPizza *fp1 = new FlatPizza(name, ingredients, inPrice, (Size)s);
  194.     cout << *fp1;
  195.      
  196.     cin.get();
  197.     cin.getline(name,20);
  198.     cin.getline(ingredients,100);
  199.     cin >> inPrice;
  200.     cin>>s;
  201.     FlatPizza *fp2 = new FlatPizza(name, ingredients, inPrice, (Size)s);
  202.     cout << *fp2;
  203.      
  204.     cin.get();
  205.     cin.getline(name,20);
  206.     cin.getline(ingredients,100);
  207.     cin >> inPrice;
  208.     FoldedPizza *fp3 = new FoldedPizza(name, ingredients, inPrice);
  209.     cout << *fp3;
  210.      
  211.     cin.get();
  212.     cin.getline(name,20);
  213.     cin.getline(ingredients,100);
  214.     cin >> inPrice;
  215.     FoldedPizza *fp4 = new FoldedPizza(name, ingredients, inPrice);
  216.     fp4->setWhiteFlour(false);
  217.     cout << *fp4;
  218.      
  219.     cout<<"Lower price: "<<endl;
  220.     if(*fp1<*fp2)
  221.         cout<<fp1->price()<<endl;
  222.     else cout<<fp2->price()<<endl;
  223.        
  224.     if(*fp1<*fp3)
  225.         cout<<fp1->price()<<endl;
  226.     else cout<<fp3->price()<<endl;    
  227.        
  228.     if(*fp4<*fp2)
  229.         cout<<fp4->price()<<endl;
  230.     else cout<<fp2->price()<<endl;
  231.    
  232.     if(*fp3<*fp4)
  233.         cout<<fp3->price()<<endl;
  234.     else cout<<fp4->price()<<endl;
  235.  
  236.   } else if (test_case == 6) {
  237.     // Test Cast - expensivePizza
  238.     int num_p;
  239.     int pizza_type;
  240.  
  241.     cin >> num_p;
  242.     Pizza **pi = new Pizza *[num_p];
  243.     for (int j = 0; j < num_p; ++j) {
  244.  
  245.       cin >> pizza_type;
  246.       if (pizza_type == 1) {
  247.         cin.get();
  248.         cin.getline(name,20);
  249.          
  250.         cin.getline(ingredients,100);
  251.         cin >> inPrice;
  252.         int s;
  253.         cin>>s;
  254.         FlatPizza *fp = new FlatPizza(name, ingredients, inPrice, (Size)s, num_p);
  255.         cout << (*fp);
  256.         pi[j] = fp;
  257.       }
  258.       if (pizza_type == 2) {
  259.  
  260.         cin.get();
  261.         cin.getline(name,20);
  262.         cin.getline(ingredients,100);
  263.         cin >> inPrice;
  264.         FoldedPizza *fp =
  265.             new FoldedPizza (name, ingredients, inPrice);
  266.         if(j%2)
  267.           (*fp).setWhiteFlour(false);
  268.         cout << (*fp);
  269.         pi[j] = fp;
  270.  
  271.       }
  272.     }
  273.  
  274.     cout << endl;
  275.     cout << "The most expensive pizza:\n";
  276.     expensivePizza(pi,num_p);
  277.  
  278.  
  279.   }
  280.   return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement