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();