CIS170C WEEK 6 PROJECT- A DJ PROGRAM.docx

30 July, 2024 | 7 Min Read

CIS170C WEEK 6 PROJECT- A DJ PROGRAM

#include <iostream>

#include<string>

#include <cstring>

#include <conio.h>

#include <iomanip>

#include <ctime>

#include <map> #include <stack>

using namespace std; const int ARRAY_SIZE = 100;

int id[ARRAY_SIZE] = { 0 };

std::string title[ARRAY_SIZE] = { “0” }; std::string artistname[ARRAY_SIZE] = { “0” }; std::string genre[ARRAY_SIZE] = { “0” }; float musiclength[ARRAY_SIZE] = { 0.0 }; int playlistID[ARRAY_SIZE] = { 0 }; std::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); //void DisplayPlaylistDetails(int, int); double CalculateAverageSongLength(int); void DisplayPlaylistDetails(int, int, int, double); 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”<<end l;

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;

while (1) { inputMenuOption = DisplayMenu();

if (inputMenuOption == “1”)//Add a Music Collection

{

MusicCount = AddMusicCollection(MusicCount);//This function creates

a new music recordand add in the collection

}

else if (inputMenuOption == “2”)//Display all Music Collections

{

DisplayAllMusics(MusicCount);

}

else if (inputMenuOption == “3”) //Create a playlist

{

PlaylistCount = AddPlaylistCollection(MusicCount, PlaylistCount); //This

function creates a new playlist recordand save it

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

{

DisplayAllPlaylists(PlaylistCount);//This function displays all playlist

records

}

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

{

DisplaySelectedPlaylist(MusicCount, PlaylistCount);//This function takes

playlist id as input parameterand displays the detail of that list

}

else if (inputMenuOption == “6”)//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;

return 0;

}

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 (Report 2)” << endl; std::cout << “5- Display Selected Playlist (Report 1)” << endl; std::cout << “6- Quit the program” << endl;

std::cout << “_____________________________________________________________” <<

endl << 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;//display

error message

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); if (inputfloat.length() == 0)

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

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);//if invalid float number is entered 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()) ; std::cout << “Please enter title of Music# " + musicNumber + “: “; std::getline(cin, title[MusicCount]); std::cout << “Please enter Artist Name# " + musicNumber + “: “; std::getline(cin, artistname[MusicCount]); std::cout << “Please enter Genre# " + musicNumber + “: “; std::getline(cin, genre[MusicCount]); 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)//create a new playlist record and save it

{ 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: “; getline(cin, playlistName[PlaylistCount]); std::cout << endl;

CurrentPlayListMusicCount = SaveMusicForPlaylist(PlaylistCount,

CurrentPlayListMusicCount);

PlayListMusicCount[PlaylistCount] = CurrentPlayListMusicCount;

DisplayPlaylistSong(MusicCount, PlaylistCount); CalculateTotalLength(MusicCount, PlaylistCount); std::cout << endl;

std::cout << “Playlist " + playlistName[PlaylistCount] << " has been created successfully” << endl << endl;

PlaylistCount++;

return PlaylistCount;

} void DisplayAllPlaylists(int PlaylistCount)

{

if (PlaylistCount == 0) std::cout << “No playlist records added yet” << endl << endl; std::cout << “ID NAME TOTAL LENGTH” << endl; std::cout << “_______________________________________________” << endl << endl;;

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

std::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)

{

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

}

}

return CurrentPlayListMusicCount;

}

void DisplayPlaylistSong(int MusicCount, int SelectedPlaylistIndex)

{

std::cout << “Details of playlist songs are as below:” << endl << endl; std::cout << “ID TITLE ARTIST NAME GENRE LENGTH” << endl;

std::cout << “___________________________________________________________________” << endl << endl

;;

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

{

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

{

if (plistMusicID[SelectedPlaylistIndex][i] == id[j]) { std::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); 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) 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