-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMilestone_2.cpp
94 lines (86 loc) · 2.71 KB
/
Milestone_2.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
// CustomerOrder Milestone - Main Program
// Milestone_2.cpp
// Chris Szalwinski
// v1.0 - 9/11/2015
// v1.1 - 9/11/2015 - customerOrder -> customerOrders for g++ 5.2 compatability (CS)
// v1.2 - 16/11/2015 - add <cstdlib> for exit (CS)
// v2.0 - 15/01/2016
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include <memory>
#include "CustomerOrder.h"
#include "Utilities.h"
#include "Item.h"
template<typename T>
void loadFromFile(const char*, std::vector<T>&, std::ostream&);
int main(int argc, char** argv) {
// process command line arguments
if (argc != 4) {
std::cerr << "*** invalid number of arguments ***\n"
<< "Usage: " << argv[0] << " customer_order_file item_file delimiter\n";
exit(1);
}
std::cout << "Command Line Arguments\n----------------------\n";
std::cout << "Customer Order File Specified = " << argv[1] << "\n";
std::cout << "Item File Specified = " << argv[2] << "\n";
std::cout << "File Record Field Delimiter = " << argv[3][0] << "\n\n";
Utilities::setDelimiter(argv[3][0]);
// end of command line processing
// Load, Accept and Display the Customer Orders
//
std::cout << "\n*** Load and Accept the Customer Orders ***\n";
std::vector<CustomerOrder> customerOrders;
loadFromFile(argv[1], customerOrders, std::cerr);
std::cout << "\nList of Accepted Customer Orders\n--------------------------------\n";
for (auto& s : customerOrders)
s.display(std::cout);
// Load, Accept and Display the Items in Stock
//
std::cout << "\n*** Load and Accept the Items in Stock ***\n";
std::vector<Item> items;
loadFromFile(argv[2], items, std::cerr);
std::cout << "\nList of Items in Stock\n----------------------\n";
for (auto& i : items)
i.display(std::cout, true);
// Fill the Customer Orders with the Items in Stock
//
std::cout << "\n*** Process the Customer Orders ***\n";
for (auto& i : items)
for (auto& s : customerOrders)
s.fill(i);
// Display the Processed Customer Orders
//
std::cout << "\nList of Processed Customer Orders\n---------------------------------\n";
for (auto& s : customerOrders)
s.display(std::cout);
// Terminate
//
std::cout << "\nDone!\nPress Enter Key to Exit ... ";
char c;
std::cin.get(c);
}
template<typename T>
void loadFromFile(const char* fileName, std::vector<T>& collection, 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())
collection.push_back(std::move(*entry));
}
catch (const std::string& msg) {
os << msg << std::endl;
}
}
}
}