data, types, objects, and references - Managing your app's data - The C# code for the main page
Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 4(data, types, objects, and references - Managing your app’s data)、p.253(Exercise)の解答を求めてみる。
コード
MenuItem.cs
namespace SloppyJoe;
public class MenuItem
{
public string[] Proteins = [
"Roast beef", "Salami", "Turkey",
"Ham", "Pastrami", "Tofu"];
public string[] Condiments = [
"yellow mustard", "brown mustard",
"honey mustard", "mayo", "relish", "French dressing"
];
public string[] Breads = ["rye", "white", "wheat", "pumpernickel", "a roll"];
public string Description = "";
public string Price = "";
public void Generate()
{
string protein = Proteins[Random.Shared.Next(Proteins.Length)];
string condiment = Condiments[Random.Shared.Next(Condiments.Length)];
string bread = Breads[Random.Shared.Next(Breads.Length)];
Description = $"{protein} with {condiment} on {bread}";
// double price = Random.Shared.Next(5, 15) + Random.Shared.Next(100) * 0.01M;
// Price = price.ToString("c");
Price = (5 + Random.Shared.NextDouble() * 10).ToString("c");
}
}