该代码是一个简单的控制台应用程序,用于随机更新汇率。我按照我的训练计划编写了它,并在这段代码中实现了delegate
和event
。版本.NET
- 4.8
代码写得有多糟糕?应该做出哪些改变?
using System;
using System.Collections.Generic;
namespace CryptoBot
{
public class Program
{
public static Dictionary<string, decimal> CryptoValute = new Dictionary<string, decimal>();
public static decimal ForExtemeValue;
private static int _count = 0;
static CryptoPriceMonitor priceMonitor = new CryptoPriceMonitor();
static TraderBot traderBot = new TraderBot(priceMonitor);
public static void PrintColor(string message, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
}
static void Main(string[] args)
{
CryptoValute.Add("Bitcoin", 50000);
CryptoValute.Add("DogeCoin", 3);
CryptoValute.Add("Ethereum", 1680);
CryptoValute.Add("USDT", 26);
PrintColor("Choose valute: Bitcoin, DogeCoin, Ethereum, USDT", ConsoleColor.Green);
PrintColor("Current Prices:", ConsoleColor.Green);
foreach (var price in CryptoValute)
{
Console.WriteLine(price);
}
string Choose = Console.ReadLine();
while (true)
{
switch (Choose)
{
case "Bitcoin":
ForExtemeValue = CryptoValute["Bitcoin"];
Iterations("Bitcoin");
Choose = Console.ReadLine();
break;
case "DogeCoin":
ForExtemeValue = CryptoValute["DogeCoin"];
Iterations("DogeCoin");
Choose = Console.ReadLine();
break;
case "Ethereum":
ForExtemeValue = CryptoValute["Ethereum"];
Iterations("Ethereum");
Choose = Console.ReadLine();
break;
case "USDT":
ForExtemeValue = CryptoValute["USDT"];
Iterations("USDT");
Choose = Console.ReadLine();
break;
default:
PrintColor("There is no currency on the list.", ConsoleColor.Green);
Choose = Console.ReadLine();
break;
}
}
}
private const int IterationsCount = 1000;
public static void Iterations(string name)
{
string valute;
decimal value;
_count = 0;
valute = name;
value = CryptoValute[name];
while (_count != IterationsCount)
{
_count++;
value = CryptoValute[name];
priceMonitor.UpdatePrice(value, valute);
}
Console.Clear();
PrintColor($"Here are a {IterationsCount} last iterations", ConsoleColor.Green);
PrintColor("Choose valute: Bitcoin, DogeCoin, Ethereum, USDT", ConsoleColor.Green);
PrintColor("Current Prices:", ConsoleColor.Green);
foreach (var price in CryptoValute)
{
Console.WriteLine(price);
}
}
}
public class CryptoPriceMonitor
{
public void UpdatePrice(decimal oldPrice, string name)
{
decimal currentPrice = oldPrice;
PriceChanged?.Invoke(oldPrice, ExtremeValues(currentPrice), name);
}
private Random _random = new Random();
private int _procent;
private const int MaxRandomProcent = 8;
private const int MinRandomProcent = -7;
private decimal ExtremeValues(decimal basePrice)
{
decimal newPrice = basePrice;
decimal maxValue = Program.ForExtemeValue * 100;
decimal minValue = Program.ForExtemeValue - (Program.ForExtemeValue * 90) / 100;
if (basePrice >= maxValue)
{
_procent = _random.Next(MinRandomProcent, 0);
newPrice -= (basePrice * Math.Abs(_procent)) / 100;
return newPrice;
}
if (basePrice <= minValue)
{
_procent = _random.Next(1, MaxRandomProcent);
newPrice += (basePrice * Math.Abs(_procent)) / 100;
return newPrice;
}
else
{
_procent = _random.Next(MinRandomProcent, MaxRandomProcent);
if (_procent > 0)
{
newPrice += (basePrice * Math.Abs(_procent)) / 100;
return newPrice;
}
else
{
newPrice -= (basePrice * Math.Abs(_procent)) / 100;
return newPrice;
}
}
}
public delegate void PriceChangeHandler(decimal oldPrice, decimal newPrice, string valute);
public event PriceChangeHandler PriceChanged;
}
public class TraderBot
{
public TraderBot(CryptoPriceMonitor monitor)
{
monitor.PriceChanged += OnPriceChanged;
}
public void OnPriceChanged(decimal oldPrice, decimal newPrice, string valute)
{
if (newPrice > oldPrice)
{
Program.PrintColor($"Цена повысилась: {oldPrice} -> {newPrice}", ConsoleColor.Green);
RestorPrice(valute, newPrice);
}
else
{
Program.PrintColor($"Цена понизилась: {oldPrice} -> {newPrice}", ConsoleColor.Red);
RestorPrice(valute, newPrice);
}
}
public static void RestorPrice(string valute, decimal value)
{
Program.CryptoValute[valute] = value;
}
}
}