I have a requirement of passing 2 different datatypes, one is a Food object and the other is an integer.
class Restaurant{
void prepareOrder(/*i need the food objects and their quantity*/){
}
}
I am passing these as the Food object and its respective quantity to a Restaurant class method to prepare order. Should i create another class with these attributes or is there any other way?
CodePudding user response:
I think you're looking for something like this:
static class Ingredient {
Food food;
int amount;
}
void prepareOrder(Ingredient[] ingredients) {
...
}
But yes, this is a perfectly reasonable way.
Maybe a Collection<Ingredient>
is more convenient for you than Ingredient[]
, but that's for you to decide. In part, it depends where the ingredients lists come from: whether they're hardwired in code or not.
CodePudding user response:
If you have to pass multiple Ingredient objects to this method, you can implement the logic like this:
public Class Ingredient {
Food food;
int amount;
}
void prepareOrder(List<Ingredient> ingredList) {
...
}
That way you are passing a List of Ingredient objects.