using System;
using System.Collections.Generic;
public class Candy
{
public string Name { get; set; }
public double Price { get; set; }
public Candy(string name, double price)
{
Name = name;
Price = price;
}
}
public class Program
{
public static void Main()
{
string candyShopName = "Sweet Tooth";
bool isOpen = true;
string[] candyNames = { "chocolate", "gummy bears", "lollipops" };
double[] candyPrices = { 2.50, 1.50, 0.50 };
List<Candy> inventory = new List<Candy>();
if (isOpen)
{
Console.WriteLine(candyShopName + " is open!");
}
else
{
Console.WriteLine(candyShopName + " is closed.");
}
for (int i = 0; i < candyNames.Length; i++)
{
Candy candy = new Candy(candyNames[i], candyPrices[i]);
inventory.Add(candy);
}
Func<List<Candy>, double> calculateTotalPrice = (candies) =>
{
double totalPrice = 0;
foreach (Candy candy in candies)
{
totalPrice += candy.Price;
}
return totalPrice;
};
Func<string, string> buyCandy = (candyName) =>
{
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[i].Name == candyName)
{
inventory.RemoveAt(i);
return "You bought a " + candyName + " for " + inventory[i].Price.ToString("0.00") + " dollars.";
}
}
throw new Exception("Sorry, we are out of " + candyName + "s.");
};
double total_price = calculateTotalPrice(inventory);
Console.WriteLine("The total price of the candy in the inventory is " + total_price.ToString("0.00") + " dollars.");
try
{
Console.WriteLine(buyCandy("gummy bears"));
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}