using System;
public class StringNode
{
private string info;
private StringNode next;
public StringNode(string x)
{
this.info = x;
this.next = null;
}
public StringNode(string x, StringNode next)
{
this.info = x;
this.next = next;
}
public StringNode GetNext() => this.next;
public void SetNext(StringNode next) => this.next = next;
public string GetInfo() => this.info;
public void SetInfo(string x) => this.info = x;
public override string ToString() => $"{info}";
}
public class Dish
{
private StringNode name; // Dish name as StringNode
private StringNode ingredientsHead; // Linked list for ingredients
public Dish(string dishName)
{
this.name = new StringNode(dishName);
this.ingredientsHead = null;
}
public void AddIngredient(string ingredientName)
{
StringNode newNode = new StringNode(ingredientName);
newNode.SetNext(ingredientsHead);
ingredientsHead = newNode;
}
public bool ContainsIngredient(string ingredientName)
{
StringNode current = ingredientsHead;
while (current != null)
{
if (current.GetInfo() == ingredientName)
return true; // Ingredient found
current = current.GetNext();
}
return false; // Ingredient not found
}
public StringNode GetIngredientsHead() => ingredientsHead; // Getter for
ingredients head
public override string ToString()
{
string ingredientList = "";
StringNode current = ingredientsHead;
while (current != null)
{
ingredientList += current.GetInfo() + ", ";
current = current.GetNext();
}
if (ingredientList.Length > 0)
ingredientList = ingredientList.Substring(0, ingredientList.Length -
2); // Remove trailing comma and space
return $"Dish: {name.GetInfo()}, Ingredients: {ingredientList}";
}
public string GetDishInfo() => name.GetInfo(); // Return the dish name directly
}
public class Menu
{
private StringNode restaurantName;
private StringNode dishesHead; // Head of the dish linked list
public Menu(string restaurantName)
{
this.restaurantName = new StringNode(restaurantName);
this.dishesHead = null; // Initialize the head to null
}
public void AddDish(Dish dish)
{
StringNode dishNode = new StringNode(dish.ToString()); // Create a
StringNode for the dish
dishNode.SetNext(dishesHead); // Point to the current head
dishesHead = dishNode; // Update head to the new node
}
public void RemoveDishesWithIngredient(string ingredientName)
{
StringNode current = dishesHead;
StringNode previous = null;
while (current != null)
{
// Extract the dish name from the StringNode
string dishInfo = current.GetInfo();
string dishName = dishInfo.Substring(5, dishInfo.IndexOf(",") - 5); //
Extract dish name directly
// Check if the dish contains the specified ingredient
bool containsIngredient = false;
StringNode ingredientCurrent = current.GetNext(); // Start with the
first ingredient after the dish name
while (ingredientCurrent != null)
{
// Get ingredient information
string ingredientInfo = ingredientCurrent.GetInfo();
if (ingredientInfo.Equals(ingredientName,
StringComparison.OrdinalIgnoreCase))
{
containsIngredient = true; // Ingredient found
}
ingredientCurrent = ingredientCurrent.GetNext();
}
// Remove the dish if it contains the ingredient
if (containsIngredient)
{
if (previous == null) // Removing head
{
dishesHead = current.GetNext(); // Update head to next node
}
else
{
previous.SetNext(current.GetNext()); // Bypass the current node
}
current = previous == null ? dishesHead : previous.GetNext(); //
Continue from new current
}
else
{
previous = current; // Move previous to current
current = current.GetNext(); // Move to the next dish
}
}
}
public override string ToString()
{
string menuString = $"Restaurant: {restaurantName.GetInfo()}\nMenu:\n";
StringNode current = dishesHead;
while (current != null)
{
menuString += current.ToString() + "\n"; // Build the menu string
current = current.GetNext(); // Move to the next dish
}
return menuString;
}
}
public class Program
{
public static void Main()
{
// Create a menu
Menu menu = new Menu("All Goes");
// Manually create dishes and add them to the menu
Dish dish1 = new Dish("Vegetable Salad");
dish1.AddIngredient("Tomato");
dish1.AddIngredient("Cucumber");
dish1.AddIngredient("Carrot");
menu.AddDish(dish1);
Dish dish2 = new Dish("Shakshuka");
dish2.AddIngredient("Eggs");
dish2.AddIngredient("Tomato");
dish2.AddIngredient("Pepper");
menu.AddDish(dish2);
Dish dish3 = new Dish("Sushi");
dish3.AddIngredient("Rice");
dish3.AddIngredient("Fish");
dish3.AddIngredient("Seaweed");
menu.AddDish(dish3);
Dish dish4 = new Dish("Pasta");
dish4.AddIngredient("Pasta");
dish4.AddIngredient("Tomato");
dish4.AddIngredient("Basil");
menu.AddDish(dish4);
Dish dish5 = new Dish("Vegetable Soup");
dish5.AddIngredient("Onion");
dish5.AddIngredient("Carrot");
dish5.AddIngredient("Potato");
menu.AddDish(dish5);
// Display the menu
Console.WriteLine(menu.ToString());
// Prompt for a food ingredient to remove dishes that contain it
Console.WriteLine("Enter an ingredient name to remove dishes that contain
it:");
string ingredientToRemove = Console.ReadLine();
menu.RemoveDishesWithIngredient(ingredientToRemove);
// Print the menu after removal
Console.WriteLine("Menu after changes:");
Console.WriteLine(menu.ToString());
}
}