diff --git a/iFacility/iFacility.pro b/iFacility/iFacility.pro index d294eb0..a31255d 100644 --- a/iFacility/iFacility.pro +++ b/iFacility/iFacility.pro @@ -12,12 +12,14 @@ SOURCES += \ main.cpp \ loginwindow.cpp \ objects/profession.cpp \ - objects/user.cpp + objects/user.cpp \ + objects/userprofession.cpp HEADERS += \ loginwindow.h \ objects/profession.h \ - objects/user.h + objects/user.h \ + objects/userprofession.h FORMS += \ loginwindow.ui diff --git a/iFacility/objects/user.cpp b/iFacility/objects/user.cpp index 26f4614..b423a0d 100644 --- a/iFacility/objects/user.cpp +++ b/iFacility/objects/user.cpp @@ -52,35 +52,53 @@ User User::createUser(QString login, QString password, UserType userType, return u; } -bool User::addProfession(const Profession &p) { - if (p.pID() == mCurrentProfession) { - return false; - } - - if (!mProfessions.contains(p.pID())) { - if (mProfessions.size() >= 4) { - mProfessions.remove(0); +bool User::hasProfession(PID pid) { + foreach (auto prof, mProfessions) { + if (prof.getProfession() == pid) { + return true; } - mProfessions.push_back(p.pID()); } - return true; + return false; } -bool User::setCurrentProfession(const Profession &p) { - if (!mProfessions.contains(p.pID())) { +bool User::addProfession(const Profession &p, ProfRank rank) { + if (hasProfession(p.pID())) { return false; } - if (p.pID() != mCurrentProfession) { - mCurrentProfession = p.pID(); + if (mProfessions.size() >= 4) { + mProfessions.remove(0); } + UserProfession up(p.pID(), rank); + mProfessions.push_back(up); return true; } -void User::removeProfession(const Profession &p) { - mProfessions.removeAll(p.pID()); +bool User::setCurrentProfession(PID pid) { + if (!hasProfession(pid)) { + return false; + } + + mCurrentProfession = pid; + return true; +} + +void User::removeProfession(PID pid) { +// QMutableVectorIterator i(mProfessions); +// while (i.hasNext()) { +// if (i.value().getProfession() == pid) { +// i.remove(); +// } +// } + + auto pred = [pid](UserProfession p) { + return p.getProfession() == pid; + }; + + mProfessions.erase(std::remove_if(mProfessions.begin(), mProfessions.end(), pred), + mProfessions.end()); } bool operator==(const User &l, const User &r) { diff --git a/iFacility/objects/user.h b/iFacility/objects/user.h index 2f920ec..63b05a5 100644 --- a/iFacility/objects/user.h +++ b/iFacility/objects/user.h @@ -4,11 +4,13 @@ #include #include #include +#include #include "profession.h" +#include "userprofession.h" typedef QUuid UID; -typedef QVector ProfessionsList; +typedef QVector ProfessionsList; enum class UserType { ADMINISTRATOR, @@ -44,9 +46,10 @@ public: static User createUser(QString login, QString password, UserType userType, QString firstName, QString secondName, QString patronymic); - bool addProfession(const Profession &p); - bool setCurrentProfession(const Profession &p); - void removeProfession(const Profession &p); + bool hasProfession(PID pid); + bool addProfession(const Profession &p, ProfRank rank); + bool setCurrentProfession(PID pid); + void removeProfession(PID pid); friend bool operator==(const User &l, const User &r); friend QDataStream& operator<<(QDataStream &stream, const User &usr); diff --git a/iFacility/objects/userprofession.cpp b/iFacility/objects/userprofession.cpp new file mode 100644 index 0000000..d29558c --- /dev/null +++ b/iFacility/objects/userprofession.cpp @@ -0,0 +1,43 @@ +#include "userprofession.h" + +UserProfession::UserProfession(PID pid, ProfRank rank) { + mProfession = pid; + mRank = rank; +} + +PID UserProfession::getProfession() const { + return mProfession; +} + +QDate UserProfession::getAcquiredDate() const { + return mAcquired; +} + +ProfRank UserProfession::getRank() const { + return mRank; +} + +bool UserProfession::setRank(quint8 newRank) { + if (newRank < 1) { + return false; + } + + mRank = newRank; + return true; +} + +bool operator==(const UserProfession &l, const UserProfession &r) { + return l.mProfession == r.mProfession && + l.mAcquired == r.mAcquired && + l.mRank == r.mRank; +} + +QDataStream& operator<<(QDataStream &stream, const UserProfession &up) { + stream << up.mProfession << up.mAcquired << up.mRank; + return stream; +} + +QDataStream& operator>>(QDataStream &stream, UserProfession &up) { + stream >> up.mProfession >> up.mAcquired >> up.mRank; + return stream; +} diff --git a/iFacility/objects/userprofession.h b/iFacility/objects/userprofession.h new file mode 100644 index 0000000..7e9b7a1 --- /dev/null +++ b/iFacility/objects/userprofession.h @@ -0,0 +1,30 @@ +#ifndef USERPROFESSION_H +#define USERPROFESSION_H + +#include + +#include "profession.h" + +typedef quint8 ProfRank; + +class UserProfession { +private: + PID mProfession; + QDate mAcquired; + ProfRank mRank; + +public: + UserProfession() = default; + UserProfession(PID pid, ProfRank rank); + + PID getProfession() const; + QDate getAcquiredDate() const; + ProfRank getRank() const; + bool setRank(quint8 newRank); + + friend bool operator==(const UserProfession &l, const UserProfession &r); + friend QDataStream& operator<<(QDataStream &stream, const UserProfession &up); + friend QDataStream& operator>>(QDataStream &stream, UserProfession &up); +}; + +#endif // USERPROFESSION_H