- Home /
I would like some help refactoring my code, I am working with switch cases concering the delivery of food items in my game
Currently, my following code checks against a list of strings that contain food names, my first case is for a Hot Dog. I would have to paste this same set of switch cases for each food type as the code is currently, and I feel like there is a better solution but cannot place my finger on it. Here is the code I currently have:
//the food submitted will be checked against the customer's desired food item(s), and their dialogue will be updated if the correct item is delivered
//note: additional pop up boxes might appear with seperate dialogue if the incorrect food is attempted to be delivered
switch (FoodName)
{
case "Hot Dog":
switch (customerRequestScript.amountToOrder)
{
case 1:
if (customerRequestScript.FoodItem1 != "Hot Dog")
{
//Customer did not ask for a hot dog
Debug.Log("I did not ask for a hot dog! case1");
break;
}
if (customerRequestScript.FoodItem1 == "Hot Dog" && customerRequestScript.FoodItem1Delivered)
{
//customer already recieved their hotdog as the 1st food item
Debug.Log("I already recieved my 1st item hotdog case1");
break;
}
if (customerRequestScript.FoodItem1 == "Hot Dog" && customerRequestScript.FoodItem1Delivered == false)
{
customerRequestScript.FoodItem1Delivered = true;
//succesfully deliver hot dog to customer, now we update this in their request box with update dialog
Debug.Log("Thank you for my 1st item, hotdog! case1");
customerRequestScript.UpdateDialog();
}
break;
//NOTE TO SELF: most of the stuff below in case 2 needs to also be added in part to case 1, to catch different scenarios etc
case 2:
if (customerRequestScript.FoodItem1 != "Hot Dog" && customerRequestScript.FoodItem2 != "Hot Dog")
{
//Customer did not ask for a hot dog
Debug.Log("I did not ask for a hot dog! case2");
break;
}
//for when the hotdog is correctly submitted
if (customerRequestScript.FoodItem1 == "Hot Dog" && customerRequestScript.FoodItem1Delivered == false)
{
//break is placed inside the if in case the customer happens to order two hot dogs, we
customerRequestScript.FoodItem1Delivered = true;
Debug.Log("Thank you for my 1st item, hotdog! case2");
//don't want one hot dog to end up counting as two
break;
}
//at this point, if the code hasn't broken out we check if the hot dog is the second food item
if (customerRequestScript.FoodItem2 == "Hot Dog" && customerRequestScript.FoodItem2Delivered == false)
{
//runs if hotdog is second item and the second item was not yet delivered to the customer
customerRequestScript.FoodItem2Delivered = true;
Debug.Log("Thank you for my 2nd item, hotdog! case2");
break;
}
//for when the customer already recieved their hotdog when it was the first food item
//Important to note that this code will never run before we check if either food item IS the correct item at the moment
if (customerRequestScript.FoodItem1 == "Hot Dog" && customerRequestScript.FoodItem1Delivered == true && customerRequestScript.FoodItem2 != "Hot Dog")
{
//customer already recieved their hotdog as the 1st food item
Debug.Log("I already recieved my 1st item hotdog case2");
break;
}
if (customerRequestScript.FoodItem2 == "Hot Dog" && customerRequestScript.FoodItem2Delivered == true && customerRequestScript.FoodItem1 != "Hot Dog")
{
//customer already recieved their hotdog as the 2nd food item
Debug.Log("I already recieved my 2nd item hotdog case2");
break;
}
if (customerRequestScript.FoodItem2 == "Hot Dog" && customerRequestScript.FoodItem2Delivered == true && customerRequestScript.FoodItem1 == "Hot Dog" && customerRequestScript.FoodItem1Delivered == true)
{
//customer already recieved their hotdog as the 2nd food item
Debug.Log("I already recieved my 1st and 2nd item hotdog case2");
break;
}
break;
}
break;
Answer by oqopo · Nov 09, 2020 at 11:56 PM
I'm something of a beginner but I would at least consider using the string as a parameter in the method, if you can. Then you only need one switch case (and that could probs be simplified a fair bit, too).
public void CheckFood(string foodName)
{
switch (customerRequestScript.amountToOrder)
{
case 1:
if (customerRequestScript.FoodItem1 != foodName)
{
Debug.Log($"I did not ask for a {foodName} case1");
break;
}
else if (customerRequestScript.FoodItem1 == foodName && customerRequestScript.FoodItem1Delivered)
{
Debug.Log($"I already recieved my 1st item {foodName} case1");
break;
}
...
Hey, my apologies for the late response but this is likely the best solution to lighten up the code that I have found in my searching (at least without heavily refactoring the workflow of my scripts). Pretty simple haha but I guess I overlooked it when initially throwing together the code, thanks!