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
|
#include <iostream>
#include <fstream>
#include <iomanip>
const int MAX_AR_SZ = 25; // Max array size
const int MAX_FILENAME_LEN = 40;// Max array size for fileName string
using namespace::std;
class inventory
{
public:
inventory();
inventory(int id, int store, int qt);
void setId(int id);
void setStoreNr(int store);
void setQuantity(int qt);
int getId(void) const;
int getStoreNr(void) const;
int getQuantity(void) const;
private:
int itemId;
int storeNr;
int quantity;
};
inventory::inventory()
{
itemId = 0;
storeNr = 0;
quantity = 0;
}
inventory::inventory(int id, int store, int qt)
{
itemId = id;
storeNr = store;
quantity = qt;
}
void inventory::setId(int id){itemId = id;}
void inventory::setStoreNr(int store){ storeNr = store;}
void inventory::setQuantity(int qt){quantity = qt;}
int inventory::getId(void) const{return itemId;}
int inventory::getStoreNr(void) const{return storeNr;}
int inventory::getQuantity(void) const{return quantity;}
bool loadArrays(const char[], inventory[], int& , int);
void printArrays(ostream&, inventory[], int);
bool extractData(const char[], long, int,inventory[], int, int &);
int main()
{
inventory invLs[MAX_AR_SZ];
int cellsLoaded, baseQty, written;
long reqId;
char fileName[MAX_FILENAME_LEN];
if (!loadArrays(fileName, invLs, cellsLoaded, MAX_AR_SZ))
{
return 0;
}
printArrays(cout, invLs, cellsLoaded);
cout << "Product :";
cin >> reqId;
cout << "Quantity:";
cin >> baseQty;
extractData(fileName, reqId, baseQty, invLs, cellsLoaded, written);
return 0;
}
bool loadArrays(const char fileName[], inventory invList[], int & count, int maxCells)
{
count = 0;
ifstream In;
In.open("C:\\Users\\Sophie\\Downloads\\Project3Records.txt");
if (!In)
{
cout << "Error 1: Cannot find file\n\t" << fileName << endl;
return false;
}
int temp1, temp2, temp3; // <--
while (count < MAX_AR_SZ && In >> temp1 >> temp2 >> temp3) //<--
{
invList[count].setId(temp1); // <--
invList[count].setStoreNr(temp2); // <--
invList[count].setQuantity(temp3); // <--
count++;
if (count >= maxCells && In)
{
cout << "Error 3: " << fileName << " contains more data than array can hold\n";
return false;
}
}
return true;
}
void printArrays(ostream & where, inventory invList[], int count)
{
cout << setw(12) << "Product ID" << setw(6) << "Store"
<< setw(5) << "Qty" << endl;
for (int i = 0; i < count; i++)
{
where << setw(12) << invList[i].getId() << setw(6) << invList[i].getStoreNr()
<< setw(5) << invList[i].getQuantity() << endl;
}
}
bool extractData(const char newFileName[], long requestId, int baseQty,inventory invList[], int count, int & newcount)
{
ofstream Out;
Out.open("C:\\Users\\Sophie\\Downloads\\Project3.txt");
if (!Out)
{
cout << "Could not open file" << endl;
return false;
}
newcount = 0;
for (int i = 0; i < count; i++)
if (invList[i].getId() == requestId && invList[i].getQuantity() < baseQty)
{
Out << invList[i].getId() << " " << invList[i].getStoreNr()
<< " " << invList[i].getQuantity() << " " << endl;
++newcount;
}
return true;
}
|