├── .gitignore ├── CMakeLists.txt ├── README.md ├── main.cpp ├── movies.csv ├── run.cpp ├── run.h ├── slides └── slides.pdf └── test ├── CMakeLists.txt └── singleTest.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | cmake-build-debug/ 2 | .idea/ 3 | 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.7) 2 | project(commandline_videostore_cpp) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) 6 | add_subdirectory(test) 7 | 8 | enable_testing() 9 | add_test(MainTest SingleTest) 10 | 11 | 12 | set(PROJ_SOURCES run.h run.cpp) 13 | add_library(StoreLib ${PROJ_SOURCES}) 14 | 15 | add_executable(commandline_videostore_cpp main.cpp) 16 | target_link_libraries(commandline_videostore_cpp StoreLib) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # commandline-videostore-cpp 2 | 3 | Original Java version of the workshop can be found at https://github.com/rickjanda/commandline-videostore. 4 | 5 | 6 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "run.h" 2 | #include 3 | 4 | int main() { 5 | run(std::cin, std::cout); 6 | } -------------------------------------------------------------------------------- /movies.csv: -------------------------------------------------------------------------------- 1 | 0;The Shawshank Redemption (1994);REGULAR 2 | 1;The Godfather (1972);REGULAR 3 | 2;The Godfather: Part II (1974);REGULAR 4 | 3;The Dark Knight (2008);REGULAR 5 | 4;Pulp Fiction (1994);REGULAR 6 | 5;The Good, the Bad and the Ugly (1966);REGULAR 7 | 6;The Lord of the Rings: The Return of the King (2003);CHILDRENS 8 | 7;Fight Club (1999);REGULAR 9 | 8;The Lord of the Rings: The Fellowship of the Ring (2001);CHILDRENS 10 | 9;Interstellar (2014);NEW_RELEASE 11 | 10;Whiplash (2014);NEW_RELEASE 12 | 11;Birdman (2014);NEW_RELEASE 13 | 12;Up (2009);CHILDRENS 14 | 13;WALL·E (2008);CHILDRENS 15 | -------------------------------------------------------------------------------- /run.cpp: -------------------------------------------------------------------------------- 1 | #include "run.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void run(std::istream& in, std::ostream& out){ 9 | using namespace std::literals; 10 | // read movies from file 11 | std::ifstream movieStream{"movies.csv"}; 12 | std::map> movies{}; 13 | for (std::string line; std::getline(movieStream, line);) { 14 | std::vector movie; 15 | for (size_t first=0, last=0; last < line.length(); first=last+1) { 16 | last = line.find(';', first); 17 | movie.push_back(line.substr(first, last-first)); 18 | } 19 | movies.insert(std::make_pair(std::stoi(movie[0]), movie)); 20 | out << movie[0] << ": " << movie[1] << "\n"; 21 | } 22 | out << "Enter customer name: "; 23 | std::string customerName; 24 | getline(in, customerName); 25 | 26 | out << "Choose movie by number followed by rental days, just ENTER for bill:\n"; 27 | double totalAmount = 0; 28 | int frequentRenterPoints = 0; 29 | std::ostringstream result; 30 | result << std::fixed << std::setprecision(1); 31 | result << "Rental Record for " + customerName + "\n"; 32 | while (true) { 33 | std::string input; 34 | std::getline(in, input); 35 | if (input.empty()) { 36 | break; 37 | } 38 | std::vector rental; 39 | for (size_t first=0, last=0; last < input.length(); first=last+1) { 40 | last = input.find(' ', first); 41 | rental.push_back(input.substr(first, last-first)); 42 | } 43 | std::vector movie = movies[std::stoi(rental[0])]; 44 | double thisAmount = 0; 45 | int daysRented = std::stoi(rental[1]); 46 | //determine amounts for rental 47 | if (movie[2] == "REGULAR") { 48 | thisAmount += 2; 49 | if (daysRented > 2) 50 | thisAmount += (daysRented - 2) * 1.5; 51 | } else if (movie[2] == "NEW_RELEASE") { 52 | thisAmount += daysRented * 3; 53 | } else if (movie[2] == "CHILDRENS") { 54 | thisAmount += 1.5; 55 | if (daysRented > 3) 56 | thisAmount += (daysRented - 3) * 1.5; 57 | } 58 | 59 | // add frequent renter points 60 | frequentRenterPoints++; 61 | // add bonus for a two day new release rental 62 | if (movie[2] == "NEW_RELEASE" && daysRented > 1) { 63 | frequentRenterPoints++; 64 | } 65 | // show figures for this rental 66 | result << "\t" << movie[1] + "\t" << thisAmount << "\n"; 67 | totalAmount += thisAmount; 68 | } 69 | 70 | // add footer lines 71 | result << "You owed " << totalAmount << "\n"; 72 | result << "You earned " << frequentRenterPoints << " frequent renter points\n"; 73 | 74 | out << result.str(); 75 | } 76 | -------------------------------------------------------------------------------- /run.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMANDLINE_VIDEOSTORE_CPP_RUN_H 2 | #define COMMANDLINE_VIDEOSTORE_CPP_RUN_H 3 | #include 4 | 5 | void run(std::istream& in, std::ostream& out); 6 | 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /slides/slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arnemertz/commandline-videostore-cpp/97a0529aff6b3746f21904ca1edb9f0c0639478d/slides/slides.pdf -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories (${CMAKE_SOURCE_DIR}) 2 | add_executable (SingleTest singleTest.cpp) 3 | target_link_libraries(SingleTest StoreLib) 4 | -------------------------------------------------------------------------------- /test/singleTest.cpp: -------------------------------------------------------------------------------- 1 | #include "run.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * Simple pin-down test. Do not use this as an example for a good test ;-) 8 | */ 9 | int main() { 10 | using namespace std::literals; 11 | const std::string consoleInput = 12 | "John Doe\n"s + 13 | "0 1\n"s + 14 | "2 3\n"s + 15 | "6 1\n"s + 16 | "9 1\n"s + 17 | "10 2\n"s + 18 | "13 5\n"s + 19 | "\n"s; 20 | 21 | std::istringstream input{consoleInput}; 22 | std::ostringstream output; 23 | run(input, output); 24 | 25 | const std::string& expected = 26 | "0: The Shawshank Redemption (1994)\n"s + 27 | "1: The Godfather (1972)\n"s + 28 | "2: The Godfather: Part II (1974)\n"s + 29 | "3: The Dark Knight (2008)\n"s + 30 | "4: Pulp Fiction (1994)\n"s + 31 | "5: The Good, the Bad and the Ugly (1966)\n"s + 32 | "6: The Lord of the Rings: The Return of the King (2003)\n"s + 33 | "7: Fight Club (1999)\n"s + 34 | "8: The Lord of the Rings: The Fellowship of the Ring (2001)\n"s + 35 | "9: Interstellar (2014)\n"s + 36 | "10: Whiplash (2014)\n"s + 37 | "11: Birdman (2014)\n"s + 38 | "12: Up (2009)\n"s + 39 | "13: WALL·E (2008)\n"s + 40 | "Enter customer name: Choose movie by number followed by rental days, just ENTER for bill:\n"s + 41 | "Rental Record for John Doe\n"s + 42 | "\tThe Shawshank Redemption (1994)\t2.0\n"s + 43 | "\tThe Godfather: Part II (1974)\t3.5\n"s + 44 | "\tThe Lord of the Rings: The Return of the King (2003)\t1.5\n"s + 45 | "\tInterstellar (2014)\t3.0\n"s + 46 | "\tWhiplash (2014)\t6.0\n"s + 47 | "\tWALL·E (2008)\t4.5\n"s + 48 | "You owed 20.5\n"s + 49 | "You earned 7 frequent renter points\n"s; 50 | if (output.str() == expected) { 51 | std::cout << "1 Test passed\n"; 52 | } else { 53 | std::cerr << "TEST FAILED! Expected: \n" << expected << "\n actual:\n" << output.str() << '\n'; 54 | } 55 | } 56 | 57 | 58 | --------------------------------------------------------------------------------