I have this assignment, in C++ related to Classes Basics. "Implement a class named 'ComplexNumber' that represents complex numbers (a+ib) where i is square root of -1, create objects to add, subtract or multiply two complex number instances." The trainee who did it had some difficulties. Indeed, he develops a ComplexNumbers class that stores inside it the results of the calculation, while my expectation is that or you develop a mechanisms like a overload operator and let ComplexNumbers returns a new instance ComplexNumbers or you have an handler utilities that provide the calculation. The proposed solution violates the SRP and moreover the proposed code was terribly messy. I am considering to provide a new assignment like the following one: "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.". This assignment should further work on the difficulties encountered in the previous exercise. Please provide a feedback. Additionally, please provide any suggestion or possible improvement.
Assistant
Your proposed assignment on implementing a VideoStore class with functionalities to manage a collection of movies is well-conceived for addressing foundational concepts of object-oriented programming, including encapsulation, single responsibility principle (SRP), and unit testing. Below, I will provide some feedback and potential improvements to enhance this assignment's educational value.
Current Assignment Breakdown
Class Definition:
Movie class to represent individual movies.
VideoStore class to manage a collection of these Movie objects.
Functional Requirements:
VideoStore methods for inserting, deleting, searching, updating, and printing movies.
Additional Requirements:
Writing unit tests for each method to ensure proper functionality and promote test-driven development.
Feedback
Clarity:
The assignment is clearly articulated, and it provides a specific set of functionalities to be implemented. This makes it easy for trainees to understand what is expected of them.
Scope:
The scope is appropriate for a fundamental exercise in class design and ensures that key OOP principles can be covered, such as encapsulation and SRP.
Complexity:
By incorporating operations like insertion, deletion, and updating, the assignment covers a range of common operations on a collection, making it a robust exercise for students.
Suggestions for Improvement
Detailed Requirements Specification:
Provide explicit details on how each method should behave, especially edge cases. For example, should the search method be case-sensitive when searching by name?
Use of Standard Libraries:
Encourage the use of C++ standard library components like std::vector for the internal storage mechanism. This will expose students to standard containers.
Data Validation:
Make sure to include requirements for data validation, such as checking that scores are within the 0-10 range.
Unit Testing:
Specify the framework to be used for unit tests (e.g., Google Test, Catch2) and provide a couple of example test cases to get them started.
Documentation:
Encourage the inclusion of comments and documentation, ensuring that students practice writing clear and maintainable code.
Example VideoStore and Movie Class
Below is a starting point for the Movie and VideoStore class, as well as an example unit test using Google Test framework.
cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class Movie {
public:
Movie(const std::string& name, bool watched, int score)
: name(name), watched(watched), score(score) {}
std::string getName() const { return name; }
bool isWatched() const { return watched; }
int getScore() const { return score; }
void setWatched(bool watched) { this->watched = watched; }
void setScore(int score) {
if (score >= 0 && score <= 10) {
this->score = score;
} else {
// Handle the invalid score, for example through an exception or error message.
std::cerr << "Score must be between 0 and 10.\n";
}
}
void print() const {
std::cout << "Name: " << name << ", Watched: "
<< (watched ? "Yes" : "No") << ", Score: " << score << '\n';
}
private:
std::string name;
bool watched;
int score;
};
class VideoStore {
public:
void insertMovie(const std::string& name, bool watched, int score) {
movies.push_back(Movie(name, watched, score));
}
void deleteMovie(const std::string& name) {
auto it = std::remove_if(movies.begin(), movies.end(),
[&name](const Movie& movie) {
return movie.getName() == name;
});
movies.erase(it, movies.end());
}
Movie* searchMovie(const std::string& name) {
for (Movie& movie : movies) {
if (movie.getName() == name) {
return &movie;
}
}
return nullptr;
}
void updateMovie(const std::string& name, bool watched, int score) {
Movie* movie = searchMovie(name);
if (movie) {
movie->setWatched(watched);
movie->setScore(score);
}
}
void printMovies() const {
for (const Movie& movie : movies) {
movie.print();
}
}
private:
std::vector<Movie> movies;
};
// Example unit test using Google Test
#include <gtest/gtest.h>
TEST(VideoStoreTest, InsertMovie) {
VideoStore store;
store.insertMovie("Inception", true, 9);
Movie* movie = store.searchMovie("Inception");
ASSERT_NE(movie, nullptr);
EXPECT_EQ(movie->getName(), "Inception");
EXPECT_TRUE(movie->isWatched());
EXPECT_EQ(movie->getScore(), 9);
}
TEST(VideoStoreTest, DeleteMovie) {
VideoStore store;
store.insertMovie("Inception", true, 9);
store.deleteMovie("Inception");
Movie* movie = store.searchMovie("Inception");
EXPECT_EQ(movie, nullptr);
}
TEST(VideoStoreTest, UpdateMovie) {
VideoStore store;
store.insertMovie("Inception", true, 9);
store.updateMovie("Inception", false, 8);
Movie* movie = store.searchMovie("Inception");
ASSERT_NE(movie, nullptr);
EXPECT_FALSE(movie->isWatched());
EXPECT_EQ(movie->getScore(), 8);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Conclusion
Overall, your assignment is well-thought-out and has the potential to address many foundational issues in class design and method separation. The suggestions above should enhance the exercise and ensure a structured and comprehensive learning experience for the trainee.