C++ Assignment: Creating a Video Store Class
Objective:
Develop a VideoStore class to manage a collection of movies. Each movie should be represented by its name, a watched status (boolean), and a score (integer between 0 and 10). The VideoStore class should allow for inserting, deleting, searching, updating, and printing movies. Additionally, write unit tests for each method.
Requirements:
- Movie Class: This will define the individual movies.
- VideoStore Class: This will manage a collection of
Movie objects using a std::vector.
- Required Methods:
- Insertion: Add a new movie to the store.
- Delete: Remove a movie from the store by its name.
- Search: Find a movie by its name.
- Update: Modify the watched status and score of a movie.
- Print: Display the details of all movies in the store.
- Optional Methods (for better usability):
- Check If Movie Exists: Verify if a movie is already in the store by its name.
- Get Movie Count: Return the total number of movies in the store.
Implementation Details:
- Use
std::vector to store the Movie objects.
- Handle edge cases such as inserting duplicate movies or updating non-existent movies.
- Each method should be unit tested.
Provided Code Structure:
Here’s a basic outline of what your code might look like.
// Movie.h
#ifndef MOVIE_H
#define MOVIE_H
#include <string>
class Movie {
public:
Movie(const std::string& name, bool watched = false, int score = -1);
std::string getName() const;
bool isWatched() const;
int getScore() const;
void setWatched(bool watched);
void setScore(int score);
private:
std::string name_;
bool watched_;
int score_;
};
#endif // MOVIE_H
// Movie.cpp
#include "Movie.h"
Movie::Movie(const std::string& name, bool watched, int score)
: name_(name), watched_(watched), score_(score) {}
std::string Movie::getName() const { return name_; }
bool Movie::isWatched() const { return watched_; }
int Movie::getScore() const { return score_; }
void Movie::setWatched(bool watched) { watched_ = watched; }
void Movie::setScore(int score) { score_ = score; }
// VideoStore.h
#ifndef VIDEOSTORE_H
#define VIDEOSTORE_H
#include "Movie.h"
#include <vector>
#include <string>
class VideoStore {
public:
void addMovie(const Movie& movie);
bool deleteMovie(const std::string& name);
Movie* searchMovie(const std::string& name);
bool updateMovie(const std::string& name, bool watched, int score);
void printAllMovies() const;
bool movieExists(const std::string& name) const;
size_t getMovieCount() const;
private:
std::vector<Movie> movies_;
};
#endif // VIDEOSTORE_H
// VideoStore.cpp
#include "VideoStore.h"
#include <iostream>
void VideoStore::addMovie(const Movie& movie) {
if (!movieExists(movie.getName())) {
movies_.push_back(movie);
}
}
bool VideoStore::deleteMovie(const std::string& name) {
for (auto it = movies_.begin(); it != movies_.end(); ++it) {
if (it->getName() == name) {
movies_.erase(it);
return true;
}
}
return false;
}
Movie* VideoStore::searchMovie(const std::string& name) {
for (auto& movie : movies_) {
if (movie.getName() == name) {
return &movie;
}
}
return nullptr;
}
bool VideoStore::updateMovie(const std::string& name, bool watched, int score) {
Movie* movie = searchMovie(name);
if (movie) {
movie->setWatched(watched);
movie->setScore(score);
return true;
}
return false;
}
void VideoStore::printAllMovies() const {
for (const auto& movie : movies_) {
std::cout << "Name: " << movie.getName()
<< ", Watched: " << (movie.isWatched() ? "Yes" : "No")
<< ", Score: " << movie.getScore() << std::endl;
}
}
bool VideoStore::movieExists(const std::string& name) const {
return std::any_of(movies_.begin(), movies_.end(), [&name](const Movie& movie){ return movie.getName() == name; });
}
size_t VideoStore::getMovieCount() const {
return movies_.size();
}
// Unit Tests (using a hypothetical testing framework)
#include "gtest/gtest.h"
// Define your test cases for each method here
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Instructions:
-
Create the Movie Class:
- Implement the
Movie class in Movie.h and Movie.cpp.
-
Create the VideoStore Class:
- Implement the
VideoStore class in VideoStore.h and VideoStore.cpp.
-
Implement Unit Tests:
- Write unit tests for methods in the
VideoStore class using a testing framework like Google Test or Catch2.
-
Compile and Run:
- Compile your classes and unit tests.
- Ensure that all tests pass.
Submission:
Submit the following files:
Movie.h
Movie.cpp
VideoStore.h
VideoStore.cpp
- Unit test files
Ensure your code is well-documented and follows proper coding standards.