Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitbe9dc71f94c40127657723e2881b9929c297a736 (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2021-05-14 14:21:20 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2021-05-14 14:21:20 +0300
Filesf94484f124775ad7e0bbf25da75ccf2bd84ba33b
Object-oriented_programming_FMI-be9dc71f94c40127657723e2881b9929c297a736.tar
Object-oriented_programming_FMI-be9dc71f94c40127657723e2881b9929c297a736.tar.gz
Object-oriented_programming_FMI-be9dc71f94c40127657723e2881b9929c297a736.zip
Parentf8ddb55bb214bcf39a0383f7556ff1a8a00560ac (diff)
sem 12
diff --git a/Sem. 12/task1/Bar/Bar.cpp b/Sem. 12/task1/Bar/Bar.cpp
new file mode 100644
index 0000000..e702d15
--- /dev/null
+++ b/Sem. 12/task1/Bar/Bar.cpp
@@ -0,0 +1,45 @@
+#include "Bar.h"
+
+void Bar::addDrink(const char* name, int ml)
+{
+ drinks.enqueue(Drink(name, ml));
+}
+void Bar::addAlocoholDrink(const char* name, int ml, int alcohol)
+{
+ alcoholDrinks.enqueue(AlcoholDrink(name, ml, alcohol));
+}
+
+Drink Bar::getDrink()
+{
+ Drink d = drinks.dequeue();
+ incrementStatistics(d);
+ return d;
+}
+AlcoholDrink Bar::getAcloholDrink()
+{
+ AlcoholDrink d = alcoholDrinks.dequeue();
+ incrementStatistics(d, true);
+ return d;
+}
+
+void Bar::incrementStatistics(const Drink& dr, bool isAlcoholDrink)
+{
+ stats.mlSold += dr.getMl();
+ stats.allDrinksSold++;
+
+ if (isAlcoholDrink)
+ stats.alcoholDrinksSold++;
+}
+
+size_t Bar::getMlSold()
+{
+ return stats.mlSold;
+}
+size_t Bar::getAllDrinksSold()
+{
+ return stats.allDrinksSold;
+}
+size_t Bar::getAlcoholDrinksSold()
+{
+ return stats.alcoholDrinksSold;
+} \ No newline at end of file
diff --git a/Sem. 12/task1/Bar/Bar.h b/Sem. 12/task1/Bar/Bar.h
new file mode 100644
index 0000000..8a1750a
--- /dev/null
+++ b/Sem. 12/task1/Bar/Bar.h
@@ -0,0 +1,36 @@
+#pragma once
+#include <iostream>
+#include "../Drinks/AlcoholDrink.h"
+#include "../Queue/Queue.hpp"
+
+
+
+class Bar
+{
+ struct Statistics
+ {
+ size_t mlSold = 0;
+ size_t allDrinksSold = 0;
+ size_t alcoholDrinksSold = 0;
+ };
+
+private:
+ Queue<Drink> drinks;
+ Queue<AlcoholDrink> alcoholDrinks;
+
+ Statistics stats;
+
+ void incrementStatistics(const Drink& dr, bool isAlcoholDrink = false);
+
+public:
+
+ void addDrink(const char* name, int ml);
+ void addAlocoholDrink(const char* name, int ml, int alcohol);
+
+ Drink getDrink();
+ AlcoholDrink getAcloholDrink();
+
+ size_t getMlSold();
+ size_t getAllDrinksSold();
+ size_t getAlcoholDrinksSold();
+}; \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/AlcoholDrink.cpp b/Sem. 12/task1/Drinks/AlcoholDrink.cpp
new file mode 100644
index 0000000..2f61087
--- /dev/null
+++ b/Sem. 12/task1/Drinks/AlcoholDrink.cpp
@@ -0,0 +1,23 @@
+#include "AlcoholDrink.h"
+
+AlcoholDrink::AlcoholDrink() : AlcoholDrink("Unknown", 0, 0){}
+
+AlcoholDrink::AlcoholDrink(const char* name, int ml, int alcoholPercentage) : Drink(name, ml)
+{
+ setAlcoholPercentage(alcoholPercentage);
+}
+int AlcoholDrink::getAlcoholPercentage() const
+{
+ return alcoholPercentage;
+}
+void AlcoholDrink::setAlcoholPercentage(int alcoholPercentage)
+{
+ const int MINPERCENTAGE = 1;
+ const int MAXPERCENTAGE = 97;
+
+ this->alcoholPercentage = MINPERCENTAGE;
+
+ if (MINPERCENTAGE <= alcoholPercentage && alcoholPercentage <= MAXPERCENTAGE)
+ this->alcoholPercentage = alcoholPercentage;
+
+} \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/AlcoholDrink.h b/Sem. 12/task1/Drinks/AlcoholDrink.h
new file mode 100644
index 0000000..89b2da3
--- /dev/null
+++ b/Sem. 12/task1/Drinks/AlcoholDrink.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "Drink.h"
+
+class AlcoholDrink : public Drink
+{
+ int alcoholPercentage;
+
+public:
+ AlcoholDrink();
+ AlcoholDrink(const char* name, int ml, int alcoholPercentage);
+
+ int getAlcoholPercentage() const;
+ void setAlcoholPercentage(int alcoholPercentage);
+}; \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/Drink.cpp b/Sem. 12/task1/Drinks/Drink.cpp
new file mode 100644
index 0000000..6f41b55
--- /dev/null
+++ b/Sem. 12/task1/Drinks/Drink.cpp
@@ -0,0 +1,68 @@
+#include "Drink.h"
+#include <iostream>
+
+Drink::Drink() : Drink("Unknown", 0)
+{}
+const char* Drink::getName() const
+{
+ return name;
+}
+int Drink::getMl() const
+{
+ return ml;
+}
+
+void Drink::setName(const char* name)
+{
+ delete[] this->name;
+
+ this->name = new char[strlen(name) + 1];
+ strcpy(this->name, name);
+
+}
+void Drink::setMl(int ml)
+{
+ const int MAX_ML = 1000;
+ const int MIN_ML = 100;
+
+ ml = MIN_ML;
+ if (MIN_ML <= ml && ml <= MAX_ML)
+ this->ml = ml;
+
+}
+
+Drink::Drink(const char* name, int ml) : name(nullptr)
+{
+ setName(name);
+ setMl(ml);
+}
+
+Drink::Drink(const Drink& other)
+{
+ copyFrom(other);
+}
+Drink& Drink::operator = (const Drink& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+Drink::~Drink()
+{
+ free();
+}
+void Drink::copyFrom(const Drink& other)
+{
+ name = new char[strlen(other.name) + 1];
+ strcpy(name, other.name);
+
+ ml = other.ml;
+}
+void Drink::free()
+{
+ delete[] name;
+ ml = 0;
+} \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/Drink.h b/Sem. 12/task1/Drinks/Drink.h
new file mode 100644
index 0000000..d4d37ae
--- /dev/null
+++ b/Sem. 12/task1/Drinks/Drink.h
@@ -0,0 +1,27 @@
+#pragma once
+
+class Drink
+{
+public:
+ Drink();
+
+ Drink(const char* name, int ml);
+
+ Drink(const Drink& other);
+ Drink& operator=(const Drink& other);
+ ~Drink();
+
+ const char* getName() const;
+ int getMl() const;
+
+ void setName(const char* name);
+ void setMl(int ml);
+
+private:
+ char* name;
+ int ml;
+
+ void copyFrom(const Drink& other);
+ void free();
+
+}; \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/EnergyDrink.cpp b/Sem. 12/task1/Drinks/EnergyDrink.cpp
new file mode 100644
index 0000000..a8ffac5
--- /dev/null
+++ b/Sem. 12/task1/Drinks/EnergyDrink.cpp
@@ -0,0 +1,48 @@
+#include "EnergyDrink.h"
+#include <iostream>
+
+EnergyDrink::EnergyDrink(const char* name, int ml, const char* sertificate) : Drink(name, ml), qualityCertificate(nullptr)
+{
+ setCertificate(sertificate);
+}
+const char* EnergyDrink::getCertifiate() const
+{
+ return qualityCertificate;
+}
+void EnergyDrink::setCertificate(const char* certficate)
+{
+ delete[] qualityCertificate;
+
+ qualityCertificate = new char[strlen(certficate) + 1];
+ strcpy(qualityCertificate, certficate);
+
+}
+EnergyDrink::EnergyDrink(const EnergyDrink& other) : Drink(other)
+{
+ copyFrom(other);
+}
+
+EnergyDrink& EnergyDrink::operator=(const EnergyDrink& other)
+{
+ if (this != &other)
+ {
+ Drink::operator=(other);
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+EnergyDrink::~EnergyDrink()
+{
+ free();
+}
+void EnergyDrink::copyFrom(const EnergyDrink& other)
+{
+ qualityCertificate = new char[strlen(other.qualityCertificate) + 1];
+ strcpy(qualityCertificate, other.qualityCertificate);
+}
+void EnergyDrink::free()
+{
+ delete[] qualityCertificate;
+} \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/EnergyDrink.h b/Sem. 12/task1/Drinks/EnergyDrink.h
new file mode 100644
index 0000000..99bbbdb
--- /dev/null
+++ b/Sem. 12/task1/Drinks/EnergyDrink.h
@@ -0,0 +1,22 @@
+// име, мл, сертификат за качество.
+#pragma once
+#include "Drink.h"
+
+class EnergyDrink : public Drink
+{
+private:
+ char* qualityCertificate;
+
+ void copyFrom(const EnergyDrink& other);
+ void free();
+
+public:
+ EnergyDrink(const char* name, int ml, const char* certificate);
+
+ EnergyDrink(const EnergyDrink& other);
+ EnergyDrink& operator=(const EnergyDrink& other);
+ ~EnergyDrink();
+
+ const char* getCertifiate() const;
+ void setCertificate(const char* certificate);
+}; \ No newline at end of file
diff --git a/Sem. 12/task1/Queue/Queue.hpp b/Sem. 12/task1/Queue/Queue.hpp
new file mode 100644
index 0000000..e47460a
--- /dev/null
+++ b/Sem. 12/task1/Queue/Queue.hpp
@@ -0,0 +1,145 @@
+#pragma once
+#include <iostream>
+
+template <typename T>
+class Queue
+{
+ T* data;
+ size_t capacity;
+ size_t count;
+
+ size_t head; //where to get
+ size_t tail; //last added element;
+
+ void resize(size_t newCapacity);
+
+ void copyFrom(const Queue<T>& other);
+ void free();
+public:
+ Queue();
+
+ Queue(const Queue<T>& other);
+ Queue<T>& operator=(const Queue<T>& other);
+
+ void enqueue(const T& obj);
+ T dequeue();
+
+ size_t getCount();
+ size_t isEmpty();
+
+ ~Queue();
+};
+
+template <typename T>
+Queue<T>::Queue()
+{
+ capacity = 4;
+ data = new T[capacity];
+
+ count = 0;
+ head = tail = 0;
+}
+
+
+
+template <typename T>
+void Queue<T>::enqueue(const T& obj)
+{
+ if (count == capacity)
+ resize(capacity * 2);
+ if (count != 0)
+ tail++;
+
+ tail %= capacity; //so we don't get an out of range exception
+
+ data[tail] = obj;
+
+ count++;
+}
+
+template <typename T>
+T Queue<T>::dequeue()
+{
+ if (count == 0)
+ throw std::exception("The collection is empty!");
+
+ T toReturn = data[head];
+ (++head) %= capacity;
+
+ count--;
+
+ return toReturn;
+}
+
+template <typename T>
+size_t Queue<T>::getCount()
+{
+ return count;
+}
+
+template <typename T>
+size_t Queue<T>::isEmpty()
+{
+ return getCount() == 0;
+}
+
+template <typename T>
+void Queue<T>::resize(size_t newCapacity)
+{
+ T* newData = new T[newCapacity];
+
+ for (int i = 0, ind = head; i < count; i++, (++ind) %= capacity)
+ newData[i] = data[ind];
+
+ delete[] data;
+ capacity = newCapacity;
+ data = newData;
+
+ head = 0;
+ tail = count - 1;
+}
+
+template <typename T>
+void Queue<T>::copyFrom(const Queue<T>& other)
+{
+ data = new T[other.capacity];
+ for (int i = 0; i < other.capacity; i++)
+ data[i] = other.data[i];
+
+ head = other.head;
+ tail = other.tail;
+
+ count = other.count;
+ capacity = other.capacity;
+
+}
+
+template <typename T>
+void Queue<T>::free()
+{
+ delete[] data;
+}
+
+template <typename T>
+Queue<T>::Queue(const Queue<T>& other)
+{
+ copyFrom(other);
+
+}
+
+template <typename T>
+Queue<T>& Queue<T>::operator=(const Queue<T>& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <typename T>
+Queue<T>::~Queue()
+{
+ free();
+}
diff --git a/Sem. 12/task1/main.cpp b/Sem. 12/task1/main.cpp
new file mode 100644
index 0000000..296f09f
--- /dev/null
+++ b/Sem. 12/task1/main.cpp
@@ -0,0 +1,27 @@
+#include <iostream>
+#include "Bar/Bar.h"
+#include "Drinks/EnergyDrink.h"
+
+using namespace std;
+
+int main()
+{
+ Bar b;
+
+ b.addDrink("Coke", 330);
+
+ b.addDrink("Fanta", 500);
+
+ b.addAlocoholDrink("Zagorka", 500, 5);
+
+ cout << b.getDrink().getName()<<endl;
+ cout << b.getDrink().getName() << endl;
+
+ cout << b.getAcloholDrink().getName() << endl;
+
+ cout << b.getAllDrinksSold() << endl;
+
+ EnergyDrink energyDrink("Monster", 500, "ECAS");
+
+ std::cout << energyDrink.getCertifiate();
+}