-
Notifications
You must be signed in to change notification settings - Fork 0
/
single-responsibility-principle.cpp
133 lines (111 loc) · 3.08 KB
/
single-responsibility-principle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
struct Journal
{
std::string title;
std::vector<std::string> entries;
Journal() = default;
Journal(const std::string& title) : title(title) {}
void add_entry(const std::string& entry)
{
static int count = 1;
entries.push_back(std::to_string(count++) + ": " + entry);
}
friend std::ostream& operator<<(std::ostream& os, const Journal& journal)
{
os << "Title: " << journal.title << "\n";
for (const std::string& entry : journal.entries)
{
os << entry << "\n";
}
return os;
}
friend std::istream& operator>>(std::istream& is, Journal& journal)
{
std::string title;
is >> title;
// Get the part after the colon in the first line.
title = title.substr(title.find(':') + 1);
// Ignore the newline character after the colon.
// title.pop_back();
journal.title = title;
std::string line;
while (std::getline(is, line))
{
journal.entries.push_back(line);
}
return is;
}
void save(const std::string& filename)
{
std::ofstream ofs(filename);
ofs << *this;
}
void load(const std::string& filename)
{
std::ifstream ifs(filename);
ifs >> *this;
}
};
struct BetterJournal
{
std::string title;
std::vector<std::string> entries;
BetterJournal() = default;
BetterJournal(const std::string& title) : title(title) {}
void add_entry(const std::string& entry)
{
static int count = 1;
entries.push_back(std::to_string(count++) + ": " + entry);
}
friend std::ostream& operator<<(std::ostream& os, const BetterJournal& journal)
{
os << "Title: " << journal.title << "\n";
for (const std::string& entry : journal.entries)
{
os << entry << "\n";
}
return os;
}
friend std::istream& operator>>(std::istream& is, BetterJournal& journal)
{
// Get the part after the colon in the first line as title.
std::string title;
is >> title;
journal.title = title.substr(title.find(':') + 1);
std::string line;
while (std::getline(is, line))
{
journal.entries.push_back(line);
}
return is;
}
};
struct PersistenceManager
{
static void save(const BetterJournal& journal, const std::string& filename)
{
std::ofstream ofs(filename);
ofs << journal;
}
static void load(BetterJournal& journal, const std::string& filename)
{
std::ifstream ifs(filename);
ifs >> journal;
}
};
int main()
{
BetterJournal journal("Dear Diary");
journal.add_entry("I ate a bug");
journal.add_entry("I cried today");
std::cout << journal << std::endl;
PersistenceManager::save(journal, "diary.txt");
BetterJournal journal2;
PersistenceManager::load(journal2, "diary.txt");
std::cout << journal2 << std::endl;
return 0;
}