-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.cpp
More file actions
119 lines (107 loc) · 3.42 KB
/
Book.cpp
File metadata and controls
119 lines (107 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "Book.h"
#include "Global.h"
namespace MyNamespace {
Book::Book(int bID, std::string bName, std::string authFirstN,std::string authLastN) {
bookID = bID;
bookName = bName;
authorFirstName = authFirstN;
authorLastName = authLastN;
dueDate = new Date(1,1,1000);
borrower = nullptr;
}
Book::~Book() {
delete borrower;
delete dueDate;
}
std::string Book::getBookID() const {
return std::to_string(bookID);
}
std::string Book::getBookName() {
return bookName;
}
std::string Book::getAuthorFirstName() {
return authorFirstName;
}
std::string Book::getAuthorLastName() {
return authorLastName;
}
Date Book::getDueDate() {
return *dueDate;
}
void Book::resetDaysSetFlag() {
numOfDaysSet = true;
}
int Book::chooseAnotherDate() {
int numOfDays = 0;
std::cout << "How many days from today would you like the set the due date to be: ";
std::string input;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //Clearing input buffer.
while (true) {
std::getline(std::cin,input);
if (input.empty()) {
std::cerr << "Input cannot be empty. Please enter a number: ";
continue;
}
try {
numOfDays = std::stoi(input);
} catch (const std::invalid_argument &) {
std::cerr << "Invalid input.Please enter an integer: ";
continue;
}
if (numOfDays > 0){
break;
} else {
std::cerr << "Invalid input. The number of days must be an integer greater than 0: ";
}
}
return numOfDays;
}
int Book::determineNumOfDays() {
char option;
bool isChoiceNotValid;
int numOfDays;
std::cout<< std::endl << "Would you like to use the default due date which is 3 days from today or choose another date?" << std::endl;
std::cout << "Enter 'Y' for custom date and 'N' for default: ";
do {
std::cin.get(option);
isChoiceNotValid = (std::toupper(option) != 'Y' && std::toupper(option) != 'N');
if (isChoiceNotValid) {
std::cerr << "Invalid input, please enter 'Y' or 'N': ";
}
} while (isChoiceNotValid);
if (std::toupper(option) == 'Y') {
numOfDays = chooseAnotherDate();
} else {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
numOfDays = 3;
}
return numOfDays;
}
void Book::setDueDate(Date* currentDate) {
Date newDueDate = *currentDate;
if (!numOfDaysSet) { // Checking to see if the number of days has already been determined
numOfDays = this->determineNumOfDays();
numOfDaysSet = true;
}
newDueDate = newDueDate.addDays(numOfDays);
dueDate = new Date(newDueDate);
}
void Book::returnBook() {
std::vector<Book*>& bookVec = borrower->getBooksBorrowed();
auto it = std::find(bookVec.begin(),bookVec.end(),this); // Searching through the booksBorrowed vector of member and deleting the pointer
if (it != bookVec.end()) {
bookVec.erase(it);
delete dueDate;
dueDate = new Date(1,1,1000);
}
}
void Book::borrowBook(Member &bur,Date due) {
dueDate = new Date(due);
bur.setBooksBorrowed(this);
borrower = &bur; // Making borrower Pointer point to new borrower
std::cout << std::endl;
std::cout <<'"'<< bookName <<'"'<< " by " << authorFirstName << " " << authorLastName <<" was successfully borrowed to member with ID " << borrower->getMemberID() <<"."<< std::endl;
}
}