Project: Catalog
Lesson 45 of 48 in Coddy's C++ for Java Developers course.
Challenge
EasyProject step 3: retain prior APIs. Catalog.add rejects negative copies and duplicate titles. borrow/give_back reject missing titles; available returns -1 for missing titles. report lists titles sorted as title:available separated by spaces, or empty. Input stage 3 then title/copies rows; add each and print report. Independent probes also borrow and return. Stages 1 and 2 still work.
Editable modules: Book.cpp, Book.hpp, LoanPolicy.cpp, LoanPolicy.hpp, Catalog.cpp, Catalog.hpp, processor.cpp, processor.hpp. Each required type has its own named header and implementation. Preserve all public signatures. The locked main.cpp reads stdin and calls the API directly. It also checks public API behavior independently where stated. Return values rather than printing inside the API. Integer inputs and results fit signed int; text is ASCII unless stated otherwise.
Try it yourself
#include "processor.hpp"
#include <iostream>
int main(){
std::vector<std::string> rows; std::string line; while(std::getline(std::cin,line)) rows.push_back(line);
int stage=std::stoi(rows.at(0));
if(stage>=1){Book b("probe",1);if(b.title()!="probe"||b.available()!=1||!b.borrow()||b.borrow())return 31;b.give_back();if(b.available()!=1)return 32;}
if(stage>=2){LoanPolicy p(7,3);if(p.fee(5)!=0||p.fee(7)!=0||p.fee(9)!=6)return 33;}
if(stage>=3){Catalog c;if(!c.add("a",1)||c.add("a",3)||c.add("b",-1)||!c.borrow("a")||c.borrow("a")||c.borrow("missing")||c.available("missing")!=-1||!c.give_back("a")||c.report()!="a:1")return 34;}
if(stage==1){std::istringstream in(rows.at(1));std::string t;int n,k;in>>t>>n>>k;Book b(t,n);int success=0;for(int i=0;i<k;++i)if(b.borrow())++success;std::cout<<b.title()<<" "<<success<<" "<<b.available()<<"\n";}
else if(stage==2){std::istringstream in(rows.at(1));int d,f,e;in>>d>>f>>e;LoanPolicy p(d,f);std::cout<<p.fee(e)<<"\n";}
else if(stage==3){Catalog c;for(size_t i=1;i<rows.size();++i){std::istringstream in(rows[i]);std::string t;int n;in>>t>>n;c.add(t,n);}std::cout<<c.report()<<"\n";}
else if(stage==4){std::cout<<process(std::vector<std::string>(rows.begin()+1,rows.end()))<<"\n";}
}
All lessons in C++ for Java Developers
1C++ Files and Values
Run the C++ FilesStrings and OutputTypes and InitializationDivision and RemaindersBooleans and ConditionsRecap: Parcel Charges4Copies and Algorithms
Value CopiesReferences and MutationFilter and TransformSorting with ComparatorsRecap: Top Unique Scores2Conditions and Iteration
Text EqualityBranches and RangesCounted IterationEarly Exit and SkippingRecap: Ticket Bands5Functions and Templates
Functions and ReturnsOverloads and DefaultsLambdas and CapturesFunction TemplatesRecap: Tiered Discounts8Composition and Ownership
Composition by ValueVirtual DispatchAbstract Base ContractsMove-only OwnershipRecap: Polymorphic Prices3Standard Containers
Fixed-size ArraysGrowing VectorsMaps and LookupSets and MembershipRecap: Shared Tags6Parsing and Lifetime
Streams and Exact ParsingExceptions and ContractsHeaders and LinkageRAII and Scope ExitRecap: Validate Lines9Project: Library Desk
Project: Book StockProject: Loan FeesPractice on your own: Online C++ compiler