Skip to content

Commit 0e4892f

Browse files
authored
Merge pull request #73 from SPauly/feat68
Closes #68
2 parents 3f8669b + 83808d0 commit 0e4892f

8 files changed

Lines changed: 104 additions & 16 deletions

File tree

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,0044,Data/Books/FirstBook.txt
2+
B123456,FirstBook,AUTHOR Name,0100,0001,Data/Books/FirstBook.txt
33
B222222,SecondBook,AUTHOR Name,0120,0026,Data/Books/SecondBook.txt
4-
B333333,ThirdBook,AUTHOR Name,0120,0013,Data/Books/ThirdBook.txt
5-
B444444,FoursBook,AUTHOR Name,9999,0009,Data/Books/FoursBook.txt
6-
B555555,FifthBook,AUTHOR Name,0025,0024,Data/Books/FifthBook.txt
7-
B666666,SixthBook,AUTHOR Name,0025,0025,Data/Books/SixthBook.txt
4+
B333333,ThirdBook,AUTHOR Name,0120,0014,Data/Books/ThirdBook.txt
5+
B444444,FoursBook,AUTHOR Name,9999,0010,Data/Books/FoursBook.txt
6+
B555555,FifthBook,AUTHOR Name,0025,0010,Data/Books/FifthBook.txt
7+
B666666,SixthBook,AUTHOR Name,0025,0010,Data/Books/SixthBook.txt

LSMS.exe

60.4 KB
Binary file not shown.

src/Book.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace LSMS
7676
return m_is_open = false;
7777
}
7878

79-
size_t Book::increase_rented()
79+
size_t Book::increase_rented(int incrementor /*= 1*/)
8080
{
8181
if (!mptr_parser)
8282
{
@@ -90,7 +90,7 @@ namespace LSMS
9090
try
9191
{
9292
currently_rented = std::stoi(mptr_row->getvalue("RENTED").data());
93-
++currently_rented;
93+
currently_rented += incrementor;
9494
//Format string to 0001
9595
for (int val = (currently_rented < 0) ? -currently_rented : currently_rented; format_length >= 0 && val != 0; --format_length, val /= 10)
9696
currently_rented_s[format_length] = '0' + val % 10;
@@ -225,7 +225,8 @@ namespace LSMS
225225
page.clear();
226226
int line_count = 0;
227227
m_file.seekg(0, std::ios::beg);
228-
while(m_file && line_count < (get_pos()*amount_of_lines)){
228+
while (m_file && line_count < (get_pos() * amount_of_lines))
229+
{
229230
fm::_getline(m_file, line);
230231
++line_count;
231232
}
@@ -237,7 +238,8 @@ namespace LSMS
237238
++line_count;
238239
}
239240

240-
if(line_count < amount_of_lines) {
241+
if (line_count < amount_of_lines)
242+
{
241243
page += "END of file\n";
242244
}
243245

src/Book.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace LSMS
1616
std::fstream m_file;
1717
bool m_is_open = false;
1818
std::string page = "";
19-
const int amount_of_lines = 30;
19+
const int amount_of_lines = 30;
2020

2121
bool mf_open();
2222

@@ -29,7 +29,7 @@ namespace LSMS
2929

3030
bool init(csv::Row *);
3131

32-
size_t increase_rented();
32+
size_t increase_rented(int incrementor = 1);
3333

3434
bool is_available();
3535
std::string_view get_BID();

src/Library.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace LSMS
4040
log("\n---------------------------- MENU -----------------------------\n");
4141
log(" [1] Rent a book [2] Read a book (beta)\n");
4242
log(" [3] Show my books [4] List books\n");
43-
log(" [5] Buy a book [6] Return a book (unavailable)\n");
43+
log(" [5] Buy a book [6] Return a book\n");
4444
log(" [7] Log out [8] Exit\n");
4545
log("\nWhat do you want to do? (1-8)>>");
4646

@@ -106,6 +106,12 @@ namespace LSMS
106106
log("Bought " + bookname);
107107
bookname.clear();
108108
break;
109+
case '6':
110+
log("Enter Book you want to return: ");
111+
std::getline(std::cin, bookname);
112+
mf_return_book(bookname);
113+
bookname.clear();
114+
break;
109115
case '7':
110116
log("Logging out\n");
111117
m_user.logout();
@@ -214,7 +220,7 @@ namespace LSMS
214220
break;
215221
}
216222
} while (tmp == "" && m_user.change_position_in_book(BOOK_PTR()->get_pos() + 1, _bookname));
217-
223+
218224
return true;
219225
}
220226
catch (csv::Error &e)
@@ -224,4 +230,24 @@ namespace LSMS
224230
}
225231
}
226232

233+
void Library::mf_return_book(std::string_view _bookname)
234+
{
235+
try
236+
{
237+
if (m_user.remove_book(_bookname))
238+
{ //remove book initializes BOOK_PTR()
239+
if (BOOK_PTR()->increase_rented(-1))
240+
log("Returned book successfully\n");
241+
else
242+
log("Failed to return to inventory\n");
243+
}
244+
else
245+
log("Failed to remove from Account\n");
246+
}
247+
catch (csv::Error &e)
248+
{
249+
log("Could not find the book in your inventory.\n");
250+
}
251+
}
252+
227253
}

src/Library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace LSMS
2828
std::string &mf_get_my_books(std::string &);
2929
std::string &mf_list_books(std::string &);
3030
bool mf_read_book(std::string_view);
31+
void mf_return_book(std::string_view);
3132

3233
public:
3334
Library();

src/User.cpp

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,10 @@ namespace LSMS
727727
{
728728
tmp.clear();
729729
last_pos = m_userinfo_txt.tellg();
730-
while (finder != '\n' )
730+
while (finder != '\n')
731731
{
732732
m_userinfo_txt.get(finder);
733-
if(finder == ',')
733+
if (finder == ',')
734734
break;
735735
tmp += finder;
736736
}
@@ -746,6 +746,64 @@ namespace LSMS
746746
}
747747
return _pos;
748748
}
749+
750+
bool User::remove_book(std::string_view _bookname)
751+
{
752+
bool flag = false;
753+
int pos_in_vector = 0;
754+
755+
for (; pos_in_vector < mvec_books.size(); pos_in_vector++)
756+
{
757+
BOOK_PTR()->init(&mvec_books.at(pos_in_vector));
758+
if (BOOK_PTR()->get_NAME() == _bookname)
759+
{
760+
mvec_books.erase(mvec_books.begin() + pos_in_vector);
761+
flag = true;
762+
break;
763+
}
764+
}
765+
766+
if (!flag)
767+
return false;
768+
769+
if (m_userinfo_txt.good() && m_userinfo_txt.is_open())
770+
{
771+
size_t last_pos = m_dimensions.beg;
772+
std::string tmp;
773+
char finder = 0;
774+
775+
m_userinfo_txt.seekg(m_dimensions.beg);
776+
std::getline(m_userinfo_txt, tmp); //skip initial line
777+
std::getline(m_userinfo_txt, tmp); //skip Name: line
778+
while (tmp != BOOK_PTR()->get_BID())
779+
{
780+
tmp.clear();
781+
last_pos = m_userinfo_txt.tellg();
782+
while (finder != '\n')
783+
{
784+
m_userinfo_txt.get(finder);
785+
if (finder == ',')
786+
break;
787+
tmp += finder;
788+
}
789+
finder = 0;
790+
}
791+
m_userinfo_txt.seekp(last_pos);
792+
793+
for (pos_in_vector; pos_in_vector < mvec_books.size(); pos_in_vector++)
794+
{
795+
m_userinfo_txt << mvec_books.at(pos_in_vector).string();
796+
}
797+
m_userinfo_txt << "---------------------\r\n";
798+
m_userinfo_txt.flush();
799+
}
800+
else
801+
{
802+
return 0;
803+
}
804+
805+
return true;
806+
}
749807
} //end user
750808

751809
} //end user

src/User.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ namespace LSMS
151151
std::string get_published();
152152
bool can_rent();
153153
bool can_buy();
154-
csv::Row* has_book(std::string_view);
154+
csv::Row *has_book(std::string_view);
155155
size_t change_position_in_book(size_t, std::string_view);
156+
bool remove_book(std::string_view);
156157
};
157158

158159
}

0 commit comments

Comments
 (0)