Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Naloga601.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream>
- #include <sstream>
- #include <vector>
- class Product {
- protected:
- std::string name;
- double price;
- public:
- Product(std::string name, double price) : name(name), price(price) {}
- virtual std::string toString() const = 0;
- virtual bool isEqual(const Product &second) {
- if (this->name == second.name) return true;
- return false;
- }
- double getPrice() {
- return this->price;
- }
- };
- enum class Gender {
- man = 0,
- woman = 1
- };
- class Clothing : public Product {
- private:
- Gender gender;
- int size;
- public:
- Clothing(std::string name, double price, Gender gender, int size)
- : Product(name, price), gender(gender), size(size) {}
- std::string toString() const {
- std::stringstream ss;
- ss << this->name << " " << this->price << " " << (this->gender == Gender::man ? "man" : "woman") << " " << this->size;
- return ss.str();
- }
- static std::string getInternationalSize(int size) {
- return "L";
- }
- bool isEqual(const Clothing &second) {
- if (this->gender == second.gender && this->size == second.size) return true;
- return false;
- }
- static Clothing createDemoClothing() {
- return Clothing("T-shirt", 20.5, Gender::man, 42);
- }
- };
- class Item {
- private:
- Product *prod; // AGREGACIJA
- int quantity;
- public:
- Item(Product *prod, int quantity) : prod(prod), quantity(quantity) {}
- Product *getProduct() const {
- return this->prod;
- }
- double getPrice() const {
- return prod->getPrice() * this->quantity;
- }
- std::string toString() const {
- std::stringstream ss;
- ss << quantity;
- return prod->toString() + " " + ss.str();
- }
- };
- class Cart {
- private:
- std::vector<Item> items;
- double discount;
- public:
- double getDiscount() const {
- return this->discount;
- }
- void setDiscount(double discount) {
- this->discount = discount;
- }
- bool contains(const Product *prod) {
- for (int i = 0; i < this->items.size(); i++) {
- if (items[i].getProduct()->isEqual(*prod)) return true;
- }
- return false;
- }
- void addEvent(Product *prod, int quantity) {
- if (!this->contains(prod)) {
- items.push_back(Item(prod, quantity));
- }
- }
- double getTotalPrice() {
- double total = 0;
- for (int i = 0; i < this->items.size(); i++) {
- total += items[i].getPrice() * (1 - this->discount);
- }
- return total;
- }
- };
- int main()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement