mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-09 11:46:24 -08:00
Merge pull request #309 from pgruderman/stock-market
Code quality improvements for Stock Market
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -8,16 +8,22 @@
|
||||
/// <summary>
|
||||
/// Gets the company's name.
|
||||
/// </summary>
|
||||
public string Name { get; init; }
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the company's three letter stock symbol.
|
||||
/// </summary>
|
||||
public string StockSymbol { get; init; }
|
||||
public string StockSymbol { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the company's current share price.
|
||||
/// </summary>
|
||||
public double SharePrice { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new Company record.
|
||||
/// </summary>
|
||||
public Company(string name, string stockSymbol, double sharePrice) =>
|
||||
(Name, StockSymbol, SharePrice) = (name, stockSymbol, sharePrice);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Game.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides additional methods for the <see cref="ImmutableArray{T}"/> class.
|
||||
/// </summary>
|
||||
public static class ImmutableArrayExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps each element in an immutable array to a new value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource">
|
||||
/// The type of elements in the source array.
|
||||
/// </typeparam>
|
||||
/// <typeparam name="TResult">
|
||||
/// The type of elements in the resulting array.
|
||||
/// </typeparam>
|
||||
/// <param name="source">
|
||||
/// The source array.
|
||||
/// </param>
|
||||
/// <param name="selector">
|
||||
/// Function which receives an element from the source array and its
|
||||
/// index and returns the resulting element.
|
||||
/// </param>
|
||||
public static ImmutableArray<TResult> Map<TSource, TResult>(this ImmutableArray<TSource> source, Func<TSource, int, TResult> selector)
|
||||
{
|
||||
var builder = ImmutableArray.CreateBuilder<TResult>(source.Length);
|
||||
|
||||
for (var i = 0; i < source.Length; ++i)
|
||||
builder.Add(selector(source[i], i));
|
||||
|
||||
return builder.MoveToImmutable();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,13 @@ namespace Game
|
||||
/// <summary>
|
||||
/// Defines the set of companies that will be simulated in the game.
|
||||
/// </summary>
|
||||
private static ImmutableArray<Company> Companies = ImmutableArray.CreateRange(new[]
|
||||
private readonly static ImmutableArray<Company> 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()
|
||||
|
||||
@@ -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.
|
||||
/// </param>
|
||||
public static IEnumerable<double> 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));
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random value for the market trend.
|
||||
@@ -143,7 +143,7 @@ namespace Game
|
||||
private static IEnumerable<int?> 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?));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user