Advertisement
Guest User

Alcoholic Drink

a guest
May 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class AlcoholicDrink {
  5. protected:
  6.     char name[100];
  7.     char country[100];
  8.     float percentAlch;
  9.     float basePrice;
  10. public:
  11.     static int discount;
  12.     AlcoholicDrink(){
  13.      strcpy(name,"");
  14.      strcpy(country,"");
  15.      percentAlch=0;
  16.      basePrice=0;
  17.     }
  18.     AlcoholicDrink(float percentAlch1, char *name1,char *country1,float basePrice1){
  19.        strcpy(name,name1);
  20.      strcpy(country,country1);
  21.      percentAlch=percentAlch1;
  22.      basePrice=basePrice1;
  23.     }
  24.     virtual ~AlcoholicDrink(){}
  25.    virtual float computePrice()=0;
  26.  
  27.    bool operator <(AlcoholicDrink &ad){
  28.     if(this->computePrice()<ad.computePrice()){
  29.         return true;
  30.     }else{
  31.     return false;
  32.     }
  33.    }
  34.    static void changeDiscount(int d){
  35.    discount=d;
  36.    }
  37.    static void total(AlcoholicDrink** ad, int n){
  38.     float suma=0;
  39.     for(int i=0;i<n;i++){
  40.         suma+=ad[i]->computePrice();
  41.     }
  42.     cout<<"Total price: "<<suma<<endl;
  43.     cout<<"Total price with discount: "<<suma-suma*discount/100<<endl;
  44.    }
  45.    friend ostream& operator<<(ostream &out,AlcoholicDrink &ad){
  46.    out<<ad.name<<" "<<ad.country<<" "<<ad.computePrice();
  47.    return out;
  48.    }
  49. };
  50. int AlcoholicDrink::discount=5;
  51. class Beer:public AlcoholicDrink{
  52.  private:
  53.      int bar_wheat;
  54.  public:
  55.     Beer(){
  56.     bar_wheat=0;
  57.     }
  58.     Beer(float percentAlch1, char *name1,char *country1,float basePrice1,int bar_wheat1)
  59.     :AlcoholicDrink(percentAlch1,name1,country1,basePrice1){
  60.      bar_wheat=bar_wheat1;
  61.     }
  62.      float computePrice(){
  63.      if(strcmp(country,"Germany")==0){
  64.         if(bar_wheat==0){
  65.             return basePrice+basePrice*0.05+basePrice*0.1;
  66.         }else{
  67.          return basePrice+basePrice*0.05;
  68.         }
  69.      }else{
  70.          if(bar_wheat==0){
  71.             return basePrice+basePrice*0.1;
  72.         }else{
  73.          return basePrice;
  74.         }
  75.      }
  76.     }
  77.  
  78. };
  79. class Wine:public AlcoholicDrink{
  80.  private:
  81.      int year;
  82.      char grapes[20];
  83.  public:
  84.     Wine(){
  85.     year=0;
  86.     strcpy(grapes,"");
  87.     }
  88.     Wine(float percentAlch1,char *name1,char *country1,float basePrice1,int year1, char *grapes1)
  89.     :AlcoholicDrink(percentAlch1,name1,country1,basePrice1){
  90.         strcpy(grapes,grapes1);
  91.      year=year1;
  92.     }
  93.  
  94.     float computePrice(){
  95.      if(strcmp(country,"Italy")==0){
  96.         if(year<2005){
  97.             return basePrice+basePrice*0.05+basePrice*0.15;
  98.         }else{
  99.          return basePrice+basePrice*0.05;
  100.         }
  101.      }else{
  102.          if(year<2005){
  103.             return basePrice+basePrice*0.15;
  104.         }else{
  105.          return basePrice;
  106.         }
  107.      }
  108.     }
  109. };
  110.  
  111. void lowestPrice(AlcoholicDrink **a, int n){
  112. float minPrice=a[0]->computePrice();
  113. int cuvajIndeks;
  114. for(int i=0;i<n;i++){
  115.     if(a[i]->computePrice()<minPrice){
  116.         minPrice=a[i]->computePrice();
  117.         cuvajIndeks=i;
  118.     }
  119.  
  120. }
  121.  cout<<*(a[cuvajIndeks]);
  122. }
  123.  
  124.  
  125. int main() {
  126.     int testCase;
  127.     cin >> testCase;
  128.     float p;
  129.     char name[100];
  130.     char country[100];
  131.     float price;
  132.     bool mainI;
  133.     int year;
  134.     char grape [20];
  135.     if(testCase == 1) {
  136.         cout << "===== TESTING CONSTRUCTORS ======" << endl;
  137.         cin >> p;
  138.         cin >> name;
  139.         cin >> country;
  140.         cin >> price;
  141.         cin >> mainI;
  142.         Beer b(p, name, country, price, mainI);
  143.         cout << b << endl;
  144.         cin >> p;
  145.         cin >> name;
  146.         cin >> country;
  147.         cin >> price;
  148.         cin >> year;
  149.         cin >> grape;
  150.         Wine w(p, name, country, price, year, grape);
  151.         cout << w << endl;
  152.  
  153.     } else if(testCase == 2) {
  154.         cout << "===== TESTING LOWEST PRICE ======" << endl;
  155.         int n;
  156.         cin >> n;
  157.         AlcoholicDrink** ad = new AlcoholicDrink*[n];
  158.         for(int i = 0; i < n; ++i) {
  159.             cin >> p;
  160.             cin >> name;
  161.             cin >> country;
  162.             cin >> price;
  163.  
  164.             if(i % 2 == 1){
  165.                 cin >> mainI;
  166.                 ad[i] = new Beer(p, name, country, price, mainI);
  167.             }
  168.             else {
  169.                 cin >> year;
  170.                 cin >> grape;
  171.                 ad[i] = new Wine(p, name, country, price, year, grape);
  172.             }
  173.         }
  174.  
  175.         lowestPrice(ad, n);
  176.         for(int i = 0; i < n; ++i) {
  177.             delete ad[i];
  178.         }
  179.         delete [] ad;
  180.     } else if(testCase == 3) {
  181.         cout << "===== TESTING DISCOUNT STATIC ======" << endl;
  182.         int n;
  183.         cin >> n;
  184.         AlcoholicDrink** ad = new AlcoholicDrink*[n];
  185.         for(int i = 0; i < n; ++i) {
  186.             cin >> p;
  187.             cin >> name;
  188.             cin >> country;
  189.             cin >> price;
  190.             if(i % 2 == 1){
  191.                 cin >> mainI;
  192.                 ad[i] = new Beer(p, name, country, price, mainI);
  193.             }
  194.             else {
  195.                 cin >> year;
  196.                 cin >> grape;
  197.                 ad[i] = new Wine(p, name, country, price, year, grape);
  198.             }
  199.         }
  200.         AlcoholicDrink::total(ad, n);
  201.         int d;
  202.         cin >> d;
  203.         AlcoholicDrink::changeDiscount(d);
  204.         AlcoholicDrink::total(ad, n);
  205.         for(int i = 0; i < n; ++i) {
  206.             delete ad[i];
  207.         }
  208.         delete [] ad;
  209.     }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement