11#include " Book.h"
2-
3- Book::Book (csv::Row* _ptr_row){
4- init (_ptr_row);
5- }
6-
7- Book::Book (csv::Row* _ptr_row, csv::CSVParser* _ptr_parser) : Book(_ptr_row){
8- if (!_ptr_parser)
9- throw csv::Error (" Book: Parser points to nullptr" );
10- mptr_parser = _ptr_parser;
11- }
12-
13- void Book::init (csv::Row *_ptr_row)
2+ namespace LSMS
143{
15- if (!_ptr_row)
16- throw csv::Error (" Book: Row points to nullptr" );
17- mptr_info = _ptr_row;
18- }
19-
20- Book::~Book (){
21- delete mptr_info;
22- }
23-
24- size_t Book::increase_rented (){
25- size_t currently_rented = 0 ;
26- int rented_length = 4 ;
27- std::string currently_rented_s (rented_length--, ' 0' );
4+ Book::Book (csv::Row *_ptr_row)
5+ {
6+ init (_ptr_row);
7+ }
288
29- try
9+ Book::Book (csv::Row *_ptr_row, csv::CSVParser *_ptr_parser) : Book(_ptr_row)
3010 {
31- currently_rented = std::stoi (mptr_info->getvalue (" RENTED" ).data ());
32- ++currently_rented;
33- // Format string to 0001
34- for (int val = (currently_rented < 0 ) ? -currently_rented : currently_rented; rented_length >= 0 && val != 0 ; --rented_length, val /= 10 )
35- currently_rented_s[rented_length] = ' 0' + val % 10 ;
36- if (rented_length >= 0 && currently_rented < 0 )
37- currently_rented_s[0 ] = ' -' ;
11+ if (!_ptr_parser)
12+ throw csv::Error (" Book: Parser points to nullptr" );
13+ mptr_parser = _ptr_parser;
14+ }
3815
16+ void Book::init (csv::Row *_ptr_row)
17+ {
18+ if (!_ptr_row)
19+ throw csv::Error (" Book: Row points to nullptr" );
20+ mptr_info = _ptr_row;
3921 }
40- catch (const std::invalid_argument &e)
22+
23+ Book::~Book ()
4124 {
42- return csv::npos ;
25+ delete mptr_info ;
4326 }
4427
45- if (mptr_info->change_value_in_to (" RENTED" , currently_rented_s))
46- if (mptr_parser->updateRow (mptr_info))
47- return currently_rented;
48- else
28+ size_t Book::increase_rented ()
29+ {
30+ size_t currently_rented = 0 ;
31+ int format_length = 4 ;
32+ std::string currently_rented_s (format_length--, ' 0' );
33+
34+ try
35+ {
36+ currently_rented = std::stoi (mptr_info->getvalue (" RENTED" ).data ());
37+ ++currently_rented;
38+ // Format string to 0001
39+ for (int val = (currently_rented < 0 ) ? -currently_rented : currently_rented; format_length >= 0 && val != 0 ; --format_length, val /= 10 )
40+ currently_rented_s[format_length] = ' 0' + val % 10 ;
41+ if (format_length >= 0 && currently_rented < 0 )
42+ currently_rented_s[0 ] = ' -' ;
43+ }
44+ catch (const std::invalid_argument &e)
45+ {
4946 return csv::npos;
50- else
51- return csv::npos;
52- }
47+ }
5348
54- bool Book::is_available (){
55- try {
56- int copies = std::stoi (mptr_info->getvalue (" COPIES" ).data ());
57- int rented = std::stoi (mptr_info->getvalue (" RENTED" ).data ());
58- return copies - rented;
49+ if (mptr_info->change_value_in_to (" RENTED" , currently_rented_s))
50+ if (mptr_parser->updateRow (mptr_info))
51+ return currently_rented;
52+ else
53+ return csv::npos;
54+ else
55+ return csv::npos;
5956 }
60- catch (const std::invalid_argument &){
57+
58+ bool Book::is_available ()
59+ {
60+ try
61+ {
62+ int copies = std::stoi (mptr_info->getvalue (" COPIES" ).data ());
63+ int rented = std::stoi (mptr_info->getvalue (" RENTED" ).data ());
64+ return copies - rented;
65+ }
66+ catch (const std::invalid_argument &)
67+ {
68+ return false ;
69+ }
6170 return false ;
6271 }
63- return false ;
64- }
6572
66- std::string_view Book::get_BID (){
67- return mptr_info->getvalue (0 );
68- }
73+ std::string_view Book::get_BID ()
74+ {
75+ return mptr_info->getvalue (0 );
76+ }
77+
78+ csv::Row &Book::get_Row ()
79+ {
80+ return *mptr_info;
81+ }
6982
70- csv::Row &Book::get_Row (){
71- return *mptr_info;
7283}
0 commit comments