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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int MAX_SIZE = 10;
typedef int myArray[MAX_SIZE];
ifstream inFile;
void InitializeUser(int& totalBudget, int& totalWeight);
void OpenFile(ifstream& infile, myArray cost, myArray weight);
void PrintName(int id);
bool ItemMenu(myArray cost, myArray weight, myArray quantity, int &totalBudget, int &totalWeight);
bool ValidateInput(const int choice, const myArray cost, const myArray weight, const int totalBudget, const int totalWeight);
void PrintSummary(const myArray quantity, const myArray cost, const int totalBudget, const int totalWeight, const int, const int);
int main()
{
myArray id,
cost,
weight,
quantity = { 0 };
int Budget,
myWeight,
startingWeight,
startingBudget;
InitializeUser(Budget, myWeight);
startingWeight = myWeight;
startingBudget = Budget;
cout << endl << endl;
OpenFile(inFile, cost, weight);
inFile.close();
while (ItemMenu(cost, weight, quantity, Budget, myWeight));
PrintSummary(quantity, cost, Budget, myWeight, startingBudget, startingWeight);
}
void InitializeUser(int& Budget, int& myWeight)
{
cout << "Please enter the company's total budget: ";
cin >> Budget;
cout << "Now enter the maximum possible total weight: ";
cin >> myWeight;
}
void OpenFile(ifstream& inFile, myArray cost, myArray weight)
{
int id, itemCost, itemWeight, i;
inFile.open("inventory.txt");
if (!inFile)
{
cout << "Error: Data file could not be opened" << endl;
exit(EXIT_FAILURE);
}
for (i = 0; i < 10; i++)
{
inFile >> id;
inFile >> itemCost;
inFile >> itemWeight;
cost[i] = itemCost;
weight[i] = itemWeight;
cout << "ID Cost Weight" << endl;
cout << "----------------------" << endl;
// print all IDs and corresponding categories
for (i = 0; i < MAX_SIZE; i++)
{
cout << left << setw(10) << i ;
cout << endl << " " << itemCost[i] << " ";
itemWeight[i];
}
}
}
void PrintName(int id)
{
switch (id)
{
case 0: cout << "computer(s)";
break;
case 1: cout << "pencil(s)";
break;
case 2: cout << "pen(s)";
break;
case 3: cout << "book(s)";
break;
case 4: cout << "beer(s)";
break;
case 5: cout << "ruler(s)";
break;
case 6: cout << "stereo(s)";
break;
case 7: cout << "refrigerator(s)";
break;
case 8: cout << "desk(s)";
break;
case 9: cout << "backpack(s)";
break;
}
}
bool ItemMenu(myArray cost, myArray weight, myArray quantity, int &totalBudget, int &totalWeight)
{
int choice;
cout << "Budget Remaining $" << totalBudget << endl;
cout << "Weight Remaining " << totalWeight << " pounds" << endl;
cout << endl << endl;
for (int i = 0; i < 10; i++)
{
cout << i << ") Buy ";
PrintName(i);
cout << "(cost $" << cost[i] << ", weight " << weight[i] << " pounds)" << endl;
}
cout << "Please enter your choice: " << endl;
cin >> choice;
if (!ValidateInput(choice, cost, weight, totalBudget, totalWeight))
cout << "Invalid choice." << endl;
else
{
if (choice != 10)
{
totalBudget = totalBudget - cost[choice];
totalWeight = totalWeight - weight[choice];
quantity[choice]++;
}
else
{
return false;
}
}
return true;
}
bool ValidateInput(const int choice, const myArray cost, const myArray weight, const int totalBudget, const int totalWeight)
{
if (cost[choice] > totalBudget)
return false;
if (weight[choice] > totalWeight)
return false;
if (choice < 0 || choice > 10)
return false;
return true;
}
void PrintSummary(const myArray quantity, const myArray cost, const int totalBudget, const int totalWeight, const int startingBudget, const int startingWeight)
{
cout << "ID" << " Name" << " Quantity" << endl;
cout << "--------------------------" << endl;
for (int i = 0; i < 10; i++)
{
cout << i << " ";
PrintName(i);
cout << " " << quantity[i] << endl;
}
cout << "Total Amount Spent : $ ";
cout << startingBudget - totalBudget << endl;
cout << "Amount of Budget Remaining : $ ";
cout << totalBudget << endl;
cout << "Total Weight of Items Purchased : " << totalWeight << " pounds" << endl;
}
|