-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompletedOrdersStack.cpp
More file actions
151 lines (132 loc) · 5.4 KB
/
CompletedOrdersStack.cpp
File metadata and controls
151 lines (132 loc) · 5.4 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
// This is the implementation file for the CompletedOrdersStack class.
// For function behaviors and details about the methods used, please refer to this file.
// The corresponding header file, CompletedOrdersStack.h, provides the class structure and function declarations.
//
// CompletedOrdersStack.cpp
//
#include "CompletedOrdersStack.h"
#include <cassert>
// Node constructor, initializes the order and sets the next pointer to NULL
CompletedOrdersStack::Node::Node(const Order& order) {
this->order = order; // Assign the order to the node
this->next = NULL; // Initialize next to NULL
}
// Default constructor, initializes an empty stack
CompletedOrdersStack::CompletedOrdersStack() {
firstOrder = NULL; // First order pointer is NULL
size = 0; // Stack size is initially 0
top = NULL; // Top of the stack is NULL
}
// Destructor, clears all nodes in the stack
CompletedOrdersStack::~CompletedOrdersStack() {
while (!isEmpty()) {
Node* temp = top; // Temporarily hold the top node
top = top->next; // Move top to the next node
delete temp; // Delete the old top node
}
}
// Returns the current size of the stack
int CompletedOrdersStack::getSize() const {
return size;
}
// Pushes a new order onto the stack
void CompletedOrdersStack::push(const Order& order) {
NodePtr newOrder = new Node(order); // Create a new node for the order
if (isEmpty()) {
firstOrder = newOrder; // If the stack is empty, set firstOrder and top to the new node
top = newOrder;
} else {
top->next = newOrder; // Otherwise, link the new node to the current top and update top
top = newOrder;
}
size++; // Increment the stack size
}
// Checks if the stack is empty
bool CompletedOrdersStack::isEmpty() const {
return firstOrder == NULL; // Return true if firstOrder is NULL
}
// Pops the top order from the stack and returns it
const Order& CompletedOrdersStack::pop() {
assert(!isEmpty()); // Ensure the stack is not empty
// If there is only one order in the stack
if (firstOrder->next == NULL) {
const Order& popOrder = firstOrder->order; // Retrieve the order
delete firstOrder; // Delete the single node
firstOrder = NULL; // Reset pointers
top = NULL;
size--; // Decrement the stack size
return popOrder;
}
// Traverse the stack to find the second-to-last node
NodePtr temp = firstOrder;
while (temp->next != top) {
temp = temp->next;
}
const Order& popOrder = top->order; // Retrieve the top order
temp->next = NULL; // Update the second-to-last node's next pointer
delete top; // Delete the old top node
top = temp; // Update the top pointer
size--; // Decrement the stack size
return popOrder;
}
// Displays the prices of all completed orders
void CompletedOrdersStack::displayOrderPrice() {
NodePtr temp = firstOrder; // Start from the first order
while (temp != NULL) {
cout << "Order " << temp->order.getOrderId() << ": $"
<< temp->order.getTotalPrice() << endl; // Display order ID and price
temp = temp->next; // Move to the next node
}
}
// Calculates the total revenue from all completed orders
double CompletedOrdersStack::calculateTotalRevenue() const {
double totalPrice = 0; // Initialize total revenue
if (isEmpty()) {
return 0; // Return 0 if the stack is empty
}
NodePtr temp = firstOrder; // Start from the first order
while (temp != NULL) {
totalPrice += temp->order.getTotalPrice(); // Accumulate order prices
temp = temp->next; // Move to the next node
}
return totalPrice; // Return the total revenue
}
// Displays all completed orders
void CompletedOrdersStack::displayCompletedOrders() const {
if (isEmpty()) {
cout << "No completed orders." << endl; // Display message if the stack is empty
return;
}
NodePtr temp = firstOrder; // Start from the first order
while (temp != NULL) {
cout << temp->order << endl; // Display the order details
temp = temp->next; // Move to the next node
}
}
// Prints the current date to the provided output stream
void CompletedOrdersStack::printTime(ostream& out) const {
time_t now = time(0); // Get the current time
tm* localTime = localtime(&now); // Convert to local time
// Format the date as YYYY-MM-DD
char dateBuffer[11];
strftime(dateBuffer, sizeof(dateBuffer), "%Y-%m-%d", localTime);
out << "Date: " << dateBuffer << endl; // Output the date
}
// Saves all completed orders to a file
bool CompletedOrdersStack::saveCompletedOrdersToFile(const string& filename) const {
ofstream file(filename.c_str()); // Open the file
if (!file) {
cerr << "Error opening file: " << filename << endl; // Display error message if file can't be opened
return false;
}
printTime(file); // Print the date to the file
file << "--- Completed Orders ---\n"; // Write a header to the file
NodePtr temp = firstOrder; // Start from the first order
while (temp != NULL) {
file << temp->order << endl; // Write each order to the file
temp = temp->next; // Move to the next node
}
file.close(); // Close the file
cout << "Orders have been saved to " << filename << endl; // Display success message
return true; // Return true on success
}