Advertisement
add1ctus

3. Hotel

May 15th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4.  
  5. class Restaurant
  6. {
  7.     char* name;
  8.     int price;
  9. public:
  10.     Restaurant()
  11.     {
  12.         name=new char[0];
  13.     }
  14.     Restaurant(char* n, int c)
  15.     {
  16.         name = new char[strlen(n)+1];
  17.         strcpy(name,n);
  18.         price=c;
  19.     }
  20.     Restaurant(const Restaurant &rhs)
  21.     {
  22.         name=new char[strlen(rhs.name)+1];
  23.         strcpy(name,rhs.name);
  24.         price=rhs.price;
  25.     }
  26.     int getPrice()
  27.     {
  28.         return price;
  29.     }
  30.     ~Restaurant()
  31.     {
  32.         delete[] name;
  33.     }
  34. };
  35.  
  36. class Hotel
  37. {
  38.     char name[15];
  39.     int prices[12];
  40.     int stars;
  41.     bool hasRestaurant;
  42.     Restaurant* restaurant;
  43. public:
  44.     Hotel(){}
  45.     Hotel(char *n, int *p, int s)
  46.     {
  47.         hasRestaurant=false;
  48.         strcpy(name,n);
  49.         for(int i=0;i<12;i++)
  50.             prices[i]=p[i];
  51.         stars=s;
  52.     }
  53.     Hotel &operator++()
  54.     {
  55.         if(stars<5)
  56.             ++stars;
  57.         return *this;
  58.     }
  59.     friend ostream &operator<< (ostream &output, const Hotel &rhs)
  60.     {
  61.         output<<rhs.name<<" "<<rhs.stars;
  62.         if(rhs.hasRestaurant)
  63.             output<<" with restaurant";
  64.             output<<endl;
  65.         return output;
  66.     }
  67.     bool operator>=(int s)
  68.     {
  69.         return this->stars>=s;
  70.     }
  71.     int computeDailyPrice(int m)
  72.     {
  73.         if(m<1 || m>12)
  74.             throw 0;
  75.         if(hasRestaurant)
  76.             return prices[m-1]+restaurant->getPrice();
  77.         return prices[m-1];
  78.     }
  79.     void setRestoran(Restaurant &r)
  80.     {
  81.         hasRestaurant=true;
  82.         restaurant=new Restaurant(r);
  83.     }
  84.     ~Hotel(){}
  85. };
  86.  
  87.  
  88. int main(){
  89.  
  90.    Hotel h; //creating object of class Hotel
  91.  
  92.    // in the following part the hotel info is read
  93.    char imeHotel[15],imeRestoran[30];
  94.    int ceni[12];
  95.    int broj,cenaRestoran;
  96.    bool daliRestoran;
  97.    cin>>imeHotel;
  98.    for (int i=0;i<12;i++) cin>>ceni[i];
  99.    cin>>broj;
  100.    cin>>daliRestoran;
  101.  
  102.    // if it is restaurant
  103.    if (daliRestoran) {
  104.       cin>>imeRestoran>>cenaRestoran;
  105.       Hotel pom(imeHotel,ceni,broj);
  106.       Restaurant r(imeRestoran,cenaRestoran);
  107.       pom.setRestoran(r);
  108.       h=pom;
  109.    }
  110.    else{
  111.       Hotel *pok=new Hotel(imeHotel,ceni,broj);
  112.       h=*pok;
  113.    }
  114.  
  115.    // increment stars for 2
  116.    ++h;
  117.    ++h;
  118.  
  119.    int cena;
  120.    int mesec;
  121.    cin>>mesec;
  122.    try{
  123.    cena=h.computeDailyPrice(mesec); //here the function is called
  124.    cout<<"Hotel info:"<<endl;
  125.    cout<<h;
  126.    if (h>=4)
  127.        cout<<"The hotel has more than 4 stars.\n";
  128.  
  129.    cout<<"The price in month "<<mesec<<" is "<<cena; //се печати цената за дадениот месец
  130.    }
  131.    catch (int){
  132.       cout<<"The index is out of bounds!";
  133.    }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement