SEPERATE THIS CODE INTO 2 OR 3 CPP FILES AND HEADER FILE . THEN WRITE A MAKE FIL
ID: 3872297 • Letter: S
Question
SEPERATE THIS CODE INTO 2 OR 3 CPP FILES AND HEADER FILE . THEN WRITE A MAKE FILE FOR THESE FILES SO IT CAN BE COMPILED AND RUN ON C++11
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Media
{
private:
string title;
string leading_actor;
string director;
int copyright;
string genre;
string media_type;
string age_rating;
int id;
bool checked_out;
string patron_name;
string patron_phone;
public:
Media(string title, string leading_actor, string director, int copyright,
string genre, string media_type, string age_rating, int id)
{
this->title = title;
this->leading_actor = leading_actor;
this->director = director;
this->copyright = copyright;
this->genre = genre;
this->media_type = media_type;
this->age_rating = age_rating;
this->id = id;
}
void check_out(string patron_name, string patron_phone)
{
checked_out = true;
this->patron_name = patron_name;
this->patron_phone = patron_phone;
}
void check_in()
{
checked_out = false;
}
bool is_checked_out()
{
return checked_out;
}
string to_string()
{
string output = """ + title + "" ";
output += "with " + leading_actor + ". Directed by " + director + ". " + std::to_string(copyright);
output += " (" + genre + "). " + age_rating + ". " + media_type + ". ID: " + std::to_string(id);
if(is_checked_out())
output += " Checked out to " + patron_name + " (" + patron_phone + ")";
return output;
}
};
class Video_Store
{
private:
vector<Media> publications;
public:
void add_media(Media pub)
{
publications.push_back(pub);
}
void check_out(int media_index, string patron_name, string patron_phone)
{
publications[media_index-1].check_out(patron_name, patron_phone);
}
void check_in(int media_index)
{
publications[media_index-1].check_in();
}
string publication_to_string(int media_index)
{
return publications[media_index-1].to_string();
}
int number_of_media()
{
return publications.size();
}
};
void show_menu()
{
cout << "=======================================" << endl;
cout << " Video Rental Management System " << endl;
cout << "=======================================" << endl;
cout << "Media" << endl;
cout << "------------" << endl;
cout << "(1) Add Media" << endl;
cout << "(2) List all Media" << endl;
cout << "(3) Check out Media" << endl;
cout << "(4) Check in Media " << endl;
cout << "Utility" << endl;
cout << "-----------" << endl;
cout << "(9) Help" << endl;
cout << "(0) Exit " << endl;
}
int main()
{
Video_Store videoStore;
int choice, index, copyright, id;
string title, leading_actor, director, genre, media_type, age_rating, patron_name, patron_phone;
show_menu();
Media *pub;
while(true)
{
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1: cout << "Enter the title of media: ";
cin >> title;
cout << "Enter the leading actor for publication: ";
cin >> leading_actor;
cout << "Enter the director for media: ";
cin >> director;
cout << "Enter the copyright year for media: ";
cin >> copyright;
cout << "Enter the genre of media: ";
cin >> genre;
cout << "Enter the medium of media: ";
cin >> media_type;
cout << "Enter the age rating of media: ";
cin >> age_rating;
cout << "Enter the ID for media: ";
cin >> id;
pub = new Media(title, leading_actor, director, copyright, genre, media_type, age_rating, id);
videoStore.add_media(*pub);
break;
case 2: for(int i = 0; i < videoStore.number_of_media(); i++)
cout << videoStore.publication_to_string(i) << endl;
break;
case 3: cout << "Enter the index of the media to checkout: ";
cin >> index;
if(index > videoStore.number_of_media() || index <= 0)
cout << "Invalid media index. We have only " << videoStore.number_of_media() << " media." << endl;
/*else if(library.publications[index].is_checked_out())
cout << "This publication readily checked out. Can't be granted to you." << endl;
*/
else
{
cout << "Enter the name of the patron: ";
cin >> patron_name;
cout << "Enter the phone number of patron: ";
cin >> patron_phone;
videoStore.check_out(index, patron_name, patron_phone);
cout << videoStore.publication_to_string(index) << endl;
}
break;
case 4: cout << "Enter the index of the media to checkin: ";
cin >> index;
if(index > videoStore.number_of_media() || index <= 0)
cout << "Invalid media index. We have only " << videoStore.number_of_media() << " media." << endl;
/*else if(!(library.publications[index].is_checked_out()))
cout << "This publication is not checked out. Can't check-in." << endl;
*/
else
videoStore.check_in(index);
break;
case 9: show_menu(); break;
case 0: return 0;
default: cout << "Invalid menu. Try again." << endl;
}
}
}
Explanation / Answer
I have separated the code into two source files:
main.cpp and media.cpp and one header file, media.h
Below is the contents of main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <media.h>
using namespace std;
class Video_Store
{
private:
vector<Media> publications;
public:
void add_media(Media pub)
{
publications.push_back(pub);
}
void check_out(int media_index, string patron_name, string patron_phone)
{
publications[media_index-1].check_out(patron_name, patron_phone);
}
void check_in(int media_index)
{
publications[media_index-1].check_in();
}
string publication_to_string(int media_index)
{
return publications[media_index-1].to_string();
}
int number_of_media()
{
return publications.size();
}
};
void show_menu()
{
cout << "=======================================" << endl;
cout << " Video Rental Management System " << endl;
cout << "=======================================" << endl;
cout << "Media" << endl;
cout << "------------" << endl;
cout << "(1) Add Media" << endl;
cout << "(2) List all Media" << endl;
cout << "(3) Check out Media" << endl;
cout << "(4) Check in Media " << endl;
cout << "Utility" << endl;
cout << "-----------" << endl;
cout << "(9) Help" << endl;
cout << "(0) Exit " << endl;
}
int main()
{
Video_Store videoStore;
int choice, index, copyright, id;
string title, leading_actor, director, genre, media_type, age_rating, patron_name, patron_phone;
show_menu();
Media *pub;
while(true)
{
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1: cout << "Enter the title of media: ";
cin >> title;
cout << "Enter the leading actor for publication: ";
cin >> leading_actor;
cout << "Enter the director for media: ";
cin >> director;
cout << "Enter the copyright year for media: ";
cin >> copyright;
cout << "Enter the genre of media: ";
cin >> genre;
cout << "Enter the medium of media: ";
cin >> media_type;
cout << "Enter the age rating of media: ";
cin >> age_rating;
cout << "Enter the ID for media: ";
cin >> id;
pub = new Media(title, leading_actor, director, copyright, genre, media_type, age_rating, id);
videoStore.add_media(*pub);
break;
case 2: for(int i = 0; i < videoStore.number_of_media(); i++)
cout << videoStore.publication_to_string(i) << endl;
break;
case 3: cout << "Enter the index of the media to checkout: ";
cin >> index;
if(index > videoStore.number_of_media() || index <= 0)
cout << "Invalid media index. We have only " << videoStore.number_of_media() << " media." << endl;
/*else if(library.publications[index].is_checked_out())
cout << "This publication readily checked out. Can't be granted to you." << endl;
*/
else
{
cout << "Enter the name of the patron: ";
cin >> patron_name;
cout << "Enter the phone number of patron: ";
cin >> patron_phone;
videoStore.check_out(index, patron_name, patron_phone);
cout << videoStore.publication_to_string(index) << endl;
}
break;
case 4: cout << "Enter the index of the media to checkin: ";
cin >> index;
if(index > videoStore.number_of_media() || index <= 0)
cout << "Invalid media index. We have only " << videoStore.number_of_media() << " media." << endl;
/*else if(!(library.publications[index].is_checked_out()))
cout << "This publication is not checked out. Can't check-in." << endl;
*/
else
videoStore.check_in(index);
break;
case 9: show_menu(); break;
case 0: return 0;
default: cout << "Invalid menu. Try again." << endl;
}
}
}
Please find below the content of media.cpp
#include <iostream>
#include <string>
#include <vector>
#include <media.h>
using namespace std;
void Media :: check_out(string patron_name, string patron_phone)
{
checked_out = true;
this->patron_name = patron_name;
this->patron_phone = patron_phone;
}
void Media :: check_in()
{
checked_out = false;
}
bool Media :: is_checked_out()
{
return checked_out;
}
string Media :: to_string()
{
string output = """ + title + "" ";
output += "with " + leading_actor + ". Directed by " + director + ". " + std::to_string(copyright);
output += " (" + genre + "). " + age_rating + ". " + media_type + ". ID: " + std::to_string(id);
if(is_checked_out())
output += " Checked out to " + patron_name + " (" + patron_phone + ")";
return output;
}
Please find below the content of media.h
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Media
{
private:
string title;
string leading_actor;
string director;
int copyright;
string genre;
string media_type;
string age_rating;
int id;
bool checked_out;
string patron_name;
string patron_phone;
public:
Media(string title, string leading_actor, string director, int copyright,
string genre, string media_type, string age_rating, int id)
{
this->title = title;
this->leading_actor = leading_actor;
this->director = director;
this->copyright = copyright;
this->genre = genre;
this->media_type = media_type;
this->age_rating = age_rating;
this->id = id;
}
void check_out(string patron_name, string patron_phone);
void check_in();
bool is_checked_out();
string to_string();
};
and lastly please find below the content of Makefile which compiles main.cpp and media.cpp and generated the binary file "main".
CC := g++
CFLAGS := -Wall -g -std=c++11 -I.
TARGET := main
SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp,%.o,$(SRCS))
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) -o $@ $^
%.o: %.cpp
$(CC) $(CFLAGS) -c $<
clean:
rm -rf $(TARGET) *.o
.PHONY: all clean
You can run the binary file "main" with below command:
./main
If you copy the makefile from here into your system, please check that no extra spaces are copied into your Makefile, otherwise, makefile behaviour will be strange.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.