Question & Answer: Many of us have large digital music collections that are not always very well organized. It would be nice to have a program that would m…..

Many of us have large digital music collections that are not always very well organized. It would be nice to have a program that would manipulate our music collection based on attributes such as artist, album title, song title, genre, song length, number times played, and rating. For this assignment you will write a basic digital music manager (DMM). Your DMM program must have a text-based interface which allows the user to select from a main menu of options including: (1) load, (2) store, (3) display, (4) insert, (5) delete, (6) edit, (7) sort, (8) rate, (9) play, (10) shuffle, and (11) exit. For Part I of the assignment, you will only need to complete the main menu, (1) load, (2) store, (3) display, (6) edit, (8) rate, (9) play, and (11) exit features. The other features will be completed in the next part of the assignment. Ø What must the main menu contain? The main menu must display the following commands: (1) load (2) store (3) display (4) insert (5) delete (6) edit (7) sort (8) rate (9) play (10) shuffle (11) exit After a command is selected and completed, your program must display the main menu again. This procedure will continue until the “exit” command is selected. Ø What must “load” do? The “load” command must read all records from a file called musicPlayList.csv (you may find a sample file here) into a dynamic doubly linked list. The doubly linked list is considered the main playlist. As each record is read from the file, it must be inserted at the front of the list. Each record consists of the following attributes: * Artist – a string * Album title – a string * Song title – a string * Genre – a string * Song length – a struct Duration type consisting of seconds and minutes, both integers * Number times played – an integer * Rating – an integer (1 – 5) Each attribute, in a single record, will be separated by a comma in the .csv (comma separated values) file. This means that you will need to design an algorithm to extract the required attributes for each record. Each field in each record will have a value. You do not need to check for null or empty values. You must define a struct called Record to represent the above attributes. Also, do not forget that the Song Length must be represented by another struct called Duration. Duration is defined as follows: * Minutes – an integer * Seconds – an integer Finally, each struct Node in the doubly linked list must be defined as follows: * Data – a Record * Pointer to the next node * Pointer to the previous node Ø What must “store” do? The “store” command writes the current records, in the dynamic doubly linked list, to the musicPlayList.csv file. The store will completely overwrite the previous contents in the file. Ø What must “display” do? The “display” command prints records to the screen. This command must support two methods, one of which is selected by the user: 1. Print all records. 2. Print all records that match an artist. Ø What must “edit” do? The “edit” command must allow the user to find a record in the list by artist. If there are multiple records with the same artist, then your program must prompt the user which one to edit. The user may modify all of the attributes in the record. Ø What must “rate” do? The “rate” command must allow the user to assign a value of 1 – 5 to a song; 1 is the lowest rating and 5 is the highest rating. The rating will replace the previous rating. Ø What must “play” do? The “play” command must allow the user to select a song, and must start “playing” each song in order from the current song. “Playing” the song for this assignment means displaying the contents of the record that represents the song for a short period of time, clearing the screen and showing the next record in the list, etc. This continues until all songs have been played. Ø What must “exit” do? The “exit” command saves the most recent list to the musicPlayList.csv file. This command will completely overwrite the previous contents in the file. IV. Logical Block Diagram The logical block diagram for your doubly linked list should look like the following:Record 1: Artist Album title Song title Genre Song length Times played Rating Record 2: Artist Album title Song title Genre Song length Times played Rating Record n-1: Artist Album title Song title Genre Song length Times played Rating Record n: Artist Album title Song title Genre Song length Times played RatingAs you can see from the illustration a doubly linked list has a pointer to the next node and the previous node in the list. The first node’s previous node pointer is always NULL and the last node’s next pointer is always NULL. When you insert and delete nodes from a doubly linked list, you must always carefully link the previous and next pointers.

Please post in C and split code into main.c,functions.c,header.h

Record 1: Artist Album title Song title Genre Song length Times played Rating Record 2: Artist Album title Song title Genre Song length Times played Rating Record n-1: Artist Album title Song title Genre Song length Times played Rating Record n: Artist Album title Song title Genre Song length Times played Rating

Expert Answer

 

Given below is the code for the question. Please don’t forget to rate the answer if it helped. Thank you very much.

header.h

#ifndef header_h
#define header_h
struct Duration
{
int min;
int sec;
};
struct Record
{
char artist[25];
char album[30];
char song[100];
char genre[10];
struct Duration duration;
int timesPlayed;
int rating;
struct Record* prev;
struct Record* next;
};

void displaySong(struct Record* node); //displays the song details
void displayAllSongs(struct Record* head); //displays all songs
struct Record* searchSong(struct Record* head, char* title); //find first matching song based on title
struct Record* searchArtist(struct Record* start, char *name); //returns the first matching artist based on artist name
//function to add to front of list
struct Record* add(struct Record* head, char *artist, char* album, char* song, char* genre, int min, int sec, int times, int rating);
char* readline(char* str, int len);//function to get a line of input from keyboard and remove ending n
#endif /* header_h */

functions.c

#include “header.h”
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void displayAllSongs(struct Record* head)
{
if(head == NULL)
printf(“List is empty n”);
else
{
struct Record* curr = head;
while(curr != NULL)
{
displaySong(curr);
curr = curr->next;
}
}
}

void displaySong(struct Record* rec)
{
printf(“Artist: %sn”, rec->artist);
printf(“Album Title: %sn”, rec->album);
printf(“Song Title: %sn”, rec->song);
printf(“Genre: %sn”, rec->genre);
printf(“Duration: %02d:%02dn”, rec->duration.min, rec->duration.sec); //display in mm:ss format
printf(“Times Played: %dn”, rec->timesPlayed);
printf(“Rating: %dnn”, rec->rating);
}

struct Record* add(struct Record* head, char *artist, char* album, char* song,
char* genre, int min, int sec, int times, int rating)
{
struct Record* n = (struct Record*)malloc(sizeof(struct Record));
n->prev = NULL;
n->next = head;
if(head != NULL)
head->prev = n;

strcpy(n->artist, artist);
strcpy(n->album, album);
strcpy(n->song, song);
strcpy(n->genre, genre);
n->duration.min = min;
n->duration.sec = sec;
n->timesPlayed = times;
n->rating = rating;
return n;

}

struct Record* searchSong(struct Record* head, char* title)
{
struct Record* curr = head;
if(head == NULL)
return NULL;

while(curr != NULL)
{
if(strcmp(curr->song, title) == 0)
return curr;
curr = curr->next;
}
return NULL;
}

struct Record* searchArtist(struct Record* start, char *name)
{
struct Record* curr = start;
if(start == NULL)
return NULL;

while(curr != NULL)
{
if(strcmp(curr->artist, name) == 0)
return curr;
curr = curr->next;
}
return NULL;
}

char* readline(char* str, int len)
{
fgets(str, len, stdin);
len = strlen(str);
if(str[len-1] == ‘n’)
str[len-1] = ‘’;
return str;
}

main.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include “header.h”
struct Record* loadFromFile(char *filename);
void storeToFile(struct Record* head, char *filename);
void displayMenu(struct Record* head);
void editMenu(struct Record* head);
void rateMenu(struct Record* head);
void playMenu(struct Record* head);

int main()
{
struct Record* head = NULL;
int choice = 0;
while(choice != 11)
{
printf(“1. loadn”);
printf(“2. storen”);
printf(“3. displayn”);
printf(“4. insertn”);
printf(“5. deleten”);
printf(“6. editn”);
printf(“7. sortn”);
printf(“8. raten”);
printf(“9. playn”);
printf(“10. shufflen”);
printf(“11. exitn”);
printf(“Enter your choice: “);
scanf(“%d”, &choice);
getchar();//remove newline
switch(choice)
{
case 1:
head = loadFromFile(“musicPlayList.csv”);
break;
case 2:
storeToFile(head, “musicPlayList.csv”);
break;
case 3:
displayMenu(head);
break;
case 4: //to implement
break;
case 5: //to implement
break;
case 6:
editMenu(head);
break;
case 7:
break;
case 8:
rateMenu(head);
break;
case 9:
playMenu(head);
break;
case 10://to implement
break;
case 11:
break;
}
}
}

void displayMenu(struct Record* head)
{
int choice;
struct Record* curr = head;
char name[25];
printf(“1. Display all songsn”);
printf(“2. Display songs by artistn”);
printf(“Enter your choice: “);
scanf(“%d”, & choice);
if(choice == 1)
displayAllSongs(head);
else
{
printf(“Enter artist name: “);
getchar();//remove newline
readline(name, 25);
while((curr = searchArtist(curr, name)) != NULL)
{
displaySong(curr);
printf(“n”);
curr = curr->next;
}
}
}

void editMenu(struct Record* head)
{

struct Record* curr = head;
char artist[25];
char ans;

printf(“Enter artist name: “);
readline(artist, 25);

while((curr = searchArtist(curr, artist)) != NULL)
{
displaySong(curr);
printf(“Edit this record? y/n: “);
scanf(“%c”, &ans);
getchar(); //remove newline
if(ans == ‘y’ || ans == ‘Y’)
{
printf(“Enter new details -n”);
printf(“Album Name: “);
readline(curr->album, 30);

printf(“Song Title: “);
readline(curr->song, 100);

printf(“Genre: “);
readline(curr->genre, 10);

printf(“Duration (min sec):” );
scanf(“%d %d”, &curr->duration.min, &curr->duration.sec);

printf(“Times played: “);
scanf(“%d”, &curr->timesPlayed);

printf(“Rating: “);
scanf(“%d”, &curr->rating);

printf(“Modified the record.n”);
break;
}
curr = curr->next;
printf(“n”);
}
}

void playMenu(struct Record* head)
{
char title[100];
struct Record* curr = NULL;
printf(“Enter song title: “);
readline(title, 100);
curr = searchSong(head, title);
if(curr == NULL)
printf(“No such songn”);
else
{
while(curr != NULL)
{
printf(“Playing the song…n”);
displaySong(curr);
curr = curr->next;
printf(“n”);
}
}

}

void rateMenu(struct Record* head)
{
char title[100];
struct Record* curr = NULL;
printf(“Enter song title: “);
readline(title, 100);
curr = searchSong(head, title);
if(curr == NULL)
printf(“No such songn”);
else
{
printf(“Current rating: %dn”, curr->rating);
printf(“Enter new rating: “);
scanf(“%d”, &curr->rating);
printf(“Updated rating for the song.n”);
}

}

void storeToFile(struct Record* head, char* filename)
{
FILE *fp = fopen(filename, “w”);
if(fp == NULL)
{
printf(“Error opening file %s for writingn”, filename);
return;
}
struct Record* curr = head;
while(curr != NULL)
{
fprintf(fp, “%s,%s,%s,%s,%d,%d:%d,%dn”, curr->artist, curr->album , curr->song, curr->genre,
curr->duration.min,curr->duration.sec, curr->timesPlayed, curr->rating);
curr = curr->next;
}
printf(“Saved to file %sn”, filename);
fclose(fp);
}

struct Record* loadFromFile(char *filename)
{
FILE *fp = fopen(filename, “r”);
struct Record *head = NULL;
char line[512];
char *token;
char artist[25];
char album[30];
char song[100];
char genre[10];
int min , sec;
int timesPlayed;
int rating;

if(fp == NULL)
printf(“Error opening file %sn”, filename);
else
{
while(fgets(line, 512, fp) != NULL)
{
token = strtok(line, “,”);
strcpy(artist, token);

token = strtok(NULL, “,”);
strcpy(album, token);

token = strtok(NULL, “,”);
strcpy(song, token);

token = strtok(NULL, “,”);
strcpy(genre, token);

token = strtok(NULL, “,”);
min = atoi(token);
token = strtok(NULL, “:”);
sec = atoi(token);

token = strtok(NULL, “,”);
timesPlayed = atoi(token);

token = strtok(NULL, “n”);
rating = atoi(token);

head = add(head, artist, album, song, genre, min, sec, timesPlayed, rating);
}
}
fclose(fp);
printf(“Finished loading from file %sn”, filename);
return head;
}

compilation:

gcc functions.c main.c

sample input file: musicPlayList.csv

Janet Jackson,Unbreakable,Black Eagle,Pop,2:45,1,2
Michael Jackson,Thriller,Baby be Mine,Rock,4:10,2,5

Still stressed from student homework?
Get quality assistance from academic writers!