-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist.cpp
221 lines (191 loc) · 6.57 KB
/
playlist.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// playlist.cpp
// CS2Final
//
// Created by Samantha-Jo Cunningham on 5/7/18.
// Copyright © 2018 Samantha-Jo Cunningham. All rights reserved.
//
#include <sstream>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include <ctime>
#include "playlist.h"
#include "song.h"
#include <algorithm>
#include "sort.h"
using namespace std;
Playlist::Playlist(){}
string Playlist::playlistName(string nameofPlaylist) {
cout << "Name your playlist: ";
cin.get();
getline(cin, nameofPlaylist);
cout << endl;
return nameofPlaylist;
}
void Playlist::loadPlaylist(){
ifstream inFS; //create an input file
vector<string> playlist;
inFS.open("playlist.txt"); //opens the input file playlist.txt
// checks to see if the input file opens and if it did not it lets the user knows it could not opened
if (!inFS.is_open()) {
cout << "Could not open file playlist.txt" << endl;
return;
}
//create variables
string artist;
string title;
string duration;
string genre;
string likes;
while (!inFS.eof()){ //loops through the file until it gets to the end of the file
getline (inFS, likes, '\t');//gets the complete string that is followed by a tab
playlist.push_back(likes);
getline (inFS, artist, '\t');
playlist.push_back(artist);
getline (inFS, title, '\t');
playlist.push_back(title);
getline (inFS, duration, '\t');
playlist.push_back(duration);
getline (inFS, genre, '\n'); //gets the complete string that is followed by the end of the line
playlist.push_back(genre);
}
cout << "Playlist loaded" << endl;
return;
}
void Playlist::playPlaylist(){
ifstream inFS; //create an input file
ifstream inFS2; //create an input file
ofstream outFS; // create an output filestream
vector<string> MyPlaylist;
vector<int> Songlikes;
song playlistSongs;
inFS.open("playlist.txt"); //opens the input file playlist.txt
//checks to see if the input file opens and if it did not it lets the user knows it could not opened
if (!inFS.is_open()) {
cout << "Could not open file playlist.txt" << endl;
return;
}
outFS.open("sortedPlaylist.txt"); //create a output file to store the sorted playlist
if (!outFS.is_open()) {
cout << "Could not open file sortedPlaylist.txt" << endl;
return;
}
//create variables
string artist;
string playlists;
string songs;
string title;
string duration;
string genre;
string likes;
while (!inFS.eof()){ //loops through the file until it gets to the end of the file
getline (inFS, playlists, '\n'); //push everysong into the MyPlaylist vector
if(playlists != ""){
MyPlaylist.push_back(playlists);
}
}
unsigned long int Playlistsize = MyPlaylist.size();
sort(MyPlaylist.rbegin(), MyPlaylist.rend()); //sort the playlist in descending order
for(int i = 0; i < Playlistsize; i++){
outFS << MyPlaylist.at(i); //Store the sorted playlist in an output file
if(i != Playlistsize-1){
outFS << "\n";
}
}
outFS.close(); //closes the output file
inFS2.open("sortedPlaylist.txt"); //opens the input file - sortedPlaylist.txt
// checks to see if the input file opens and if it did not it lets the user knows it could not opened
if (!inFS2.is_open()) {
cout << "Could not open file sortedPlaylist.txt" << endl;
return;
}
//for (unsigned long int j = Playlistsize; j > 0; j--){
while (!inFS2.eof()){ //loops through the file until it gets to the end of the file
getline(inFS2, likes, '\t');
playlistSongs.SetLikes(likes); //gets the complete string that is followed by a tab
getline(inFS2, artist, '\t');
playlistSongs.SetArtist(artist);
getline (inFS2, title, '\t');
playlistSongs.SetTitle(title);
getline (inFS2, duration, '\t');
playlistSongs.SetDuration(duration);
getline (inFS2, genre, '\n');//gets the complete string that is followed by the end of the line
playlistSongs.SetGenre(genre);
//outputs to file file class_songs.txt
cout << playlistSongs.GetLikes() << ". " << "\"" << playlistSongs.GetTitle() << "\" by " << playlistSongs.GetArtist() << " with a duration of " << playlistSongs.GetDuration() << " and genre of " << playlistSongs.GetGenre() << endl;
if(inFS2.eof()){break;};
}
inFS.close(); //closes the playlist.txt input file
inFS2.close(); //closes the playlist.txt input file
}
void Playlist::AddNewSong(int max_likes){
string artist, dummy;
string title;
string duration;
string genre;
int likes;
cout << "\tSong Addition\n\n";
cin.get();
try {
cout << "Song Name: ";
//cin.get();
getline(cin,title); cout << endl;
if (title == "") {
throw runtime_error("Invalid Title");
}
}
catch (runtime_error& excpt){
cout << excpt.what() << endl;
cout << "Enter a valid title" << endl;
}
try {
cout << "Song Artist: ";
//cin.get();
getline(cin,artist); cout << endl;
if (artist == "") {
throw runtime_error("Invalid Artist");
}
}
catch (runtime_error& excpt){
cout << excpt.what() << endl;
cout << "Enter a valid title" << endl;
}
try {
cout << "Song Duration: ";
//cin.get();
getline(cin,duration); cout << endl;
if (duration == "") {
throw runtime_error("Invalid Duration");
}
}
catch (runtime_error& excpt){
cout << excpt.what() << endl;
cout << "Enter a valid duration" << endl;
}
try {
cout << "Song Genre: ";
//cin.get();
getline(cin, genre); cout << endl;
if (artist == "") {
throw runtime_error("Invalid Genre");
}
}
catch (runtime_error& excpt){
cout << excpt.what() << endl;
cout << "Enter a valid Genre" << endl;
}
likes = max_likes + 1;
//Printing new song to the file
ofstream file;
file.open("playlist.txt",ios_base::app);
file << "\n" << likes << "\t" << artist << "\t" << title << "\t" << duration << "\t" << genre;
file.close();
//Now to write the new song data to the playlist file
//suggest to user to reload playlist
}
Playlist::~Playlist(){}