/
MorningKoffe
/
VendingMachineLogic
Обзор
Документация
Войти
/
MorningKoffe
/
VendingMachineLogic
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
VendingMachine/VendingMachine.cpp
119 строк
3 KB
Tsidik Vitaliy
init
04 июн 2026, 18:56
04 июн 2026, 18:56
e194e16
Код
Авторство
О чём код?
#include "VendingMachine.h" #include <iostream> #include <windows.h> VendingMachine::VendingMachine(int slotsCount) : total_slots_count_(slotsCount), total_revenue_(0) { slots_ = std::move(vector<Slot*>(total_slots_count_, nullptr)); } VendingMachine::~VendingMachine() { for (auto slot : slots_) { delete slot; slot = nullptr; } } int VendingMachine::getEmptySlotsCount() const { int empty = 0; for (const auto* slot : slots_) { if (slot == nullptr || slot && slot->isEmpty()) { ++empty; } } return empty; } int VendingMachine::getTotalSlotsCount() const { return static_cast<int>(slots_.size()); } int VendingMachine::getFirstEmptySlot() const { for (auto i = 0; i < slots_.size(); ++i) { if (!slots_[i] || slots_[i]->isEmpty()) { return i; } } return -1; } bool VendingMachine::addProduct(Product* product, int productCount, int productPrice) { for (auto* slot : slots_) { if (slot && !slot->isEmpty()) { if (slot->getProduct()->get_name() == product->get_name()) { slot->addProductItems(productCount); slot->setPrice(productPrice); delete product; return true; } } } int freeSlotIndex = getFirstEmptySlot(); if (freeSlotIndex == -1) { delete product; return false; } slots_[freeSlotIndex] = new Slot(product,productCount,productPrice); return true; } bool VendingMachine::sellProduct(const string& productName) { for (auto* slot : slots_) { if (!slot) continue; auto product = slot->getProduct(); if (!product) continue; if (product->get_name() == productName) { if (slot->getOccupiedCount() > 0) { total_revenue_ += slot->getPrice(); slot->setOccupiedCount(slot->getOccupiedCount()-1); cout << "Sold: "; product->showInfo(); cout << ". Revenue: " << total_revenue_ << " rub.\n"; return true; } cout << productName + "out of stock\n"; return false; } } cout << productName << "\" not found.\n"; return false; } void VendingMachine::showContent() const { cout << "\n--- Slots ---\n"; int slotNum = 1; for (const auto* slot : slots_) { if (slot == nullptr || slot->isEmpty()) { cout << "Slot " << slotNum++ << " empty\n"; } else { cout << "Slot " << slotNum++ << " (free space: " << slot->getFreeSpace() << "): \n"; slot->showContent(); } } cout << "\n--- Common info ---\n"; cout << "Empty snack slots: " << getEmptySlotsCount() << "\n"; cout << "Total slots count: " << getTotalSlotsCount() << "\n"; cout << "Revenue: " << total_revenue_ << " rub.\n"; }