Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "gift.h"
- #include "Container.h"
- using namespace std;
- //Funktionsprototyper
- int menu();
- void addGift(Container& objekt1, Container& objekt2);
- void editGift(Container& objekt1, Container& objekt2);
- void size(Container& objekt1);
- void showAllBoughtGifts(Container& objekt1);
- void showAllSuggestedGifts(Container& objekt2);
- void removeGift(Container& objekt1, Container& objekt2);
- void showGiftsName(Container& objekt1, Container& objekt2);
- void showGiftsPriceIntervallBought(Container& objekt1);
- void showGiftsPriceIntervallSuggested(Container& objekt2);
- //main-funktionen
- int main()
- {
- //2 Containerobjekt
- Container objekt1(2);
- Container objekt2(2);
- cout << "Welcome!" << endl;
- int menuChoice;
- menuChoice=menu();
- //While-loop som gàr att menyvalen kommer upp hela tiden
- while (menuChoice != 0)
- {
- switch (menuChoice)
- {
- case 1: addGift(objekt1, objekt2); break;
- case 2: editGift(objekt1, objekt2); break;
- case 3: size(objekt1); break;
- case 4: showAllBoughtGifts(objekt1); break;
- case 5: showAllSuggestedGifts(objekt2); break;
- case 6: removeGift(objekt1, objekt2); break;
- case 8: showGiftsName(objekt1, objekt2); break;
- case 9: showGiftsPriceIntervallBought(objekt1); break;
- case 10: showGiftsPriceIntervallSuggested(objekt2); break;
- }
- menuChoice = menu();
- }
- return 0;
- }
- //Funktionsdeklarationer
- int menu()
- {
- int choice;
- cout << "Add gift by entering 1. " << endl;
- cout << "Edit gift by entering 2. " << endl;
- cout << "Show size of the array by entering 3. " << endl;
- cout << "Show all gifts by entering 4. " << endl;
- cout << "Remove bought gift by entering 8. " << endl;
- cout << "Remove suggested gift by entering 9. " << endl;
- cout << "Show all bought gifts related to a name by entering 10. " << endl;
- cout << "Show all suggested gifts related to a name by entering 11. " << endl;
- cout << "Show all bought gifts by interval by entering 12. " << endl;
- cout << "Show all suggested gifts by interval by entering 13. " << endl;
- cin >> choice;
- return choice;
- }
- //Add gift
- void addGift(Container& objekt1, Container& objekt2)
- {
- string name;
- string gift;
- double price;
- int choice;
- cout << "Add Present" << endl;
- cout << "Name: "; //getline(cin, name);
- cin >> name;
- cout << "Gift: "; //getline(cin, gift);
- cin >> gift;
- cout << "Price: "; //cin >> price;
- cin >> price;
- cout << "Press 1 to add present to suggestions. " << endl;
- cout << "Press 2 to add present to bought presents. " << endl;
- cin >> choice;
- if(choice == 1)
- {
- objekt2.addGift(name, gift, price); //suggested
- }
- else
- {
- objekt1.addGift(name, gift, price); //bought
- }
- }
- //Edit gifts
- void editGift(Container& objekt1, Container& objekt2)
- {
- string name;
- string newName;
- string gift;
- string newGift;
- double price;
- double newPrice;
- int choice;
- cout << "Edit a suggested gift by pressing 1." << endl;
- cout << "Edit a bought gift by pressing 2." << endl;
- cin >> choice;
- if(choice == 1)
- {
- objekt2.showAllGifts(); //suggested
- cout << "Change name" << endl;
- cin >> name;
- cout << "New name" << endl;
- cin >> newName;
- cout << "Change gift" << endl;
- cin >> gift;
- cout << "New gift" << endl;
- cin >> newGift;
- cout << "Change price" << endl;
- cin >> price;
- cout << "New price" << endl;
- cin >> newPrice;
- objekt2.editGift(name, newName, gift, newGift, price, newPrice);
- }
- else
- {
- objekt1.showAllGifts(); //bought
- cout << "Change name" << endl;
- cin >> name;
- cout << "New name" << endl;
- cin >> newName;
- cout << "Change gift" << endl;
- cin >> gift;
- cout << "New gift" << endl;
- cin >> newGift;
- cout << "Change price" << endl;
- cin >> price;
- cout << "New price" << endl;
- cin >> newPrice;
- objekt1.editGift(name, newName, gift, newGift, price, newPrice);
- }
- }
- //Show size of array
- void size(Container& objekt1)
- {
- int nmr;
- cout << "The size of the array is: " << endl;
- nmr = objekt1.size();
- cout << nmr << endl;
- }
- //Show all bought gifts
- void showAllBoughtGifts(Container& objekt1)
- {
- cout << "Bought gifts: " << endl;
- objekt1.showAllGifts();
- }
- //Show all suggested gifts
- void showAllSuggestedGifts(Container& objekt2)
- {
- cout << "Suggested gifts: " << endl;
- objekt2.showAllGifts();
- }
- //Remove gift
- void removeGift(Container& objekt1, Container& objekt2)
- {
- string name;
- string gift;
- int choice;
- cout << "Remove a suggested gift by pressing 1." << endl;
- cout << "Remove a bought gift by pressing 2." << endl;
- if(choice == 1)
- {
- objekt2.showAllGifts(); //suggested
- cout << "Enter persons name. " << endl;
- cin >> name;
- cout << "Enter gifts name to remove. " << endl;
- cin >> gift; objekt2.removeGift(name, gift);
- }
- else
- {
- objekt1.showAllGifts(); //bought
- cout << "Enter persons name. " << endl;
- cin >> name;
- cout << "Enter gifts name to remove. " << endl;
- cin >> gift;
- objekt1.removeGift(name, gift);
- }
- }
- //Show gifts linked to name
- void showGiftsName(Container& objekt1, Container& objekt2)
- {
- string name;
- int choice;
- cout << "Remove bought gift linked to name by pressing 1." << endl;
- cout << "Remove suggested gift linked to name by pressing 2." << endl;
- if(choice == 1)
- {
- objekt2.showAllGifts(); //suggested
- cout << "Enter persons name. " << endl;
- cin >> name;
- objekt2.showGiftsName(name);
- }
- else
- {
- objekt1.showGiftsName(name); //bought
- cout << "Enter persons name. " << endl;
- cin >> name;
- objekt1.showGiftsName(name);
- }
- }
- void showGiftsPriceIntervallBought(Container& objekt1)
- {
- int max;
- int min;
- cout << "Enter max value: " << endl;
- cin >> max;
- cout << "Enter min value: " << endl;
- cin >> min;
- objekt1.showGiftsPriceIntervall(min, max);
- }
- void showGiftsPriceIntervallSuggested(Container& objekt2)
- {
- int max;
- int min;
- cout << "Enter max value: " << endl;
- cin >> max;
- cout << "Enter min value: " << endl;
- cin >> min;
- objekt2.showGiftsPriceIntervall(min, max);
- }
- GIFT.H
- #ifndef GIFT_H
- #define GIFT_H
- //Includes
- #include <string>
- #include <iostream>
- #include <sstream>
- using namespace std;
- class Gift
- {
- public:
- //Medlemsfunktioner
- //Konstruktor skapar objekt och ger dem ett standard/defaultvärde.
- Gift();
- //Konstruktor skapar objekt utan värden, värden sätts av användaren.
- Gift(string name, string gift, double price);
- //copykonstruktor
- Gift(const Gift& originalObjekt);
- //temp konstruktor
- Gift(const Gift *temp);
- //destruktor, förstör objekt
- virtual ~Gift();
- //ToString funktion, gör all information till en sträng
- string ToString()const;
- // == - operator < - less then operator
- bool operator>(const Gift &originalObjekt);
- bool operator<(const Gift &originalObjekt);
- bool operator=(const Gift &originalObjekt);
- //getfunktioner, hämta ett specifikt värde
- string getname()const;
- string getgift()const;
- double getprice()const;
- //setfunktioner, bestämmer värdet på en variabel
- void setname(string name);
- void setgift(string gift);
- void setprice(double price);
- private:
- //Medlemsvariabler/Klassvariabler
- string name;
- string gift;
- double price;
- };
- #endif
- GIFT
- #include "gift.h"
- //Konstruktor skapar objekt och ger dem ett standard/defaultv‰rde.
- Gift::Gift()
- {
- this->name = "";
- this->gift = "";
- this->price = 0;
- }
- //Konstruktor skapar objekt som saknar v‰rden, dessa v‰rden s‰tts ist‰llet av anv‰ndaren.
- Gift::Gift(string name, string gift, double price)
- {
- this->name = name;
- this->gift = gift;
- this->price = price;
- }
- //
- Gift::Gift(const Gift *temp)
- {
- this->name = temp->name;
- this->gift = temp->gift;
- this->price = temp->price;
- }
- //Destruktorn
- Gift::~Gift()
- {
- }
- //ToString funktionen fˆr att skriva ut objektet och dess v‰rden.
- string Gift::ToString()const
- {
- stringstream project;
- project << this->name << endl;
- project << this->gift << endl;
- project << this->price << endl;
- return project.str();
- }
- //Bool operatorer, j‰mfˆr objekts v‰rden och returnerar true eller false.
- bool Gift::operator=(const Gift &originalObjekt)
- {
- this->name = originalObjekt.name;
- this->gift = originalObjekt.gift;
- this->price = originalObjekt.price;
- return this;
- }
- //Less then operator
- bool Gift::operator<(const Gift &originalObjekt)
- {
- return this->price < originalObjekt.price;
- }
- //More then operator
- bool Gift::operator>(const Gift &originalObjekt)
- {
- return this->price > originalObjekt.price;
- }
- //V‰rden h‰mtas.
- string Gift::getname()const
- {
- return this->name;
- }
- string Gift::getgift()const
- {
- return this->gift;
- }
- double Gift::getprice()const
- {
- return this->price;
- }
- //Anv‰ndaren s‰tter v‰rden.
- void Gift::setname(string name)
- {
- this->name = name;
- }
- void Gift::setgift(string gift)
- {
- this->gift = gift;
- }
- void Gift::setprice(double price)
- {
- this->price = price;
- }
- CONTAINER.H
- #ifndef CONTAINER_H
- #define CONTAINER_H
- //Includes
- #include "gift.h"
- using namespace std;
- class Container
- {
- public:
- //Medlemsfunktioner
- //Konstruktorn skapar objekt och ger dem ett standard/defaultvärde.
- Container(int cap =10);
- //Copykonstruktor
- Container(const Container& originalObjekt);
- //Destruktor, förstör objekt
- virtual ~Container();
- //Remove variables in the array
- void remove();
- //Add gifts
- void addGift(string name, string gift, double price);
- //View all gifts
- void showAllGifts()const;
- //View all gifts linked to a name
- void showGiftsName(string name)const;
- //View all gifts within a certain priceintervall
- void showGiftsPriceIntervall(int min, int max)const;
- //Remove gifts
- void removeGift(string name, string gift);
- //Edit gifts
- void editGift(string name, string newName, string gift, string newGift, double price, double newPrice);
- //Show all gifts and their total prices
- double showAllGiftsPrices()const;
- //Tilldelningsoperatorn
- Container operator=(const Container& original);
- //Storleksfunktion
- int size()const;
- private:
- //Medlemsvariabler
- //Gift dubbelpekare
- Gift** arrayPointer;
- //Capacity
- int cap;
- //Total price
- int giftsTotalPrice = 0;
- //Amount of gifts
- int nmrOfGifts;
- //Expand
- void expand();
- };
- #endif
- CONTAINER
- #include "Container.h"
- //Konstruktor
- Container::Container(int cap)
- {
- this->cap = cap;
- this->nmrOfGifts = 0;
- this->giftsTotalPrice = 0;
- this->arrayPointer = new Gift*[cap];
- //Loopar igenom arrayen
- for (int i = 0; i < this->cap; i++)
- {
- this->arrayPointer[i] = nullptr;
- }
- }
- //Destruktorn
- Container::~Container()
- {
- this->remove();
- }
- //Copykonstruktor
- Container::Container(const Container& originalObjekt)
- {
- //skapar temp objekt
- Gift* temp;
- //Kopierar original
- this->nmrOfGifts = originalObjekt.nmrOfGifts;
- this->cap = originalObjekt.cap;
- this->giftsTotalPrice = originalObjekt.giftsTotalPrice;
- //Skapar nytt giftobjekt
- this->arrayPointer = new Gift*[this->cap];
- for (int i = 0; i < this->nmrOfGifts; i++)
- {
- temp = originalObjekt.arrayPointer[i];
- this->arrayPointer[i] = new Gift(temp);
- }
- for (int i = this->nmrOfGifts; i < this->cap; i++)
- {
- //Tomma platser i arrayen pekas till nullptr
- this->arrayPointer[i] = nullptr;
- }
- }
- //Remove
- void Container::remove()
- {
- //loopar igenom array upp till antalet presenter
- for (int i = 0; i < nmrOfGifts; i++)
- {
- delete this->arrayPointer[i];
- }
- delete[]arrayPointer;
- }
- //Expanderar array och kopierar med temp
- void Container::expand()
- {
- //Skapar temp pekare
- Gift** temp;
- this->cap += 10;
- temp = new Gift*[this->cap];
- for (int i = 0; i < this->nmrOfGifts; i++)
- {
- temp[i] = arrayPointer[i];
- }
- for (int i = this->nmrOfGifts; i < cap; i++)
- {
- temp[i] = nullptr;
- }
- //Tar bort array
- delete[]this->arrayPointer;
- this->arrayPointer = temp;
- }
- //addgift kollar om det finns plats, expandar och sätter ut gift i arrayen
- void Container::addGift(string name, string gift, double price)
- {
- if (this->nmrOfGifts == cap)
- {
- expand();
- }
- this->arrayPointer[nmrOfGifts++] = new Gift(name, gift, price);
- }
- //View all gifts linked to a name
- void Container::showAllGifts()const
- {
- for (int i = 0; i < this->nmrOfGifts; i++)
- {
- cout << this->arrayPointer[i]->ToString();
- }
- }
- //View all gifts within a certain priceintervall
- void Container::showGiftsPriceIntervall(int min, int max)const
- {
- for (int i = 0; i < this->nmrOfGifts; i++)
- {
- if ((this->arrayPointer[i]->getprice() < max) && (this->arrayPointer[i]->getprice() > min))
- {
- cout << this->arrayPointer[i]->ToString();
- }
- }
- }
- //Remove gifts
- void Container::removeGift(string name, string gift)
- {
- for (int i = 0; i < this->nmrOfGifts; i++)
- {
- if (this->arrayPointer[i]->getgift() == gift && this->arrayPointer[i]->getname() == name)
- {
- int index;
- index = i;
- for (int i = index; i < this->nmrOfGifts; i++)
- {
- this->arrayPointer[i] = this->arrayPointer[this->nmrOfGifts];
- }
- nmrOfGifts--;
- }
- }
- }
- //Edit gifts
- void Container::editGift(string name, string newName, string gift, string newGift, double price, double newPrice)
- {
- for (int i = 0; i < nmrOfGifts; i++)
- {
- if (name == arrayPointer[i]->getname())
- {
- arrayPointer[i]->setname(newName);
- }
- if (arrayPointer[i]->getgift() == gift)
- {
- arrayPointer[i]->setgift(newGift);
- }
- if (arrayPointer[i]->getprice() == price)
- {
- arrayPointer[i]->setprice(newPrice);
- }
- }
- }
- //Show all gifts and their total prices
- double Container::showAllGiftsPrices()const
- {
- double price = 0;
- for (int i = 0; i < this->nmrOfGifts; i++)
- {
- price += this->arrayPointer[i]->getprice();
- }
- return price;
- }
- //Show all bought gifts linked to a name
- void Container::showGiftsName(string name)const
- {
- for (int i = 0; i < this->nmrOfGifts; i++)
- {
- cout << this->arrayPointer[i]->ToString();
- }
- }
- //Tilldelningsoperator
- Container Container::operator=(const Container& originalObjekt)
- {
- if (this != &originalObjekt)
- {
- this->remove();
- }
- Gift* temp;
- this->nmrOfGifts = originalObjekt.nmrOfGifts;
- this->cap = originalObjekt.cap;
- this->giftsTotalPrice = originalObjekt.giftsTotalPrice;
- this->arrayPointer = new Gift*[this->cap];
- for (int i = 0; i < this->nmrOfGifts; i++)
- {
- temp = originalObjekt.arrayPointer[i]; this->arrayPointer[i] = new Gift(temp);
- }
- for (int i = this->nmrOfGifts; i < this->cap; i++)
- {
- this->arrayPointer[i] = nullptr;
- }
- return NULL;
- }
- //Storleksfunktion
- int Container::size()const
- {
- return this->nmrOfGifts;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement