SHARE
TWEET

Final

a guest Aug 18th, 2019 118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <exception>
  3. #include <string>
  4. #include <sstream>
  5. #include <iomanip>
  6. #include <map>
  7. #include <set>
  8. using namespace std;
  9.  
  10. class Date {
  11. public:
  12.     Date() {};
  13.  
  14.     Date(const int& year, const int& month, const int& day) {
  15.            if (month < 1 || month > 12) {
  16.             throw invalid_argument("Month value is invalid: " + to_string(month));
  17.         }
  18.  
  19.         if (day < 1 || day > 31) {
  20.             throw invalid_argument("Day value is invalid: " + to_string(day));
  21.         }
  22.  
  23.         year_ = year;
  24.         month_ = month;
  25.         day_ = day;
  26.     }
  27.  
  28.     int GetYear() const { return year_; }
  29.  
  30.     int GetMonth() const { return month_; }
  31.  
  32.     int GetDay() const { return day_; }
  33.  
  34. private:
  35.     int year_;
  36.     int month_;
  37.     int day_;
  38. };
  39.  
  40. bool operator<(const Date& lhs, const Date& rhs)
  41. {
  42.     if (lhs.GetYear() < rhs.GetYear())
  43.         return true;
  44.     else if (lhs.GetYear() == rhs.GetYear() && lhs.GetMonth() < rhs.GetMonth())
  45.         return true;
  46.     else if (lhs.GetMonth() == rhs.GetMonth() && lhs.GetDay() < rhs.GetDay())
  47.         return true;
  48.     else return false;
  49. }
  50.  
  51.  
  52. stringstream& operator>> (stringstream& stream, Date& date) {
  53.     string date_string;
  54.     stream >> date_string;
  55.     stringstream datestream(date_string);
  56.     int year, month, day;
  57.     datestream >> year;
  58.     if (datestream.peek() != '-') {
  59.         throw invalid_argument("Wrong date format: " + date_string);
  60.     }
  61.     datestream.ignore( );
  62.     datestream >> month;
  63.     if (datestream.peek() != '-') {
  64.         throw invalid_argument("Wrong date format: " + date_string);
  65.     }
  66.     datestream.ignore();
  67.     datestream >> day;
  68.     string tail;
  69.     datestream >> tail;
  70.     if (tail !="" ) {
  71.         throw invalid_argument("Wrong date format: " + date_string);
  72.     }
  73.     date = { year, month, day };
  74.     return stream;
  75. }
  76.  
  77. ostream& operator<< (ostream& stream, const Date& date) {
  78.     stream << setw(4) << setfill('0') << date.GetYear() << '-'
  79.         << setw(2) << setfill('0') << date.GetMonth() << '-'
  80.         << setw(2) << setfill('0') << date.GetDay();
  81.     return stream;
  82. }
  83.  
  84. class Database {
  85. public:
  86.     void AddEvent(const Date& date, const string& event) {
  87.         events_[date].insert(event);
  88.     }
  89.  
  90.     bool DeleteEvent(const Date& date, const string& event) {
  91.         if (events_.count(date) && events_[date].count(event)) {
  92.             events_[date].erase(event);
  93.             return true;
  94.         }
  95.  
  96.         return false;
  97.     }
  98.  
  99.     int  DeleteDate(const Date& date) {
  100.         int events_count = events_[date].size();
  101.         events_.erase(date);
  102.         return events_count;
  103.     }
  104.  
  105.     set<string> Find(const Date& date) const {
  106.         set<string> events;
  107.         if (events_.count(date) > 0) {
  108.             try {
  109.                 events = events_.at(date);
  110.             }
  111.             catch (exception& ex) {}
  112.         }
  113.         return events;
  114.     }
  115.  
  116.     void Print() const {
  117.         for (auto item : events_) {
  118.             for (auto event : item.second) {
  119.                 cout << item.first << ' ' << event << endl;
  120.             }
  121.         }
  122.     }
  123. private:
  124.     map<Date, set<string>> events_;
  125. };
  126.  
  127. int main() {
  128.     Database db;
  129.  
  130.     string line;
  131.     while (getline(cin, line)) {
  132.         stringstream stream(line);
  133.         string cmd;
  134.         stream >> cmd;
  135.  
  136.         if (cmd == "Add") {
  137.             Date date;
  138.             try {
  139.                 stream >> date;
  140.             }
  141.             catch (invalid_argument& ex) {
  142.                 cout << ex.what() << endl;
  143.                 return 0;
  144.             }
  145.  
  146.             string event;
  147.             stream >> event;
  148.  
  149.             db.AddEvent(date, event);
  150.         }
  151.         else if (cmd == "Del") {
  152.             Date date;
  153.             try {
  154.                 stream >> date;
  155.             }
  156.             catch (invalid_argument& ex) {
  157.                 cout << ex.what() << endl;
  158.                 return 0;
  159.             }
  160.  
  161.             string event;
  162.             stream >> event;
  163.  
  164.             if (event != "") {
  165.                 cout << (db.DeleteEvent(date, event) ? "Deleted successfully" : "Event not found") << endl;
  166.             }
  167.             else {
  168.                 cout << "Deleted " << db.DeleteDate(date) << " events" << endl;
  169.             }
  170.         }
  171.         else if (cmd == "Find") {
  172.             Date date;
  173.             try {
  174.                 stream >> date;
  175.             }
  176.             catch (invalid_argument& ex) {
  177.                 cout << ex.what() << endl;
  178.                 return 0;
  179.             }
  180.  
  181.             for (auto event : db.Find(date)) {
  182.                 cout << event << endl;
  183.             }
  184.         }
  185.         else if (cmd == "Print") {
  186.             db.Print();
  187.         }
  188.         else if (cmd != "") {
  189.             cout << "Unknown command: " << cmd << endl;
  190.             return 0;
  191.         }
  192.     }
  193.  
  194.     return 0;
  195. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top