Advertisement
sergezhu

Untitled

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