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
|
//This is the implementation file for SongList class
#include "songlist.h"
#include "tools.h"
//SongList implementation functions
//default constructor
SongList::SongList()
{
size = 0;
head->prev = nullptr;
head->next = tail;
tail->prev = head;
tail->next = nullptr;
Node * next = NULL;
Node * prev = NULL;
}
//constructor from file
SongList::SongList(const char fileName[])
{
ifstream inFile;
Song aSong;
char tempTitle[MAXCHAR], tempArtist[MAXCHAR], tempAlbum[MAXCHAR];
int tempMin = 5;
int tempSec = 5;
inFile.open(fileName);
head = new Node();
tail = new Node();
head->prev = nullptr;
head->next = tail;
tail->prev = head;
tail->next = nullptr;
if (!inFile)
{
cout << "File not found! Program terminating!!" << endl;
exit(0);
}
size = 0;
inFile.get(tempTitle, MAXCHAR, ';');
while (!inFile.eof())
{
inFile.ignore(5, ';');
inFile.get(tempArtist, MAXCHAR, ';');
inFile.ignore(5, ';');
inFile >> tempMin;
inFile.ignore(5, ';');
inFile >> tempSec;
inFile.ignore(5, ';');
inFile.get(tempAlbum, MAXCHAR, '\n');
inFile.ignore(5, '\n');
// populate aSong with all the information
aSong.setTitle(tempTitle);
aSong.setArtist(tempArtist);
aSong.setMin(tempMin);
aSong.setSec(tempSec);
aSong.setAlbum(tempAlbum);
this->addSong(aSong);
inFile.get(tempTitle, MAXCHAR, ';');
}
inFile.close();
}
//destructor
SongList::~SongList()
{
// delete[] list;
}
bool SongList::addSong(Song& aSong)
{
Node * newTail = new Node;
newTail->data = aSong;
newTail->prev = tail;
if(tail){
tail->next = newTail;
}
tail = newTail;
size++;
return true;
}
// handle edge cases when current = head or current = tail
void SongList::delSong()
{
int selection = 0;
displayList();
Node * current = head;
cout << "Which song would you like to delete?: ";
cin >> selection;
while (!cin or selection <= 0){
cout << "Invalid selection. Try again: ";
cin.clear();
cin.ignore(100, '\n');
cin >> selection;
}
for (int i = 0; i < selection; i++){
current = current->next;
}
Node * prevNode = current->prev;
prevNode->next = current->next;
current->prev = prevNode->next;
delete current;
size--;
displayList();
}
//prints the list
void const SongList::displayList()
{
Node * current = head;
while (current != nullptr)
{
current->data.printSong();
current = current->next;
}
}
//finds a song in the list
void const SongList::findSong()
{
char srchTitle[MAXCHAR], tempTitle[MAXCHAR];
cout << "Enter the song you are looking for:";
cin.get(srchTitle, MAXCHAR, '\n');
convertCase(srchTitle);
Node * current = head;
while (current != nullptr)
{
current->data.getTitle(tempTitle);
convertCase(tempTitle);
if (strcmp(tempTitle, srchTitle) == 0)
{
}
}
}
//copy constructor for song class?
//writes the data back to the file
void SongList::writeFile()
{
ofstream outFile;
Node * current = head;
outFile.open("songs.txt");
if (!outFile)
{
cout << "File not found! Program terminating!!" << endl;
exit(0);
}
while (current != nullptr)
{
current->data.printFile(outFile);
current = current->next;
}
outFile.close();
}
|