Загрузка данных


#include <iostream>
#include <vector>
#include <memory>

using namespace std;

class Apartment
{
private:
    double area_;
    int rooms_;

public:
    Apartment(double area, int rooms);
    ~Apartment();

    friend ostream& operator<<(ostream& out, const Apartment& apartment);
};

class Building
{
private:
    vector<unique_ptr<Apartment>> apartments_;

public:
    Building();
    ~Building();

    void addApartment(double area, int rooms);
    void removeApartment(int index);
    void demolish();

    friend ostream& operator<<(ostream& out, const Building& building);
};

Apartment::Apartment(double area, int rooms) : area_(area), rooms_(rooms)
{
    cout << "Квартира создана" << endl;
}

Apartment::~Apartment()
{
    cout << "Квартира удалена" << endl;
}

ostream& operator<<(ostream& out, const Apartment& apartment)
{
    out << "Площадь: " << apartment.area_
        << ", комнат: " << apartment.rooms_;

    return out;
}

Building::Building() : apartments_()
{
    cout << "Постройка создана" << endl;
}

Building::~Building()
{
    cout << "Постройка удалена" << endl;
}

void Building::addApartment(double area, int rooms)
{
    apartments_.push_back(make_unique<Apartment>(area, rooms));
}

void Building::removeApartment(int index)
{
    if (index >= 0 && index < static_cast<int>(apartments_.size()))
    {
        apartments_.erase(apartments_.begin() + index);
        cout << "Квартира удалена из постройки" << endl;
    }
    else
    {
        cout << "Неверный индекс" << endl;
    }
}

void Building::demolish()
{
    apartments_.clear();
    cout << "Постройка снесена" << endl;
}

ostream& operator<<(ostream& out, const Building& building)
{
    out << "\nКвартиры в постройке:" << endl;

    if (building.apartments_.empty())
    {
        out << "Нет квартир" << endl;
    }
    else
    {
        for (int i{}; i < static_cast<int>(building.apartments_.size()); i++)
        {
            out << i + 1 << ". "
                << *building.apartments_.at(i)
                << endl;
        }
    }

    return out;
}

int main()
{
    setlocale(LC_ALL, "Russian");

    Building building;

    building.addApartment(45.5, 2);
    building.addApartment(62.3, 3);
    building.addApartment(80.0, 4);

    cout << building << endl;

    building.removeApartment(1);

    cout << building << endl;

    building.demolish();

    cout << building << endl;

    return 0;
}