From 4993c6468aa07e7c6a006a721fe82aaa4413eede Mon Sep 17 00:00:00 2001 From: angeld55 Date: Tue, 17 May 2022 20:42:42 +0300 Subject: added bar task --- Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp | 19 +++ Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h | 15 ++ Sem. 11/Examples/Bar/Bar/Bar.cpp | 53 ++++++++ Sem. 11/Examples/Bar/Bar/Bar.h | 31 +++++ Sem. 11/Examples/Bar/Drink/Drink.cpp | 62 +++++++++ Sem. 11/Examples/Bar/Drink/Drink.h | 21 +++ Sem. 11/Examples/Bar/MyString/MyString.cpp | 142 +++++++++++++++++++ Sem. 11/Examples/Bar/MyString/MyString.h | 44 ++++++ Sem. 11/Examples/Bar/Pair/Pair.hpp | 43 ++++++ Sem. 11/Examples/Bar/Queue/Queue.hpp | 151 +++++++++++++++++++++ Sem. 11/Examples/Bar/Source2.cpp | 23 ++++ 11 files changed, 604 insertions(+) create mode 100644 Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp create mode 100644 Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h create mode 100644 Sem. 11/Examples/Bar/Bar/Bar.cpp create mode 100644 Sem. 11/Examples/Bar/Bar/Bar.h create mode 100644 Sem. 11/Examples/Bar/Drink/Drink.cpp create mode 100644 Sem. 11/Examples/Bar/Drink/Drink.h create mode 100644 Sem. 11/Examples/Bar/MyString/MyString.cpp create mode 100644 Sem. 11/Examples/Bar/MyString/MyString.h create mode 100644 Sem. 11/Examples/Bar/Pair/Pair.hpp create mode 100644 Sem. 11/Examples/Bar/Queue/Queue.hpp create mode 100644 Sem. 11/Examples/Bar/Source2.cpp (limited to 'Sem. 11/Examples/Bar') 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(dr, count)); +} +void Bar::addDrink(const Drink& dr, size_t count) +{ + if (count > 0) + drinks.push(Pair(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> alcoholDrinks; + MyQueue> 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 +#include + +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 +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 +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 +Pair::Pair(const T& first, const D& second) : first(first), second(second) +{} + +template +const T& Pair::getFirst() const +{ + return first; +} +template +const D& Pair::getSecond() const +{ + return second; +} + +template +void Pair::setFirst(const T& newValue) +{ + first = newValue; +} + +template +void Pair::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 +#include +template +class MyQueue +{ + T* data; + size_t capacity; + size_t size; + + size_t get; + size_t put; + + void resize(); + + void copyFrom(const MyQueue& other); + void free(); +public: + MyQueue(); + + MyQueue(const MyQueue& other); + MyQueue& operator=(const MyQueue& other); + + void push(const T& obj); + void push(T&& obj); //позволява и местене + void pop(); + + const T& peek() const; + bool isEmpty() const; + + ~MyQueue(); + +}; + +template +MyQueue::MyQueue() +{ + capacity = 4; + data = new T[capacity]; + size = 0; + get = put = 0; +} + +template +void MyQueue::push(const T& obj) +{ + if (size == capacity) + resize(); + + data[put] = obj; //operator= + (++put) %= capacity; + size++; +} + +template +void MyQueue::push(T&& obj) +{ + if (size == capacity) + resize(); + + data[put] = std::move(obj); //move operator = + (++put) %= capacity; + size++; +} + +template +bool MyQueue::isEmpty() const +{ + return size == 0; +} + + +template +const T& MyQueue::peek() const +{ + if (isEmpty()) + throw std::logic_error("Empty queue!"); + + return data[get]; +} + +template +void MyQueue::pop() +{ + if (isEmpty()) + throw std::logic_error("Empty queue!"); + (++get) %= capacity; + size--; +} + +template +void MyQueue::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 +void MyQueue::copyFrom(const MyQueue& 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 +void MyQueue::free() +{ + delete[] data; +} + +template +MyQueue::MyQueue(const MyQueue& other) +{ + copyFrom(other); + +} + +template +MyQueue& MyQueue::operator=(const MyQueue& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} + +template +MyQueue::~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 +#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 -- cgit v1.3.1-23-gd82d