Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
1,757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. #include <fstream>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Car //ALL OF THESE CLASSES NEED GETTERS/SETTERS
  9. {
  10. public:
  11. Car(string iBrand, string iModel, int iYear);
  12. private:
  13. string brand;
  14. string model;
  15. int year;
  16. };
  17.  
  18. class Parts
  19. {
  20. friend void populate(ifstream& file, Oil oilArr[][15], Brakes brakeArr[][15], Tires tireArr[][15], Lights lightArr[][15]);
  21. public:
  22. Parts(Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
  23. protected:
  24. Car carType;
  25. string description;
  26. string manufacturer;
  27. float price;
  28. int qtySold;
  29. };
  30.  
  31. class Brakes : public Parts
  32. {
  33. public:
  34. Brakes(string iMaterial, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
  35. private:
  36. string material;
  37. };
  38.  
  39. class Lights : public Parts
  40. {
  41. public:
  42. Lights(int iWatts, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
  43. private:
  44. int watts;
  45. };
  46.  
  47. class Oil : public Parts
  48. {
  49. private:
  50. string weight;
  51. string type;
  52. int quarts;
  53. public:
  54. Oil(string iWeight, string iType, int iQuarts, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
  55. };
  56.  
  57. class Tires : public Parts
  58. {
  59. public:
  60. Tires(string iSize, int iWarranty, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold);
  61. private:
  62. string size;
  63. int warranty;
  64. };
  65.  
  66. void parseLineToTokens(string lineText, string tokens[]);
  67. void partCounter(ifstream& file, int objectHolder[]);
  68. void bestSeller(ifstream& file, Oil oilArr[], Brakes brakeArr[], Tires tireArr[], Lights lightArr[]);
  69. int partCount(string lineText);
  70.  
  71. /*int main()
  72. {
  73. //open the file from which to read the data
  74.  
  75. //call a global function to find out how many objects of each type to create
  76.  
  77. //create arrays to contain the necessary objects
  78.  
  79. //global function to read information from the file into the arrays of objects
  80.  
  81. //call functions to find the best selling item for each category, output best to a file
  82.  
  83. //close the file explicitly
  84. }*/
  85.  
  86. int main()
  87. {
  88. int qtypartTypes[4];
  89. int oilObjects = 0;
  90. int brakeObjects = 0;
  91. int lightObjects = 0;
  92. int tireObjects = 0;
  93. ifstream inputFile("ayy.txt"); //opens file streams to input and output files
  94. ofstream outputFile("Best_Parts.txt");
  95. partCounter(inputFile, qtypartTypes);
  96.  
  97. oilObjects = qtypartTypes[0];
  98. brakeObjects = qtypartTypes[1];
  99. lightObjects = qtypartTypes[2];
  100. tireObjects = qtypartTypes[3];
  101.  
  102. Oil** oilArray; //creates 2d array (I hope) //create arrays for other part types
  103. oilArray = new Oil*[oilObjects];
  104. for (int i = 0; i < oilObjects; ++i)
  105. {
  106. oilArray[i] = new Oil[15];
  107. }
  108.  
  109.  
  110.  
  111. /*Oil *oilArray = NULL;
  112. oilArray = new Oil[*qtypartTypes];
  113. Brakes *brakeArray = NULL;
  114. brakeArray = new Brakes[*(qtypartTypes + 1)];
  115. Lights *lightArray = NULL;
  116. lightArray = new Lights[*(qtypartTypes + 2)];
  117. Tires *tireArray = NULL;
  118. tireArray = new Tires[*(qtypartTypes + 3)];*/
  119.  
  120. 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
  121. string tokens[15]; //make this the number of , aka PARTINFO_CNT
  122. parseLineToTokens(lineText, tokens);
  123. /*for (int i = 0; i < 16; ++i)
  124. {
  125. cout << "token " << i + 1 << ": " << tokens[i] << endl; //test lines
  126. }
  127. cout << lineText;*/
  128.  
  129. inputFile.clear(); //resets the pointer
  130. inputFile.seekg(0, ios::beg);
  131.  
  132. /*populate(inputFile, oilArray, brakeArray, tireArray, lightArray);
  133. bestSeller(inputFile, oilArray, brakeArray, tireArray, lightArray);*/
  134.  
  135. inputFile.close();
  136. outputFile.close();
  137. /*delete[]lightArray;
  138. delete[]brakeArray;
  139. delete[]tireArray;
  140. delete[]oilArray;*/
  141. cout << endl;
  142. }
  143.  
  144. void populate(ifstream& file, Oil oilArr[][15], Brakes brakeArr[][15], Tires tireArr[][15], Lights lightArr[][15]) //friend
  145. {
  146. string tokens[15]; //stores 15 pieces of data from each line " "
  147. string lineText;
  148. while (getline(file, lineText))
  149. {
  150. parseLineToTokens(lineText, tokens);
  151.  
  152. }
  153. }
  154.  
  155. int partCount(string lineText) //might not need
  156. {
  157. int PARTINFO_CNT = 0;
  158. for (int i = 0; i < lineText.size(); i++)
  159. {
  160. if (lineText.at(i) == ',')
  161. {
  162. PARTINFO_CNT++;
  163. }
  164. }
  165. cout << "count: " << PARTINFO_CNT << endl;
  166. return PARTINFO_CNT;
  167. }
  168.  
  169. // Parse a line of text into tokens and store them in an array of strings
  170. void parseLineToTokens(string lineText, string tokens[]) //function acts only on one line at a time
  171. {
  172. int end, start, value;
  173. value = partCount(lineText);
  174. start = -3;
  175. for (int j = 0; j < value + 1; j++)
  176. {
  177. start = start + 4; //start+4 allows insures start always lands on the first character after a quotation mark
  178. end = lineText.find('"', start); //cout end to test // paramaters mean it looks for the next " from where it 'start'ed
  179. //cout << " start: " << start << " end: " << end << endl; //test
  180. tokens[j] = lineText.substr(start, end - start); //splits up by word
  181. start = end; //IDEA: experimental if start==end and another if changing whether start is end or end+1
  182. }
  183. }
  184.  
  185. void partCounter(ifstream& file, int objectHolder[])
  186. {
  187. int oilCount = 0;
  188. int tireCount = 0;
  189. int lightCount = 0;
  190. int brakeCount = 0;
  191. string line;
  192. string name;
  193. int start = -2;
  194. int end = 0;
  195. int len = 0; //stores the number of lines in the file
  196. file.clear(); //resets the pointer
  197. file.seekg(0, ios::beg);
  198. while (getline(file, line))
  199. {
  200. start = 1;
  201. end = line.find('"', start);
  202. name = line.substr(start, end - start); //paramaters are (beginning, number of chars)
  203. //cout << name << endl;
  204. if (name == "Oil")
  205. {
  206. oilCount++;
  207. }
  208. else if (name == "Brakes")
  209. {
  210. brakeCount++;
  211. }
  212. else if (name == "Lights")
  213. {
  214. lightCount++;
  215. }
  216. else if (name == "Tires")
  217. {
  218. tireCount++;
  219. }
  220. }
  221. objectHolder[0] = oilCount;
  222. objectHolder[1] = brakeCount;
  223. objectHolder[2] = lightCount;
  224. objectHolder[3] = tireCount;
  225. }
  226.  
  227. Car::Car(string iBrand, string iModel, int iYear)
  228. : brand(iBrand), model(iModel), year(iYear)
  229. {
  230. }
  231.  
  232. Parts::Parts(Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
  233. : carType(iCar), description(iDescription), manufacturer(iManufacturer), price(iPrice), qtySold(iQtySold)
  234. {
  235. }
  236.  
  237. Brakes::Brakes(string iMaterial, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
  238. : Parts(iCar, iDescription, iManufacturer, iPrice, iQtySold)
  239. {
  240. material = iMaterial;
  241. }
  242.  
  243. Lights::Lights(int iWatts, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
  244. : Parts(iCar, iDescription, iManufacturer, iPrice, iQtySold)
  245. {
  246. watts = iWatts;
  247. }
  248.  
  249. Oil::Oil(string iWeight, string iType, int iQuarts, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
  250. : Parts(iCar, iDescription, iManufacturer, iPrice, iQtySold)
  251. {
  252. weight = iWeight;
  253. type = iType;
  254. quarts = iQuarts;
  255. }
  256.  
  257. Tires::Tires(string iSize, int iWarranty, Car iCar, string iDescription, string iManufacturer, float iPrice, int iQtySold)
  258. : Parts(iCar, iDescription, iManufacturer, iPrice, iQtySold)
  259. {
  260. size = iSize;
  261. warranty = iWarranty;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement