Search code examples
c#.netif-statementconsole

How to use two if statements in C#?


This is my code for an ATM, but somehow when I press the number 2 on the numpad I get my else statement that there was no choice for this number.

Console.WriteLine("What is your name? ");
string userName = Console.ReadLine();
Console.WriteLine("You are: " + userName);
Console.WriteLine("How much money do you have? ");
string Balance = Console.ReadLine();
float startBalance = float.Parse(Balance);

Console.WriteLine(userName + ", your balance is " + startBalance + " EUR");
Console.WriteLine("Press 1 for WITHDRAWAL");
Console.WriteLine("Press 2 for DEPOSIT");


if (Console.ReadKey().Key == ConsoleKey.NumPad1)
{
    Console.ReadLine();
    Console.WriteLine("How much money do you wish to withdraw? ");
    string Withdrawal = Console.ReadLine();
    float wBalance = float.Parse(Withdrawal);

    Console.WriteLine("Your new balance is " + (startBalance - wBalance) + " EUR");
    Console.ReadLine();
    Environment.Exit(0);

} 



if (Console.ReadKey().Key == ConsoleKey.NumPad2)
{
    Console.ReadLine();
    Console.WriteLine("How much money do you wish to deposit? ");
    string Deposit = Console.ReadLine();
    float dBalance = float.Parse(Deposit);

    Console.WriteLine("Your new balance is " + (startBalance - dBalance) + " EUR");
    Console.ReadLine();
    Environment.Exit(0);

}

else Console.WriteLine("There was no choice for this number");

Solution

  • Copy + Paste is evil: please, note that subtracting (adding expected) deposit

    "Your new balance is " + (startBalance - dBalance) + " EUR"
    

    is a typical copy + paste error.

    Instead of copying yourself, let's extract a method:

    private static float ReadFloat(string title) {
      // keep asking user...
      while (true) {
        if (!string.IsNullOrWhiteSpace(title))
          Console.WriteLine(title);
    
        if (!float.TryParse(Console.ReadLine(), out float result))
          Console.WriteLine("Syntax error. Please, try again.");
        else if (result < 0)
          Console.WriteLine("Negative sum is not allowed. Please, try again.");
        else
          return result; // ... until valid value is provided
      }
    }
    
    

    Then use it

    ...
    
    Console.WriteLine("Press 1 for WITHDRAWAL");
    Console.WriteLine("Press 2 for DEPOSIT");
    
    // Ask user once - Console.ReadKey().Key - then operate with his/her choice 
    var choice = Console.ReadKey().Key;
    
    // balance change (either negative - withdraw or positive - deposit)
    float delta = 
        choice == ConsoleKey.NumPad1 ? -ReadFloat("How much money do you wish to withdraw?")
      : choice == ConsoleKey.NumPad2 ?  ReadFloat("How much money do you wish to deposit?")
      : 0f;
    
    if (delta != 0) {
      Console.WriteLine($"Your new balance is {startBalance + delta} EUR");
      Console.ReadLine();
      Environment.Exit(0);
    }
    else 
      Console.WriteLine("There was no choice for this number");