Java problem

Associate
Joined
1 Jan 2010
Posts
1,021
Hey guys,

Programming in Java I have to design an object that you would buy from a vending machine. I've got a chocolate bar, just a standard magenta rectangle, if you invoke the method "unwrap" I made it changes colour as if to unwrap it.

Now I want to somehow have a method that when invokes it essentially "eats" the chocolate bar, erasing it off the page. BUT, I want to do this so that it is not possible to eat it if it isn't unwrapped, and somehow have a way of querying if it is edible (aka is it unwrapped) is this at all possible?
 
OOhh I like poker too!

Cant help with the Java, All I got in my head is what my old Java Proffesor used to drum in to us: ' Java Script is NOT Java! Understanable? (Before you can say no) He'd say 'Excellent'!!
 
My Java syntax is long forgotten, but you get the idea...


Code:
public StatusType StatusType
{
    get;
    set;
}

public ChocolateBar()
{
     this.StatusType = StatusType.Wrapped;
     [...]
}

public void Unwrap()
{
    this.StatusType = StatusType.Unwrapped;
    [...]
}

public void Consume()
{
     if (this.StatusType != StatusType.Unwrapped)
    {
         throw new Exception("Unwrap First!");
    }

    this.StatusType = StatusType.Consumed;
    [...]
}

private Enum StatusType
{
      0 = None,
      1 = Wrapped,
      2 = Unwrapped,
      3 = Consumed,
}
 
Last edited:
Semantically, the consumable object should not expose a Consume or Eat property (or whatever you want to call it), since it's not an action that the consumable object would perform (i.e. it wouldn't eat itself).

For example, if the object is responsible for its own consumption, how will it erase itself? An object obviously can't remove all references to itself (or at least shouldn't as this would suggest high coupling, which is bad), so the thing/environment that's doing the eating should have that responsibility.
 
Back
Top Bottom