Need help with c++ assignment! Thank you. An iTune entry in music library is a d
ID: 3669231 • Letter: N
Question
Need help with c++ assignment! Thank you.
An iTune entry in music library is a descriptor that summarizes information about the tune that it describes. (It is not the actual tune, which is contained in a large music data file.)
This application will start you on your way to building your very own music library. Using descriptors to identify each iTune entry, you will be able to build your repository of music favorites. Not only that, as your library grows you be able to find your iTune entry thanks to the organized filing system that you are about to explore.
For each iTune entry in your own music library file there are between 20 and 50 fields – i.e. members. If you were to look at the file on your system, you would find that the fields have names like Genre, Track ID, Name, Artist, Bit Rate, Sample Rate, Track Type,and so on.
To get things started in this music business we will develop our own iTune class.
The Program Spec
We will consider a simplified iTune class, that stores these iTune entry objects, with only the following four members for each object:
Name (the title of the song)
Artist (the performing musician or group)
Bit Rate (the number of kilobits per second that the iTune streams at or plays at –- higher for better quality, lower for smaller data file size)
Total Time (the playing time of the iTune, represented in milliseconds, i.e., 36500 would stand for 36.5 seconds)
Create a class called iTune that represents these four items, and provides solid protection for the fields. Then, provide a sample client that instantiates between three and six iTune objects and mutates and displays these objects. The details follow.
Private class instance members:
string name – the title of the song. All legal strings should be between 1 and 80 characters.
string artist –the performing musician or group. All legal strings should be between 1 and 80 characters.
int bitrate – the number of kilobits per second. A legal value should be between 64 and 706.
int total_time – the playing time in milliseconds. A legal value should be between 1 second and 1 hour (expressed in milliseconds)
As stated in our class lectures, we never want to see a literal in our methods. So the class should have static members to hold values for the limits described above, as well as default values for any field that is constructed using illegal arguments from the client. These are put in the public static section.
Public class static constants:
MIN_BITRATE = 64
MAX_BITRATE = 705
MIN_STR_LENGTH = 1
MAX_STR_LENGTH = 80
MIN_PLAY_TIME =1
MAX_PLAY_TIME = 1000*60*60
DEFAULT_BITRATE = 64
DEFAULT_PLAY_TIME = 0
DEFAULT_STRING = “ (undefined) “
You should supply all of the following public instance methods:
Constructors – one that takes no parameters (sets values to default) and one that takes all four parameters.
Accessors (get()s) and Mutators (set()s) for each instance member.
string toString() – a method that returns a string which contains all the information of the iTune. This string can be in any format as long as it is understandable and clearly formatted. Two of the many possible formats for such a string for one iTune might be:
“Janiva Magness, You Were Never Mine, 276 seconds, 128 bits per second”
Title: You Were Never Mind / Artist: Janiva Magness / Playing Time: 4 minutes 36 seconds / Bit Rate: 128k”
void display() – an output method that sends the string returned by the toString() to the screen. display() can, alternatively, send the data directly to the screen on several lines in a different manner than toString(). It can also call upon toString() but prepend and append extra formatting for the console.
Happy listening!
Input Error Checking: Always check for invalid client input in set() methods.
Test Run Requirements: Only submit one run that shows a sample client that instantiates between three to six iTune objects and mutates and displays these objects. Verify set() methods honor limits defined in the spec.
Here are some tips and requirements:
The client has been described specifically in the program spec, but to summarize, it should instantiate, mutate, display objects and thoroughly test your class. It does not have to take any input from the user. It must certainly confirm, for instance, that your mutators correctly filter out and report badarguments to the client through its return value.
Inline methods are allowed for get() Methods only.
Explanation / Answer
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Itunes
{
private:
string name;
string artist;
int bit_rate;
int total_time;
public:
static const int MIN_BITRATE = 64;
static const int MAX_BITRATE = 705;
static const int MIN_STR_LENGTH = 1;
static const int MAX_STR_LENGTH = 80;
static const int MIN_PLAY_TIME = 1000;
static const int MAX_PLAY_TIME = 1000 * 60 * 60;
static const int DEFAULT_BITRATE = 64;
static const int DEFAULT_PLAY_TIME = 1000;
static const string DEFAULT_STRING;
Itunes();
Itunes(string, string, int, int);
string to_string();
void set_defaults();
void display();
//getters and setters
bool set_name(string);
bool set_artist(string);
bool set_bit_rate(int);
bool set_total_time(int);
string get_name();
string get_artist();
int get_bit_rate();
int get_total_time();
};
const string Itunes::DEFAULT_STRING = "(undefined)";
//Global Scope Method
void display_songs(Itunes song_1, Itunes song_2, Itunes song_3, Itunes song_4, Itunes song_5)
{
song_1.display();
song_2.display();
song_3.display();
song_4.display();
song_5.display();
}
Itunes::Itunes()
{
name = DEFAULT_STRING;
artist = DEFAULT_STRING;
bit_rate = DEFAULT_BITRATE;
total_time = DEFAULT_PLAY_TIME;
}
Itunes::Itunes(string song , string singer, int rate, int time)
{
if (!set_name(song))
name = DEFAULT_STRING;
if (!set_artist(singer))
artist = DEFAULT_STRING;
if (!set_bit_rate(rate))
bit_rate = DEFAULT_BITRATE;
if (!set_total_time(time))
total_time = DEFAULT_PLAY_TIME;
}
string Itunes::to_string()
{
string info_in_string, rate_str, time_str;
ostringstream convert_rate_to_str, convert_time_to_str;
convert_rate_to_str << bit_rate;
convert_time_to_str << total_time;
rate_str = convert_rate_to_str.str();
time_str = convert_time_to_str.str();
info_in_string = name + ", " + artist + ", " + rate_str +
"k bits per second, " + time_str + " milliseconds.";
return info_in_string;
}
void Itunes::set_defaults()
{
name = DEFAULT_STRING;
artist = DEFAULT_STRING;
bit_rate = DEFAULT_BITRATE;
total_time = DEFAULT_PLAY_TIME;
}
void Itunes::display()
{
cout << to_string() << endl;
}
bool Itunes::set_name(string song)
{
if (song.length() >= MIN_STR_LENGTH && song.length() <= MAX_STR_LENGTH)
{
name = song;
return true;
}
else
{
name = DEFAULT_STRING;
return false;
}
}
bool Itunes::set_artist(string singer)
{
if (singer.length() >= MIN_STR_LENGTH && singer.length() <= MAX_STR_LENGTH)
{
artist = singer;
return true;
}
else
{
artist = DEFAULT_STRING;
return false;
}
}
bool Itunes::set_bit_rate(int rate)
{
if(rate >= MIN_BITRATE && rate <= MAX_BITRATE)
{
bit_rate = rate;
return true;
}
else
{
bit_rate = DEFAULT_BITRATE;
return false;
}
}
bool Itunes::set_total_time(int time)
{
if (time >= MIN_PLAY_TIME && time <= MAX_PLAY_TIME)
{
total_time = time;
return true;
}
else
{
total_time = DEFAULT_PLAY_TIME;
return false;
}
}
string Itunes::get_name()
{
return name;
}
string Itunes::get_artist()
{
return artist;
}
int Itunes::get_bit_rate()
{
return bit_rate;
}
int Itunes::get_total_time()
{
return total_time;
}
int main()
{
Itunes song_1, song_2,
song_3("Stairway to Heaven", "Led Zeppelin", 128, 481000),
song_4("Fingers to the Bone", "Brown Bird", 380, 191000),
song_5("I'll Get Along", "Michael Kiwanuka", 360, 228000);
//Start of main section
display_songs(song_1, song_2, song_3, song_4, song_5);
cout << " ";
song_1.set_name("The Boy Who Wouldn't Hoe Corn");
song_1.set_artist("Alison Krauss & Union Station");
song_1.set_bit_rate(128);
song_1.set_total_time(281000);
song_2.set_artist("Unknown");
song_3.set_bit_rate(5000); // Out of bounds
song_4.set_total_time(-3590); //Out of bounds
song_5.set_name("Home Again");
display_songs(song_1, song_2, song_3, song_4, song_5);
cout << " ";
song_1.set_defaults();
song_2.set_defaults();
song_3.set_defaults();
song_4.set_defaults();
song_5.set_defaults();
display_songs(song_1, song_2, song_3, song_4, song_5);
cout << " ";
//End of main section
//Explicitly testing mutators
if (!song_1.set_name(""))
cout << "That is not a song name!" << endl;
if (song_1.set_name("Black Dog"))
cout << song_1.get_name() << " is one stone groove." << endl;
if (!song_3.set_bit_rate(0))
cout << "That is not an accepted bit rate!" << endl;
if (song_3.set_bit_rate(128))
cout << song_3.get_bit_rate() << "k bits per second"
" is a good and proper bit rate!" << endl;
cout << " ";
//Explicitly testing accessors
cout << song_1.get_name() << endl;
cout << song_3.get_bit_rate() << "k bits per second." << endl;
}
sample output
(undefined), (undefined), 64k bits per second, 1000 milliseconds.
(undefined), (undefined), 64k bits per second, 1000 milliseconds.
Stairway to Heaven, Led Zeppelin, 128k bits per second, 481000 milliseconds.
Fingers to the Bone, Brown Bird, 380k bits per second, 191000 milliseconds.
I'll Get Along, Michael Kiwanuka, 360k bits per second, 228000 milliseconds.
The Boy Who Wouldn't Hoe Corn, Alison Krauss & Union Station, 128k bits per second, 281000 milliseconds.
(undefined), Unknown, 64k bits per second, 1000 milliseconds.
Stairway to Heaven, Led Zeppelin, 64k bits per second, 481000 milliseconds.
Fingers to the Bone, Brown Bird, 380k bits per second, 1000 milliseconds.
Home Again, Michael Kiwanuka, 360k bits per second, 228000 milliseconds.
(undefined), (undefined), 64k bits per second, 1000 milliseconds.
(undefined), (undefined), 64k bits per second, 1000 milliseconds.
(undefined), (undefined), 64k bits per second, 1000 milliseconds.
(undefined), (undefined), 64k bits per second, 1000 milliseconds.
(undefined), (undefined), 64k bits per second, 1000 milliseconds.
That is not a song name!
Black Dog is one stone groove.
That is not an accepted bit rate!
128k bits per second is a good and proper bit rate!
Black Dog
128k bits per second.