Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. //GSM.h
  2.  
  3. #pragma once
  4. class GSM
  5. {
  6. private:
  7. unsigned long SKU;
  8. char *brand;
  9. char *model;
  10. float price;
  11. unsigned int count;
  12. bool dualSIM;
  13.  
  14. public:
  15. GSM();
  16.  
  17. GSM(unsigned long SKU,
  18. const char *brand,
  19. const char *model,
  20. float price,
  21. unsigned int count,
  22. bool dualSIM);
  23.  
  24. GSM(const GSM &m);
  25.  
  26. unsigned long getSKU() const;
  27. void setSKU(unsigned long SKU);
  28.  
  29. char *getBrand() const;
  30. void setBrand(const char *brand);
  31.  
  32. char *getModel() const;
  33. void setModel(const char *model);
  34.  
  35. float getPrice() const;
  36. void setPrice(float price);
  37.  
  38. unsigned int getCount() const;
  39. void setCount(unsigned int count);
  40.  
  41. bool getDualSIM() const;
  42. void setDualSIM(bool dualSIM);
  43.  
  44. ~GSM();
  45.  
  46. };
  47. ----------------------------------------------------------------------
  48. //store.h
  49.  
  50. #pragma once
  51. #include "GSM.h"
  52. class Store
  53. {
  54. private:
  55. GSM **listGSM;
  56. size_t countGSM;
  57. size_t maxCountGSM;
  58.  
  59. size_t IncrementCountGSM();
  60.  
  61. public:
  62.  
  63. Store();
  64. size_t getCountGSM() const;
  65. size_t getMaxCountGSM() const;
  66.  
  67. void addGSM(const GSM &m);
  68. bool deleteGSM(size_t index);
  69. bool changeGSM(const GSM &m, size_t index);
  70. void displayGSM() const;
  71.  
  72. ~Store();
  73. };
  74. --------------------------------------------------------------------------------
  75. //GSM.cpp
  76.  
  77. #include "GSM.h"
  78. #include <iostream>
  79. #include <cstring>
  80.  
  81. GSM::GSM() :SKU(0), price(0), count(0), dualSIM(false), brand(NULL), model(NULL)
  82. {
  83. setBrand("");
  84. setModel("");
  85. }
  86.  
  87. GSM::GSM(unsigned long SKU,
  88. const char * brand,
  89. const char * model,
  90. float price,
  91. unsigned int count,
  92. bool dualSIM) : brand(NULL), model(NULL)
  93. {
  94. setSKU(SKU);
  95. setBrand(brand);
  96. setModel(model);
  97. setPrice(price);
  98. setCount(count);
  99. setDualSIM(dualSIM);
  100. }
  101.  
  102. GSM::GSM(const GSM & m) :
  103. GSM(m.SKU,
  104. m.brand,
  105. m.model,
  106. m.price,
  107. m.count,
  108. m.dualSIM)
  109. {
  110. }
  111.  
  112. unsigned long GSM::getSKU() const
  113. {
  114. return SKU;
  115. }
  116.  
  117. void GSM::setSKU(unsigned long SKU)
  118. {
  119. this->SKU = SKU;
  120. }
  121.  
  122. char * GSM::getBrand() const
  123. {
  124. return brand;
  125. }
  126.  
  127. void GSM::setBrand(const char * brand)
  128. {
  129. if (this->brand) delete[] this->brand;
  130. size_t size = strlen(brand) + 1;
  131. this->brand = new char[size];
  132. strcpy_s(this->brand, size, brand);
  133. }
  134.  
  135. char * GSM::getModel() const
  136. {
  137. return model;
  138. }
  139.  
  140. void GSM::setModel(const char * model)
  141. {
  142. if (this->model)delete[] this->model;
  143. size_t size = strlen(model) + 1;
  144. this->model = new char[size];
  145. strcpy_s(this->model, size, model);
  146. }
  147.  
  148. float GSM::getPrice() const
  149. {
  150. return price;
  151. }
  152.  
  153. void GSM::setPrice(float price)
  154. {
  155. this->price = price;
  156. }
  157.  
  158. unsigned int GSM::getCount() const
  159. {
  160. return count;
  161. }
  162.  
  163. void GSM::setCount(unsigned int count)
  164. {
  165. this->count = count;
  166. }
  167.  
  168. bool GSM::getDualSIM() const
  169. {
  170. return dualSIM;
  171. }
  172.  
  173. void GSM::setDualSIM(bool dualSIM)
  174. {
  175. this->dualSIM = dualSIM;
  176. }
  177.  
  178. GSM::~GSM()
  179. {
  180. delete[] brand;
  181. delete[] model;
  182. }
  183. ---------------------------------------------------------------------------
  184. //store.cpp
  185.  
  186. #include "Store.h"
  187. #include <iostream>
  188.  
  189. size_t Store::IncrementCountGSM()
  190. {
  191. countGSM++;
  192. return getCountGSM();
  193. }
  194.  
  195. Store::Store() : countGSM(0), maxCountGSM(4)
  196. {
  197. listGSM = new GSM*[maxCountGSM];
  198. }
  199.  
  200. size_t Store::getCountGSM() const
  201. {
  202. return countGSM;
  203. }
  204.  
  205. size_t Store::getMaxCountGSM() const
  206. {
  207. return maxCountGSM;
  208. }
  209.  
  210. void Store::addGSM(const GSM &m)
  211. {
  212. if (getCountGSM() >= getMaxCountGSM())
  213. {
  214. maxCountGSM *= 2;
  215. GSM** newListGSM = new GSM*[getMaxCountGSM()];
  216. for (size_t i = 0; i < getCountGSM(); i++)
  217. {
  218. newListGSM[i] = listGSM[i];
  219. }
  220. delete[] listGSM;
  221. listGSM = newListGSM;
  222. }
  223.  
  224. GSM* pGSM = new GSM(m);
  225. listGSM[getCountGSM()] = pGSM;
  226. IncrementCountGSM();
  227. }
  228.  
  229. bool Store::deleteGSM(size_t index)
  230. {
  231. if (index >= getCountGSM())return false;
  232. delete listGSM[index];
  233. countGSM--;
  234. for (size_t i = index; i < getCountGSM(); i++)
  235. {
  236. listGSM[i] = listGSM[i + 1];
  237. }
  238. return true;
  239. }
  240.  
  241. bool Store::changeGSM(const GSM &m, size_t index)
  242. {
  243. if (index >= getCountGSM())
  244. {
  245. return false;
  246. }
  247. delete listGSM[index];
  248. listGSM[index] = new GSM(m);
  249. return true;
  250. }
  251.  
  252. void Store::displayGSM() const
  253. {
  254. for (size_t i = 0; i < getCountGSM(); i++)
  255. {
  256. GSM* m = listGSM[i];
  257. std::cout << "GSM number: " << i << std::endl;
  258. std::cout << " brand: " << m->getBrand() << ' ';
  259. std::cout << "quantity: " << m->getCount() << ' ';
  260. std::cout << "dualSIM: " << m->getDualSIM() << ' ';
  261. std::cout << "model: " << m->getModel() << ' ';
  262. std::cout << "price: " << m->getPrice() << ' ';
  263. std::cout << "SKU: " << m->getSKU() << std::endl;
  264. }
  265. }
  266.  
  267. Store::~Store()
  268. {
  269. for (size_t i = 0; i < getCountGSM(); i++)
  270. {
  271. delete listGSM[i];
  272. }
  273. delete[] listGSM;
  274. }
  275.  
  276. ---------------------------------------------------------------------------
  277.  
  278. //main.cpp
  279.  
  280. #include "Store.h"
  281. #include "GSM.h"
  282. #include <iostream>
  283.  
  284. int main()
  285. {
  286. Store s;
  287. s.addGSM({ 123456, "Nokia", "3310", 50.50, 12, false });
  288. s.addGSM({ 234598, "iPhone", "8", 950.50, 50, true });
  289. s.addGSM({ 897608, "Samsung", "galaxy", 850.50, 15, false });
  290. s.addGSM({ 876343, "iPhone", "6 Plus", 430.50, 3, true });
  291. while (1)
  292. {
  293. char command;
  294. std::cin >> command;
  295. if (command == 'Q')
  296. {
  297. break;
  298. }
  299. switch (command)
  300. {
  301. case 'A':
  302. {
  303. std::cout << "You are add to change GSM. ";
  304.  
  305. unsigned long SKU;
  306. std::cout << "SKU : ";
  307. std::cin >> SKU;
  308. char brand[64];
  309. std::cout << "brand : ";
  310. std::cin >> brand;
  311. char model[64];
  312. std::cout << "model : ";
  313. std::cin >> model;
  314. float price;
  315. std::cout << "price : ";
  316. std::cin >> price;
  317. unsigned int count;
  318. std::cout << "count : ";
  319. std::cin >> count;
  320. bool dualSIM;
  321. std::cout << "dualSIM(true/false) : ";
  322. std::cin >> dualSIM;
  323.  
  324. GSM m(SKU, brand, model, price, count, dualSIM);
  325. s.addGSM(m);
  326. break;
  327. }
  328. case 'X':
  329. {
  330. std::cout << "You are about to remove GSM. ";
  331.  
  332. size_t index;
  333. std::cin >> index;
  334. s.deleteGSM(index);
  335.  
  336. break;
  337. }
  338. case 'C':
  339. {
  340. std::cout << "You are about to change GSM. ";
  341.  
  342. unsigned long SKU;
  343. std::cout << "SKU : ";
  344. std::cin >> SKU;
  345. char brand[64];
  346. std::cout << "brand : ";
  347. std::cin >> brand;
  348. char model[64];
  349. std::cout << "model : ";
  350. std::cin >> model;
  351. float price;
  352. std::cout << "price : ";
  353. std::cin >> price;
  354. unsigned int count;
  355. std::cout << "count : ";
  356. std::cin >> count;
  357. bool dualSIM;
  358. std::cout << "dualSIM(true/false) : ";
  359. std::cin >> dualSIM;
  360.  
  361. size_t index;
  362. std::cout << "index : ";
  363. std::cin >> index;
  364.  
  365. GSM m(SKU, brand, model, price, count, dualSIM);
  366. s.changeGSM(m, index);
  367. break;
  368. }
  369.  
  370. case 'D':
  371. {
  372. std::cout << "Printing all GSMs";
  373.  
  374. s.displayGSM();
  375. break;
  376. }
  377. case ' ':
  378. case '\n':
  379. break;
  380. default:
  381. std::cout << "invalid command" << std::endl;
  382. }
  383. }
  384.  
  385. int x;
  386. std::cin >> x;
  387. }
  388.  
  389. ---------------------------------------
  390. end. имаш два хедъра - GSM и store и съответно същите .cpp файла, както и един мейн. Всичко работи
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement