Advertisement
rozman50

Untitled

Apr 10th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. // Naloga601.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <sstream>
  7. #include <vector>
  8.  
  9. class Product {
  10. protected:
  11.     std::string name;
  12.     double price;
  13.  
  14. public:
  15.     Product(std::string name, double price) : name(name), price(price) {}
  16.     virtual std::string toString() const = 0;
  17.     virtual bool isEqual(const Product &second) {
  18.         if (this->name == second.name) return true;
  19.         return false;
  20.     }
  21.     double getPrice() {
  22.         return this->price;
  23.     }
  24. };
  25.  
  26. enum class Gender {
  27.     man = 0,
  28.     woman = 1
  29. };
  30.  
  31. class Clothing : public Product {
  32. private:
  33.     Gender gender;
  34.     int size;
  35. public:
  36.     Clothing(std::string name, double price, Gender gender, int size)
  37.         : Product(name, price), gender(gender), size(size) {}
  38.  
  39.     std::string toString() const {
  40.         std::stringstream ss;
  41.         ss << this->name << " " << this->price << " " << (this->gender == Gender::man ? "man" : "woman") << " " << this->size;
  42.         return ss.str();
  43.     }
  44.     static std::string getInternationalSize(int size) {
  45.         return "L";
  46.     }
  47.     bool isEqual(const Clothing &second) {
  48.         if (this->gender == second.gender && this->size == second.size) return true;
  49.         return false;
  50.     }
  51.     static Clothing createDemoClothing() {
  52.         return Clothing("T-shirt", 20.5, Gender::man, 42);
  53.     }
  54. };
  55.  
  56. class Item {
  57. private:
  58.     Product *prod; // AGREGACIJA
  59.     int quantity;
  60. public:
  61.     Item(Product *prod, int quantity) : prod(prod), quantity(quantity) {}
  62.     Product *getProduct() const {
  63.         return this->prod;
  64.     }
  65.     double getPrice() const {
  66.         return prod->getPrice() * this->quantity;
  67.     }
  68.     std::string toString() const {
  69.         std::stringstream ss;
  70.         ss << quantity;
  71.         return prod->toString() + " " + ss.str();
  72.     }
  73. };
  74.  
  75. class Cart {
  76. private:
  77.     std::vector<Item> items;
  78.     double discount;
  79. public:
  80.     double getDiscount() const {
  81.         return this->discount;
  82.     }
  83.     void setDiscount(double discount) {
  84.         this->discount = discount;
  85.     }
  86.     bool contains(const Product *prod) {
  87.         for (int i = 0; i < this->items.size(); i++) {
  88.             if (items[i].getProduct()->isEqual(*prod)) return true;
  89.         }
  90.         return false;
  91.     }
  92.     void addEvent(Product *prod, int quantity) {
  93.         if (!this->contains(prod)) {
  94.             items.push_back(Item(prod, quantity));
  95.         }
  96.     }
  97.  
  98.     double getTotalPrice() {
  99.         double total = 0;
  100.         for (int i = 0; i < this->items.size(); i++) {
  101.             total += items[i].getPrice() * (1 - this->discount);
  102.         }
  103.         return total;
  104.     }
  105. };
  106.  
  107.  
  108. int main()
  109. {
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement