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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <limits>
#include <cctype>
constexpr size_t MAX_RECORDS {10};
constexpr size_t SIZE_DESC {20};
const std::string defname {"inventory.dat"};
struct Item {
char description[SIZE_DESC] {};
double price {};
int in_stock {};
int sold {};
};
Item items[MAX_RECORDS] {};
void createEmptyInventory();
void editInventory();
void displayInventory();
void makeAnOrder();
void createOrderReport();
void createRevenueReport();
void saveInventory(const std::string& fnam = defname);
void readInventory(const std::string& fnam = defname);
void createInventory();
void initInventory();
double getDouble(const std::string& prm = "")
{
double d {};
while ((std::cout << prm) && !(std::cin >> d)) {
std::cout << "Invalid number\n";
if (prm.empty()) std::cout << "Please re-enter: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return d;
}
int getInt(const std::string& prm = "")
{
int i {};
while ((std::cout << prm) && !(std::cin >> i)) {
std::cout << "Invalid number\n";
if (prm.empty()) std::cout << "Please re-enter: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return i;
}
void createEmptyInventory()
{
char ch {};
std::cout << "\nWarning! This will destroy all data.\n";
std::cout << "Press 'y' to continue: ";
std::cin >> ch;
if (std::tolower(ch) == 'y') {
initInventory();
saveInventory();
}
}
void createInventory()
{
size_t itemNo {};
for (; itemNo < MAX_RECORDS && items[itemNo].description[0]; ++itemNo);
if (itemNo < MAX_RECORDS) {
std::cout << "New item " << itemNo + 1 << '\n';
std::cout << "Enter description: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.getline(items[itemNo].description, SIZE_DESC);
items[itemNo].price = getDouble("Enter Price: ");
items[itemNo].in_stock = getInt("Enter quantity in stock : ");
items[itemNo].sold = getInt("Enter quantity sold: ");
} else
std::cout << "Inventory full\n";
}
void initInventory()
{
for (size_t i = 0; i < MAX_RECORDS; ++i) {
items[i].description[0] = 0;
items[i].price = 0.0;
items[i].in_stock = 0;
items[i].sold = 0;
}
}
void displayInventory()
{
std::cout << std::left << std::setw(8) << "\nITEM#" << std::setw(SIZE_DESC + 2) << "DESCRIPTION" << std::setw(8) << "PRICE" << std::setw(11) << "#INSTOCK" << "#SOLD\n";
for (size_t i = 0; i < MAX_RECORDS; ++i)
if (items[i].description[0])
std::cout << std::left << std::setw(8) << i + 1 << std::setw(SIZE_DESC + 2) << items[i].description << std::right << std::setw(5) << items[i].price
<< std::setw(11) << items[i].in_stock << std::setw(8) << items[i].sold << '\n';
}
void editInventory()
{
char again {'y'};
do {
displayInventory();
const int itemNo {getInt("Which item # do you want to edit (0 to create, -1 to quit): ") - 1};
if (itemNo == -2)
break;
if (itemNo == -1) {
createInventory();
continue;
}
if (itemNo < 0 || itemNo >= MAX_RECORDS || items[itemNo].description[0] == 0) {
std::cout << "Please enter valid item #\n";
continue;
}
std::cout << "1. Description\n";
std::cout << "2. Price\n";
std::cout << "3. Quantity In Stock\n";
std::cout << "4. Quantity Sold\n";
std::cout << "Enter choice: ";
switch (getInt()) {
case 1:
std::cout << "Enter new description: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.getline(items[itemNo].description, SIZE_DESC);
break;
case 2:
items[itemNo].price = getDouble("Enter new Price: ");
break;
case 3:
items[itemNo].in_stock = getInt("Enter new quantity in stock : ");
break;
case 4:
items[itemNo].sold = getInt("Enter new quantity sold: ");
break;
default:
std::cout << "Invalid choice\n";
break;
}
std::cout << "Do you want to edit another record (y/n)? ";
std::cin >> again;
} while (std::tolower(again) == 'y');
saveInventory();
}
void makeAnOrder()
{
while (true) {
displayInventory();
const int itemNo {getInt("Which item # do you want to order (-1 to quit): ") - 1};
if (itemNo == -2)
break;
if (itemNo < 0 || itemNo >= MAX_RECORDS || items[itemNo].description[0] == 0) {
std::cout << "Please enter valid item #\n";
continue;
}
std::cout << "Description: " << items[itemNo].description << '\n';
std::cout << "Price: " << items[itemNo].price << '\n';
std::cout << "Quantity In Stock: " << items[itemNo].in_stock << '\n';
std::cout << "Quantity Sold: " << items[itemNo].sold << '\n';
const int qty {getInt("How many? ")};
if (qty > items[itemNo].in_stock)
std::cout << "Sorry, your order exceeds number in stock.\n";
else {
items[itemNo].in_stock -= qty;
items[itemNo].sold += qty;
std::cout << "Amount due: " << items[itemNo].price * qty << '\n';
}
}
saveInventory();
}
void createOrderReport()
{
std::cout << "\nItems to reorder\n\n";
std::cout << std::left << std::setw(8) << "ITEM#" << std::setw(SIZE_DESC + 2) << "DESCRIPTION" << std::setw(8) << "PRICE" << std::setw(11) << "#INSTOCK" << "#SOLD\n";
for (size_t i = 0; i < MAX_RECORDS; ++i)
if (items[i].description[0] && items[i].in_stock <= 5)
std::cout << std::left << std::setw(8) << i + 1 << std::setw(SIZE_DESC + 2) << items[i].description << std::right << std::setw(5) << items[i].price
<< std::setw(11) << items[i].in_stock << std::setw(8) << items[i].sold << '\n';
}
void saveInventory(const std::string& fnam)
{
const std::string fn {fnam.empty() ? defname : fnam};
std::ofstream outfile(fn, std::ios::binary);
if (outfile.is_open())
if (outfile.write((char*)&items, sizeof(items)))
std::cout << "Wrote data file " << fn << " OK\n";
else
std::cout << "Problem writing data file " << fn << '\n';
else
std::cout << "File opening error for " << fn << '\n';
}
void readInventory(const std::string& fnam)
{
const std::string fn {fnam.empty() ? defname : fnam};
std::ifstream infile(fn, std::ios::binary);
if (infile.is_open())
if (infile.read((char*)&items, sizeof(items)))
std::cout << "Read data file " << fn << " OK\n";
else
std::cout << "Problem reading data file " << fn << '\n';
else
std::cout << "Input file does not yet exist " << fn << '\n';
}
int main()
{
readInventory();
while (true) {
std::cout << "\n ORCHARD TO TABLE ORDERING SYSTEM \n\n";
std::cout << "1. Create Empty Inventory\n";
std::cout << "2. Display State of Inventory\n";
std::cout << "3. Edit/Create Inventory\n";
std::cout << "4. Make an Order\n";
std::cout << "5. Create Reorder Report\n";
std::cout << "6. Create Revenue Report\n";
std::cout << "7. Save Current Inventory\n";
std::cout << "8. Load New Inventory\n";
std::cout << "9. Quit \n";
std::cout << "Enter your choice: ";
switch (getInt()) {
case 1:
createEmptyInventory();
break;
case 2:
displayInventory();
break;
case 3:
editInventory();
break;
case 4:
makeAnOrder();
break;
case 5:
createOrderReport();
break;
case 6:
break;
case 7:
{
std::string name;
std::cout << "\nFile name to save to [" << defname << "] :";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, name);
saveInventory(name);
}
break;
case 8:
{
std::string name;
std::cout << "\nFile name to load from [" << defname << "] :";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, name);
saveInventory();
initInventory();
readInventory(name);
}
break;
case 9:
return 0;
default:
std::cout << "Invalid choice\n";
break;
}
}
}
|