diff --git a/83 Stock Market/csharp/Game.csproj b/83 Stock Market/csharp/Game.csproj
index 20827042..849a99d4 100644
--- a/83 Stock Market/csharp/Game.csproj
+++ b/83 Stock Market/csharp/Game.csproj
@@ -1,8 +1,7 @@
-
Exe
net5.0
+ enable
-
diff --git a/83 Stock Market/csharp/src/Company.cs b/83 Stock Market/csharp/src/Company.cs
index 59f40e8d..86c4e4ba 100644
--- a/83 Stock Market/csharp/src/Company.cs
+++ b/83 Stock Market/csharp/src/Company.cs
@@ -8,16 +8,22 @@
///
/// Gets the company's name.
///
- public string Name { get; init; }
+ public string Name { get; }
///
/// Gets the company's three letter stock symbol.
///
- public string StockSymbol { get; init; }
+ public string StockSymbol { get; }
///
/// Gets the company's current share price.
///
public double SharePrice { get; init; }
+
+ ///
+ /// Initializes a new Company record.
+ ///
+ public Company(string name, string stockSymbol, double sharePrice) =>
+ (Name, StockSymbol, SharePrice) = (name, stockSymbol, sharePrice);
}
}
diff --git a/83 Stock Market/csharp/src/Controller.cs b/83 Stock Market/csharp/src/Controller.cs
index bccfde0c..87a1dc7d 100644
--- a/83 Stock Market/csharp/src/Controller.cs
+++ b/83 Stock Market/csharp/src/Controller.cs
@@ -97,10 +97,15 @@ namespace Game
while (true)
{
View.PromptBuySellCompany(company);
- if (Int32.TryParse(Console.ReadLine(), out var amount))
- return amount;
+
+ var input = Console.ReadLine();
+ if (input is null)
+ Environment.Exit(0);
else
+ if (!Int32.TryParse(input, out var amount))
View.PromptValidInteger();
+ else
+ return amount;
}
}
}
diff --git a/83 Stock Market/csharp/src/Extensions/ImmutableArrayExtensions.cs b/83 Stock Market/csharp/src/Extensions/ImmutableArrayExtensions.cs
new file mode 100644
index 00000000..544c7625
--- /dev/null
+++ b/83 Stock Market/csharp/src/Extensions/ImmutableArrayExtensions.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Immutable;
+
+namespace Game.Extensions
+{
+ ///
+ /// Provides additional methods for the class.
+ ///
+ public static class ImmutableArrayExtensions
+ {
+ ///
+ /// Maps each element in an immutable array to a new value.
+ ///
+ ///
+ /// The type of elements in the source array.
+ ///
+ ///
+ /// The type of elements in the resulting array.
+ ///
+ ///
+ /// The source array.
+ ///
+ ///
+ /// Function which receives an element from the source array and its
+ /// index and returns the resulting element.
+ ///
+ public static ImmutableArray Map(this ImmutableArray source, Func selector)
+ {
+ var builder = ImmutableArray.CreateBuilder(source.Length);
+
+ for (var i = 0; i < source.Length; ++i)
+ builder.Add(selector(source[i], i));
+
+ return builder.MoveToImmutable();
+ }
+ }
+}
diff --git a/83 Stock Market/csharp/src/Program.cs b/83 Stock Market/csharp/src/Program.cs
index 552d8c9a..a7ab841a 100644
--- a/83 Stock Market/csharp/src/Program.cs
+++ b/83 Stock Market/csharp/src/Program.cs
@@ -9,13 +9,13 @@ namespace Game
///
/// Defines the set of companies that will be simulated in the game.
///
- private static ImmutableArray Companies = ImmutableArray.CreateRange(new[]
+ private readonly static ImmutableArray Companies = ImmutableArray.CreateRange(new[]
{
- new Company { Name = "INT. BALLISTIC MISSILES", StockSymbol = "IBM", SharePrice = 100 },
- new Company { Name = "RED CROSS OF AMERICA", StockSymbol = "RCA", SharePrice = 85 },
- new Company { Name = "LICHTENSTEIN, BUMRAP & JOKE", StockSymbol = "LBJ", SharePrice = 150 },
- new Company { Name = "AMERICAN BANKRUPT CO.", StockSymbol = "ABC", SharePrice = 140 },
- new Company { Name = "CENSURED BOOKS STORE", StockSymbol = "CBS", SharePrice = 110 }
+ new Company("INT. BALLISTIC MISSILES", "IBM", sharePrice:100),
+ new Company("RED CROSS OF AMERICA", "RCA", sharePrice:85 ),
+ new Company("LICHTENSTEIN, BUMRAP & JOKE", "LBJ", sharePrice:150),
+ new Company("AMERICAN BANKRUPT CO.", "ABC", sharePrice:140),
+ new Company("CENSURED BOOKS STORE", "CBS", sharePrice:110)
});
static void Main()
diff --git a/83 Stock Market/csharp/src/StockMarket.cs b/83 Stock Market/csharp/src/StockMarket.cs
index ba2f52ee..36201074 100644
--- a/83 Stock Market/csharp/src/StockMarket.cs
+++ b/83 Stock Market/csharp/src/StockMarket.cs
@@ -38,13 +38,13 @@ namespace Game
},
(parameters, previousDay) => previousDay with
{
- Companies = ImmutableArray.CreateRange(
- previousDay.Companies.Select ((company, index) => AdjustSharePrice(
+ Companies = previousDay.Companies.Map(
+ (company, index) => AdjustSharePrice(
random,
company,
parameters.trend,
parameters.positiveSpike == index,
- parameters.negativeSpike == index)))
+ parameters.negativeSpike == index))
});
}
@@ -106,7 +106,7 @@ namespace Game
/// The maximum number of days each trend should last.
///
public static IEnumerable Trends(Random random, int minDays, int maxDays) =>
- random.Integers(minDays, maxDays + 1).SelectMany(days => Enumerable.Repeat(GenerateTrend(random), days));
+ random.Integers(minDays, maxDays + 1).SelectMany(daysInCycle => Enumerable.Repeat(GenerateTrend(random), daysInCycle));
///
/// Generates a random value for the market trend.
@@ -143,7 +143,7 @@ namespace Game
private static IEnumerable PriceSpikes(Random random, int companyCount, int minDays, int maxDays) =>
random.Integers(minDays, maxDays + 1)
.SelectMany(
- days => Enumerable.Range(0, days),
- (days, dayNumber) => dayNumber == 0 ? random.Next(companyCount) : default(int?));
+ daysInCycle => Enumerable.Range(0, daysInCycle),
+ (daysInCycle, dayNumber) => dayNumber == 0 ? random.Next(companyCount) : default(int?));
}
}