Repository navigation
Expand file tree
/
Copy pathOrderProcessor.cpp
More file actions
71 lines (58 loc) · 2.5 KB
/
Copy pathOrderProcessor.cpp
File metadata and controls
71 lines (58 loc) · 2.5 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
#include "OrderProcessor.h"
#include "NotificationService.h"
#include "PaymentProcessor.h"
#include <chrono>
#include <iostream>
#include <sstream>
OrderProcessor::OrderProcessor(InventoryManager& inventory)
: inventory(inventory), nextOrderNumber(1) {}
bool OrderProcessor::placeOrder(int productId, int quantity,
const std::string& customerName,
const std::string& customerEmail,
const std::string& cardLastFour) {
if (quantity <= 0) {
std::cout << "Quantity must be positive.\n";
return false;
}
int productIndex = -1;
for (int i = 0; i < inventory.productCount; ++i) {
if (inventory.products[i].id == productId) {
productIndex = i;
break;
}
}
if (productIndex == -1) {
std::cout << "Product not found.\n";
return false;
}
if (inventory.products[productIndex].quantity < quantity) {
std::cout << "Not enough stock available.\n";
return false;
}
const int totalCents = inventory.products[productIndex].priceCents * quantity;
const std::string productName = inventory.products[productIndex].name;
const auto now = std::chrono::system_clock::now().time_since_epoch();
const auto timestamp = std::chrono::duration_cast<std::chrono::microseconds>(now).count();
const std::string orderId = std::to_string(timestamp) + "_" +
std::to_string(nextOrderNumber++);
PaymentProcessor payment;
if (!payment.chargeCreditCard(customerName, cardLastFour, totalCents, orderId)) {
std::cout << "Order canceled: payment record could not be saved.\n";
return false;
}
// Tight coupling: directly changes InventoryManager's internal array.
inventory.products[productIndex].quantity -= quantity;
// Tight coupling: assumes the only notification is an email.
std::ostringstream message;
message << "Hello " << customerName << ",\n";
message << "Order " << orderId << " has been recorded.\n";
message << "Item: " << productName << "\n";
message << "Quantity: " << quantity << "\n";
message << "Total: $" << totalCents / 100 << '.';
if (totalCents % 100 < 10) message << '0';
message << totalCents % 100 << "\n";
message << "An IOU file was saved locally; no real payment was made.";
NotificationService notifications;
notifications.sendEmail(customerEmail, "Order confirmation", message.str());
return true;
}