I have to write a java program for a vending machine which:

  1. Accepts coins of 1,5,10,25 Cents i.e. penny, nickel, dime, and quarter.
  2. Allow user to select products Coke(25), Pepsi(35), Soda(45)
  3. Allow user to take refund by canceling the request.
  4. Return selected product and remaining change if any
  5. Allow reset operation for vending machine supplier.

I searched different design patterns and i saw that the best design pattern to write this kind of programs is the State Pattern. So i want your help to find the states because this is the first time that im going to use this pattern. I think that the states must be the following:

  1. Cancel Request(refund)
  2. Return product and Remaining change
  3. Reset Operation

Are those all the states that i must use or i must add something else?

  • As far as I see it, your only state is "Accepting Coins" or "Active" (while keeping track of current deposit) everything else is an edge (operation) back to that state. Eg. Starting from "Accepting Coins" you put in a dime, then it's still accepting coins, put in a quarter, still accepting coins, select Coke, it spits out a coke and your change, then it's still accepting coins... What you list as "Refund", "Return Product", "Reset Operation" are not states, they are transitional operations. – xtratic Jun 29 at 13:57
  • 1
    Does this answer your question? Use Case Diagram for Vending machine – xtratic Jun 29 at 14:01

A state is defined very broadly in UML as:

A State models a situation in the execution of a StateMachine Behavior during which some invariant condition holds.

To make it more practical, imagine that a state corresponds to the machine waiting for some things to happen, and when they happen the machine may change its state.

Here there are clearly several different states belonging to your state machine:

  • There are states about what to do with coins: it starts with Accepting coins until it will Not accepting coins anymore, at least temporarily.
  • There are states about money: At the beginning, there's No credit. At every insertion of a coin, the credit increases. When the first coin is inserted, the machine reaches a state Credit available.
  • There are states about product selection: as soon as there is credit, we have Cancelation selectable. Depending on the credit available, different products may become selectable: Coke selectable, Coke and Pepsi selectable, Coke and Pepsi and Soda selectable. Then it's about Product selected.
  • Then there are states about the outcome: Product delivered which would automatically decrease the money available. Cash returned which means that we start a new.

But I'll leave you some (home)work: you now need to see which states are mutually exclusive, and which can happen together and merge those in a composite state. You may then add the state transitions. Some transitions are automatic (e.g. product delivered-> cash returned) . Some transition are event driven (e.g. coin inserted).

Now it's up to you to assemble the pieces of the puzzle. Good luck

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.