diff options
renamed folder
10 files changed, 349 insertions, 0 deletions
diff --git a/Getters_and_setters_simple_examples/Beer/Beer.cpp b/Getters_and_setters_simple_examples/Beer/Beer.cpp new file mode 100644 index 0000000..2eaa444 --- /dev/null +++ b/Getters_and_setters_simple_examples/Beer/Beer.cpp @@ -0,0 +1,43 @@ +#include "Beer.h" + +Beer::Beer(const char* mark, int ml) +{ + setMark(mark); + setMl(ml); +} +void Beer::printAddress() const +{ + cout << this << endl; +} +void Beer::printBeer() const +{ + cout << mark << " " << ml << endl; +} +void Beer::setMl(int ml) +{ + if (ml < MINBEERML) + ml = MINBEERML; + else if (ml > MAXBEERML) + ml = MAXBEERML; + this->ml = ml; +} +void Beer::setMark(const char* mark) +{ + int len = strlen(mark); + + if (len >= 20 || len == 0) //ако ни дадат невалидна марка на бирата (стринг с дължина >= 20 ) + this->mark[0] = '\0'; //правим марката да е празния стринг + else + strcpy(this->mark, mark); +} +int Beer::getMl() const +{ + return ml; +} +const char* Beer::getMark() const +{ + return mark; +} + + + diff --git a/Getters_and_setters_simple_examples/Beer/Beer.h b/Getters_and_setters_simple_examples/Beer/Beer.h new file mode 100644 index 0000000..4bc8adc --- /dev/null +++ b/Getters_and_setters_simple_examples/Beer/Beer.h @@ -0,0 +1,27 @@ +#include <iostream> +using namespace std; + +const int MAXBEERML = 500; +const int MINBEERML = 330; + +class Beer +{ +public: + Beer(const char* mark, int ml); + + void printAddress() const; + + void printBeer() const; + void setMl(int ml); + + void setMark(const char* mark); + + int getMl() const; // const сложен тук означава, че метода РЅРµ променя член-данните РЅР° класа.Съответно можем РґР° РіРѕ викаме РѕС‚ константни обекти + + const char* getMark() const; + +private: + + char mark[20]; //Стринг СЃ максимална дължина 19, понеже 1 СЃРёРјРІРѕР» РЅРё трябва Р·Р° '\0' + int ml; +}; diff --git a/Getters_and_setters_simple_examples/Beer/main.cpp b/Getters_and_setters_simple_examples/Beer/main.cpp new file mode 100644 index 0000000..9e6801d --- /dev/null +++ b/Getters_and_setters_simple_examples/Beer/main.cpp @@ -0,0 +1,8 @@ +#include "Beer.h" + +int main() +{ + Beer t("Zagorka", 5); // 5 е невалидно количество бира. Но в конструктора извикваме setML, която се грижи в променливата да не попаднат невалидни данни. + t.setMl(330); + t.printBeer(); +}
\ No newline at end of file diff --git a/Getters_and_setters_simple_examples/Daily_schedule/Event.cpp b/Getters_and_setters_simple_examples/Daily_schedule/Event.cpp new file mode 100644 index 0000000..084c7b3 --- /dev/null +++ b/Getters_and_setters_simple_examples/Daily_schedule/Event.cpp @@ -0,0 +1,40 @@ +#include "Event.h" + + +Event::Event() :start(0, 0, 0), end(0, 0, 0), isInit(false) +{ } +Event::Event(const char* name, int startHours, int startMin, int startSec, int endHours, int endMin, int endSec) : start(startHours, startMin, startSec), end(endHours, endMin, endSec) +{ + if (!IsValid()) + end = start; + strcpy(this->name, name); + +} +void Event::print() const +{ + std::cout << name << std::endl; + std::cout << "start: "; + start.print(); + std::cout << "end: "; + end.print(); +} +const Time& Event::getStart() const +{ + return start; +} +const Time& Event::getEnd() const +{ + return end; +} +const char* Event::getName() const +{ + return name; +} +bool Event::getIsInit() const +{ + return isInit; +} +bool Event::IsValid() +{ + return start.CompareTo( end) >= 0; +}
\ No newline at end of file diff --git a/Getters_and_setters_simple_examples/Daily_schedule/Event.h b/Getters_and_setters_simple_examples/Daily_schedule/Event.h new file mode 100644 index 0000000..32c332d --- /dev/null +++ b/Getters_and_setters_simple_examples/Daily_schedule/Event.h @@ -0,0 +1,19 @@ +#include "Time.h" + +class Event +{ + char name[30]; + Time start; + Time end; + bool isInit; +public: + Event(); + Event(const char* name, int startHours, int startMin, int startSec, int endHours, int endMin, int endSec); + bool IsValid(); + void print() const; + const Time& getStart() const; + const Time& getEnd() const; + const char* getName() const; + bool getIsInit() const; +}; + diff --git a/Getters_and_setters_simple_examples/Daily_schedule/Schedule.cpp b/Getters_and_setters_simple_examples/Daily_schedule/Schedule.cpp new file mode 100644 index 0000000..b4c9017 --- /dev/null +++ b/Getters_and_setters_simple_examples/Daily_schedule/Schedule.cpp @@ -0,0 +1,62 @@ +#include "Schedule.h" + +int Schedule::getFirstFreeSlotIndex() +{ + for (int i = 0; i < MAXEVENTS; i++) + { + if (!events[i].getIsInit()) + return i; + } + return -1; +} +void Schedule::DeleteEvent(int ind) +{ + Event emptyEvent; + events[ind] = emptyEvent; +} + +bool Schedule::AddEvent(const char* name, int startHours, int startMin, int startSec, int endHours, int endMin, int endSec) +{ + int freeIndex = getFirstFreeSlotIndex(); + if (freeIndex == -1) + return false; + Event eventToAdd(name, startHours, startMin, startSec, endHours, endMin, endSec); + events[freeIndex] = eventToAdd; + + return true; +} +bool Schedule::RemoveEvent(const char* name) +{ + for (int i = 0; i < MAXEVENTS; i++) + { + if (strcmp(events[i].getName(), name) == 0) + { + DeleteEvent(i); + return true; + } + } + return false; +} +bool Schedule::RemoveEvent(int startHours, int startMin, int startSec, int endHours, int endMin, int endSec) +{ + Time startToRemove(startHours, startMin, startSec); + Time endToRemove(endHours, endMin, endSec); + + for (int i = 0; i < MAXEVENTS; i++) + { + if ((events[i].getStart().CompareTo(startToRemove)) == 0 && (events[i].getEnd().CompareTo(endToRemove) == 0)) + { + DeleteEvent(i); + return true; + } + } + return false; +} +void Schedule::Print() const +{ + for (int i = 0; i < MAXEVENTS; i++) + { + if (events[i].getIsInit()) + events[i].print(); + } +}
\ No newline at end of file diff --git a/Getters_and_setters_simple_examples/Daily_schedule/Schedule.h b/Getters_and_setters_simple_examples/Daily_schedule/Schedule.h new file mode 100644 index 0000000..2ad2c7a --- /dev/null +++ b/Getters_and_setters_simple_examples/Daily_schedule/Schedule.h @@ -0,0 +1,19 @@ +#include "Event.h" + +const int MAXEVENTS = 10; +class Schedule +{ +private: + Event events[MAXEVENTS]; + + int getFirstFreeSlotIndex(); + void DeleteEvent(int ind); + + +public: + bool AddEvent(const char* name, int startHours, int startMin, int startSec, int endHours, int endMin, int endSec); + bool RemoveEvent(const char* name); + bool RemoveEvent(int startHours, int startMin, int startSec, int endHours, int endMin, int endSec); + void Print() const; + bool isValidSchedule() const; +}; diff --git a/Getters_and_setters_simple_examples/Daily_schedule/Source.cpp b/Getters_and_setters_simple_examples/Daily_schedule/Source.cpp new file mode 100644 index 0000000..678eb98 --- /dev/null +++ b/Getters_and_setters_simple_examples/Daily_schedule/Source.cpp @@ -0,0 +1,37 @@ +#include <iostream> +using namespace std; + + +#include "Schedule.h" + //абстракция + //капсулация -> + +// дневен Орг -> събития +// Събитие -> начален час и краен час +// Модификатори за достъп: private, public: +// Клас: Структура, при която мод. за достъп е private по default + + + +int main() +{ + + Schedule s; + + cout<< s.AddEvent("Party", 22, 30, 00, 23, 30, 00); + + cout<< s.AddEvent("Uni", 12, 00, 00, 14, 00, 00); + + s.AddEvent("Lunch", 11, 30, 00, 12, 00, 00); + s.Print(); + cout << "after remove"<<endl; + s.RemoveEvent(12,00,00,14,00,00); + + s.Print(); + + + + + + +}
\ No newline at end of file diff --git a/Getters_and_setters_simple_examples/Daily_schedule/Time.cpp b/Getters_and_setters_simple_examples/Daily_schedule/Time.cpp new file mode 100644 index 0000000..f4fc702 --- /dev/null +++ b/Getters_and_setters_simple_examples/Daily_schedule/Time.cpp @@ -0,0 +1,73 @@ +#include "Time.h" + +Time::Time(int hours, int mins, int seconds) +{ + setHours(hours); + setMins(mins); + setSeconds(seconds); +} +/*Time() +{ +setHours(0); +setMins(0); +setSeconds(0); +}*/ +int Time::getSeconds() const +{ + return seconds; +} +int Time::getMins() const +{ + return mins; +} +int Time::getHours() const +{ + return hours; +} +void Time::print() const +{ + std::cout << hours << " " << mins << " " << seconds << std::endl; +} +void Time::setSeconds(int seconds) +{ + if (seconds < 0 || seconds > 59) + seconds = 0; + this->seconds = seconds; +} +void Time::setMins(int mins) +{ + if (mins < 0 || mins > 59) + mins = 0; + this->mins = mins; +} +void Time::setHours(int hours) +{ + if (hours < 0 || hours > 23) + hours = 0; + this->hours = hours; +} +int Time::CompareTo(const Time& other) const +{ + if (getHours() > other.getHours()) + return -1; + else if (getHours() < other.getHours()) + { + return 1; + } + else + { + if (getMins() > other.getMins()) + return -1; + else if (getMins() < other.getMins()) + return 1; + else + { + if (getSeconds() > other.getSeconds()) + return -1; + else if (getSeconds() < other.getSeconds()) + return 1; + else + return 0; + } + } +}
\ No newline at end of file diff --git a/Getters_and_setters_simple_examples/Daily_schedule/Time.h b/Getters_and_setters_simple_examples/Daily_schedule/Time.h new file mode 100644 index 0000000..97529e0 --- /dev/null +++ b/Getters_and_setters_simple_examples/Daily_schedule/Time.h @@ -0,0 +1,21 @@ +#include <iostream> + +class Time +{ + int seconds; + int mins; + int hours; +public: + Time(int hours, int mins, int seconds); + //Time(); + int getSeconds() const; + int getMins() const; + int getHours() const; + void setSeconds(int seconds); + void setMins(int mins); + void setHours(int hours); + + void print() const; + + int CompareTo(const Time& other) const; +}; |
