Search code examples
c#consoleswitch-statementreadline

Switch Statement for Banking System


Hi I'm new to this and I'm trying to figure out a way where the customer can input their chosen option from the DisplayMenu which then takes them to another screen like DisplayBalance() this is currently working. But then I want them to hit enter and return them back to the DisplayMenu() and be able to input another option to take them to another screen but currently what is happening is the console exits. Does anyone know how to fix this?

// Display Menu

private static void DisplayMenu()
    {
        Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
        Console.WriteLine("");
        Console.WriteLine("1.View Client Info");
        Console.WriteLine("");
        Console.WriteLine("2. View Account Balance:");
        Console.WriteLine("     2A.Everyday\n     2B.Investment\n     2C.Omni");
        Console.WriteLine("");
        Console.WriteLine("3.Deposit Funds:\n     3A.Everyday\n     3B.Investment\n     3C.Omni");
        Console.WriteLine("");
        Console.WriteLine("4.Withdraw Funds:\n     4A.Everyday\n     4B.Investment\n     4C.Omni");
        Console.WriteLine("");
        Console.WriteLine("5.View Everyday Banking Details");
        Console.WriteLine("6.View Investment Banking Details");
        Console.WriteLine("7.View Omni Banking details");
        Console.WriteLine("");
        Console.WriteLine("8.Exit");
    }

// Main

   private static void Main(string[] args)
    {
       

        Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd"); // Our one client

        Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 2000m);
        Investment investment = new Investment(client, 500m);
        Omni omni = new Omni(client, 1000m);

      

            DisplayMenu();

            string userchoice = Console.ReadLine();//user input from menu options

            switch (userchoice.ToUpper())
            {
                case "1": // Display Client Info
                Console.Clear();
                Console.WriteLine(client.ClientInfo);
                    break;

                case "2A": // Display Everday Account Balance
                Console.Clear();
                DisplayBalance(everyday);
                    break;

                case "2B": // Display Investment Account Balance
                    Console.Clear();
                    DisplayBalance(investment);
                    break;

                case "2C": // Display Omni Account Balance
                    Console.Clear();
                    DisplayBalance(omni);
                    break;

                case "3A": // Everyday Account Deposit
                       
                    DepositAmount(everyday);
                    break;

                case "3B": // Investment Account Deposit

                    DepositAmount(investment);
                    break;

                case "3C": // Omni Account Deposit

                    DepositAmount(omni);
                    break;

                case "4A": // Everyday account Withdrawal
                          
                    WithdrawAmount(everyday);
                    break;

                case "4B": // Investment Account Withdrawal

                    WithdrawAmount(investment);
                    break;

                case "4C": // Omni Account Withdrawal

                    WithdrawAmount(omni);
                    break;

                case "5": // Everyday Details

                    DisplayDetails(everyday);
                    break;

                case "6": // Investment Details

                    DisplayDetails(investment);
                    break;

                case "7": // Omni Details

                    DisplayDetails(omni);
                    break;

                case "8": // Exit Banking

                    Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
                   
                    break;
            default: // catch all, breaks the loop
                     //Console.Clear()
                break;
        }
        
    }

// Display Balance

   private static void DisplayBalance(Account account)
    {
        Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
        Console.ReadLine();
        DisplayMenu();
    }

Solution

  • string userchoice = Console.ReadLine(); and all of the options below it need to be wrapped in an infinite loop i.e. while (true) { ... } . This makes it so once the switch statement breaks, the user is prompted for input again and the cycle continues. If you don't want to terminate with ctrl-C, you will have to move the terminating prompt to before the switch. For example: while (true) { string userchoice = Console.ReadLine(); if (userchoice == 8) { Console.WriteLine("You have chosen to exit the online banking. Thanks"); break; } else { switch ... EDIT: code with infinite loop

    while (true) {
          string userchoice = Console.ReadLine();//user input from menu options
    
                switch (userchoice.ToUpper())
                {
                    case "1": // Display Client Info
                    Console.Clear();
                    Console.WriteLine(client.ClientInfo);
                        break;
    
                    case "2A": // Display Everday Account Balance
                    Console.Clear();
                    DisplayBalance(everyday);
                        break;
    
                    case "2B": // Display Investment Account Balance
                        Console.Clear();
                        DisplayBalance(investment);
                        break;
    
                    case "2C": // Display Omni Account Balance
                        Console.Clear();
                        DisplayBalance(omni);
                        break;
    
                    case "3A": // Everyday Account Deposit
                           
                        DepositAmount(everyday);
                        break;
    
                    case "3B": // Investment Account Deposit
    
                        DepositAmount(investment);
                        break;
    
                    case "3C": // Omni Account Deposit
    
                        DepositAmount(omni);
                        break;
    
                    case "4A": // Everyday account Withdrawal
                              
                        WithdrawAmount(everyday);
                        break;
    
                    case "4B": // Investment Account Withdrawal
    
                        WithdrawAmount(investment);
                        break;
    
                    case "4C": // Omni Account Withdrawal
    
                        WithdrawAmount(omni);
                        break;
    
                    case "5": // Everyday Details
    
                        DisplayDetails(everyday);
                        break;
    
                    case "6": // Investment Details
    
                        DisplayDetails(investment);
                        break;
    
                    case "7": // Omni Details
    
                        DisplayDetails(omni);
                        break;
    
                    case "8": // Exit Banking
    
                        Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
                       
                        break;
                default: // catch all, breaks the loop
                         //Console.Clear()
                    break;
            }
        }