-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMilestone_3.cpp
133 lines (120 loc) · 4 KB
/
Milestone_3.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
// Manager Milestone - Main Program
// Milestome_3.cpp
// Chris Szalwinski
// v1.0 - 16/11/2015
// v2.0 - 15/01/2016
#include <iostream>
#include <fstream>
#include <string>
#include <memory>
#include "Task.h"
#include "TaskManager.h"
#include "CustomerOrder.h"
#include "OrderManager.h"
#include "Item.h"
#include "ItemManager.h"
#include "Utilities.h"
template<typename M, typename T>
void loadFromFile(const char*, M&, std::ostream&);
template<>
void loadFromFile<TaskManager, Task>(const char*, TaskManager&, std::ostream&);
int main(int argc, char** argv) {
// process command line arguments
if (argc != 5) {
std::cerr << "*** invalid number of arguments ***\n"
<< "Usage: " << argv[0] << " tasks_file order_file item_file delimiter\n";
exit(1);
}
std::cout << "Command Line Arguments\n----------------------\n";
std::cout << "Task File Specified = " << argv[1] << "\n";
std::cout << "Customer Order File Specified = " << argv[2] << "\n";
std::cout << "Item File Specified = " << argv[3] << "\n";
std::cout << "File Record Field Delimiter = " << argv[4][0] << "\n\n";
Utilities::setDelimiter(argv[4][0]);
// end of command line processing
// Load, accept, validate and display the Tasks
//
std::cout << "Building the Network of Tasks\n-----------------------------\n";
TaskManager taskManager;
loadFromFile<TaskManager, Task>(argv[1], taskManager, std::cerr);
taskManager.display(std::cout);
std::cout << "\nValidating the Set of Tasks\n---------------------------\n";
taskManager.validate(std::cerr);
std::cout << "\nList of Accepted Tasks\n----------------------\n";
taskManager.display(std::cout);
// Load, accept and display the Customer Orders
//
std::cout << "\n*** Load and Accept the Customer Orders ***\n";
OrderManager orderManager;
loadFromFile<OrderManager, CustomerOrder>(argv[2], orderManager, std::cerr);
std::cout << "\nList of Accepted Customer Orders\n--------------------------------\n";
orderManager.display(std::cout);
// Load, accept and display the Items in Stock
//
std::cout << "\n*** Load and accept the Items in Stock ***\n";
ItemManager itemManager;
loadFromFile<ItemManager, Item>(argv[3], itemManager, std::cerr);
std::cout << "\nList of In-Stock Items\n----------------------\n";
itemManager.display(std::cout, true);
// Validate the Items in the Customer Orders
orderManager.validate(itemManager, std::cout);
// Validate the Tasks in each Item in Stock
taskManager.validate(itemManager, std::cerr);
// Fill the Customer Orders with the Items in Stock
std::cout << "\n*** Fill the Customer Orders ***\n";
for (auto i = itemManager.begin(); i != itemManager.end(); i++) {
for (auto s = orderManager.begin(); s != orderManager.end(); s++)
s->fill(*i);
}
// Display the Processed Customer Orders
std::cout << "\nList of Processed Customer Orders\n---------------------------------\n";
orderManager.display(std::cout);
// Terminate
std::cout << "\nDone!\nPress Enter Key to Exit ... ";
char c;
std::cin.get(c);
}
template<typename M, typename T>
void loadFromFile(const char* fileName, M& manager, std::ostream& os) {
std::ifstream file(fileName);
if (!file) {
os << "*** Cannot open file named " << fileName << " ***\n";
exit(1);
}
while (file) {
std::string record;
std::getline(file, record);
if (file) {
try {
std::unique_ptr<T> entry(new T(record));
if (!entry->empty())
manager.push_back(std::move(*entry));
}
catch (const std::string& msg) {
os << msg << std::endl;
}
}
}
}
template<>
void loadFromFile<TaskManager, Task>(const char* fileName, TaskManager& manager, std::ostream& os) {
std::ifstream file(fileName);
if (!file) {
os << "*** Cannot open file named " << fileName << " ***\n";
exit(1);
}
while (file) {
std::string record;
std::getline(file, record);
if (file) {
try {
std::unique_ptr<Task> entry(new Task(record));
if (!entry->getName().empty())
manager.push_back(std::move(*entry));
}
catch (const std::string& msg) {
os << msg << std::endl;
}
}
}
}