Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 11 / Examples / Bar
diff options
context:
space:
mode:
Commit4993c6468aa07e7c6a006a721fe82aaa4413eede (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-05-17 20:42:42 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-05-17 20:42:42 +0300
Files819dc184dc1c4d23ff71bdd5d9d538b8ce32a5de /Sem. 11/Examples/Bar
Object-oriented_programming_FMI-4993c6468aa07e7c6a006a721fe82aaa4413eede.tar
Object-oriented_programming_FMI-4993c6468aa07e7c6a006a721fe82aaa4413eede.tar.gz
Object-oriented_programming_FMI-4993c6468aa07e7c6a006a721fe82aaa4413eede.zip
Parent8af74b1eeed52119ecaaf57ff6779edbc219c34b (diff)
added bar task
diff --git a/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp
new file mode 100644
index 0000000..ddd890e
--- /dev/null
+++ b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp
@@ -0,0 +1,19 @@
+#pragma once
+#include "AlcoholDrink.h"
+
+AlcoholDrink::AlcoholDrink():alcP(5) {}
+
+AlcoholDrink::AlcoholDrink(const MyString& name, int ml, int alcP) :Drink(name, ml)
+{
+ setAlcP(alcP);
+}
+void AlcoholDrink::setAlcP(int alcP)
+{
+ if (alcP < 5 || alcP > 98)
+ alcP = 50;
+ this->alcP = alcP;
+}
+int AlcoholDrink::getAlcP() const
+{
+ return alcP;
+} \ No newline at end of file
diff --git a/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h
new file mode 100644
index 0000000..3029580
--- /dev/null
+++ b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h
@@ -0,0 +1,15 @@
+#pragma once
+#include "../Drink/Drink.h"
+#include "../Queue/Queue.hpp"
+#include "../Pair/Pair.hpp"
+
+class AlcoholDrink : public Drink
+{
+ int alcP;
+public:
+ AlcoholDrink();
+ AlcoholDrink(const MyString& name, int ml, int alcP);
+
+ void setAlcP(int alcP);
+ int getAlcP() const;
+}; \ No newline at end of file
diff --git a/Sem. 11/Examples/Bar/Bar/Bar.cpp b/Sem. 11/Examples/Bar/Bar/Bar.cpp
new file mode 100644
index 0000000..2d20195
--- /dev/null
+++ b/Sem. 11/Examples/Bar/Bar/Bar.cpp
@@ -0,0 +1,53 @@
+#include "Bar.h"
+
+void Bar::addAlcoholDrink(const AlcoholDrink& dr, size_t count)
+{
+ if (count > 0)
+ alcoholDrinks.push(Pair<AlcoholDrink, size_t>(dr, count));
+}
+void Bar::addDrink(const Drink& dr, size_t count)
+{
+ if (count > 0)
+ drinks.push(Pair<Drink, size_t >(dr, count));
+}
+
+Drink Bar::getDrink()
+{
+
+ if (drinks.isEmpty())
+ throw std::logic_error("Empty drinks collection");
+
+ Drink toReturn = drinks.peek().getFirst();
+
+ stats.mlSold += toReturn.getMl();
+ stats.soldDrinks++;
+
+ drinks.peek().setSecond(drinks.peek().getSecond() - 1);
+ if (drinks.peek().getSecond() == 0)
+ drinks.pop();
+
+ return toReturn;
+}
+
+AlcoholDrink Bar::getAlcDrink()
+{
+ if (alcoholDrinks.isEmpty())
+ throw std::logic_error("Empty alc. drinks collection");
+
+ AlcoholDrink toReturn = alcoholDrinks.peek().getFirst();
+
+ stats.mlSold += toReturn.getMl();
+ stats.soldAlcoholDrinks++;
+
+ alcoholDrinks.peek().setSecond(alcoholDrinks.peek().getSecond() - 1);
+ if (alcoholDrinks.peek().getSecond() == 0)
+ alcoholDrinks.pop();
+
+ return toReturn;
+
+}
+
+const Bar::Statistics& Bar::getStats() const
+{
+ return stats;
+} \ No newline at end of file
diff --git a/Sem. 11/Examples/Bar/Bar/Bar.h b/Sem. 11/Examples/Bar/Bar/Bar.h
new file mode 100644
index 0000000..4bf5513
--- /dev/null
+++ b/Sem. 11/Examples/Bar/Bar/Bar.h
@@ -0,0 +1,31 @@
+#pragma once
+#include "../Queue/Queue.hpp"
+#include "../Pair/Pair.hpp"
+#include "../AlcoholDrink/AlcoholDrink.h"
+
+class Bar
+{
+ MyQueue<Pair<AlcoholDrink, size_t>> alcoholDrinks;
+ MyQueue<Pair<Drink, size_t>> drinks;
+
+public:
+ struct Statistics
+ {
+ size_t soldDrinks;
+ size_t soldAlcoholDrinks;
+ size_t mlSold;
+ } stats;
+
+
+ Bar() = default;
+
+ void addAlcoholDrink(const AlcoholDrink& dr, size_t count = 1);
+ void addDrink(const Drink& dr, size_t count = 1);
+
+
+ Drink getDrink();
+ AlcoholDrink getAlcDrink();
+
+ const Statistics& getStats() const;
+
+};
diff --git a/Sem. 11/Examples/Bar/Drink/Drink.cpp b/Sem. 11/Examples/Bar/Drink/Drink.cpp
new file mode 100644
index 0000000..4bb921f
--- /dev/null
+++ b/Sem. 11/Examples/Bar/Drink/Drink.cpp
@@ -0,0 +1,62 @@
+#include "Drink.h"
+
+Drink::Drink() : Drink("Unknown", 0)
+{}
+
+const MyString& Drink::getName() const
+{
+ return name;
+}
+int Drink::getMl() const
+{
+ return ml;
+}
+
+bool isLower(char ch)
+{
+ return ch >= 'a' && ch <= 'z';
+}
+
+bool areAllLowerCase(const char* str)
+{
+ while (*str)
+ {
+ if (!isLower(*str))
+ return false;
+ str++;
+ }
+ return true;
+}
+
+bool isUpper(char ch)
+{
+ return ch >= 'A' && ch <= 'Z';
+}
+
+bool isNameValid(const char* name)
+{
+ return isUpper(*name) && areAllLowerCase(name + 1);
+}
+
+void Drink::setName(const MyString& name)
+{
+ if (isNameValid(name.c_str()))
+ 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 MyString& name, int ml)
+{
+ setName(name);
+ setMl(ml);
+}
+
diff --git a/Sem. 11/Examples/Bar/Drink/Drink.h b/Sem. 11/Examples/Bar/Drink/Drink.h
new file mode 100644
index 0000000..d48a725
--- /dev/null
+++ b/Sem. 11/Examples/Bar/Drink/Drink.h
@@ -0,0 +1,21 @@
+#pragma once
+#include "../MyString/MyString.h"
+
+class Drink
+{
+public:
+
+ Drink();
+ Drink(const MyString& name, int ml);
+
+ const MyString& getName() const;
+ int getMl() const;
+
+ void setName(const MyString& name);
+ void setMl(int ml);
+
+private:
+ MyString name;
+ int ml;
+
+}; \ No newline at end of file
diff --git a/Sem. 11/Examples/Bar/MyString/MyString.cpp b/Sem. 11/Examples/Bar/MyString/MyString.cpp
new file mode 100644
index 0000000..3e60278
--- /dev/null
+++ b/Sem. 11/Examples/Bar/MyString/MyString.cpp
@@ -0,0 +1,142 @@
+#include "MyString.h"
+#include <cstring>
+#include <iostream>
+
+void MyString::copyFrom(const MyString& other)
+{
+ str = new char[strlen(other.str) + 1];
+ strcpy(str, other.str);
+ size = other.size;
+}
+void MyString::free()
+{
+ delete[] str;
+}
+
+MyString::MyString()
+{
+ str = new char[1];
+ str[0] = '\0';
+ size = 0;
+}
+
+MyString::MyString(const char* data)
+{
+ if(data == nullptr)
+ {
+ str = new char[1];
+ str[0] = '\0';
+ size = 0;
+ }
+ else
+ {
+ size = strlen(data);
+ str = new char[size + 1];
+ strcpy(str, data);
+ }
+}
+
+MyString::MyString(const MyString& other)
+{
+ copyFrom(other);
+}
+MyString& MyString::operator=(const MyString& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+MyString::~MyString()
+{
+ free();
+}
+
+
+size_t MyString::getSize() const
+{
+ return size;
+}
+void MyString::concat(const MyString& other)
+{
+ char* temp = new char[getSize() + other.getSize() + 1];
+ strcpy(temp, str);
+ strcat(temp, other.str);
+
+ delete[] str;
+ str = temp;
+ size = size + other.getSize();
+}
+
+const char* MyString::c_str() const
+{
+ return str;
+}
+
+MyString& MyString::operator+=(const MyString& other)
+{
+ concat(other);
+ return *this;
+}
+
+MyString operator+(const MyString& lhs, const MyString& rhs)
+{
+ MyString copyOfLeft(lhs);
+ copyOfLeft += rhs;
+ return copyOfLeft;
+}
+
+std::ostream& operator<<(std::ostream& stream, const MyString& str)
+{
+ stream << str.str;
+ return stream;
+}
+
+std::istream& operator>>(std::istream& stream, MyString& str)
+{
+ delete[] str.str;
+ char buff[1024];
+ stream >> buff;
+
+ str.size = strlen(buff);
+ str.str = new char[str.size + 1];
+ strcpy(str.str, buff);
+
+ return stream;
+}
+
+bool operator==(const MyString& lhs, const MyString& rhs)
+{
+ return strcmp(lhs.c_str(), rhs.c_str()) == 0;
+}
+bool operator<=(const MyString& lhs, const MyString& rhs)
+{
+ return strcmp(lhs.c_str(), rhs.c_str()) <= 0;
+
+}
+bool operator<(const MyString& lhs, const MyString& rhs)
+{
+ return strcmp(lhs.c_str(), rhs.c_str()) < 0;
+
+}
+
+MyString::MyString(MyString&& otherString)
+{
+ str = otherString.str;
+ size = otherString.size;
+ otherString.str = nullptr;
+}
+
+MyString& MyString::operator=(MyString&& otherString)
+{
+ if (this != &otherString)
+ {
+ free();
+ str = otherString.str;
+ otherString.str = nullptr;
+ size = otherString.size;
+ }
+ return *this;
+}
diff --git a/Sem. 11/Examples/Bar/MyString/MyString.h b/Sem. 11/Examples/Bar/MyString/MyString.h
new file mode 100644
index 0000000..b37733a
--- /dev/null
+++ b/Sem. 11/Examples/Bar/MyString/MyString.h
@@ -0,0 +1,44 @@
+#pragma once
+
+#include <fstream>
+class MyString
+{
+ char* str;
+ size_t size;
+
+ void copyFrom(const MyString& other);
+ void free();
+
+public:
+ MyString();
+ MyString(const char*);
+
+ MyString(const MyString&);
+
+ MyString(MyString&&); //move constructor that accepts rvalue;
+
+ MyString& operator=(const MyString& other);
+ MyString& operator=(MyString&& other); //move op= that accepts rvalue;
+
+ ~MyString();
+
+ size_t getSize() const;
+ void concat(const MyString& other);
+
+ const char* c_str() const;
+
+ MyString& operator+=(const MyString& other);
+
+ friend std::ostream& operator<<(std::ostream& stream, const MyString& str);
+ friend std::istream& operator>>(std::istream& stream, MyString& str);
+
+};
+
+MyString operator+(const MyString& lhs, const MyString& rhs);
+
+bool operator==(const MyString& lhs, const MyString& rhs);
+bool operator<=(const MyString& lhs, const MyString& rhs);
+bool operator<(const MyString& lhs, const MyString& rhs);
+
+
+//MyString name = "sdfsdf" \ No newline at end of file
diff --git a/Sem. 11/Examples/Bar/Pair/Pair.hpp b/Sem. 11/Examples/Bar/Pair/Pair.hpp
new file mode 100644
index 0000000..5f83d0c
--- /dev/null
+++ b/Sem. 11/Examples/Bar/Pair/Pair.hpp
@@ -0,0 +1,43 @@
+#pragma once
+
+template<typename T, typename D>
+class Pair
+{
+ T first;
+ mutable D second; // For Drinks task!!
+public:
+ Pair() = default;
+ Pair(const T& first, const D& second);
+ const T& getFirst() const;
+ const D& getSecond() const;
+
+ void setFirst(const T& newValue);
+ void setSecond(const D& newValue) const ;
+};
+
+template<typename T, typename D>
+Pair<T,D>::Pair(const T& first, const D& second) : first(first), second(second)
+{}
+
+template<typename T, typename D>
+const T& Pair<T, D>::getFirst() const
+{
+ return first;
+}
+template<typename T, typename D>
+const D& Pair<T, D>::getSecond() const
+{
+ return second;
+}
+
+template<typename T, typename D>
+void Pair<T, D>::setFirst(const T& newValue)
+{
+ first = newValue;
+}
+
+template<typename T, typename D>
+void Pair<T, D>::setSecond(const D& newValue) const
+{
+ second = newValue;
+} \ No newline at end of file
diff --git a/Sem. 11/Examples/Bar/Queue/Queue.hpp b/Sem. 11/Examples/Bar/Queue/Queue.hpp
new file mode 100644
index 0000000..5c9af06
--- /dev/null
+++ b/Sem. 11/Examples/Bar/Queue/Queue.hpp
@@ -0,0 +1,151 @@
+#pragma once
+#include <iostream>
+#include <exception>
+template <typename T>
+class MyQueue
+{
+ T* data;
+ size_t capacity;
+ size_t size;
+
+ size_t get;
+ size_t put;
+
+ void resize();
+
+ void copyFrom(const MyQueue<T>& other);
+ void free();
+public:
+ MyQueue();
+
+ MyQueue(const MyQueue<T>& other);
+ MyQueue<T>& operator=(const MyQueue<T>& other);
+
+ void push(const T& obj);
+ void push(T&& obj); //позволява и местене
+ void pop();
+
+ const T& peek() const;
+ bool isEmpty() const;
+
+ ~MyQueue();
+
+};
+
+template <typename T>
+MyQueue<T>::MyQueue()
+{
+ capacity = 4;
+ data = new T[capacity];
+ size = 0;
+ get = put = 0;
+}
+
+template <typename T>
+void MyQueue<T>::push(const T& obj)
+{
+ if (size == capacity)
+ resize();
+
+ data[put] = obj; //operator=
+ (++put) %= capacity;
+ size++;
+}
+
+template <typename T>
+void MyQueue<T>::push(T&& obj)
+{
+ if (size == capacity)
+ resize();
+
+ data[put] = std::move(obj); //move operator =
+ (++put) %= capacity;
+ size++;
+}
+
+template <typename T>
+bool MyQueue<T>::isEmpty() const
+{
+ return size == 0;
+}
+
+
+template <typename T>
+const T& MyQueue<T>::peek() const
+{
+ if (isEmpty())
+ throw std::logic_error("Empty queue!");
+
+ return data[get];
+}
+
+template <typename T>
+void MyQueue<T>::pop()
+{
+ if (isEmpty())
+ throw std::logic_error("Empty queue!");
+ (++get) %= capacity;
+ size--;
+}
+
+template <typename T>
+void MyQueue<T>::resize()
+{
+ T* resizedData = new T[capacity * 2];
+ for (size_t i = 0; i < size; i++)
+ {
+ resizedData[i] = data[get];
+ (++get) %= capacity;
+ }
+ capacity *= 2;
+ delete[] data;
+ data = resizedData;
+ get = 0;
+ put = size;
+}
+
+
+template <typename T>
+void MyQueue<T>::copyFrom(const MyQueue<T>& other)
+{
+ data = new T[other.capacity];
+ for (int i = 0; i < other.capacity; i++)
+ data[i] = other.data[i];
+
+ get = other.get;
+ put = other.put;
+
+ size = other.size;
+ capacity = other.capacity;
+
+}
+
+template <typename T>
+void MyQueue<T>::free()
+{
+ delete[] data;
+}
+
+template <typename T>
+MyQueue<T>::MyQueue(const MyQueue<T>& other)
+{
+ copyFrom(other);
+
+}
+
+template <typename T>
+MyQueue<T>& MyQueue<T>::operator=(const MyQueue<T>& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <typename T>
+MyQueue<T>::~MyQueue()
+{
+ free();
+}
diff --git a/Sem. 11/Examples/Bar/Source2.cpp b/Sem. 11/Examples/Bar/Source2.cpp
new file mode 100644
index 0000000..e27b025
--- /dev/null
+++ b/Sem. 11/Examples/Bar/Source2.cpp
@@ -0,0 +1,23 @@
+#include <iostream>
+#include "Bar/Bar.h"
+
+int main()
+{
+ Bar b;
+
+ b.addDrink(Drink("Coke", 330), 2); //2x Coca cola 330 ml
+
+ b.addDrink(Drink("Fanta", 500)); //1x Fanta 500 ml
+
+ b.addAlcoholDrink(AlcoholDrink("Zagorka", 500, 5)); //1x Zagorka, 5% alc, 500 ml
+
+ std::cout << b.getAlcDrink().getName() << std::endl; // Zagorka
+
+ std::cout << b.getDrink().getName() << std::endl; // Coke
+ std::cout << b.getDrink().getName() << std::endl; // Coke
+ std::cout << b.getDrink().getName() << std::endl; // Fanta
+
+ std::cout<< b.getStats().soldAlcoholDrinks << std::endl; // 1
+ std::cout << b.getStats().soldDrinks << std::endl; // 3
+
+} \ No newline at end of file