Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ConsoleApp1;
- using System.Text;
- public class Task12_02
- {
- private const string RubName = "Rub";
- private const string UsdName = "Usd";
- private const string EurName = "Eur";
- private const float UsdToRub = 70f;
- private const float RubToUsd = 1f / 67f;
- private const float EurToRub = 80f;
- private const float RubToEur = 1f / 77f;
- private const float UsdToEur = 0.9f;
- private const float EurToUsd = 1.1f;
- public void Run()
- {
- Console.InputEncoding = Encoding.Unicode;
- Console.OutputEncoding = Encoding.Unicode;
- string[] currencies = new[] { RubName, UsdName, EurName };
- string currentCurrency = InputCurrency();
- Console.Write("Input currency start value: ");
- float currentCurrencyValue = float.Parse( Console.ReadLine() );
- var currencyWallets = new Dictionary<string, float>();
- var currencyRates = new Dictionary<(string, string), float>()
- {
- { (RubName, UsdName), RubToUsd },
- { (UsdName, RubName), UsdToRub },
- { (RubName, EurName), RubToEur },
- { (EurName, RubName), EurToRub },
- { (EurName, UsdName), EurToUsd },
- { (UsdName, EurName), UsdToEur },
- };
- foreach ( var currency in currencies )
- {
- float currencyValue = string.Equals( currentCurrency, currency ) ? currentCurrencyValue : 0;
- currencyWallets[currency] = currencyValue;
- }
- bool canExit = false;
- while ( canExit == false )
- {
- Console.Clear();
- WriteWalletsInfo( currencyWallets );
- Console.WriteLine( "Select source currency" );
- string sourceCurrency = InputCurrency();
- Console.WriteLine( "Select target currency" );
- string targetCurrency = InputCurrency();
- Console.WriteLine( $"Enter value in {sourceCurrency}" );
- float sourceCurrencyValue = float.Parse( Console.ReadLine() );
- ConvertCurrency( currencyWallets, currencyRates, sourceCurrency, targetCurrency, sourceCurrencyValue );
- WriteWalletsInfo( currencyWallets );
- string properlyExitAnswer = "n";
- Console.WriteLine( $"\nContinue? Enter {properlyExitAnswer} for exit" );
- string continueAnswer = Console.ReadLine();
- canExit = string.Equals( continueAnswer, properlyExitAnswer );
- }
- }
- private string InputCurrency()
- {
- string currentCurrency;
- bool isCurrencyValid;
- do
- {
- Console.WriteLine( $"Input currency ( {RubName}, {UsdName}, {EurName} ): " );
- currentCurrency = Console.ReadLine();
- isCurrencyValid = IsCurrencyValid( currentCurrency );
- if ( isCurrencyValid == false )
- Console.WriteLine( $"Currency {currentCurrency} is not valid. Try again." );
- }
- while ( isCurrencyValid == false );
- return currentCurrency;
- }
- private bool IsCurrencyValid( string currency )
- {
- return string.Equals( currency, RubName ) || string.Equals( currency, UsdName ) || string.Equals( currency, EurName );
- }
- private void WriteWalletsInfo( Dictionary<string, float> currencyWallets )
- {
- Console.WriteLine( "=== Wallets Info ===" );
- foreach ( var currencyKey in currencyWallets.Keys )
- Console.WriteLine( $"[{currencyKey}] : {currencyWallets[currencyKey]}" );
- }
- private void ConvertCurrency( Dictionary<string, float> currencyWallets, Dictionary<(string, string), float> currencyRates,
- string sourceCurrency, string targetCurrency, float sourceCurrencyValue )
- {
- if ( string.Equals( sourceCurrency, targetCurrency ) )
- return;
- Console.WriteLine( $"Start convertation: {sourceCurrencyValue} from {sourceCurrency} to {targetCurrency}" );
- if ( sourceCurrencyValue > currencyWallets[sourceCurrency] )
- {
- Console.WriteLine( $"Not enough moneys in wallet [{sourceCurrency}]" );
- return;
- }
- Console.WriteLine( $"Convertation is successfully!" );
- currencyWallets[sourceCurrency] -= sourceCurrencyValue;
- currencyWallets[targetCurrency] += sourceCurrencyValue * currencyRates[(sourceCurrency, targetCurrency)];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement