-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.cpp
More file actions
44 lines (38 loc) · 1.25 KB
/
Item.cpp
File metadata and controls
44 lines (38 loc) · 1.25 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
#include "Item.h"
#include <iomanip> // ADDED THIS LINE
// Constructor implementation
Item::Item(std::string name, int quantity, double price)
: name(name), quantity(quantity), price(price) {
// Basic validation
if (quantity < 0) {
this->quantity = 0; // Ensure quantity is not negative
}
if (price < 0.0) {
this->price = 0.0; // Ensure price is not negative
}
}
// Getter implementations
std::string Item::getName() const {
return name;
}
int Item::getQuantity() const {
return quantity;
}
double Item::getPrice() const {
return price;
}
// Setter implementation for quantity
void Item::setQuantity(int newQuantity) {
if (newQuantity >= 0) { // Ensure new quantity is not negative
quantity = newQuantity;
} else {
std::cout << "Quantity cannot be negative. Quantity remains unchanged."; // ADDED SEMICOLON HERE
}
}
// Display method implementation
void Item::displayItem() const {
std::cout << "Name: " << name
<< ", Quantity: " << quantity
<< ", Price: R" << std::fixed << std::setprecision(2) << price
<< " (Total: R" << std::fixed << std::setprecision(2) << (quantity * price) << ")" << std::endl;
}