Advertisement
Guest User

bettercode

a guest
Sep 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. //main.cpp
  2. //#include "cml6895_Auto_Part.h"
  3. #include "cml6895_Inventory.h"
  4. #include <iostream>
  5. #include <vector>
  6.  
  7. int main()
  8. {
  9. std::vector<Auto_Part> parts;
  10. Inventory inventory;
  11.  
  12. int num_parts;
  13. std::cout << "How many parts do you wish to create?" << std::endl;
  14. std::cin >> num_parts;
  15.  
  16. for(int i = 0; i < num_parts; i++)
  17. {
  18. std::string type;
  19. std::string name;
  20. int part_number, amount;
  21. double price;
  22. std::cout << "Enter type of part: ";
  23. std::cin >> type;
  24. std::cout << "Enter name of part: ";
  25. std::cin >> name;
  26. std::cout <<"Enter part number: ";
  27. std::cin >> part_number;
  28. std::cout <<"Enter price: ";
  29. std::cin >> price;
  30. std::cout << "Enter how many are there in the inventory: ";
  31. std::cin >> amount;
  32.  
  33. parts.push_back(Auto_Part(type, name, part_number, price));
  34. inventory.add_part(Auto_Part(type, name, part_number, price));
  35. }
  36.  
  37. for(auto x : parts)
  38. {
  39. std::cout << x;
  40. }
  41.  
  42. double sum = 0;
  43. for(Auto_Part part : parts)
  44. {
  45. sum += part.get_price();
  46. }
  47. std::cout << "Average: " << sum/parts.size() << std::endl;
  48.  
  49. //std::cout << inventory.first << inventory.second << std::endl;
  50.  
  51. /*
  52. std::map<Auto_Part::part, int>::iterator it = mymap.begin();
  53. for(it = inventory.begin(); it!= inventory.end(); ++it)
  54. {
  55. std::cout << it->first << it->second << std::endl;
  56. }
  57. */
  58. return 0;
  59. }
  60.  
  61. ///auto_part.cpp
  62. #include "cml6895_Auto_Part.h"
  63. //#include "cml6895_Inventory.h"
  64.  
  65. Auto_Part::Auto_Part()
  66. {
  67. type = "null";
  68. name = "null";
  69. part_number = 0;
  70. price = 0.0;
  71. }
  72.  
  73. std::string Auto_Part::get_type() const
  74. {
  75. return type;
  76. }
  77.  
  78. std::string Auto_Part::get_name() const
  79. {
  80. return name;
  81. }
  82.  
  83. int Auto_Part::get_part_number() const
  84. {
  85. return part_number;
  86. }
  87.  
  88. double Auto_Part::get_price() const
  89. {
  90. return price;
  91. }
  92.  
  93. void Auto_Part::set_type(std::string t)
  94. {
  95. type = t;
  96. }
  97.  
  98. void Auto_Part::set_name(std::string n)
  99. {
  100. name = n;
  101. }
  102.  
  103. void Auto_Part::set_part_number(int pn)
  104. {
  105. part_number = pn;
  106. }
  107.  
  108. void Auto_Part::set_price(double p)
  109. {
  110. price = p;
  111. }
  112.  
  113. std::ostream& operator<<(std::ostream& ost, const Auto_Part& part)
  114. {
  115. ost << "Type: " << part.type << ", Name: " << part.name << ", Part Numer: " << part.part_number << ", Price: " << std::put_money(part.price) << std::endl;
  116. return ost;
  117. }
  118.  
  119. bool Auto_Part::operator<(const Auto_Part& part_one)
  120. {
  121. if()
  122. {
  123.  
  124. }
  125. return true;
  126. }
  127.  
  128. //auto_part.h
  129. #include <string>
  130. #include <iomanip>
  131. #include <iostream>
  132.  
  133. class Auto_Part
  134. {
  135. public:
  136. Auto_Part();
  137. Auto_Part(std::string t, std::string n, int pn, double p): type(t), name(n), part_number(pn), price(p) {};
  138. std::string get_type() const;
  139. std::string get_name() const;
  140. int get_part_number() const;
  141. double get_price() const;
  142. void set_type(std::string);
  143. void set_name(std::string);
  144. void set_part_number(int);
  145. void set_price(double);
  146. friend std::ostream& operator<<(std::ostream&, const Auto_Part&);
  147. bool operator<(const Auto_Part& rhs);
  148. private:
  149. std::string type;
  150. std::string name;
  151. int part_number;
  152. double price;
  153. };
  154.  
  155. //inventory.cpp
  156. #include "cml6895_Inventory.h"
  157. //#include "cml6895_Auto_Part.h"
  158. #include <string>
  159.  
  160. Inventory::Inventory() {}
  161.  
  162. Inventory::Inventory(std::map<Auto_Part, int> inv)
  163. {
  164. inventory = inv;
  165. }
  166.  
  167. bool Inventory::add_part(Auto_Part p)
  168. {
  169. int i;
  170. std::pair<Auto_Part, int> temp1(p, 1);
  171. if(inventory.count(p) > 1)
  172. {
  173. inventory.second ++;
  174. return inventory.insert(std::pair<Auto_Part, int>(p, i++)).second;
  175.  
  176. }
  177. else
  178. {
  179. return inventory.insert(std::pair<Auto_Part, int>(p, 1)).second;
  180. }
  181. }
  182. void Inventory::add_parts(Auto_Part, int number)
  183. {
  184.  
  185. }
  186. int Inventory::get_num_parts(Auto_Part)
  187. {
  188.  
  189. }
  190. void Inventory::remove_part(Auto_Part)
  191. {
  192.  
  193. }
  194. void Inventory::remove_parts(Auto_Part, int number)
  195. {
  196.  
  197. }
  198.  
  199.  
  200. std::ostream& operator << (std::ostream& ost, const Auto_Part& part)
  201. {
  202. ost << "Type: " << part.type << ", Name: " << part.name << ", Part Numer: " << part.part_number << ", Price: " << std::put_money(part.price) << std::endl;
  203. return ost;
  204. }
  205.  
  206. //inventory.h
  207. #include "cml6895_Auto_Part.h"
  208. #include <iomanip>
  209. #include <map>
  210. //#include <typeindex>
  211.  
  212.  
  213.  
  214. class Inventory
  215. {
  216. public:
  217. Inventory();
  218. Inventory(std::map<Auto_Part, int>);
  219. bool add_part(Auto_Part);
  220. void add_parts(Auto_Part, int);
  221. int get_num_parts(Auto_Part);
  222. void remove_part(Auto_Part);
  223. void remove_parts(Auto_Part, int);
  224. friend std::ostream& operator<<(std::ostream&, const Auto_Part&);
  225. private:
  226. std::map<Auto_Part, int> inventory;
  227. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement