Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.16 KB | None | 0 0
  1.  
  2. #include "gift.h"
  3. #include "Container.h"
  4.  
  5. using namespace std;
  6.  
  7. //Funktionsprototyper
  8. int menu();
  9. void addGift(Container& objekt1, Container& objekt2);
  10. void editGift(Container& objekt1, Container& objekt2);
  11. void size(Container& objekt1);
  12. void showAllBoughtGifts(Container& objekt1);
  13. void showAllSuggestedGifts(Container& objekt2);
  14. void removeGift(Container& objekt1, Container& objekt2);
  15. void showGiftsName(Container& objekt1, Container& objekt2);
  16. void showGiftsPriceIntervallBought(Container& objekt1);
  17. void showGiftsPriceIntervallSuggested(Container& objekt2);
  18.  
  19. //main-funktionen
  20. int main()
  21. {
  22. //2 Containerobjekt
  23. Container objekt1(2);
  24. Container objekt2(2);
  25.  
  26. cout << "Welcome!" << endl;
  27.  
  28. int menuChoice;
  29. menuChoice=menu();
  30. //While-loop som gàr att menyvalen kommer upp hela tiden
  31.  
  32. while (menuChoice != 0)
  33. {
  34. switch (menuChoice)
  35. {
  36.  
  37. case 1: addGift(objekt1, objekt2); break;
  38.  
  39. case 2: editGift(objekt1, objekt2); break;
  40.  
  41. case 3: size(objekt1); break;
  42.  
  43. case 4: showAllBoughtGifts(objekt1); break;
  44.  
  45. case 5: showAllSuggestedGifts(objekt2); break;
  46.  
  47. case 6: removeGift(objekt1, objekt2); break;
  48.  
  49. case 8: showGiftsName(objekt1, objekt2); break;
  50.  
  51. case 9: showGiftsPriceIntervallBought(objekt1); break;
  52.  
  53. case 10: showGiftsPriceIntervallSuggested(objekt2); break;
  54.  
  55. }
  56. menuChoice = menu();
  57. }
  58. return 0;
  59. }
  60.  
  61. //Funktionsdeklarationer
  62.  
  63. int menu()
  64. {
  65. int choice;
  66.  
  67. cout << "Add gift by entering 1. " << endl;
  68. cout << "Edit gift by entering 2. " << endl;
  69. cout << "Show size of the array by entering 3. " << endl;
  70. cout << "Show all gifts by entering 4. " << endl;
  71. cout << "Remove bought gift by entering 8. " << endl;
  72. cout << "Remove suggested gift by entering 9. " << endl;
  73. cout << "Show all bought gifts related to a name by entering 10. " << endl;
  74. cout << "Show all suggested gifts related to a name by entering 11. " << endl;
  75. cout << "Show all bought gifts by interval by entering 12. " << endl;
  76. cout << "Show all suggested gifts by interval by entering 13. " << endl;
  77.  
  78. cin >> choice;
  79. return choice;
  80.  
  81. }
  82.  
  83. //Add gift
  84. void addGift(Container& objekt1, Container& objekt2)
  85. {
  86.  
  87. string name;
  88. string gift;
  89. double price;
  90. int choice;
  91.  
  92. cout << "Add Present" << endl;
  93. cout << "Name: "; //getline(cin, name);
  94. cin >> name;
  95. cout << "Gift: "; //getline(cin, gift);
  96. cin >> gift;
  97. cout << "Price: "; //cin >> price;
  98. cin >> price;
  99.  
  100. cout << "Press 1 to add present to suggestions. " << endl;
  101. cout << "Press 2 to add present to bought presents. " << endl;
  102. cin >> choice;
  103. if(choice == 1)
  104. {
  105. objekt2.addGift(name, gift, price); //suggested
  106. }
  107. else
  108. {
  109. objekt1.addGift(name, gift, price); //bought
  110. }
  111.  
  112. }
  113.  
  114.  
  115.  
  116. //Edit gifts
  117. void editGift(Container& objekt1, Container& objekt2)
  118. {
  119. string name;
  120. string newName;
  121. string gift;
  122. string newGift;
  123. double price;
  124. double newPrice;
  125. int choice;
  126.  
  127. cout << "Edit a suggested gift by pressing 1." << endl;
  128. cout << "Edit a bought gift by pressing 2." << endl;
  129. cin >> choice;
  130.  
  131. if(choice == 1)
  132. {
  133. objekt2.showAllGifts(); //suggested
  134. cout << "Change name" << endl;
  135. cin >> name;
  136. cout << "New name" << endl;
  137. cin >> newName;
  138.  
  139. cout << "Change gift" << endl;
  140. cin >> gift;
  141. cout << "New gift" << endl;
  142. cin >> newGift;
  143.  
  144. cout << "Change price" << endl;
  145. cin >> price;
  146. cout << "New price" << endl;
  147. cin >> newPrice;
  148. objekt2.editGift(name, newName, gift, newGift, price, newPrice);
  149. }
  150. else
  151. {
  152. objekt1.showAllGifts(); //bought
  153. cout << "Change name" << endl;
  154. cin >> name;
  155. cout << "New name" << endl;
  156. cin >> newName;
  157.  
  158. cout << "Change gift" << endl;
  159. cin >> gift;
  160. cout << "New gift" << endl;
  161. cin >> newGift;
  162.  
  163. cout << "Change price" << endl;
  164. cin >> price;
  165. cout << "New price" << endl;
  166. cin >> newPrice;
  167. objekt1.editGift(name, newName, gift, newGift, price, newPrice);
  168. }
  169.  
  170. }
  171.  
  172.  
  173. //Show size of array
  174. void size(Container& objekt1)
  175. {
  176. int nmr;
  177. cout << "The size of the array is: " << endl;
  178. nmr = objekt1.size();
  179. cout << nmr << endl;
  180. }
  181.  
  182. //Show all bought gifts
  183. void showAllBoughtGifts(Container& objekt1)
  184. {
  185. cout << "Bought gifts: " << endl;
  186. objekt1.showAllGifts();
  187. }
  188.  
  189. //Show all suggested gifts
  190. void showAllSuggestedGifts(Container& objekt2)
  191. {
  192. cout << "Suggested gifts: " << endl;
  193. objekt2.showAllGifts();
  194. }
  195.  
  196.  
  197. //Remove gift
  198. void removeGift(Container& objekt1, Container& objekt2)
  199. {
  200. string name;
  201. string gift;
  202. int choice;
  203.  
  204. cout << "Remove a suggested gift by pressing 1." << endl;
  205. cout << "Remove a bought gift by pressing 2." << endl;
  206.  
  207. if(choice == 1)
  208. {
  209. objekt2.showAllGifts(); //suggested
  210. cout << "Enter persons name. " << endl;
  211. cin >> name;
  212. cout << "Enter gifts name to remove. " << endl;
  213. cin >> gift; objekt2.removeGift(name, gift);
  214. }
  215. else
  216. {
  217. objekt1.showAllGifts(); //bought
  218. cout << "Enter persons name. " << endl;
  219. cin >> name;
  220. cout << "Enter gifts name to remove. " << endl;
  221. cin >> gift;
  222. objekt1.removeGift(name, gift);
  223.  
  224. }
  225. }
  226.  
  227. //Show gifts linked to name
  228. void showGiftsName(Container& objekt1, Container& objekt2)
  229. {
  230. string name;
  231. int choice;
  232.  
  233. cout << "Remove bought gift linked to name by pressing 1." << endl;
  234. cout << "Remove suggested gift linked to name by pressing 2." << endl;
  235.  
  236. if(choice == 1)
  237. {
  238. objekt2.showAllGifts(); //suggested
  239. cout << "Enter persons name. " << endl;
  240. cin >> name;
  241. objekt2.showGiftsName(name);
  242. }
  243. else
  244. {
  245. objekt1.showGiftsName(name); //bought
  246. cout << "Enter persons name. " << endl;
  247. cin >> name;
  248. objekt1.showGiftsName(name);
  249. }
  250. }
  251.  
  252. void showGiftsPriceIntervallBought(Container& objekt1)
  253. {
  254. int max;
  255. int min;
  256. cout << "Enter max value: " << endl;
  257. cin >> max;
  258. cout << "Enter min value: " << endl;
  259. cin >> min;
  260. objekt1.showGiftsPriceIntervall(min, max);
  261. }
  262.  
  263. void showGiftsPriceIntervallSuggested(Container& objekt2)
  264. {
  265. int max;
  266. int min;
  267. cout << "Enter max value: " << endl;
  268. cin >> max;
  269. cout << "Enter min value: " << endl;
  270. cin >> min;
  271. objekt2.showGiftsPriceIntervall(min, max);
  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. GIFT.H
  300. #ifndef GIFT_H
  301. #define GIFT_H
  302.  
  303. //Includes
  304. #include <string>
  305. #include <iostream>
  306. #include <sstream>
  307.  
  308. using namespace std;
  309.  
  310. class Gift
  311.  
  312. {
  313.  
  314. public:
  315. //Medlemsfunktioner
  316. //Konstruktor skapar objekt och ger dem ett standard/defaultvärde.
  317. Gift();
  318. //Konstruktor skapar objekt utan värden, värden sätts av användaren.
  319. Gift(string name, string gift, double price);
  320. //copykonstruktor
  321. Gift(const Gift& originalObjekt);
  322. //temp konstruktor
  323. Gift(const Gift *temp);
  324.  
  325. //destruktor, förstör objekt
  326. virtual ~Gift();
  327.  
  328. //ToString funktion, gör all information till en sträng
  329. string ToString()const;
  330.  
  331. // == - operator < - less then operator
  332. bool operator>(const Gift &originalObjekt);
  333. bool operator<(const Gift &originalObjekt);
  334. bool operator=(const Gift &originalObjekt);
  335.  
  336. //getfunktioner, hämta ett specifikt värde
  337. string getname()const;
  338. string getgift()const;
  339. double getprice()const;
  340.  
  341. //setfunktioner, bestämmer värdet på en variabel
  342. void setname(string name);
  343. void setgift(string gift);
  344. void setprice(double price);
  345.  
  346. private:
  347. //Medlemsvariabler/Klassvariabler
  348. string name;
  349. string gift;
  350. double price;
  351.  
  352. };
  353. #endif
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372. GIFT
  373. #include "gift.h"
  374.  
  375. //Konstruktor skapar objekt och ger dem ett standard/defaultv‰rde.
  376. Gift::Gift()
  377. {
  378. this->name = "";
  379. this->gift = "";
  380. this->price = 0;
  381. }
  382.  
  383. //Konstruktor skapar objekt som saknar v‰rden, dessa v‰rden s‰tts ist‰llet av anv‰ndaren.
  384. Gift::Gift(string name, string gift, double price)
  385. {
  386. this->name = name;
  387. this->gift = gift;
  388. this->price = price;
  389. }
  390.  
  391. //
  392. Gift::Gift(const Gift *temp)
  393. {
  394. this->name = temp->name;
  395. this->gift = temp->gift;
  396. this->price = temp->price;
  397. }
  398.  
  399. //Destruktorn
  400. Gift::~Gift()
  401. {
  402.  
  403. }
  404.  
  405. //ToString funktionen fˆr att skriva ut objektet och dess v‰rden.
  406. string Gift::ToString()const
  407. {
  408. stringstream project;
  409. project << this->name << endl;
  410. project << this->gift << endl;
  411. project << this->price << endl;
  412. return project.str();
  413. }
  414.  
  415. //Bool operatorer, j‰mfˆr objekts v‰rden och returnerar true eller false.
  416. bool Gift::operator=(const Gift &originalObjekt)
  417. {
  418. this->name = originalObjekt.name;
  419. this->gift = originalObjekt.gift;
  420. this->price = originalObjekt.price;
  421. return this;
  422. }
  423.  
  424. //Less then operator
  425. bool Gift::operator<(const Gift &originalObjekt)
  426. {
  427. return this->price < originalObjekt.price;
  428. }
  429.  
  430. //More then operator
  431. bool Gift::operator>(const Gift &originalObjekt)
  432. {
  433. return this->price > originalObjekt.price;
  434. }
  435.  
  436. //V‰rden h‰mtas.
  437. string Gift::getname()const
  438. {
  439. return this->name;
  440. }
  441.  
  442. string Gift::getgift()const
  443. {
  444. return this->gift;
  445. }
  446.  
  447. double Gift::getprice()const
  448. {
  449. return this->price;
  450. }
  451.  
  452.  
  453. //Anv‰ndaren s‰tter v‰rden.
  454. void Gift::setname(string name)
  455. {
  456. this->name = name;
  457. }
  458.  
  459. void Gift::setgift(string gift)
  460. {
  461. this->gift = gift;
  462. }
  463.  
  464. void Gift::setprice(double price)
  465. {
  466. this->price = price;
  467. }
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488. CONTAINER.H
  489. #ifndef CONTAINER_H
  490. #define CONTAINER_H
  491.  
  492. //Includes
  493. #include "gift.h"
  494.  
  495. using namespace std;
  496.  
  497. class Container
  498.  
  499. {
  500. public:
  501. //Medlemsfunktioner
  502. //Konstruktorn skapar objekt och ger dem ett standard/defaultvärde.
  503. Container(int cap =10);
  504.  
  505. //Copykonstruktor
  506. Container(const Container& originalObjekt);
  507.  
  508. //Destruktor, förstör objekt
  509. virtual ~Container();
  510.  
  511. //Remove variables in the array
  512. void remove();
  513.  
  514. //Add gifts
  515. void addGift(string name, string gift, double price);
  516.  
  517. //View all gifts
  518. void showAllGifts()const;
  519.  
  520. //View all gifts linked to a name
  521. void showGiftsName(string name)const;
  522.  
  523. //View all gifts within a certain priceintervall
  524. void showGiftsPriceIntervall(int min, int max)const;
  525.  
  526. //Remove gifts
  527. void removeGift(string name, string gift);
  528.  
  529. //Edit gifts
  530. void editGift(string name, string newName, string gift, string newGift, double price, double newPrice);
  531.  
  532. //Show all gifts and their total prices
  533. double showAllGiftsPrices()const;
  534.  
  535. //Tilldelningsoperatorn
  536. Container operator=(const Container& original);
  537.  
  538. //Storleksfunktion
  539. int size()const;
  540.  
  541. private:
  542.  
  543. //Medlemsvariabler
  544. //Gift dubbelpekare
  545. Gift** arrayPointer;
  546.  
  547. //Capacity
  548. int cap;
  549.  
  550. //Total price
  551. int giftsTotalPrice = 0;
  552.  
  553. //Amount of gifts
  554. int nmrOfGifts;
  555.  
  556. //Expand
  557. void expand();
  558.  
  559. };
  560.  
  561. #endif
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588. CONTAINER
  589.  
  590. #include "Container.h"
  591.  
  592. //Konstruktor
  593. Container::Container(int cap)
  594. {
  595. this->cap = cap;
  596. this->nmrOfGifts = 0;
  597. this->giftsTotalPrice = 0;
  598. this->arrayPointer = new Gift*[cap];
  599.  
  600. //Loopar igenom arrayen
  601. for (int i = 0; i < this->cap; i++)
  602. {
  603. this->arrayPointer[i] = nullptr;
  604. }
  605. }
  606.  
  607. //Destruktorn
  608. Container::~Container()
  609. {
  610. this->remove();
  611. }
  612.  
  613. //Copykonstruktor
  614. Container::Container(const Container& originalObjekt)
  615. {
  616.  
  617. //skapar temp objekt
  618. Gift* temp;
  619.  
  620. //Kopierar original
  621. this->nmrOfGifts = originalObjekt.nmrOfGifts;
  622. this->cap = originalObjekt.cap;
  623. this->giftsTotalPrice = originalObjekt.giftsTotalPrice;
  624.  
  625. //Skapar nytt giftobjekt
  626. this->arrayPointer = new Gift*[this->cap];
  627.  
  628. for (int i = 0; i < this->nmrOfGifts; i++)
  629. {
  630. temp = originalObjekt.arrayPointer[i];
  631. this->arrayPointer[i] = new Gift(temp);
  632. }
  633. for (int i = this->nmrOfGifts; i < this->cap; i++)
  634. {
  635. //Tomma platser i arrayen pekas till nullptr
  636. this->arrayPointer[i] = nullptr;
  637. }
  638. }
  639.  
  640. //Remove
  641. void Container::remove()
  642. {
  643. //loopar igenom array upp till antalet presenter
  644. for (int i = 0; i < nmrOfGifts; i++)
  645. {
  646. delete this->arrayPointer[i];
  647. }
  648. delete[]arrayPointer;
  649. }
  650.  
  651. //Expanderar array och kopierar med temp
  652. void Container::expand()
  653. {
  654. //Skapar temp pekare
  655. Gift** temp;
  656. this->cap += 10;
  657. temp = new Gift*[this->cap];
  658.  
  659. for (int i = 0; i < this->nmrOfGifts; i++)
  660. {
  661. temp[i] = arrayPointer[i];
  662. }
  663.  
  664. for (int i = this->nmrOfGifts; i < cap; i++)
  665. {
  666. temp[i] = nullptr;
  667. }
  668.  
  669. //Tar bort array
  670. delete[]this->arrayPointer;
  671. this->arrayPointer = temp;
  672. }
  673.  
  674.  
  675. //addgift kollar om det finns plats, expandar och sätter ut gift i arrayen
  676. void Container::addGift(string name, string gift, double price)
  677. {
  678. if (this->nmrOfGifts == cap)
  679. {
  680. expand();
  681. }
  682. this->arrayPointer[nmrOfGifts++] = new Gift(name, gift, price);
  683. }
  684.  
  685.  
  686. //View all gifts linked to a name
  687. void Container::showAllGifts()const
  688. {
  689. for (int i = 0; i < this->nmrOfGifts; i++)
  690. {
  691. cout << this->arrayPointer[i]->ToString();
  692. }
  693. }
  694.  
  695. //View all gifts within a certain priceintervall
  696. void Container::showGiftsPriceIntervall(int min, int max)const
  697. {
  698. for (int i = 0; i < this->nmrOfGifts; i++)
  699. {
  700. if ((this->arrayPointer[i]->getprice() < max) && (this->arrayPointer[i]->getprice() > min))
  701. {
  702. cout << this->arrayPointer[i]->ToString();
  703. }
  704. }
  705. }
  706.  
  707. //Remove gifts
  708. void Container::removeGift(string name, string gift)
  709. {
  710. for (int i = 0; i < this->nmrOfGifts; i++)
  711. {
  712. if (this->arrayPointer[i]->getgift() == gift && this->arrayPointer[i]->getname() == name)
  713. {
  714. int index;
  715. index = i;
  716.  
  717. for (int i = index; i < this->nmrOfGifts; i++)
  718. {
  719. this->arrayPointer[i] = this->arrayPointer[this->nmrOfGifts];
  720. }
  721. nmrOfGifts--;
  722. }
  723. }
  724. }
  725.  
  726.  
  727.  
  728. //Edit gifts
  729. void Container::editGift(string name, string newName, string gift, string newGift, double price, double newPrice)
  730. {
  731. for (int i = 0; i < nmrOfGifts; i++)
  732. {
  733. if (name == arrayPointer[i]->getname())
  734. {
  735. arrayPointer[i]->setname(newName);
  736. }
  737.  
  738. if (arrayPointer[i]->getgift() == gift)
  739. {
  740. arrayPointer[i]->setgift(newGift);
  741. }
  742.  
  743. if (arrayPointer[i]->getprice() == price)
  744. {
  745. arrayPointer[i]->setprice(newPrice);
  746. }
  747. }
  748. }
  749.  
  750.  
  751. //Show all gifts and their total prices
  752. double Container::showAllGiftsPrices()const
  753. {
  754. double price = 0;
  755. for (int i = 0; i < this->nmrOfGifts; i++)
  756. {
  757. price += this->arrayPointer[i]->getprice();
  758. }
  759. return price;
  760. }
  761.  
  762.  
  763.  
  764. //Show all bought gifts linked to a name
  765. void Container::showGiftsName(string name)const
  766. {
  767. for (int i = 0; i < this->nmrOfGifts; i++)
  768. {
  769. cout << this->arrayPointer[i]->ToString();
  770. }
  771. }
  772.  
  773.  
  774.  
  775. //Tilldelningsoperator
  776. Container Container::operator=(const Container& originalObjekt)
  777. {
  778. if (this != &originalObjekt)
  779. {
  780. this->remove();
  781. }
  782.  
  783. Gift* temp;
  784.  
  785. this->nmrOfGifts = originalObjekt.nmrOfGifts;
  786. this->cap = originalObjekt.cap;
  787. this->giftsTotalPrice = originalObjekt.giftsTotalPrice;
  788. this->arrayPointer = new Gift*[this->cap];
  789.  
  790. for (int i = 0; i < this->nmrOfGifts; i++)
  791. {
  792. temp = originalObjekt.arrayPointer[i]; this->arrayPointer[i] = new Gift(temp);
  793. }
  794.  
  795. for (int i = this->nmrOfGifts; i < this->cap; i++)
  796. {
  797. this->arrayPointer[i] = nullptr;
  798. }
  799. return NULL;
  800. }
  801.  
  802. //Storleksfunktion
  803. int Container::size()const
  804. {
  805. return this->nmrOfGifts;
  806. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement