CIS170C WEEK 7 PROJECT-DJ PLAYLIST PROGRAM

30 July, 2024 | 9 Min Read

CIS170C WEEK 7 PROJECT-DJ PLAYLIST PROGRAM DJ Playlist Program

// Purpose: DJ Playlist Program

#include <iostream>

#include<string>

#include <cstring>

#include <conio.h>

#include <iomanip>

#include <ctime>

#include <map>

#include <stack> #include <fstream>

using namespace std; const char FileName[] = “DataFile.txt”; const int ARRAY_SIZE = 100; int id[ARRAY_SIZE] = { 0 }; string title[ARRAY_SIZE] = { “0” }; string artistname[ARRAY_SIZE] = { “0” }; string genre[ARRAY_SIZE] = { “0” }; float musiclength[ARRAY_SIZE] = { 0.0 }; int playlistID[ARRAY_SIZE] = { 0 }; string playlistName[ARRAY_SIZE]; int plistMusicID[ARRAY_SIZE][ARRAY_SIZE]; int PlayListMusicCount[ARRAY_SIZE] = { 0 }; double playListTotalLength[ARRAY_SIZE] = { 0.0 }; string DisplayMenu(); void DisplayAllMusics(int); int ValidateInteger(string); float ValidateFloat(string); int AddMusicCollection(int); int AddPlaylistCollection(int, int); void DisplayAllPlaylists(int); int SaveMusicForPlaylist(int, int); void CalculateTotalLength(int, int); void DisplayPlaylistSong(int, int); void DisplaySelectedPlaylist(int, int); double CalculateAverageSongLength(int); void DisplayPlaylistDetails(int, int, int, double); void LoadDataFromFile(int&, int&); void SaveDataToFile(int, int); int main()

{

int MusicCount = 0; int PlaylistCount = 0; string inputMenuOption = “"; string input = “"; std::cout << endl;

std::cout <<

“***************************************************************************” << endl;

std::cout << “****************************Create Music Collection!” << " * **********************” << endl;

std::cout <<

“***************************************************************************\n\n” << endl; std::cout << “This program creates 100 music collections and 100 playlist " << endl;

std::cout << “and store all data in arrays. " << endl;

std::cout << “Working menu, Data validation, Report 1 and report 2 completed. Functional

programming completed” << endl;

/*for(int i = 0 ; i < 10; i++)

{ id[i] = i + 10; title[i] = “title” + std::to_string(i); artistname[i] = “artist name” + std::to_string(i); genre[i] = “genre”+ std::to_string(i); musiclength[i] = (i + 0.1) ;

MusicCount++;

} */ while (1) { inputMenuOption = DisplayMenu(); if (inputMenuOption == “1”)

{

MusicCount = AddMusicCollection(MusicCount);

}

else if (inputMenuOption == “2”)

{

DisplayAllMusics(MusicCount);

}

else if (inputMenuOption == “3”)

{

PlaylistCount = AddPlaylistCollection(MusicCount, PlaylistCount);

} else if (inputMenuOption == “4”)//Display all playlists

{

DisplayAllPlaylists(PlaylistCount);

}

else if (inputMenuOption == “5”)//Display selected playlist data

{

DisplaySelectedPlaylist(MusicCount, PlaylistCount);

}

else if (inputMenuOption == “6”)//Save data to the file

{

SaveDataToFile(MusicCount, PlaylistCount);

}

else if (inputMenuOption == “7”)//Load data from the file

{

LoadDataFromFile(MusicCount, PlaylistCount);

}

else if (inputMenuOption == “8”)//terminate the program

{

break;

}

else

{

std::cout << “Invalid Input.Please select a valid option from the menu!!!” << endl;

}

} std::cout << endl; std::cout << “Press any key to continue…” << endl;

_getch(); return 0; }

void LoadDataFromFile(int& MusicCount, int& PlaylistCount)

{ const int LINE_SIZE = 80; char* line = new char[LINE_SIZE]; std::ifstream inputFile; inputFile.open(FileName, std::fstream::in); inputFile.getline(line, LINE_SIZE); MusicCount = atoi(line); inputFile.getline(line, LINE_SIZE); PlaylistCount = atoi(line); for (int i = 0; i < MusicCount; i++)

{ inputFile.getline(line, LINE_SIZE); id[i] = atoi(line); inputFile.getline(line, LINE_SIZE); title[i] = std::string(line); inputFile.getline(line, LINE_SIZE); artistname[i] = std::string(line); inputFile.getline(line, LINE_SIZE); genre[i] = std::string(line); inputFile.getline(line, LINE_SIZE); musiclength[i] = atof(line);

} for (int i = 0; i < PlaylistCount; i++)

{ inputFile.getline(line, LINE_SIZE); playlistID[i] = atoi(line); inputFile.getline(line, LINE_SIZE); playlistName[i] = std::string(line); inputFile.getline(line, LINE_SIZE); playListTotalLength[i] = atof(line); inputFile.getline(line, LINE_SIZE);

PlayListMusicCount[i] = atoi(line);

} for (int i = 0; i < PlaylistCount; i++)

{ for (int j = 0; j < PlayListMusicCount[i]; j++)

{ inputFile » plistMusicID[i][j];

}

} inputFile.close(); std::cout << endl << “Data has been loaded successfully from the file” << endl << endl;

}

void SaveDataToFile(int MusicCount, int PlaylistCount)

{ string input = “"; std::cout << “Save data into a file overwrites existing records. Press Y to continue: “; std::getline(cin, input); if (input == “Y” || input == “y”)

{

fstream outputFile; outputFile.open(FileName, fstream::out); outputFile << MusicCount << endl; outputFile << PlaylistCount << endl; for (int i = 0; i < MusicCount; i++) outputFile << id[i] << endl << title[i] << endl << artistname[i] << endl << genre[i] << endl << musiclength[i] << endl;

for (int i = 0; i < PlaylistCount; i++) outputFile << playlistID[i] << endl << playlistName[i] << endl << playListTotalLength[i] << endl << PlayListMusicCount[i] << endl;

for (int i = 0; i < PlaylistCount; i++)

{ for (int j = 0; j < PlayListMusicCount[i]; j++)

{ outputFile << plistMusicID[i][j] << " “;

}

outputFile << endl;

} outputFile.close(); std::cout << endl << “Your data has been saved successfully into a file” << endl << endl;

}

}

void DisplayAllMusics(int MusicCount)

{

if (MusicCount == 0) std::cout << “No Music Record added yet” << endl; else

{

std::cout << “Details of all Musics:” << endl << endl; std::cout << “ID TITLE ARTIST NAME GENRE LENGTH” << endl;

std::cout <<

“___________________________________________________________________” <<

endl << endl;;

for (int i = 0; i < MusicCount; i++) std::cout << id[i] << “\t” << title[i] << “\t” << artistname[i] << “\t” << genre[i] << “\t” << musiclength[i] << endl;

} std::cout << endl << endl;

}

string DisplayMenu()//This function display menu options

{ std::cout << endl << endl; string menuinput = “";

std::cout << “————————————————————-” << endl; std::cout << “———————– MAIN MENU —————————” << endl; std::cout << “————————————————————-” << endl; std::cout << “1- Add a Music Collection” << endl; std::cout << “2- Display all Music Collections” << endl; std::cout << “3- Create a Playlist” << endl; std::cout << “4- Display Playlist Details” << endl; std::cout << “5- Display Selected Playlist” << endl; std::cout << “6- Save data to File” << endl; std::cout << “7- Load Data To File” << endl; std::cout << “8- Quit the program” << endl; std::cout << “Please select an option: “; std::getline(cin, menuinput); std::cout << endl << endl;

return menuinput;

}

int ValidateInteger(string outputmessage)

{

bool isValid = true; string inputint = “"; do { isValid = true; std::cout << “Please enter " << outputmessage; std::getline(cin, inputint); if (inputint.length() == 0)

{ std::cout << “Invalid Input. Please enter an integer!!! " << endl << endl;

isValid = false;

} for (int j = 0; j < inputint.length(); j++) {

if (!isdigit(inputint[j]))

{ std::cout << “Invalid Input. Please enter an integer!!! " << endl << endl;

isValid = false; break;

}

} } while (!isValid); return atoi(inputint.c_str());

}

float ValidateFloat(string outputmessage)

{ bool isValid = true; string inputfloat = “"; bool isDecimalFound = false; do

{

isValid = true; std::cout << “Please enter " << outputmessage; std::getline(cin, inputfloat);//variable used to input float number if (inputfloat.length() == 0)

{ std::cout << “Invalid Input. Please enter a valid float number!!!!!! " << endl

<< endl;//display error message isValid = false;

}

isDecimalFound = false;

for (int j = 0; j < inputfloat.length(); j++) { if (!isdigit(inputfloat[j])) //if entered string contains invalid input

if (inputfloat[j] == ‘.’ && isDecimalFound == false) isDecimalFound = true; else

{

std::cout << “Invalid Input. Please enter a valid float number!!!” << endl << endl;//display error

message

isValid = false; break;

}

} } while (!isValid); return atof(inputfloat.c_str()); }

int AddMusicCollection(int MusicCount)

{

if (MusicCount == ARRAY_SIZE) { std::cout << “The Music Collection is full " << endl << endl; return MusicCount;

}

string musicNumber = to_string(MusicCount + 1); id[MusicCount] = ValidateInteger(“id of Music# " + musicNumber + “: “); ////id[MusicCount] = atoi(input.c_str()) ; //convert entered string to id std::cout << “Please enter title of Music# " + musicNumber + “: “; std::getline(cin, title[MusicCount]); std::cout << “Please enter Artist Name# " + musicNumber + “: “; getline(cin, artistname[MusicCount]); //variable used to input artist name std::cout << “Please enter Genre# " + musicNumber + “: “; getline(cin, genre[MusicCount]); //variable used to input genre musiclength[MusicCount] = ValidateFloat(” Length of the music# " + musicNumber + “:"); std::cout << endl; std::cout << “Music# " << (MusicCount + 1) << " has been created successfully” << endl

<< endl; MusicCount++; return MusicCount;

} int AddPlaylistCollection(int MusicCount, int PlaylistCount)

{ if (PlaylistCount == ARRAY_SIZE) { std::cout << “The Playlist Collection is full " << endl << endl; return PlaylistCount;

}

DisplayAllMusics(MusicCount);//displaying all musics fromt he collection int CurrentPlayListMusicCount = 0; playlistID[PlaylistCount] = ValidateInteger(” Playlist ID: “);

//creating playlist std::cout << “Enter Playlist Name: “; std::getline(cin, playlistName[PlaylistCount]); cout << endl;

CurrentPlayListMusicCount = SaveMusicForPlaylist(PlaylistCount,

CurrentPlayListMusicCount);//This function get music ids for a newly created playlist and

save it.

PlayListMusicCount[PlaylistCount] = CurrentPlayListMusicCount;

DisplayPlaylistSong(MusicCount, PlaylistCount); CalculateTotalLength(MusicCount, PlaylistCount); cout << endl; cout << “Playlist " + playlistName[PlaylistCount] << " has been created successfully” << endl << endl; PlaylistCount++; return PlaylistCount;

} void DisplayAllPlaylists(int PlaylistCount)//This function displays all playlist records

{

if (PlaylistCount == 0) { cout << “No playlist records added yet” << endl << endl; return;

}

cout << “ID NAME TOTAL LENGTH” << endl; cout << “_______________________________________________” << endl << endl;;

for (int i = 0; i < PlaylistCount; i++)

cout << playlistID[i] << “\t” << playlistName[i] << “\t” << playListTotalLength[i] << endl;

}

int SaveMusicForPlaylist(int PlaylistCount, int CurrentPlayListMusicCount)

{

while (1) { string outputmessage = " Music ID, to create playlist: Press 0 when done: “; plistMusicID[PlaylistCount][CurrentPlayListMusicCount] =

ValidateInteger(outputmessage); if (plistMusicID[PlaylistCount][CurrentPlayListMusicCount] == 0) break;

CurrentPlayListMusicCount++;

if (CurrentPlayListMusicCount == ARRAY_SIZE)

{

cout << “Only " << ARRAY_SIZE << " records can be added” << endl; break;

}

}

return CurrentPlayListMusicCount;

}

void DisplayPlaylistSong(int MusicCount, int SelectedPlaylistIndex)

{ cout << “The details of the playlist songs are as below:” << endl << endl; cout << “ID TITLE ARTIST NAME GENRE LENGTH” << endl;

cout << “___________________________________________________________________” << endl <<

endl;;

for (int i = 0; i < PlayListMusicCount[SelectedPlaylistIndex]; i++)

{ for (int j = 0; j < MusicCount; j++)

{ if (plistMusicID[SelectedPlaylistIndex][i] == id[j]) { cout << id[j] << “\t” << title[j] << “\t” << artistname[j] << “\t”

<< genre[j] << “\t” << musiclength[j] << endl;

}

}

}

}

void CalculateTotalLength(int MusicCount, int PlaylistCount)

{ for (int i = 0; i < PlayListMusicCount[PlaylistCount]; i++) for (int j = 0; j < MusicCount; j++) if (plistMusicID[PlaylistCount][i] == id[j]) playListTotalLength[PlaylistCount] = playListTotalLength[PlaylistCount] + musiclength[j];

} void DisplaySelectedPlaylist(int MusicCount, int PlaylistCount)

{ string inputint = “"; cout << “All playlist details are as below. Select an ID from the list.” << endl << endl; DisplayAllPlaylists(PlaylistCount); int selectedPlaylistID = 0; int selectedPlaylistIndex = 0; cout << endl << endl; string outputmessage = " Playlist ID to view the detail:"; selectedPlaylistID = ValidateInteger(outputmessage);//Input Playlist ID to be checked for (int i = 0; i < PlaylistCount; i++)

if (selectedPlaylistID == playlistID[i]) selectedPlaylistIndex = i;

double AverageSongLength = CalculateAverageSongLength(selectedPlaylistIndex);

DisplayPlaylistDetails(MusicCount, selectedPlaylistID, selectedPlaylistIndex, AverageSongLength);

}

double CalculateAverageSongLength(int selectedPlaylistIndex)

{ if (playListTotalLength[selectedPlaylistIndex] == 0)//If total length is zero return 0; return (playListTotalLength[selectedPlaylistIndex] /

PlayListMusicCount[selectedPlaylistIndex]);

}

void DisplayPlaylistDetails(int MusicCount, int selectedPlaylistID, int selectedPlaylistIndex, double AverageSongLength)

{ cout << “\n\nPlaylist Details of ID " << selectedPlaylistID << " is displayed below: " << endl << endl; cout << “Playlist Name: " << playlistName[selectedPlaylistIndex] << endl; cout << “Total Length: " << playListTotalLength[selectedPlaylistIndex] << endl; cout << “Average Song Length: " << AverageSongLength << endl;

DisplayPlaylistSong(MusicCount, selectedPlaylistIndex);

}ā€ƒ

Related posts