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 <iomanip>
#include <string>
using namespace std;
class Seller
{
private:
string FirstName;
string LastName;
string ID;
double SalesTotal;
public:
// Constructors
Seller(); // Default constructor
Seller(string, string, string , double);
// Accessor methods
void setFirstName(string);
string getFirstName();
void setLastName(string);
string getLastName();
void setID(string);
string ID();
void setSalesTotal(double);
double setSalesTotal();
// Other methods
void print();
};
class SalesRegion
{
private:
double numSellers;
public:
SalesRegion();
SalesRegion(string, string);
void setRegionName(string);
string getRegionName();
void sortSellersByID();
void findSellerID(string);
void setRegionName(string);
string getRegionName();
void processSales(string);
void print();
};
Seller::Seller() //default constructor for the Seller Class
{
setFirstName("None");
setLastName("None");
setID("ZZZ000");
setSalesTotal(0);
}
Seller::Seller(string newFirstName, string newLastName, string newID, double newSalesTotal) //contructor for seller class
{
setFirstName(newFirstName);
setLastName(newLastName);
setID (newID);
setSalesTotal(newSalesTotal);
}
void Seller::setFirstName(string newFirstName) //check if the length of newFirstName is greater than 0
{
if (newFirstName.length() > 0)
{
firstName = newFirstName;
}
else
firstName = "None";
}
void Seller::setLastName(string newLastName) //check if the length of newLastName is greater than 0
{
if (newLastName.length() > 0)
{
lastName = newLastName;
}
else
lastName = "None";
}
void Seller::setID(string newID) //check if the length of newID is greater than 0
{
if (newID.length() > 0 && newID.length() < 7)
{
sellerID = newID;
}
else
sellerID = "ZZZ000";
}
void Seller::setSalesTotal(double newSalesTotal) //checks if the sales is greater than 0
{
if (newSalesTotal> 0)
{
salesTotal = newSalesTotal;
}
else
salesTotal = 0;
}
string Seller::getFirstName() //returns the seller's first name
{
return firstName;
}
string Seller::getLastName() //returns the seller's last name
{
return lastName;
}
string Seller::getID() //returns the seller's id
{
return sellerID;
}
double Seller::getSalesTotal() //returns the seller's sales total
{
return salesTotal;
}
void Seller::print() //prints data members of seller into formatted columns
{
cout << fixed << setprecision(2);
cout << left << setw (25) << lastName + ", " + firstName << " " << setw(9) << sellerID
<< right << setw (10) <<salesTotal << endl;
}
void SalesRegion::sortSellersByID() //sorts selers in ascending order by seller id
{
int i;
int j;
int min;
Seller temp;
for (i = 0; i < numSellers - 1; i++)
{
min = i;
for (j = i+1; j < numSellers; j++)
{
if (sellerArray[j].getID() < sellerArray[min].getID())
min = j;
}
temp = sellerArray[i];
sellerArray[i] = sellerArray[min];
sellerArray[min] = temp;
}
}
int SalesRegion::findSellerID(string searchID) // finds the position of the seller array element with a seller ID that matches search id
{
int i;
for (i = 0; i < numSellers; i++)
{
if (searchID == sellerArray[i].getID())
return i;
}
return -1;
}
SalesRegion::SalesRegion() //default constructor for the Sales Region Class
{
numSellers = 0;
}
SalesRegion::SalesRegion(string newRegionName, string fileName) //constructor for the Sales Region Class, builds sales array
{
ifstream inFile;
string firstName;
string lastName;
string sellerID;
double salesTotal;
numSellers = 0;
setRegionName (newRegionName);
inFile.open(fileName.c_str());
if (!inFile)
{
cout << "Error - unable to open input file " << fileName << endl;
exit(1);
}
inFile >> lastName;
while (inFile)
{
inFile >> firstName;
inFile >> sellerID;
inFile >> salesTotal;
Seller newSeller (firstName, lastName, sellerID, salesTotal);
sellerArray [numSellers] = newSeller;
numSellers++;
inFile >> lastName;
}
inFile.close();
sortSellersByID ();
}
void SalesRegion::setRegionName(string newRegionName) //checks if the lenght of the newRegionName is greater than 0
{
if (newRegionName.length() > 0)
{
regionName = newRegionName;
}
else
regionName = "None";
}
string SalesRegion::getRegionName() //returns the region name
{
return regionName;
}
void SalesRegion::processSales(string fileName) //reads a series of sales records and processes them into the seller array
{
ifstream inFile;
string id; //seller's id
double salesAmount; //sales of the transaction made
cout << endl
<< internal << setw(22) << "Sales Transactions" << endl
<< endl
<< left << setw(15) << "Seller ID" << "Sales Amount" <<endl
<< endl;
inFile.open(fileName.c_str());
if (!inFile)
{
cout << "Error - unable to open input file\n";
exit(1);
}
inFile >> id;
while (inFile)
{
inFile >> salesAmount;
int index = findSellerID (id); //proccesses the sales transaction to the proper element in the sellerArray
if (index == - 1)
{
cout << left << setw (15) << id << right << setw(10) << "Error - ID not found" <<endl;
}
sellerArray[index].setSalesTotal(sellerArray[index].getSalesTotal() + salesAmount);
cout << left << setw (15) << id << right << setw(10) << salesAmount << endl;
inFile >> id;
}
inFile.close ();
cout << endl;
}
void SalesRegion::print() //prints the seller array
{
cout << internal << setw(30)<< "Sales Listing for " << regionName << endl <<endl;
int i;
for (i = 0; i < numSellers; i++)
{
sellerArray[i].print();
}
}
int main()
{
SalesRegion region("Illinois", "sellersIL.txt");
region.print();
region.processSales("salesILJuly.txt");
region.print();
return 0;
}
| |