Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was thinking in a more OO approach, using the <a href="http://en.wikipedia.org/wiki/State_pattern"><code>State Pattern</code></a>:</p> <h2>The Machine:</h2> <pre><code>// machine.h #pragma once #include "MachineStates.h" class AbstractState; class Machine { friend class AbstractState; public: Machine(int inStockQuantity); void sell(int quantity); void refill(int quantity); int getCurrentStock(); ~Machine(); private: int mStockQuantity; AbstractState* mState; }; // machine.cpp #include "Machine.h" Machine::Machine(int inStockQuantity) : mStockQuantity(inStockQuantity), mState(new Normal()) { } Machine::~Machine() { delete mState; } void Machine::sell(int quantity) { mState-&gt;sell(*this, quantity); } void Machine::refill(int quantity) { mState-&gt;refill(*this, quantity); } int Machine::getCurrentStock() { return mStockQuantity; } </code></pre> <h2>The States:</h2> <pre><code>// MachineStates.h #pragma once #include "Machine.h" #include &lt;exception&gt; #include &lt;stdexcept&gt; class Machine; class AbstractState { public: virtual void sell(Machine&amp; machine, int quantity) = 0; virtual void refill(Machine&amp; machine, int quantity) = 0; virtual ~AbstractState(); protected: void setState(Machine&amp; machine, AbstractState* st); void updateStock(Machine&amp; machine, int quantity); }; class Normal : public AbstractState { public: virtual void sell(Machine&amp; machine, int quantity); virtual void refill(Machine&amp; machine, int quantity); virtual ~Normal(); }; class SoldOut : public AbstractState { public: virtual void sell(Machine&amp; machine, int quantity); virtual void refill(Machine&amp; machine, int quantity); virtual ~SoldOut(); }; // MachineStates.cpp #include "MachineStates.h" AbstractState::~AbstractState() { } void AbstractState::setState(Machine&amp; machine, AbstractState* state) { AbstractState* aux = machine.mState; machine.mState = state; delete aux; } void AbstractState::updateStock(Machine&amp; machine, int quantity) { machine.mStockQuantity = quantity; } Normal::~Normal() { } void Normal::sell(Machine&amp; machine, int quantity) { int currStock = machine.getCurrentStock(); if (currStock &lt; quantity) { throw std::runtime_error("Not enough stock"); } updateStock(machine, currStock - quantity); if (machine.getCurrentStock() == 0) { setState(machine, new SoldOut()); } } void Normal::refill(Machine&amp; machine, int quantity) { int currStock = machine.getCurrentStock(); updateStock(machine, currStock + quantity); } SoldOut::~SoldOut() { } void SoldOut::sell(Machine&amp; machine, int quantity) { throw std::runtime_error("Sold out!"); } void SoldOut::refill(Machine&amp; machine, int quantity) { updateStock(machine, quantity); setState(machine, new Normal()); } </code></pre> <p>I'm not used to program in C++, but this code aparently compiles against GCC 4.8.2 and valgrind shows no leaks, so I guess it's fine. I'm not computing money, but I don't need this to show you the idea.</p> <p>To test it:</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdexcept&gt; #include "Machine.h" #include "MachineStates.h" int main() { Machine m(10); m.sell(10); std::cout &lt;&lt; "Sold 10 items" &lt;&lt; std::endl; try { m.sell(1); } catch (std::exception&amp; e) { std::cerr &lt;&lt; e.what() &lt;&lt; std::endl; } m.refill(20); std::cout &lt;&lt; "Refilled 20 items" &lt;&lt; std::endl; m.sell(10); std::cout &lt;&lt; "Sold 10 items" &lt;&lt; std::endl; std::cout &lt;&lt; "Remaining " &lt;&lt; m.getCurrentStock() &lt;&lt; " items" &lt;&lt; std::endl; m.sell(5); std::cout &lt;&lt; "Sold 5 items" &lt;&lt; std::endl; std::cout &lt;&lt; "Remaining " &lt;&lt; m.getCurrentStock() &lt;&lt; " items" &lt;&lt; std::endl; try { m.sell(10); } catch (std::exception&amp; e) { std::cerr &lt;&lt; e.what() &lt;&lt; std::endl; } return 0; } </code></pre> <p>Output is:</p> <blockquote> <pre><code>Sold 10 items Sold out! Refilled 20 items Sold 10 items Remaining 10 items Sold 5 items Remaining 5 items Not enough stock </code></pre> </blockquote> <p>Now, if you want to add a <code>Broken</code> state, all you need is another <code>AbstractState</code> child. Maybe you'll need to add a <code>broken</code> property on <code>Machine</code> also.</p> <p>To add more products, you must have a map of products and its respective in-stock quantity and so on...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload