Objective: Implement the storage portion of the tsuPod (pronounced \"su Pod\" -
ID: 3675759 • 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
tsuPod.h
#ifndef TSU_POD_H
#define TSU_POD_H
#include <bits/stdc++.h>
using namespace std;
const int NUM_SONGS = 8;
const int MEMORY_SIZE = 25;
struct TsuPod {
string title;
string artist;
int size;
};
bool addSong(TsuPod*[], int&, string, string, int);
bool removeSong(TsuPod*[], int&, string);
bool clearList(TsuPod*[], int&);
#endif // TSU_POD_H
tsuPod.cpp
#include "tsuPod.h"
#include <bits/stdc++.h>
using namespace std;
// add song to songs list
// if addition is successful returns true else false
bool addSong(TsuPod *songs[], int &songs_added,
string title, string artist, int size) {
// validate inputs
if (title == "" || artist == "" || size <= 0) {
cout << "Error: Invalid input data!" << endl;
return false;
}
// no more slots for songs
if (songs_added == NUM_SONGS) {
cout << "Error: All slots are full!" << endl;
return false;
}
// calculate size of added songs
int total_size = 0;
for (int i = 0; i < songs_added; ++i) {
total_size += songs[i]->size;
}
// check if newly added songs pushes TsuPad memory over 25 MB
if (total_size + size > 25) {
cout << "Error: Maximum allowed size is 25 MB!" << endl;
return false;
}
// add given song
songs[songs_added] = new TsuPod();
songs[songs_added]->title = title;
songs[songs_added]->artist = artist;
songs[songs_added]->size = size;
// increment counter
songs_added += 1;
// song addition successful
return true;
}
// remove the given title
bool removeSong(TsuPod *songs[], int &songs_added, string title) {
for (int i = 0; i < songs_added; ++i) {
if (songs[i]->title == title) {
int j = i + 1;
while (j < songs_added) {
songs[j - 1]->title = songs[j]->title;
songs[j - 1]->artist = songs[j]->artist;
songs[j - 1]->size = songs[j]->size;
j += 1;
}
delete songs[j - 1];
songs_added -= 1;
return true;
}
}
cout << "Error: Given title not found!" << endl;
return false;
}
bool clearList(TsuPod *songs[], int &songs_added) {
for (int i = 0; i < songs_added; ++i) {
delete songs[i];
}
songs_added = 0;
return true;
}
test_tsuPod.cpp
#include <bits/stdc++.h>
#include "tsuPod.h"
using namespace std;
int main() {
int songs_added = 0;
TsuPod *songs[NUM_SONGS];
string artist = "A R Rehaman";
string title = "Dil Se";
int size = 10;
// successful addition
bool result = addSong(songs, songs_added, title, artist, size);
if (result) cout << "Successful addition!" << endl;
// unsuccessful addition due to memory overflow
artist = "Atif Aslam";
title = "Who are you?";
size = 100;
result = addSong(songs, songs_added, title, artist, size);
if (!result) cout << "Unsuccessful addition!" << endl;
// unsuccessful addition due to invalid input
artist = "";
title = "Hello World!";
size = 10;
result = addSong(songs, songs_added, title, artist, size);
if (!result) cout << "Unsuccessful addition!" << endl;
// unsuccessful removal
title = "Me!";
result = removeSong(songs, songs_added, title);
if (!result) cout << "Unsuccessful removal!" << endl;
// successful removal
title = "Dil Se";
result = removeSong(songs, songs_added, title);
if (result) cout << "Successful removal!" << endl;
// successful clearing of list
result clearList(songs, songs_added);
if (result) cout << "List successfully cleared!" << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.