C++ Assignment: Video Store Management System
Objective:
The task is to create a VideoStore class in C++ that manages a collection of movies. Each movie has the following attributes:
name (a string representing the name of the movie),
is_watched (a boolean representing whether the movie has been watched),
score (an integer between 0 and 10 representing the score of the movie).
You are also required to implement various methods to manipulate and retrieve information from the movie collection. All methods should be unit tested to verify their correctness.
Requirements:
-
Class Definition:
- Define a class
Movie with the above three attributes.
- Define a class
VideoStore that uses a std::unordered_map to store and manage the collection of Movie objects.
-
Methods in VideoStore:
- Insertion: Add a new movie to the collection.
- Deletion: Remove a movie from the collection by its name.
- Search: Check if a movie exists in the collection by its name.
- Update: Update the
is_watched status and score of a movie.
- Print: Print the list of all movies with their details.
-
Additional Methods (optional but recommended):
- List Watched Movies: Print all movies that have been watched.
- List Unwatched Movies: Print all movies that have not been watched.
-
Unit Testing:
- Create unit tests for all the methods mentioned above to ensure they work as expected.
Sample Class Definitions:
#include <unordered_map>
#include <string>
#include <iostream>
#include <stdexcept>
class Movie {
public:
std::string name;
bool is_watched;
int score;
Movie(std::string name, bool is_watched, int score)
: name(name), is_watched(is_watched), score(score) {
if (score < 0 || score > 10) {
throw std::out_of_range("Score must be between 0 and 10");
}
}
void print() const {
std::cout << "Name: " << name << ", Watched: " << std::boolalpha << is_watched << ", Score: " << score << std::endl;
}
};
class VideoStore {
private:
std::unordered_map<std::string, Movie> collection;
public:
void addMovie(const std::string& name, bool is_watched, int score) {
if (collection.find(name) != collection.end()) {
throw std::runtime_error("Movie already exists in the collection");
}
collection[name] = Movie(name, is_watched, score);
}
void deleteMovie(const std::string& name) {
if (collection.find(name) == collection.end()) {
throw std::runtime_error("Movie not found in the collection");
}
collection.erase(name);
}
bool searchMovie(const std::string& name) const {
return collection.find(name) != collection.end();
}
void updateMovie(const std::string& name, bool is_watched, int score) {
if (collection.find(name) == collection.end()) {
throw std::runtime_error("Movie not found in the collection");
}
collection[name].is_watched = is_watched;
collection[name].score = score;
}
void printCollection() const {
for (const auto& pair : collection) {
pair.second.print();
}
}
void printWatchedMovies() const {
for (const auto& pair : collection) {
if (pair.second.is_watched) {
pair.second.print();
}
}
}
void printUnwatchedMovies() const {
for (const auto& pair : collection) {
if (!pair.second.is_watched) {
pair.second.print();
}
}
}
};
Unit Testing:
Create a test suite to validate the functionality of the VideoStore class using a framework like Google Test or Catch2.
Example Test Suite (pseudo-code):
#include <iostream>
#include <cassert>
#include "video_store.hpp" // Assuming your class definitions are here
int main() {
// Test addMovie
VideoStore store;
store.addMovie("Inception", false, 9);
assert(store.searchMovie("Inception") == true);
// Test deleteMovie
store.deleteMovie("Inception");
assert(store.searchMovie("Inception") == false);
// Test updateMovie
store.addMovie("Avatar", false, 8);
store.updateMovie("Avatar", true, 9);
assert(store.searchMovie("Avatar") == true);
// Test printCollection
store.printCollection();
// Add more movies and test watched/unwatched lists
store.addMovie("Matrix", true, 8);
store.addMovie("Titanic", false, 7);
store.printWatchedMovies();
store.printUnwatchedMovies();
std::cout << "All tests passed!" << std::endl;
return 0;
}
Submission:
Please submit the following files:
- The
video_store.hpp containing the class definitions.
- A
main.cpp file containing test cases.
- A brief report explaining your implementation and the results of the tests conducted.
This assignment will help you understand basic class and object-oriented design principles in C++ and provide practice in writing unit tests to ensure code robustness. Good luck!