Skip to content

Commit b49abcc

Browse files
authored
Merge pull request #61 from SPauly/optimization
namespace LSMS + Optimizations
2 parents 8f6ed2a + 733352c commit b49abcc

File tree

15 files changed

+948
-865
lines changed

15 files changed

+948
-865
lines changed

Data/Inventory.csv

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
BID,NAME,AUTHOR,COPIES,RENTED,LINK
2-
B123456,FirstBook,AUTHOR Name,0100,0022,LINK
3-
B222222,SecondBook,AUTHOR Name,0120,0021,LINK
4-
B333333,ThirdBook,AUTHOR Name,0120,0004,LINK
5-
B444444,FoursBook,AUTHOR Name,9999,0000,LINK
2+
B123456,FirstBook,AUTHOR Name,0100,0028,LINK
3+
B222222,SecondBook,AUTHOR Name,0120,0022,LINK
4+
B333333,ThirdBook,AUTHOR Name,0120,0002,LINK
5+
B444444,FoursBook,AUTHOR Name,9999,0001,LINK
66
B555555,FifthBook,AUTHOR Name,0025,0020,LINK
7-
B666666,SixthBook,AUTHOR Name,0025,0022,LINK
7+
B666666,SixthBook,AUTHOR Name,0025,0023,LINK

LSMS.exe

1005 Bytes
Binary file not shown.

src/Book.cpp

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,83 @@
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
}

src/Book.h

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22
#include "CSVParser.h"
33
#include <string>
44

5-
class Book{
6-
private:
7-
csv::Row* mptr_info = nullptr;
8-
csv::CSVParser* mptr_parser = nullptr;
9-
public:
10-
Book() = delete;
11-
Book(csv::Row*);
12-
Book(csv::Row*, csv::CSVParser*);
13-
~Book();
14-
15-
void init(csv::Row*);
5+
namespace LSMS
6+
{
7+
class Book
8+
{
9+
private:
10+
csv::Row *mptr_info = nullptr;
11+
csv::CSVParser *mptr_parser = nullptr;
1612

17-
size_t increase_rented();
13+
public:
14+
Book() = delete;
15+
Book(csv::Row *);
16+
Book(csv::Row *, csv::CSVParser *);
17+
~Book();
1818

19-
bool is_available();
20-
std::string_view get_BID();
21-
csv::Row& get_Row();
22-
};
19+
void init(csv::Row *);
20+
21+
size_t increase_rented();
22+
23+
bool is_available();
24+
std::string_view get_BID();
25+
csv::Row &get_Row();
26+
};
27+
28+
}

0 commit comments

Comments
 (0)