-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_2.cpp
More file actions
239 lines (231 loc) · 6.88 KB
/
Assignment_2.cpp
File metadata and controls
239 lines (231 loc) · 6.88 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//=====================================================================================
// Name : Index.cpp
// Author : Jose Solis Salazar
// Version : 1
// Copyright : Copyright (C). CS Department Foothill College 2015. All rights reserved.
// Program purpose: Read in txt file data, parse and store data in objects. Array of pointers will conatin addresses for each object created.
//=====================================================================================
#include <iostream>
#include <fstream>
//#include <conio.h>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
//global
typedef enum { pcNew, pcUsed, pcRefurbished, pcDefective } T_ProductCondition;
const int g_array_size = 512;
int generateProductID() {
int id = 0;
id = rand() % 10000;
return id;
}
//InvntoryItem
class InventoryItem {
protected:
string name_;
int quantity_;
public:
InventoryItem();
InventoryItem(string, int);
~InventoryItem();
void set_name_and_quantity(string, int);
void Display()const;
string get_name()const;
int get_quantity()const;
};
InventoryItem::InventoryItem():name_("Product"), quantity_(0) {}
InventoryItem::InventoryItem(string name, int quantity):name_(name), quantity_(quantity) {}
InventoryItem::~InventoryItem() {
cout << " InventoryItem: " << name_ << " with " << quantity_ << " items destroyed ..." << endl;
}
void InventoryItem::set_name_and_quantity(string name, int quantity){
name_ = name;
quantity_ = quantity;
}
void InventoryItem::Display()const {
cout << "Item: " << name_ << " Quanity: " << quantity_;
}
string InventoryItem::get_name()const {
return name_;
}
int InventoryItem::get_quantity()const {
return quantity_;
}
//Product
class Product : public InventoryItem {
private:
int product_id_;
double price_;
T_ProductCondition condition_;
public:
Product();
Product(double, T_ProductCondition);
~Product();
//int get_id()const;
double get_price()const;
T_ProductCondition get_condition()const;
char get_condition_letter()const;
void Display()const;
};
Product::Product():product_id_(0), price_(0.00), InventoryItem() {}
Product::Product(double price, T_ProductCondition condition):product_id_(generateProductID()), price_(price), condition_(condition){}
Product::~Product() {
cout << "Product: " << product_id_ << " priced $" << setprecision(6) << price_;
}
char Product::get_condition_letter()const{
char condition_letter;
switch (condition_) {
case pcNew:
condition_letter = 'N';
break;
case pcUsed:
condition_letter = 'U';
break;
case pcRefurbished:
condition_letter = 'R';
break;
case pcDefective:
condition_letter = 'D';
break;
}
return condition_letter;
}
void Product::Display()const{
InventoryItem::Display();
cout << " Product ID: " << product_id_ << " Price: " << setprecision(6) << price_ << " Condition: " << get_condition_letter() << endl;
}
//int Product::get_id()const{
// return product_id_;
//}
double Product::get_price()const{
return price_;
}
T_ProductCondition Product::get_condition()const{
return condition_;
}
//InventorySystem
class InventorySystem {
private:
string store_name_;
int store_id_;
InventoryItem * item_list_[g_array_size];
int item_count_;
public:
InventorySystem();
InventorySystem(string, int);
~InventorySystem();
void BuildInventory();
void ShowInventory()const;
void ShowDefectInventory()const;
void Terminate()const;
};
InventorySystem::InventorySystem():store_name_("Generic Store"), store_id_(0), item_count_(0){
item_list_[g_array_size] = { NULL };
srand(time(NULL)); //seed invoked
}
InventorySystem::InventorySystem(string name, int id):store_name_(name), store_id_(id), item_count_(0) {
item_list_[g_array_size] = { NULL };
srand(time(NULL)); //seed invoked
}
InventorySystem::~InventorySystem() {
for (int i = 0; i < item_count_; i++) {
InventoryItem * p_temp = item_list_[i];
Product * p_product = static_cast<Product*>(p_temp);
delete p_product;
}
}
void InventorySystem::BuildInventory(){
ifstream file("product_list.txt"); //assuming list is in the same folder as cpp
string buffer;
string product_name;
int quantity;
double price;
char condition_letter;
T_ProductCondition condition;
if (!file) {
cout << "ERROR: Failed to open input file\n";
exit(-1);
}
else {
while (getline(file, buffer, ';')) {
product_name = buffer;
getline(file, buffer, ';');
quantity = stoi(buffer);
getline(file, buffer, ';');
price = atof(buffer.c_str());
getline(file, buffer, '\n');
condition_letter = buffer[0];
switch (condition_letter) {
case 'N':
condition = pcNew;
break;
case 'U':
condition = pcUsed;
break;
case 'R':
condition = pcRefurbished;
break;
case 'D':
condition = pcDefective;
break;
} //memory dynamically allocated for a product object instantiated w/ non default constructor
item_list_[item_count_] = new Product(price, condition);
Product * p_product = static_cast<Product*>(item_list_[item_count_]); //InventoryItem pointer downcasted to product
p_product->set_name_and_quantity(product_name, quantity);
item_list_[item_count_] = p_product;
++item_count_;
}
}
}
void InventorySystem::ShowInventory()const{
for (int i = 0; i < item_count_; i++) {
InventoryItem * p_temp = item_list_[i];
Product * p_product = static_cast<Product*>(p_temp);
p_product->Display();
}
}
void InventorySystem::ShowDefectInventory()const{
T_ProductCondition condition;
for (int i = 0; i < item_count_; i++) {
InventoryItem * p_temp = item_list_[i];
Product * p_product = static_cast<Product*>(p_temp);
condition = p_product->get_condition();
if (condition == pcDefective) {
p_product->Display();
}
}
}
void InventorySystem::Terminate()const{
string buffer;
string product_name;
int quantity;
double price;
char condition;
//int id;
ofstream fileout("despacito.txt"); //writes to a file called despacito.txt to same folder this cpp file is in
for (int i = 0; i < item_count_; i++) {
InventoryItem * p_temp = item_list_[i];
Product * p_product = static_cast<Product*>(p_temp);
product_name = p_product->get_name();
quantity = p_product->get_quantity();
price = p_product->get_price();
condition = p_product->get_condition_letter();
//id = p_product->get_id();
fileout << product_name << ';' << quantity << ';' << setprecision(6) << price << ';' << condition << endl;
}
fileout.close();
}
int main() {
string store = "Circuit City";
int store_id = 1234;
InventorySystem * p_InventorySystem = new InventorySystem(store, store_id);
p_InventorySystem->BuildInventory();
p_InventorySystem->ShowInventory();
p_InventorySystem->ShowDefectInventory();
p_InventorySystem->Terminate();
delete p_InventorySystem;
//_getch();
return 0;
}