Skip to content

Instantly share code, notes, and snippets.

@rnelson
Created July 1, 2024 18:00
Show Gist options
  • Save rnelson/9fe3abb4890c16d6bf54d7d076adc000 to your computer and use it in GitHub Desktop.
Save rnelson/9fe3abb4890c16d6bf54d7d076adc000 to your computer and use it in GitHub Desktop.
Rendezvous.FruitStand
// Kept the example run as-is, with the following exceptions:
// 1. Changed "let" to "var"
// 2. Added "new" on line 7
// 3. Added semicolons to the statements
// Create a new fruit stand
var stand = new FruitStand();
// Add fruits to the stand
stand.addFruit("apple", 10, 0.5);
stand.addFruit("banana", 5, 0.2);
stand.addFruit("cherry", 20, 0.1);
// Update the quantity of an existing fruit
stand.updateQuantity("banana", 10);
// Calculate the total value of all fruits in the stand
console.log(stand.totalValue());
internal class FruitStand
{
private Dictionary<string, Tuple<int, double>> _d = new();
public void addFruit(string fruit, int quantity, double cost)
{
_d.Add(fruit, new(quantity, cost));
}
public void updateQuantity(string fruit, int quantity)
{
_d[fruit] = new(quantity, _d[fruit].Item2);
}
public double totalValue() =>
_d.Sum(i => i.Value.Item1 * i.Value.Item2);
}
#pragma warning disable CS8981 // allow lowercase first letter w/o warning
internal static class console
#pragma warning restore CS8981
{
public static void log(object o) =>
Console.WriteLine(o.ToString());
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
[rnelson@roto Rendezvous.FruitStand]$ dotnet build
MSBuild version 17.8.5+b5265ef37 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
Rendezvous.FruitStand -> /home/rnelson/dev/Rendezvous.FruitStand/bin/Debug/net8.0/Rendezvous.FruitStand.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.98
[rnelson@roto Rendezvous.FruitStand]$ dotnet run
9
[rnelson@roto Rendezvous.FruitStand]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment