Advertisement
sergezhu

Untitled

Apr 23rd, 2023
779
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 1 0
  1. namespace ConsoleApp1;
  2.  
  3. using System.Text;
  4.  
  5. public class Task12_02
  6. {
  7.     private const string RubName = "Rub";
  8.     private const string UsdName = "Usd";
  9.     private const string EurName = "Eur";
  10.    
  11.     private const float UsdToRub = 70f;
  12.     private const float RubToUsd = 1f / 67f;
  13.     private const float EurToRub = 80f;
  14.     private const float RubToEur = 1f / 77f;
  15.     private const float UsdToEur = 0.9f;
  16.     private const float EurToUsd = 1.1f;
  17.  
  18.     public void Run()
  19.     {
  20.         Console.InputEncoding = Encoding.Unicode;
  21.         Console.OutputEncoding = Encoding.Unicode;
  22.  
  23.         string[] currencies = new[] { RubName, UsdName, EurName };
  24.  
  25.         string currentCurrency = InputCurrency();
  26.        
  27.         Console.Write("Input currency start value: ");
  28.         float currentCurrencyValue = float.Parse( Console.ReadLine() );
  29.  
  30.         var currencyWallets = new Dictionary<string, float>();
  31.         var currencyRates = new Dictionary<(string, string), float>()
  32.         {
  33.             { (RubName, UsdName), RubToUsd },
  34.             { (UsdName, RubName), UsdToRub },
  35.             { (RubName, EurName), RubToEur },
  36.             { (EurName, RubName), EurToRub },
  37.             { (EurName, UsdName), EurToUsd },
  38.             { (UsdName, EurName), UsdToEur },
  39.         };
  40.        
  41.         foreach ( var currency in currencies )
  42.         {
  43.             float currencyValue = string.Equals( currentCurrency, currency ) ? currentCurrencyValue : 0;
  44.             currencyWallets[currency] = currencyValue;
  45.         }
  46.  
  47.         bool canExit = false;
  48.  
  49.         while ( canExit == false )
  50.         {
  51.             Console.Clear();
  52.             WriteWalletsInfo( currencyWallets );
  53.  
  54.             Console.WriteLine( "Select source currency" );
  55.             string sourceCurrency = InputCurrency();
  56.            
  57.             Console.WriteLine( "Select target currency" );
  58.             string targetCurrency = InputCurrency();
  59.  
  60.             Console.WriteLine( $"Enter value in {sourceCurrency}" );
  61.             float sourceCurrencyValue = float.Parse( Console.ReadLine() );
  62.  
  63.             ConvertCurrency( currencyWallets, currencyRates, sourceCurrency, targetCurrency, sourceCurrencyValue );
  64.  
  65.             WriteWalletsInfo( currencyWallets );
  66.            
  67.             string properlyExitAnswer = "n";
  68.             Console.WriteLine( $"\nContinue? Enter {properlyExitAnswer} for exit" );
  69.             string continueAnswer = Console.ReadLine();
  70.  
  71.             canExit = string.Equals( continueAnswer, properlyExitAnswer );
  72.         }
  73.     }
  74.  
  75.     private string InputCurrency()
  76.     {
  77.         string currentCurrency;
  78.         bool isCurrencyValid;
  79.        
  80.         do
  81.         {
  82.             Console.WriteLine( $"Input currency ( {RubName}, {UsdName}, {EurName} ): " );
  83.             currentCurrency = Console.ReadLine();
  84.             isCurrencyValid = IsCurrencyValid( currentCurrency );
  85.  
  86.             if ( isCurrencyValid == false )
  87.                 Console.WriteLine( $"Currency {currentCurrency} is not valid. Try again." );
  88.         }
  89.         while ( isCurrencyValid == false );
  90.  
  91.         return currentCurrency;
  92.     }
  93.  
  94.     private bool IsCurrencyValid( string currency )
  95.     {
  96.         return string.Equals( currency, RubName ) || string.Equals( currency, UsdName ) || string.Equals( currency, EurName );
  97.     }
  98.  
  99.  
  100.     private void WriteWalletsInfo( Dictionary<string, float> currencyWallets )
  101.     {
  102.         Console.WriteLine( "=== Wallets Info ===" );
  103.  
  104.         foreach ( var currencyKey in currencyWallets.Keys )
  105.             Console.WriteLine( $"[{currencyKey}] : {currencyWallets[currencyKey]}" );
  106.     }
  107.  
  108.     private void ConvertCurrency( Dictionary<string, float> currencyWallets, Dictionary<(string, string), float> currencyRates,
  109.                                   string sourceCurrency, string targetCurrency, float sourceCurrencyValue )
  110.     {
  111.         if ( string.Equals( sourceCurrency, targetCurrency ) )
  112.             return;
  113.  
  114.         Console.WriteLine( $"Start convertation: {sourceCurrencyValue} from {sourceCurrency} to {targetCurrency}" );
  115.  
  116.         if ( sourceCurrencyValue > currencyWallets[sourceCurrency] )
  117.         {
  118.             Console.WriteLine( $"Not enough moneys in wallet [{sourceCurrency}]" );
  119.             return;
  120.         }
  121.  
  122.         Console.WriteLine( $"Convertation is successfully!" );
  123.  
  124.         currencyWallets[sourceCurrency] -= sourceCurrencyValue;
  125.         currencyWallets[targetCurrency] += sourceCurrencyValue * currencyRates[(sourceCurrency, targetCurrency)];
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement