0

So i need to make a new repo in my app who will throw exceptions with a given probability in his constructor.

This is what i have in my repo.h file: Repository class is the main repo class which uses memory, RepositoryFile is using a txt file and then i want to make RepositoryM the repo who will throw exceptions. But i dont know how can i do this. RepositoryM should also work like a normal repository if he doesnt throw an exception.

#pragma once

#include "Domain.h"
#include <string>
#include <vector>
#include <algorithm>
#include <exception>

using namespace std;

class Repository
{
private:
    vector<Activitate> all;

public:
    Repository() = default;

    Repository(const Activitate& ot) = delete;
    virtual ~Repository()=default;

    virtual void store(const Activitate& a) {
        auto found = std::find_if(all.begin(), all.end(), [a](const Activitate& aa) {
            return aa.getDescriere() == a.getDescriere() && aa.getTitlu() == a.getTitlu();
            });
        if (found != all.end()) {
            throw ActivitateException{ "Activitate existenta !" };
        }
        all.push_back(a);
    }

    const Activitate& find(std::string titlu, std::string descriere) {
        auto found = std::find_if(all.begin(), all.end(), [&](const Activitate& aa) {
            return aa.getTitlu() == titlu && aa.getDescriere() == descriere;
            });
        if (found == all.end()) {
            throw ActivitateException{ "Activitate inexistenta !" };
        }
        return *found;
    }

    const vector<Activitate>& getAll() const {
        return all;
    }

    virtual void clearAll() {
        all.clear();
    }

    virtual int sizeAll() const {
        int i = 0;
        for (const auto& activitate : getAll()) {
            i++;
        }
        return i;
    }

    virtual void stergere(const Activitate& a) {
        auto found = std::find_if(all.begin(), all.end(), [a](const Activitate& aa) {
            return aa.getTitlu() == a.getTitlu() && aa.getDescriere() == a.getDescriere();
            });
        if (found == all.end()) {
            throw ActivitateException{ "Activitate inexistenta !" };
        }
        auto rez = all.erase(found);
    }

    virtual void modifica(const Activitate& a, const Activitate& b) {
        auto found = std::find_if(all.begin(), all.end(), [a](const Activitate& aa) {
            return aa.getTitlu() == a.getTitlu() && aa.getTip() == a.getTip();
            });
        if (found == all.end()) {
            throw ActivitateException{ "Activitate inexistenta !" };
        }
        auto rez = all.erase(found);
        all.push_back(b);
    }

};

class RepositoryFile : public Repository {

private:
    string fName;
    void loadActivitateFromFile();
    void writeActivitateToFile();

public:
    RepositoryFile(std::string fName) :Repository(), fName{ fName } {
        loadActivitateFromFile();
    }
    void store(const Activitate& act) override {
        Repository::store(act);
        writeActivitateToFile();
    }
    void stergere(const Activitate& act) override {
        Repository::stergere(act);
        writeActivitateToFile();
    }
    void modifica(const Activitate& a, const Activitate& b) override {
        Repository::modifica(a,b);
        writeActivitateToFile();
    }
    void clearAll() override {
        Repository::clearAll();
        writeActivitateToFile();
    }
};

class RepositoryM: public Repository{
private:
    int prob;
public:
    void store(const Activitate& act) override {
        Repository::store(act);
    }
    void stergere(const Activitate& act) override {
        Repository::stergere(act);
    }
    void modifica(const Activitate& a, const Activitate& b) override {
        Repository::modifica(a, b);
    }
    void clearAll() override {
        Repository::clearAll();
    }
    RepositoryM(int prob) :Repository(), prob{ prob } {

    }
};

void testeRepo();
Luke
  • 7
  • 3
  • What do you mean with *throwing exception with certain probability*? Does it means it should *sometimes* throwing an exception if the failure occurred, and sometimes not? It means you have unpredectict behaviour of your code. I guess it is not wished. Otherwise please check [this](https://stackoverflow.com/questions/810839/throwing-exceptions-from-constructors) – dboy May 01 '20 at 11:42
  • Does this answer your question? [Throwing exceptions from constructors](https://stackoverflow.com/questions/810839/throwing-exceptions-from-constructors) – dboy May 01 '20 at 11:43

1 Answers1

0

You can use the random library. So the contructor of RepositoryM would look like the following

RepositoryM(int prob) :Repository(), prob{ prob } {
    if (((double) rand() / RAND_MAX) < ((double) prob / 100))
        throw "some error";
    std::cout << "Running normally" << std::endl;
}

Don't forget to include <random> and <ctime>

Teshan Shanuka J
  • 1,448
  • 2
  • 17
  • 31