public static bool changeBalance(float change, char symbol) { balance = float.Parse(SQLoperator.CheckBalance(cardNumber)); float newBalance; if (symbol == '+') { newBalance = (balance + change); } else { newBalance = (balance - change); } try { if (balance + change < 0) { Console.WriteLine("You can't withdraw amount that is more than your current balance: " + balance.ToString() + "$"); return(false); } else { SQLoperator.ChangeBalance(cardNumber, newBalance); SQLoperator.CheckBalance(cardNumber); return(true); } } catch (Exception ex) { LogProvider.LogString(ex.ToString()); return(false); } }
static void AccountOperation() { string[] accountActions = new string[] { "Check balance", "Deposit money", "Withdraw money" }; Console.WriteLine("Select action:"); int inputN; while (true) { int n = 1; foreach (string c in accountActions) { Console.WriteLine(n + ". " + c); n++; } string input = Console.ReadLine(); if (Regex.IsMatch(input, @"^\d+$")) { Int32.TryParse(input, out inputN); if ((inputN <= commands.Length) & inputN > 0) { break; } } Console.WriteLine("\nNo such command."); } string command = accountActions[inputN - 1].ToLower(); switch (command) { case "check balance": { balance = float.Parse(SQLoperator.CheckBalance(cardNumber)); Console.WriteLine("Balance: " + balance.ToString("0.00") + "$"); return; } case "deposit money": { Console.WriteLine("Amount you want to deposit: "); while (true) { float amountF; string amount = Console.ReadLine(); if (Regex.IsMatch(amount, @"^\d+$")) { float.TryParse(amount, out amountF); changeBalance(amountF, '+'); break; } else { Console.WriteLine("Please, set the amount of money you want to depoit:"); } } return; } case "withdraw money": { SQLoperator.CheckBalance(cardNumber); Console.WriteLine("Amount you want to withdraw: "); while (true) { float amountF; string amount = Console.ReadLine(); if (Regex.IsMatch(amount, @"^\d+$")) { float.TryParse(amount, out amountF); if (changeBalance(amountF, '-')) { break; } } else { Console.WriteLine("Please, set the amount of money you want to withdraw:"); } } return; } default: { SQLoperator.CloseConnection(); LogProvider.EndLog(); Environment.Exit(0); return; } } }