Let\'s say I want to design a class whose clients would need to call functions in a particular sequence, e.g.,
hasNext();
next();
or, as a
There can be different approaches, one is listed here. Although this approach considers that you need to call the other function once before calling the other one but not always. You may edit it as per you need:
Create variables to check the state of function calls. Whenever someone calls listOfItems then you can set the isListed variable to true. Then check the value of isListed in mixAllItems to be sure that getListOfItems was called earlier.
class CookFood {
boolean isListed;
boolean isMixed;
boolean isHeated;
public String getListOfItems() {
// do listing and mark as listed
isListed = true;
return "something";
}
public void mixAllItems() {
// check if listed first
if (isListed) {
// do mixing
// mark as mixed
isMixed = true;
} else {
System.out.println("You need to call getListOfItems before mixing");
return;
}
}
public void heat() {
if (isMixed) {
// do heating
// mark as mixed
isHeated = true;
} else {
System.out.println("You need to call isMixed before heating");
return;
}
}
}