-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_11.cpp
More file actions
80 lines (67 loc) · 2.03 KB
/
Problem_11.cpp
File metadata and controls
80 lines (67 loc) · 2.03 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Item structure to store details of an item
struct Item {
int code;
double price;
};
class ShoppingList {
private:
vector<Item> items; // Vector to store the list of items
public:
// Add an item to the shopping list
void addItem(int code, double price) {
items.push_back({code, price});
cout << "Item with code " << code << " and price " << price << " added." << endl;
}
// Delete an item from the shopping list by code
void deleteItem(int code) {
for (auto it = items.begin(); it != items.end(); ++it) {
if (it->code == code) {
items.erase(it);
cout << "Item with code " << code << " deleted." << endl;
return;
}
}
cout << "Item with code " << code << " not found." << endl;
}
// Calculate and return the total price of the shopping list
double calculateTotal() const {
double total = 0;
for (const auto& item : items) {
total += item.price;
}
return total;
}
// Display all items in the shopping list
void displayItems() const {
if (items.empty()) {
cout << "The shopping list is empty." << endl;
return;
}
cout << "Shopping List:" << endl;
for (const auto& item : items) {
cout << "Code: " << item.code << ", Price: " << item.price << endl;
}
}
};
// Main function to demonstrate the functionality
int main() {
ShoppingList shoppingList;
// Add items
shoppingList.addItem(101, 15.5);
shoppingList.addItem(102, 25.0);
shoppingList.addItem(103, 40.75);
// Display items
shoppingList.displayItems();
// Delete an item
shoppingList.deleteItem(102);
// Display items after deletion
shoppingList.displayItems();
// Calculate total
double total = shoppingList.calculateTotal();
cout << "Total Order Value: " << total << endl;
return 0;
}