-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInventoryBook.cpp
More file actions
159 lines (137 loc) · 3.45 KB
/
InventoryBook.cpp
File metadata and controls
159 lines (137 loc) · 3.45 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#define _CRT_SECURE_NO_WARNINGS
#include "InventoryBook.h"
/*
Default constructor, calls base class Book default constructor
Set all member variables to 0
Set dateAdded using the time function
*/
InventoryBook::InventoryBook() : Book()
{
quantity = wholCost = retCost = lastPurchase = 0;
time(&dateAdded);
}
/*
getData
Prompt user for each member variable
Set member variables using strncpy to avoid overflow
Throw exceptions for invalid numerical input
*/
void InventoryBook::getData()
{
string userInput;
cout << "Enter book title: ";
getline(cin, userInput);
strncpy(title, userInput.c_str(), 100);
cout << "Enter ISBN no. : ";
getline(cin, userInput);
strncpy(isbn, userInput.c_str(), 11);
cout << "Enter author's name: ";
getline(cin, userInput);
strncpy(author, userInput.c_str(), 30);
cout << "Enter publisher's name: ";
getline(cin, userInput);
strncpy(publisher, userInput.c_str(), 20);
cout << "Enter quantity: ";
getline(cin, userInput);
try
{
quantity = stol(userInput);
}
catch (invalid_argument)
{
cout << "Invalid argument.\n";
return;
}
catch (out_of_range)
{
cout << "Number is too large.\n";
return;
}
cout << "Enter wholesale price: ";
getline(cin, userInput);
try
{
wholCost = stod(userInput);
}
catch (invalid_argument)
{
cout << "Invalid argument.\n";
return;
}
catch (out_of_range)
{
cout << "Number is too large.\n";
return;
}
cout << "Enter retail price: ";
getline(cin, userInput);
try
{
retCost = stod(userInput);
}
catch (invalid_argument)
{
cout << "Invalid argument.\n";
return;
}
catch (out_of_range)
{
cout << "Number is too large.\n";
return;
}
setDate();
return;
}
/*
operator<<
Construct a time string using localtime and asctime functions of the ctime library
Return an ostream object with properly formatted data
*/
ostream& operator<< (ostream &outstream, InventoryBook &ib)
{
struct tm *timeinfo = localtime(&ib.dateAdded);
outstream << ib.isbn << endl;
outstream << ib.title << endl;
outstream << ib.author << endl;
outstream << "Quantity: " << ib.quantity << endl;
outstream << "Wholesale Cost: $" << setprecision(2) << fixed << ib.wholCost << endl;
outstream << "Retail Cost: $" << setprecision(2) << fixed << ib.retCost << endl;
outstream << "Added On: " << asctime(timeinfo) << endl << endl;
return outstream;
}
/*
operator==
If 2 InventoryBook objects have the same ISBN
Return true
Else
Return false
*/
bool InventoryBook::operator== (const InventoryBook &right)
{
if (strcmp(isbn, right.isbn) == 0)
return true;
else
return false;
}
/*
sale
If the quantity sold is greater than or equal to the quantity on hand
Subtract the quantity sold
Return true
Else
Display an error for the user
Return false
*/
bool InventoryBook::sale(int s)
{
if (quantity >= s)
{
quantity -= s;
return true;
}
else
{
cout << "The quantity exceeds number of available books" << endl;
return false;
}
}