-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest10.cpp
164 lines (112 loc) · 5.93 KB
/
test10.cpp
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
160
161
162
163
164
#include <iostream>
#include <array>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
namespace n215
{
template<auto... item_codes>
struct InventoryBatch
{
static constexpr size_t size() { return sizeof...(item_codes); }
// Let's imagine these codes represent specific inventory items.
// In the 1950s, maybe they were punch card IDs.
// Simulating a "second argument" of the template conceptually:
// We can associate a batch ID with each instantiation.
int batch_id;
// Simulating a "third argument" conceptually:
// Perhaps a date code associated with the batch.
std::string shipment_date;
// Simulating a "fourth argument" conceptually:
// Maybe the warehouse location.
std::string warehouse_location;
InventoryBatch(int id, std::string date, std::string location) : batch_id(id), shipment_date(std::move(date)), warehouse_location(std::move(location)) {}
// A simple function to access the item codes.
static constexpr auto getItemCodes()
{
return std::make_tuple(item_codes...);
}
// TO-DO: The core logic - calculating the total value of the batch.
// This requires looking up prices for each item code.
double calculateTotalValue() const
{
double total = 0.0;
// Here's where the magic should happen. Imagine looking up prices
// from a ledger based on the item codes. This is the hard part.
// Think about how you would iterate through the 'item_codes'
// and fetch the corresponding prices.
// Consider potential discounts for bulk orders or older stock.
// Maybe certain item codes have fixed prices.
// Remember, we're in the 1950s, so no fancy databases!
// Perhaps a series of if-else statements or a lookup table.
// This part is intentionally left for you to implement the core logic.
// Think about how to access the individual elements of the
// 'item_codes' parameter pack.
// You might need helper functions or data structures to store prices.
// Example:
// if constexpr (size() > 0) {
// if constexpr (std::get<0>(getItemCodes()) == 123) {
// total += getItemPrice(123); // Assume getItemPrice exists
// }
// // ... and so on for other item codes
// }
// Good luck! This is where your template metaprogramming skills
// will be tested. No simple loops here!
// TODO: Implement the logic to calculate the total value based on item_codes.
return sumOfItems();
}
constexpr double sumOfItems() const
{
return std::apply([](auto... args) { return (getItemPrice(args) + ...); }, getItemCodes());
}
// Helper function (you might need to implement this or similar)
double getItemPrice(auto itemCode) const
{
// In a real 1950s system, this might involve looking up in a table.
// For this exercise, let's just provide some dummy prices.
if constexpr (std::is_same_v<decltype(itemCode), int>) {
if (itemCode == 1001) return 5.0;
if (itemCode == 1002) return 10.0;
if (itemCode == 1003) return 7.5;
} else if constexpr (std::is_same_v<decltype(itemCode), char>) {
if (itemCode == 'A') return 3.0;
if (itemCode == 'B') return 6.0;
}
return 0.0; // Default price
}
};
} // namespace n215
void run_tests() {
using namespace n215;
// Test case 1: Empty batch
InventoryBatch<> batch1(1, "1955-03-15", "Warehouse A");
static_assert(batch1.size() == 0, "Test Case 1 Failed: Size should be 0");
// The total value of an empty batch should be 0.
// static_assert(batch1.calculateTotalValue() == 0.0, "Test Case 1 Failed: Total value should be 0"); // Will fail until you implement calculateTotalValue
// Test case 2: Batch with integer item codes
InventoryBatch<1001, 1002, 1001> batch2(2, "1955-04-01", "Warehouse B");
static_assert(batch2.size() == 3, "Test Case 2 Failed: Size should be 3");
// Expected total value: getItemPrice(1001) + getItemPrice(1002) + getItemPrice(1001) = 5.0 + 10.0 + 5.0 = 20.0
// static_assert(batch2.calculateTotalValue() == 20.0, "Test Case 2 Failed: Incorrect total value"); // Will fail until you implement calculateTotalValue
// Test case 3: Batch with character item codes
InventoryBatch<'A', 'B', 'A'> batch3(3, "1955-04-15", "Warehouse A");
static_assert(batch3.size() == 3, "Test Case 3 Failed: Size should be 3");
// Expected total value: getItemPrice('A') + getItemPrice('B') + getItemPrice('A') = 3.0 + 6.0 + 3.0 = 12.0
// static_assert(batch3.calculateTotalValue() == 12.0, "Test Case 3 Failed: Incorrect total value"); // Will fail until you implement calculateTotalValue
// Test case 4: Batch with mixed item codes
InventoryBatch<1001, 'B', 1003> batch4(4, "1955-05-01", "Warehouse C");
static_assert(batch4.size() == 3, "Test Case 4 Failed: Size should be 3");
// Expected total value: getItemPrice(1001) + getItemPrice('B') + getItemPrice(1003) = 5.0 + 6.0 + 7.5 = 18.5
// static_assert(batch4.calculateTotalValue() == 18.5, "Test Case 4 Failed: Incorrect total value"); // Will fail until you implement calculateTotalValue
// Test case 5: Batch with duplicate item codes
InventoryBatch<1002, 1002, 1002> batch5(5, "1955-05-15", "Warehouse B");
static_assert(batch5.size() == 3, "Test Case 5 Failed: Size should be 3");
// Expected total value: getItemPrice(1002) + getItemPrice(1002) + getItemPrice(1002) = 10.0 + 10.0 + 10.0 = 30.0
// static_assert(batch5.calculateTotalValue() == 30.0, "Test Case 5 Failed: Incorrect total value"); // Will fail until you implement calculateTotalValue
std::cout << "All static asserts passed (initial checks). Implement calculateTotalValue to run the value checks." << std::endl;
}
int main() {
run_tests();
return 0;
}