Console Application: Managing a bank account

Hi readers,

We’ve seen quite a few things in the last few days. Now, it’s time to put all the fundamentals to good use. Today, we’ll create a console application in which a user can access the menu of its bank account.  At first, the user will need to set up his password to get inside his account. Then in order to withdraw money, a user will need to make sure he had deposit money earlier before. In a situation in which he tries to withdraws more than he previously had, the bank will print an error message to tell him that the operation is impossible. If he tries to withdraw money that he owns, the console application will print the amount of money that was withdrawn and how much he has left. After each operation, the console application will always ask the user if he wants to quit, if so, he will only have to press the ‘q’ letter.

In this console application, we’ll mostly use methods to reduce the code that has to be in the main method. For the purpose of just looking how something like would work, we’ll start with a bank account with 2000$. This our first real console application, so we won’t go too deep. We won’t manage, for now, what happens if we were to withdraw more than what we have in our bank account, the console application will show a negative balance.


class Program{
  static void Main(){

       String confirmedPassword=null; //The first password of our user
       String userPassword=null; //The password we'll try to confirm with confirmedPassword
       char selectedAnswerFromUser='n'; //Selected answer from user after each operation
       char selectedOperationFromUser='0';
       int numberOfTimesTypingWrongPassword=0; //we'll verify if the user types the wrong password
       confirmedPassword=CreatePassword();
       while(selectedAnswerFromUser != 'q' ||                  numberOfTimesTypingWrongPassword >=3 ||                selectedOperationFromUser != 'q')
             {
                Console.WriteLine("Please, enter your password");
                userPassword=Console.ReadLine();
                 if(ConfirmPasswordBankAccount(userPassword,confirmedPassword) == false)
             {
         while(ConfirmPasswordBankAccount(userPassword,confirmedPassword) ==false && numberOfTimesTtpingWrongPassword <3)
          {
             Console.WriteLine("Please enter the good password to access your account");
             userPassword = Console.ReadLine();
             numberOfTimesTypingWrongPassword++;
              AlertUser(numberOfTimesTypingWrongPassword); //won't do anything until we reach the value of 3
           }
          }else{
             selectedAnswerFromUser =         ConfirmOperation();
             if(selectedAnswerFromUser!='q')
             {
                decimal accountBalance=2000;
                decimal withdraw;
                decimal deposit;
                switch(selectedAnswerFromUser)
                {
                  case p:
                      PrintAccountBalance(accountBalance);
                      break;
                  case d:
                        Console.WriteLine("Please insert money to deposit");
                        String input = Console.ReadLine();
                        if(Decimal.TryParse(input,out deposit)
                        {
DepositMoney(balance,deposit);
                        }
                        else
                        {
                          deposit=0.0m;
                                   DepositMoney(balance,deposit);
                          Console.WriteLine("The balance did not change because you did not type numerical values");
                        }
                        break;
                       case w:
                           Console.WriteLine("Please inser the amount you wish to withdraw from your account");
                           String input=Console.ReadLine();
                           if(Decimal.TryParse(input, out withdraw)
                           {
                             WithdrawMoney(balance, withdraw);
                           }
                           else
                           {
                             withdraw=0.0m;
                             WithdrawMoney(balance,withdraw);
                             Console.WriteLine("The balance did not change because you did not insert a correct amount");
                            }
                           break;
                   }
                }
              }
            }
          }
        }
     }
   } 

 //Creates the password for the bank account
 static String CreatePassword()
 {
   String aPassword= null; //initialize the string to null, it has to value for the moment
   Console.WriteLine("Please, insert a password for your bank account");
   aPassword = Console.ReadLine();
   return aPassword;
 }

 //Will make sure that we enter the good password
 static bool ConfirmPasswordBankAccount(String firstPassword, String secondPassword)
 {
 return secondPassword.Equals(firstPassword); //Verifies if the two password are a match, if yes, return true. Otherwise false
 }

//Alerts a user after 3 type he types the wrong password
 static void AlertUser(int numberOfErrors)
 {
    if(numberOfErrors >=3)
    {
      Console.WriteLine("There is something wrong with your bank account. You cannot access your bank account today.");
     }
 }

//Method which will confirm what operation the user wants to achieve
static char ConfirmOperation()
{
   char answer;
   Console.WriteLine("If you wish to continue, please press c. Otherwise, press q");
   answer = Console.ReadLine();
   if(answer != 'q' || answer != 'c')
   {
     while(answer != 'q' || answer != 'c')
     {
         Console.WriteLine("Please press either c or q to pursue with this software");
         answer= Console.ReadLine();
      }
     }
   else{
        Console.WriteLine("To print the current balance of the account, press p. To deposit money in the acocunt, press d. To withdraw money from the account, press w. To quit, press q");
        if(answer!='p' || answer!= 'd' || answer !='w' || answer !='q')
        {
           while(answer != 'p' || answer !='d' || answer!='w' || answer !='q')
           {
              Console.WriteLine("Please enter either of the following : q, c,p or w");
              answer=Console.ReadLine();
           }
        }
     }
 return answer;
}

static decimal DepositMoney(decimal balance, decimal moneyToDeposit)
 {
   balance+=moneyToDeposit; // The += operator is equal to say              balance= balance+moneyToDeposit;
   PrintDepositMoney(moneyToDeposit); // Prints what we've deposited

   PrintAccountBalance(balance); //Prints the new balance

   return balance;
 }

 static decimal WithdrawMoney(decimal balance, decimal moneyToWithdraw)
 {
   balance -= moneyToWithdraw; //Substracts money from the account balance

   PrintWithdrawMoney(moneyToWithdraw); //Prints the money we wanted to take from our account

   PrintAccountBalance(balance); //Prints the account new balance
 return balance;
 }

static void PrintAccountBalance(decimal balance)
 {

   Console.WriteLine("This is the current balance : {0}",balance); //Prints the accounts current balance

 }

 static void PrintDepositMoney(decimal deposit)
 {

   Console.WriteLine("You have deposit this in your account : {0}",deposit); //Prints the money we wished to insert in our account

 }

 static void PrintWithdrawMoney(decimal withdraw)
 {

    Console.WriteLine("You have withdraw this from your account : {0}", withdraw); //Prints the money we wished to take from our account

 }

Kevin

Advertisement

2 thoughts on “Console Application: Managing a bank account

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s