-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCornerGrocer.cpp
More file actions
237 lines (220 loc) · 7.32 KB
/
CornerGrocer.cpp
File metadata and controls
237 lines (220 loc) · 7.32 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
/*******************************************************
* CornerGrocer.cpp
* Author: Kristi Huggins SNHU
* Date: December 2025
*
* Description:
* Item-tracking program for the Corner Grocer.
* - Reads daily purchase log from CS210_Project_Three_Input_File.txt
* - Builds frequency map of items
* - Writes backup file frequency.dat automatically
* - Provides menu with options:
* 1. Query item frequency
* 2. Print all item frequencies
* 3. Print histogram
* 4. Exit
*
* Styling:
* - Menu: Options 1–3 plain text, Exit in red
* - Item list: plain text
* - Histogram: stars colored by frequency ranges
*******************************************************/
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <stdexcept>
#include <limits>
#define NOMINMAX
#include <windows.h> // For SetConsoleTextAttribute
/*------------------------------------------------------
Utility: setColor
Sets console text color using Windows API.
Common color codes:
7 = Gray (default)
10 = Green
12 = Red
14 = Yellow
6 = Dark Yellow (approx orange)
------------------------------------------------------*/
void setColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
/*------------------------------------------------------
Class: CornerGrocer
Responsibilities:
- Load item frequencies from file
- Write backup file
- Query single item frequency
- Print all frequencies
- Print histogram with colored stars
- Run interactive menu
------------------------------------------------------*/
class CornerGrocer {
private:
std::map<std::string, int> freq_; // Item -> frequency
std::string inputFilePath_; // Input file path
public:
// Constructor: initialize with input file path
explicit CornerGrocer(std::string inputPath)
: inputFilePath_(std::move(inputPath)) {
}
/*----------------------------------------------
Load frequencies from input file.
Throws runtime_error if file cannot be opened.
----------------------------------------------*/
void loadFromFile() {
freq_.clear();
std::ifstream fin(inputFilePath_);
if (!fin) {
throw std::runtime_error("Failed to open input file: " + inputFilePath_);
}
std::string item;
while (fin >> item) {
++freq_[item]; // Increment count for each item
}
}
/*----------------------------------------------
Write backup file (frequency.dat).
Contains item and frequency pairs.
----------------------------------------------*/
void writeBackup(const std::string& outPath = "frequency.dat") const {
std::ofstream fout(outPath);
if (!fout) {
throw std::runtime_error("Failed to write backup file: " + outPath);
}
for (const auto& kv : freq_) {
fout << kv.first << " " << kv.second << "\n";
}
}
/*----------------------------------------------
Query frequency of a single item.
Returns 0 if item not found.
----------------------------------------------*/
int getFrequency(const std::string& item) const {
auto it = freq_.find(item);
return (it != freq_.end()) ? it->second : 0;
}
/*----------------------------------------------
Print all items and their frequencies.
Black/white text only.
----------------------------------------------*/
void printAll() const {
std::cout << "\n=== Item Frequencies ===\n";
if (freq_.empty()) {
std::cout << "[No items loaded]\n";
return;
}
for (const auto& kv : freq_) {
std::cout << kv.first << " " << kv.second << "\n";
}
}
/*----------------------------------------------
Print histogram with colored stars.
Frequency determines star color:
1-3 = Red
4-6 = Orange (approx)
7-9 = Yellow
10+ = Green
----------------------------------------------*/
void printHistogram() const {
std::cout << "\n=== Purchase Histogram ===\n";
if (freq_.empty()) {
std::cout << "[No items loaded]\n";
return;
}
for (const auto& kv : freq_) {
std::cout << kv.first << " ";
int count = kv.second;
// Determine color based on frequency
int color;
if (count <= 3) {
color = 12; // Red
}
else if (count <= 6) {
color = 6; // Dark Yellow (approx orange)
}
else if (count <= 9) {
color = 14; // Yellow
}
else {
color = 10; // Green
}
setColor(color);
for (int i = 0; i < count; ++i) {
std::cout << "*";
}
setColor(7); // Reset
std::cout << "\n";
}
}
/*----------------------------------------------
Run interactive menu loop.
Options 1-3: black/white
Option 4: red
----------------------------------------------*/
void runMenu() const {
while (true) {
std::cout << "\nCorner Grocer Item Tracker\n";
std::cout << "1. Query item frequency\n";
std::cout << "2. Print all item frequencies\n";
std::cout << "3. Print histogram\n";
setColor(12); // Red
std::cout << "4. Exit\n";
setColor(7); // Reset
std::cout << "Select an option (1-4): ";
int option;
if (!(std::cin >> option)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input. Please enter a number from 1 to 4.\n";
continue;
}
if (option == 1) {
std::cout << "Enter item name: ";
std::string query;
std::cin >> query;
int count = getFrequency(query);
std::cout << query << " " << count << "\n";
}
else if (option == 2) {
printAll();
}
else if (option == 3) {
printHistogram();
}
else if (option == 4) {
setColor(12);
std::cout << "Exiting program. Goodbye!\n";
setColor(7);
break;
}
else {
std::cout << "Invalid option. Please select 1-4.\n";
}
}
}
};
/*------------------------------------------------------
Main function:
- Creates CornerGrocer object
- Loads data from input file
- Writes backup file
- Runs interactive menu
------------------------------------------------------*/
int main() {
const std::string inputPath = "CS210_Project_Three_Input_File.txt";
try {
CornerGrocer app(inputPath);
app.loadFromFile(); // Load data
app.writeBackup(); // Write backup file
app.runMenu(); // Run menu loop
}
catch (const std::exception& ex) {
setColor(12); // Red for error
std::cerr << "Fatal error: " << ex.what() << "\n";
setColor(7);
return 1;
}
return 0;
}