-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.cpp
More file actions
185 lines (165 loc) · 3.96 KB
/
User.cpp
File metadata and controls
185 lines (165 loc) · 3.96 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "User.h"
User::User()
{
this->name = "";
this->password = "";
this->myPlaylists.clear();
}
User::User(std::string name, std::string password)
{
this->name = name;
this->password = password;
}
std::string User::getName() const
{
return this->name;
}
std::string User::getPassword() const
{
return this->password;
}
void User::setName(std::string name)
{
this->name = name;
}
void User::setPassword(std::string password)
{
this->password = password;
}
void User::writePlaylistToFile(Playlist playlist)
{
try {
std::ofstream g(name + "Playlist.txt", std::ios::app);
g << playlist.getNameOfPlaylist() << std::endl;
g.close();
}
catch (std::exception const& e) {
std::cout << "There was error:" << e.what() << std::endl;
}
}
void User::createFileOfPlaylist(std::string fileName)
{
try {
std::ofstream g(fileName + ".txt", std::ios::app);
g.close();
}
catch (std::exception const& e) {
std::cout << "There was error:" << e.what() << std::endl;
}
}
bool User::createNewPlaylist(std::string nameOfPlaylist)
{
Playlist newPlaylist(nameOfPlaylist);
if (!isPlaylistExist(newPlaylist)) {
createFileOfPlaylist(nameOfPlaylist);
myPlaylists.push_back(newPlaylist);
writePlaylistToFile(newPlaylist);
std::cout << "Playlist is created successfully" << std::endl;
return true;
}
return false;
}
void User::readMyPlaylistsFromFile()
{
try {
std::string line;
std::string nameOfPlaylist;
std::ifstream f(name + "Playlist.txt");
while (std::getline(f, line)) {
std::istringstream is(line);
is >> nameOfPlaylist;
myPlaylists.push_back(Playlist(nameOfPlaylist));
}
}
catch (std::exception const& e) {
std::cout << "There was error:" << e.what() << std::endl;
}
}
void User::printMyPlaylists()
{
for (Playlist& p : myPlaylists) {
std::cout << p.getNameOfPlaylist() << std::endl;
}
}
bool User::isPlaylistExist(Playlist newPlaylist)
{
for (Playlist &p : myPlaylists) {
if (p.getNameOfPlaylist() == newPlaylist.getNameOfPlaylist()) {
return true;
}
}
return false;
}
void User::printSongsInPlaylist(std::string& nameOfPlaylist)
{
for (Playlist& p : myPlaylists) {
if (p.getNameOfPlaylist() == nameOfPlaylist) {
p.printSongsInPlaylist();
}
}
}
void User::addSongToPlaylist(std::string nameOfPlaylist, Song& newSong)
{
for (Playlist& p : myPlaylists) {
if (p.getNameOfPlaylist() == nameOfPlaylist) {
addSongToFilePlaylist(nameOfPlaylist, newSong);
p.addSongToPlaylist(newSong);
return;
}
}
std::cout << "Playlist not found";
}
void User::addSongToFilePlaylist(std::string nameOfFileOfPlaylist, Song newSong)
{
try {
std::ofstream g(nameOfFileOfPlaylist + ".txt", std::ios::app);
g << newSong;
g.close();
}
catch (std::exception const& e) {
std::cout << "There was error:" << e.what() << std::endl;
}
}
void User::readSongsFromPlaylistsFile()
{
for (Playlist& p : myPlaylists) {
try {
std::string line;
std::string songName;
std::string songerName;
std::string ganre;
std::string album;
int year;
float rating;
std::ifstream f(p.getNameOfPlaylist() + ".txt");
while (std::getline(f, line)) {
std::istringstream is(line);
is >> songName >> songerName >> ganre >> album >> year >> rating;
p.addSongToPlaylist(Song(songName, songerName, ganre, album, year , rating));
}
}
catch (std::exception const& e) {
std::cout << "There was error:" << e.what() << std::endl;
}
}
}
bool User::isSongExistInPlaylist(std::string nameOfPlaylist, std::string newSong)
{
for (Playlist &p : myPlaylists) {
if (p.getNameOfPlaylist() == nameOfPlaylist) {
if (p.checkSong(newSong)) {
return true;
}
}
}
return false;
}
void User::readData()
{
readMyPlaylistsFromFile();
readSongsFromPlaylistsFile();
}
void User::print() const
{
std::cout << getName() << " " << getPassword() << std::endl;
}