Advertisement
Garey

Untitled

Dec 11th, 2018
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. #ifndef _DATE_HPP_
  6. #define _DATE_HPP_
  7.  
  8. class Date
  9. {
  10. private:
  11.     size_t day;
  12.     size_t month;
  13.     size_t year;
  14.  
  15. public:
  16.     Date() : day(1), month(1), year(1970) {};
  17.     Date(size_t day, size_t month, size_t year) :
  18.         day(day),
  19.         month(month),
  20.         year(year)
  21.     {};
  22.  
  23.     Date(const Date &obj) :
  24.         day(obj.day),
  25.         month(obj.month),
  26.         year(obj.year)
  27.         {};
  28.  
  29.     // Destructor
  30.     virtual ~Date();
  31.  
  32.     // Get Methods
  33.     inline auto getDay() -> size_t {
  34.         return this->day;
  35.     };
  36.  
  37.     inline auto getMonth() -> size_t {
  38.         return this->month;
  39.     };
  40.  
  41.     inline auto getYear() -> size_t {
  42.         return this->year;
  43.     };
  44.  
  45.     // Set Methods
  46.  
  47.     inline auto setDay(size_t value) -> void {
  48.         this->day = value;
  49.     }
  50.  
  51.     inline auto setMonth(size_t value) -> void {
  52.         this->month = value;
  53.     }
  54.  
  55.     inline auto setYear(size_t value) -> void {
  56.         this->year = value;
  57.     }
  58.  
  59.     inline auto operator < (Date &obj) -> bool {
  60.         //return ((this->year < obj.year) ? true : this->month < obj.month ? true : this->day < obj.day ? true : false );
  61.         return (this->year < obj.year || this->month < obj.month || this->day < this->day);
  62.     }
  63.  
  64.     inline auto operator== (const Date &obj) -> bool {
  65.         return (this->year == obj.year && this->month == obj.month && this->day == obj.day);
  66.     }
  67.  
  68.     auto operator+(size_t count) -> Date {
  69.         do {
  70.             if(this->day + count > 31) {
  71.  
  72.                 if(this->month % 2 == 0 && this->month != 12)
  73.                     this->day = ((this->day + count) - 30);
  74.                 else
  75.                     this->day = ((this->day + count) - 31);
  76.            
  77.  
  78.                 if(this->month > 11) {
  79.                
  80.                     this->month = 1;
  81.                     this->year++;
  82.                 }
  83.             }
  84.         } while(this->day > 31);
  85.  
  86.         return *this;
  87.     }
  88.  
  89.     auto operator-(size_t count) -> Date {
  90.         this->day -= 20;
  91.         return *this;
  92.     }
  93.  
  94.     auto operator+=(size_t count) -> Date {
  95.         if(this->day + count > 31) {
  96.  
  97.             if(this->month % 2 == 0 && this->month != 12)
  98.                 this->day = ((this->day + count) - 30);
  99.             else
  100.                 this->day = ((this->day + count) - 31);
  101.            
  102.  
  103.             if(this->month > 11) {
  104.                
  105.                 this->month = 1;
  106.                 this->year++;
  107.             }
  108.         }
  109.  
  110.         return *this;
  111.     }
  112.  
  113.     friend auto operator <<(ostream &output, const Date &obj) -> ostream& {
  114.         output << obj.day << "/" << obj.month << "/" << obj.year;
  115.  
  116.         return output;
  117.     }
  118. };
  119.  
  120.  
  121. #endif
  122.  
  123. --------------
  124.  
  125. #pragma once
  126.  
  127. #include "Date.hpp"
  128.  
  129. #include <string>
  130.  
  131. #ifndef _LIBRARY_HPP_
  132. #define _LIBRARY_HPP_
  133.  
  134. class Library
  135. {
  136. private:
  137.     string id;
  138.     string reader;
  139.     Date date;
  140.  
  141. public:
  142.     Library() : id(""), reader(""), date(Date()) {};
  143.     Library(string id, string reader, Date &date) :
  144.         id(id),
  145.         reader(reader),
  146.         date(date) {};
  147.  
  148.     Library(const Library &obj) :
  149.         id(obj.id),
  150.         reader(obj.reader),
  151.         date(obj.date) {};
  152.  
  153.     // Destructor
  154.     virtual ~Library(void);
  155.  
  156.     // Get Methods
  157.     inline auto getId() -> string {
  158.         return this->id;
  159.     }
  160.  
  161.     inline auto getReader() -> string {
  162.         return this->reader;
  163.     }
  164.  
  165.     inline auto getDate() -> Date& {
  166.         return this->date;
  167.     }
  168.  
  169.     // Operators
  170.     auto operator<(Library &obj) -> bool {
  171.         return (this->date < obj.date);
  172.     }
  173.  
  174.     auto operator==(const Library &obj) -> bool {
  175.         return (this->id == obj.id && this->reader == obj.reader && this->date == obj.date);
  176.     }
  177.  
  178.     friend auto operator<<(ostream &output, const Library &obj) -> ostream& {
  179.  
  180.         output << "Book ID: " << obj.id << endl;
  181.         output << "Reader Name: " << obj.reader << endl;
  182.         output << "Date: " << obj.date << endl;
  183.  
  184.         return output;
  185.     }
  186.  
  187. };
  188.  
  189. #endif
  190.  
  191. ----------------
  192.  
  193. #include "Library.hpp"
  194.  
  195. #include <vector>
  196. #include <algorithm>
  197.  
  198. int main() {
  199.  
  200.     vector<Library> array_libraries;
  201.     vector<Date> array_date;
  202.     Date now(11, 12, 2018);
  203.  
  204.     array_date.push_back(Date(25, 10, 2018));
  205.     array_date.push_back(Date(5, 12, 2019));
  206.  
  207.     array_libraries.push_back(Library("1", "Georgi", array_date[1]));
  208.     array_libraries.push_back(Library("2", "Petur", array_date[0]));
  209.  
  210.     for(auto &element : array_libraries) {
  211.         cout << element;
  212.     }
  213.  
  214.     sort(array_libraries.begin(), array_libraries.end(), [](Library &left, Library &right) {
  215.         return left.getDate() < right.getDate();
  216.     });
  217.  
  218.     cout << "\n\nSorted: \n\n";
  219.  
  220.     for(auto &element : array_libraries) {
  221.         cout << element;
  222.     }
  223.  
  224.     cout << "\n\nLate: \n\n";
  225.  
  226.     for(auto &element :array_libraries) {
  227.  
  228.         if(element.getDate() < (now -20)) {
  229.             cout << element;
  230.         }
  231.     }
  232.  
  233.     return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement