Top Banner
1 Modeling CDs (Continued)
21

1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete

Dec 13, 2015

Download

Documents

Blake Hall
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

1

Modeling CDs (Continued)

Page 2: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

2

Getting Started

Download: http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Dow

nloads/2011_02_07_CD_Collection_Incomplete/ File CD_Collection_Incomplete.zip

Expand Open in Visual Studio

Page 3: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

3

CD Types

We have two enums for CDs: genre recording technology

We will need to convert these types to strings and vice versa.

Output with << operator.

Let's put them into their own file: CD_Types.h Implementation of conversion function in CD_Types.cpp.

Add CD_Types.h and CD_Types.cpp to the project. Note: This is not a class

Use Project > Add New Item

Page 4: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

4

CD_Types.h

http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_02_07_CDs/CD_Types.h.txt

#pragma once

#include <string>

using namespace std;

enum Recording_Technology {AAD, ADD, DDD, UNK};

ostream& operator<<(ostream& os, const Recording_Technology& rt);

void Convert(const Recording_Technology& val, string& str);

void Convert(const string& str, Recording_Technology& val);

enum Genre {Classical, Pop, Country, Folk, Rap, Hip_Hop, Unknown};

ostream& operator<<(ostream& os, const Genre& genre);

void Convert(const Genre& val, string& str);

void Convert(const string& str, Genre& val);

Page 5: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

5

CD_Types.cpp

http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_02_07_CDs/CD_Types.cpp.txt

#include "CD_Types.h"

ostream& operator<<(ostream& os, const Recording_Technology& rt)

{

string str;

Convert(rt, str);

os << str;

return os;

}

void Convert(const Recording_Technology& val, string& str)

{

switch (val)

{

case AAD: str = "AAD"; break;

case ADD: str = "ADD"; break;

case DDD: str = "DDD"; break;

default: str = "UNK"; break;

}

}

Page 6: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

6

CD_Types.cpp

void Convert(const string& str, Recording_Technology& val)

{

if (str == "AAD") val = AAD;

else if (str == "ADD") val = ADD;

else if (str == "DDD") val = DDD;

else val = UNK;

}

Page 7: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

7

CD_Types.cpp

ostream& operator<<(ostream& os, const Genre& genre)

{

string str;

Convert(genre, str);

os << str;

return os;

}

void Convert(const Genre& val, string& str)

{

switch (val)

{

case Classical: str = "Classical"; break;

case Pop: str = "Pop"; break;

case Country: str = "Country"; break;

case Folk: str = "Folk"; break;

case Rap: str = "Rap"; break;

case Hip_Hop: str = "Hip Hop"; break;

default: str = "Unknown"; break;

}

}

Page 8: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

8

CD_Types.cpp

void Convert(const string& str, Genre& val)

{

if (str == "Classical") val = Classical;

else if (str == "Pop") val = Pop;

else if (str == "Country") val = Country;

else if (str == "Folk") val = Folk;

else if (str == "Rap") val = Rap;

else if (str == "Hip_Hop") val = Hip_Hop;

else val = Unknown;

}

Remove defintion of enums from Track.h and CD.hAdd #include "CD_Types.h" to these files.

Page 9: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

9

Implement Class CD

http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2010_09_08_In_Class/CD.cpp.txt

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <stdlib.h>#include <cassert>#include "CD_Types.h"#include "Track.h"#include "CD.h"

using namespace std;

Page 10: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

10

The Constructor

CD::CD(string title_, string id_, string artist_, string manufacturer_, int year_, Recording_Technology technology_) :

title(title_), id(id_), artist(artist_), manufacturer(manufacturer_), year(year_), technology(technology_), nr_tracks(0){}

Initialization List

Page 11: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

11

Add_Track

void CD::Add_Track(const Track* track)

{

assert(nr_tracks < max_tracks);

tracks[nr_tracks++] = track;

}

Page 12: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

12

The Destructor

CD::~CD(void){ for (int i = 0; i < nr_tracks; ++i) { delete tracks[i]; } }

Page 13: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

13

Total_Play_Time()

int CD::Total_Play_Time() const

{

int total = 0;

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

{

total += tracks[i]->Play_Time();

}

return total;

}

Page 14: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

14

Display

void CD::Display() const

{

cout.fill('0');

cout << "CD: " << title << endl;

cout << "ID: " << id << endl;

cout << "Artist: " << artist << endl;

cout << "Mfgr: " << manufacturer << endl;

cout << "Year: " << year << endl;

cout << "Recording technology: " << technology << endl;

cout << "Total play time: " << (Total_Play_Time()/ 60) << ":";

cout.width(2);

cout << (Total_Play_Time()%60) << endl;

cout << endl;

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

{

cout << "\tTrack " << i+1 << ": ";

tracks[i]->Display();

cout << endl;

}

}

Page 15: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

15

Capturing the Data

Media players have all of the information that we need for our CD catalog. Some comes from the CD. Some comes from an on-line database.

We can't get the information directly from the CD. An audio CD does not have a file system

But we can copy the information from iTunes.

Page 16: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

16

A CD in iTunes

Page 17: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

17

CD Data

The data from three CDs is available in the Downloads area of the class web site:

http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_02_09_CD_Information/

Comma Separated Values Widely used format for structured text files. Read and written by Excel

Page 18: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

18

La_Luna.csv in Notepad

Page 19: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

19

La_Luna.csv in Excel

Page 20: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

20

CDs.csv

Page 21: 1 Modeling CDs (Continued). 2 Getting Started Download: turnerr/Object_Oriented_Design/Downloads/ 2011_02_07_CD_Collection_Incomplete/

21

main.cpp

http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2010_09_08_In_Class/Test_CD.cpp.txt

Replace the current "Hello, World" version