From 1794bb048db0ff5d4915a284c4864bc5bbb17d4a Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 12 Jul 2021 22:20:51 -0400 Subject: [PATCH 1/2] Enabled nullable reference types --- 83 Stock Market/csharp/Game.csproj | 3 +-- 83 Stock Market/csharp/src/Company.cs | 10 ++++++++-- 83 Stock Market/csharp/src/Controller.cs | 9 +++++++-- 83 Stock Market/csharp/src/Program.cs | 10 +++++----- 4 files changed, 21 insertions(+), 11 deletions(-) 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/Program.cs b/83 Stock Market/csharp/src/Program.cs index 552d8c9a..8d2b2a98 100644 --- a/83 Stock Market/csharp/src/Program.cs +++ b/83 Stock Market/csharp/src/Program.cs @@ -11,11 +11,11 @@ namespace Game /// private 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() From cc23f806c4ae6ab190c1bb504f43236c87e10029 Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 17 Jul 2021 06:46:53 -0400 Subject: [PATCH 2/2] Minor code clean-up --- .../Extensions/ImmutableArrayExtensions.cs | 37 +++++++++++++++++++ 83 Stock Market/csharp/src/Program.cs | 2 +- 83 Stock Market/csharp/src/StockMarket.cs | 12 +++--- 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 83 Stock Market/csharp/src/Extensions/ImmutableArrayExtensions.cs 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 8d2b2a98..a7ab841a 100644 --- a/83 Stock Market/csharp/src/Program.cs +++ b/83 Stock Market/csharp/src/Program.cs @@ -9,7 +9,7 @@ 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("INT. BALLISTIC MISSILES", "IBM", sharePrice:100), new Company("RED CROSS OF AMERICA", "RCA", sharePrice:85 ), 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?)); } }