Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Implement the storage portion of the tsuPod (pronounced \"su Pod\" -

ID: 3865032 • Letter: O

Question

Objective:

Implement the storage portion of the tsuPod (pronounced "su Pod" - the much smaller version of the iPod). Learn how to use structures.

Specifications:

General design:

The code must be designed to allow the storage of the following data:

up to 8 songs not to exceed 25MB

for each song we will store the title (string), artist (string), and size (int) in MB

the title and artist cannot be blank

the size of each song must be greater than zero

Code design:

you are provided with the tsuPod.h file with prototypes of the functions that you need to define. (Note: some of the prototypes have to be modified for the test code to work correctly)

you have to create file tsuPod.cpp where you define functions specified in tsuPod.h file

you have to edit file test_tsuPod.cpp that tests the functionality of tsuPod

·         following variables and structures must be declared in your tsuPod.h or tsuPod.cpp code (Note: in your code 8 songs should be presented by an array of structure variables)

const int NUM_SONGS = 8; /*we will have the maximum 8 slots with some slots being empty /
const int MEMORY_SIZE = 25; /*total memory size of tsuPod in MB*/

struct TsuPod

{

   string title;

   string artist;

   int size;

};

tsuPod.h should be included in both tsuPod.cpp and test_tsuPod.cpp

You must use linux for this project. you need to type “g++ tsuPod.cpp test_tsuPod.cpp” to compile and get the executable file. Note: all code will compiled by the grader under linux on eros.cs.txstate.edu.

Testing:

Edit file test_tsuPod.cpp where you are going to test required functionality of the tsuPod program.

Following storage functionalities have to be tested to receive full grade:

Initialization

Successful song addition

Unsuccessful song addition – failure due to the lack of memory

Unsuccessful song addition – failure due to other reason (ex: no empty slots)

Successful song removal

Unsuccessful song removal – failure because a song with such name was not found

Clearing of the song list

Shuffle functionality – implementation and testing of this function will result in an extra grade.

In your test_tsuPod.cpp explain you what exactly you are testing with each block of code. Examples: test addition of a song to a play list, test shuffling capabilities, etc.

tsuPod.h file and the example of the test_tsuPod.cpp are located in the archive of the assignment

Explanation / Answer

Ans:

The following 5 files are to achieve the task which are well explained with comments.

1.test_tsuPo.cpp

2.tsu_Pod.cpp

3.tsuPod.h

4.Song.cpp

5.Song.h

//test_tsuPod.cpp

#include "Song.h"

class tsuPod

{

private:

struct SongNode // nodes of the linked list

{

Song s; // song data

SongNode *next; // points to next node in the list

};

SongNode *head; // the head pointer

int totalMemory, // total memory in tsuPod

memoryUsed, // memory used by songs

numberSongs; // number of songs in list

public:

tsuPod(int); // constructor

~tsuPod(); // deconstructor

int addSong(Song), // adds songs to list

removeSong(Song); // removes song from list

void showSongList(), // displays song list contents

sortSongList(), // sorts song list in ascending order

shuffle(), // shuffle song list

clearSongList(); // clears all songs from list

int getTotalMemory(), // get total memory of TsuPod

getRemainingMemory(); // get remaining memory left

Song searchLeast(); // find lowest value song

bool remove(Song); // removes specific song (used in removeSong())

};

//tsuPod.h//

#include "Song.h"

class tsuPod

{

private:

struct SongNode // the Nodes of the linked list

{

Song s; // song data

SongNode *next; // points to next node in list

};

SongNode *head; // the head pointer

int totalMemory, // total memory in tsuPod

memoryUsed, // memory used by songs

numberSongs; // number of songs in list

public:

tsuPod(int); // constructor

~tsuPod(); // deconstructor

int addSong(Song), // adds songs to list

removeSong(Song); // removes song from list

void showSongList(), // displays song list contents

sortSongList(), // sorts song list in ascending order

shuffle(), // shuffle song list

clearSongList(); // clears all songs from list

int getTotalMemory(), // get total memory of TsuPod

getRemainingMemory(); // get remaining memory left

Song searchLeast(); // find lowest value song

bool remove(Song); // removes specific song

};

//tsuPod.cpp/

#include <iostream>

#include <iomanip>

#include <string>

#include <ctime>

#include <time.h>

#include <stdlib.h>

#include <stdio.h>

using namespace std;

#include "tsuPod.h"

// Constructor for tsuPod

tsuPod::tsuPod(int memorySpace)

{

totalMemory = memorySpace;

head = NULL;

memoryUsed = 0;

numberSongs = 0;

}

// Deconstructor for tsuPod

tsuPod::~tsuPod(){}

int tsuPod::addSong(Song song)

{

//return -2 if no title, no artist, and size less than 0

if(song.getTitle() == "" || song.getArtist() == "" || song.getSize() < 1)

return -2; // return unsuccessful

// return -1 if no memory is available

else if(song.getSize() > getRemainingMemory())

return -1; // return unsuccessful

//return 0 if song was added successfully

else

{

numberSongs++;

memoryUsed += song.getSize();

SongNode *newNode; // To point to a new node

SongNode *nodePtr; // To move through the list

// Allocate a new node and store song

newNode = new SongNode;

newNode->s = song;

newNode->next = NULL;

// If empty, make newNode the first node

if (!head)

head = newNode;

else

{

// make nodePtr to head of list

nodePtr = head;

// traverse list to find last node

while (nodePtr->next)

nodePtr = nodePtr->next;

// make last node point to newNode

nodePtr->next = newNode;

}

return 0; // return successful

}

}

/* FUNCTION - int removeSong

* attempts to remove a song from the tsuPod

o returns 0 if successful

o returns -1 if nothing is removed

input parms - s object

output parms - 0(int) or -1(int)

*/

int tsuPod::removeSong(Song song)

{

bool found; // true if removeOne did remove one

// keep trying to remove one, until there are no more

found = remove(song);

while (found) // if song found, remove

{

found = remove(song);

numberSongs--; // subtract song from number of songs

memoryUsed -= song.getSize(); // decrease memory used

return 0; // return successful

}

return -1; // return unsuccessful

}

/* FUNCTION - bool removeSong

* attempts to remove a song from the tsuPod

o returns 0 if successful

o returns -1 if nothing is removed

input parms - s object

output parms - true(bool or false (bool)

*/

bool tsuPod::remove(Song song)

{

SongNode *nodePtr = head; // to traverse the list of songs

SongNode *previousNode = NULL; // node before nodePtr

while (nodePtr) // traverse list if songs

{

if (nodePtr->s == song) // if song matchs is found, delete song

{

if (previousNode == NULL) // if node reaches end of list

head = nodePtr->next;

else

previousNode->next = nodePtr->next;

delete nodePtr; // deletes song

return true; // returns successful

}

previousNode = nodePtr; //else: advance both pointers

nodePtr = nodePtr->next;

}

return false; // returns unsuccessful

}

/* FUNCTION - void showSongList

*displays current list of songs

input parms - none

output parms - prints song list to console.

*/

void tsuPod::showSongList()

{

SongNode *nodePtr; // pointer used to traverse the list

nodePtr = head; // sets node to beginning of list

while (nodePtr) // traverses through all elements in the list.

{

// displays list contents

cout << "Title: " << nodePtr->s.getTitle()

<< " Artist: " << nodePtr->s.getArtist()

<< " Size: " << nodePtr->s.getSize() << endl;

//moves to next song

nodePtr = nodePtr->next;

}

}

/* FUNCTION - void sortSongList

*sorts list according to title, artist, and size

input parms - none

output parms - none

*/

void tsuPod::sortSongList()

{

SongNode *buff; // buff initialized

buff = NULL; // set to NULL

SongNode *pointer; // pointer to traverse song list

while(head) //Loop is executed as long as there are movies in movieList

{

// Allocate a new node for song.

SongNode *newNode; // create node

newNode = new SongNode; // create new songNode

newNode->s = searchLeast(); // newNode equal to lowest value vound

newNode->next = NULL;

// Remove lowest song found

remove(searchLeast());

// If empty, make newNode first node.

if (!buff)

buff = newNode;

else

{

// make pointer to head of buff list.

pointer = buff;

// traverse list to find last node in buff.

while(pointer->next)

pointer = pointer->next;

// makes last node point to newNode

pointer->next = newNode;

}

}

//makes old head point to new head.

head = buff;

}

/* FUNCTION - Song lowest()

*traverses list to find lowest alphabetical value

input parms - none

output parms - none

*/

Song tsuPod::searchLeast()

{

SongNode *nodePtr; // to traverse list of songs.

nodePtr = head;

Song temp = nodePtr->s; // initializes temp and sets

// values of song to temp

nodePtr = nodePtr->next; //points node to next song in list.

while(nodePtr) // while node available to traverse.

{

if(nodePtr->s < temp) //comparison of song values.

temp = nodePtr->s; // set temp song to value of node

nodePtr = nodePtr->next; // node set values of next song

}

return temp; // return lowest valued song

}

/* FUNCTION - int getTotalMemory

*Retrieves current memory of tsuPod

input parms - none

output parms - returns totalMemory(int)

*/

int tsuPod::getTotalMemory()

{

return totalMemory;

}

/* FUNCTION - void clearSongList

*clears all the songs from memory

input parms - none

output parms - none

*/

void tsuPod::clearSongList()

{

SongNode *temp;

temp = head; // temp set to beginning

while(temp != NULL) // while temp has not reached end

{

head->next = temp->next;

temp->next = NULL;

delete temp; // deletes temp songNode

temp = head->next;

}

head = NULL; // no songs in list

numberSongs = 0; // resets song counter to 0

memoryUsed = 0; // resets memory used to 0

cout << " Song list cleared. ";

}

/* FUNCTION - int remainingMemory

*Retrieves unused memory

input parms - none

output parms - returns remainingMemory(int)

*/

int tsuPod::getRemainingMemory()

{

return (getTotalMemory() - memoryUsed);

}

/* FUNCTION - void shuffle

*shuffles the songs into random order

o will do nothing if there are less than two songs in the current list

input parms - none

output parms - none void

*/

void tsuPod::shuffle ()

{

srand(time(NULL));

int randomNum; // stores random number

SongNode *prevSong, *swapSong, *pointer1, *pointer2; // pointers to travel

//the list

Song buffer; // buffer to store song temporarily

int newNumber = numberSongs;

pointer1 = head; // sets pointer 1 to beginning of song list

for(int a = 0; a < newNumber; a++)

{

// traverse to newNumber position in song list

pointer2 = head;// start at beginning

prevSong = pointer1;

randomNum = (rand() % newNumber) + 1; // generates random number

// but not 0

for(int k = 1; k <= randomNum; k++)

{

if(k == randomNum) // when k matches random number do not switch

swapSong = pointer2; // set swapSong equal to pointer2

pointer2 = pointer2->next; // traverse to next song

}

//swapping of songs

buffer = prevSong->s; // prevSong stored in buffer

prevSong->s = swapSong->s; // previous song now holds song in switchsong

swapSong->s = buffer; // switchsong holds song in prevSong

pointer1 = pointer1->next; // go to next song in the list

}

}

//Song.cpp

#include "Song.h"

// constructor, initializes title, artist, size

Song::Song()

{

title = "";

artist = "";

size = 0;

}

// mutator constructor sets values to title, artist, size

Song::Song(string newTitle, string newArtist, int newSize)

{

title = newTitle;

artist = newArtist;

size = newSize;

}

// getTitle: accessor function to return name (string)

string Song::getTitle()

{

return title;

}

// getArtist: function accessor to return artist (string)

string Song::getArtist()

{

return artist;

}

// getSize: function that retrieves size (int)

int Song::getSize()

{

return size;

}

// setTitle: mutator function to set the title

void Song::setTitle(string newTitle)

{

title = newTitle;

}

// setArtist: mutator function to set the artist

void Song::setArtist(string newArtist)

{

artist = newArtist;

}

// setSize: function to set the size

void Song::setSize(int newSize)

{

size = newSize;

}

// operator>: compares this song to another

// compares title, artist, size

bool Song::operator< (const Song &rhs)

{

if(title != rhs.title)

return (title < rhs.title);

if(artist != rhs.artist)

return (artist < rhs.artist);

if(size != rhs.size)

return (size < rhs.size);

return false;

}

// operator>: compares this song to another

// compares title, artist, size

bool Song::operator> (const Song &rhs)

{

if(title != rhs.title)

return (title > rhs.title);

if(artist != rhs.artist)

return (artist > rhs.artist);

if(size != rhs.size)

return (size > rhs.size);

return false;

}

// operator==: compares this song to another

// songs are equal if all three member variables are equal

bool Song::operator== (const Song &right)

{

if (title != right.title)

return false;

if (artist != right.artist)

return false;

if (size != right.size)

return false;

return true;

}

//Song.h

#include <string>

using namespace std;

class Song

{

private:

string title, // title of song

artist; // artist of the Song

int size; // size of song

public:

Song(); // constructor

Song(string, string, int); // mutator constructor

~Song(){}; // destructor

string getTitle(), // retrieves title of song

getArtist(); // retrieves artist of song

int getSize(); // retrieves size of song

void setTitle(string); // sets title of song

void setArtist(string); // sets artist of song

void setSize(int); // sets size of song

bool operator < (const Song &); // comparison operator

bool operator > (const Song &); // comparison operator

bool operator == (const Song &); // comparison operator

};