Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iomanip>
- #include <iostream>
- #include <string>
- using namespace std;
- class Car //ALL OF THESE CLASSES NEED GETTERS/SETTERS
- {
- public:
- Car(string iBrand, string iModel, int iYear);
- private:
- string brand;
- string model;
- int year;
- };
- class Parts
- {
- friend void populate(ifstream& file, Oil oilArr[][15], Brakes brakeArr[][15], Tires tireArr[][15], Lights lightArr[][15]);
- public:
- Parts(Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
- protected:
- Car carType;
- string description;
- string manufacturer;
- float price;
- int qtySold;
- };
- class Brakes : public Parts
- {
- public:
- Brakes(string iMaterial, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
- private:
- string material;
- };
- class Lights : public Parts
- {
- public:
- Lights(int iWatts, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
- private:
- int watts;
- };
- class Oil : public Parts
- {
- private:
- string weight;
- string type;
- int quarts;
- public:
- Oil(string iWeight, string iType, int iQuarts, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
- };
- class Tires : public Parts
- {
- public:
- Tires(string iSize, int iWarranty, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
- private:
- string size;
- int warranty;
- };
- void parseLineToTokens(string lineText, string tokens[]);
- void partCounter(ifstream& file, int objectHolder[]);
- void bestSeller(ifstream& file, Oil oilArr[], Brakes brakeArr[], Tires tireArr[], Lights lightArr[]);
- int partCount(string lineText);
- /*int main()
- {
- //open the file from which to read the data
- //call a global function to find out how many objects of each type to create
- //create arrays to contain the necessary objects
- //global function to read information from the file into the arrays of objects
- //call functions to find the best selling item for each category, output best to a file
- //close the file explicitly
- }*/
- int main()
- {
- int qtypartTypes[4];
- int oilObjects = 0;
- int brakeObjects = 0;
- int lightObjects = 0;
- int tireObjects = 0;
- ifstream inputFile("ayy.txt"); //opens file streams to input and output files
- ofstream outputFile("Best_Parts.txt");
- partCounter(inputFile, qtypartTypes);
- oilObjects = qtypartTypes[0];
- brakeObjects = qtypartTypes[1];
- lightObjects = qtypartTypes[2];
- tireObjects = qtypartTypes[3];
- Oil** oilArray; //creates 2d array (I hope) //create arrays for other part types
- oilArray = new Oil*[oilObjects];
- for (int i = 0; i < oilObjects; ++i)
- {
- oilArray[i] = new Oil[15];
- }
- /*Oil *oilArray = NULL;
- oilArray = new Oil[*qtypartTypes];
- Brakes *brakeArray = NULL;
- brakeArray = new Brakes[*(qtypartTypes + 1)];
- Lights *lightArray = NULL;
- lightArray = new Lights[*(qtypartTypes + 2)];
- Tires *tireArray = NULL;
- tireArray = new Tires[*(qtypartTypes + 3)];*/
- string lineText = "\"oil\", \"\", \"54\", \"tacos rule\", \"456\""; //adjust this loop to cycle through every line; requires reading new line from the file on each loop
- string tokens[15]; //make this the number of , aka PARTINFO_CNT
- parseLineToTokens(lineText, tokens);
- /*for (int i = 0; i < 16; ++i)
- {
- cout << "token " << i + 1 << ": " << tokens[i] << endl; //test lines
- }
- cout << lineText;*/
- inputFile.clear(); //resets the pointer
- inputFile.seekg(0, ios::beg);
- /*populate(inputFile, oilArray, brakeArray, tireArray, lightArray);
- bestSeller(inputFile, oilArray, brakeArray, tireArray, lightArray);*/
- inputFile.close();
- outputFile.close();
- /*delete[]lightArray;
- delete[]brakeArray;
- delete[]tireArray;
- delete[]oilArray;*/
- cout << endl;
- }
- void populate(ifstream& file, Oil oilArr[][15], Brakes brakeArr[][15], Tires tireArr[][15], Lights lightArr[][15]) //friend
- {
- string tokens[15]; //stores 15 pieces of data from each line " "
- string lineText;
- while (getline(file, lineText))
- {
- parseLineToTokens(lineText, tokens);
- }
- }
- int partCount(string lineText) //might not need
- {
- int PARTINFO_CNT = 0;
- for (int i = 0; i < lineText.size(); i++)
- {
- if (lineText.at(i) == ',')
- {
- PARTINFO_CNT++;
- }
- }
- cout << "count: " << PARTINFO_CNT << endl;
- return PARTINFO_CNT;
- }
- // Parse a line of text into tokens and store them in an array of strings
- void parseLineToTokens(string lineText, string tokens[]) //function acts only on one line at a time
- {
- int end, start, value;
- value = partCount(lineText);
- start = -3;
- for (int j = 0; j < value + 1; j++)
- {
- start = start + 4; //start+4 allows insures start always lands on the first character after a quotation mark
- end = lineText.find('"', start); //cout end to test // paramaters mean it looks for the next " from where it 'start'ed
- //cout << " start: " << start << " end: " << end << endl; //test
- tokens[j] = lineText.substr(start, end - start); //splits up by word
- start = end; //IDEA: experimental if start==end and another if changing whether start is end or end+1
- }
- }
- void partCounter(ifstream& file, int objectHolder[])
- {
- int oilCount = 0;
- int tireCount = 0;
- int lightCount = 0;
- int brakeCount = 0;
- string line;
- string name;
- int start = -2;
- int end = 0;
- int len = 0; //stores the number of lines in the file
- file.clear(); //resets the pointer
- file.seekg(0, ios::beg);
- while (getline(file, line))
- {
- start = 1;
- end = line.find('"', start);
- name = line.substr(start, end - start); //paramaters are (beginning, number of chars)
- //cout << name << endl;
- if (name == "Oil")
- {
- oilCount++;
- }
- else if (name == "Brakes")
- {
- brakeCount++;
- }
- else if (name == "Lights")
- {
- lightCount++;
- }
- else if (name == "Tires")
- {
- tireCount++;
- }
- }
- objectHolder[0] = oilCount;
- objectHolder[1] = brakeCount;
- objectHolder[2] = lightCount;
- objectHolder[3] = tireCount;
- }
- Car::Car(string iBrand, string iModel, int iYear)
- : brand(iBrand), model(iModel), year(iYear)
- {
- }
- Parts::Parts(Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
- : carType(iCar), description(iDescription), manufacturer(iManufacturer), price(iPrice), qtySold(iQtySold)
- {
- }
- Brakes::Brakes(string iMaterial, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
- : Parts(iCar, iDescription, iManufacturer, iPrice, iQtySold)
- {
- material = iMaterial;
- }
- Lights::Lights(int iWatts, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
- : Parts(iCar, iDescription, iManufacturer, iPrice, iQtySold)
- {
- watts = iWatts;
- }
- Oil::Oil(string iWeight, string iType, int iQuarts, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
- : Parts(iCar, iDescription, iManufacturer, iPrice, iQtySold)
- {
- weight = iWeight;
- type = iType;
- quarts = iQuarts;
- }
- Tires::Tires(string iSize, int iWarranty, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
- : Parts(iCar, iDescription, iManufacturer, iPrice, iQtySold)
- {
- size = iSize;
- warranty = iWarranty;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement