Esempio n. 1
0
 //allow user to take cash from the account with datarace. It receives as input amount of money to be taken
 private void takeCashTrue(int cash)
 {
     if (cash == 0) //if amount of money is equal to zero, user is prompt to specify valid amount
     {
         screen7();
     }
     else
     {
         textBox1.Text = "";
         if (activeAccount.getBalance() < cash) // checks if there is enought money to be taken
         {
             activeAccount.Unlock();            // unlock the access to the balance
             setScreen4False();
         }
         else
         {
             activeAccount.Unlock(); // unlock the access to the balance
             int oldBalance = activeAccount.getBalance();
             activeAccount.Unlock(); // unlock access to the balance, that won't prevent datarace condition
             Thread.Sleep(1000);     // sleep to give to a user chance to perform simultaneous work for another ATM thread
             setScreen4True();
             Thread.Sleep(2000);
             activeAccount.setBalance(oldBalance - cash);
         }
         state = 4;
     }
 }
Esempio n. 2
0
        // check provided by user data and proceed to the next step. Called by 2 methods above
        private void nextStep()
        {
            switch (state)
            {
            case 0:
                activeAccount = findAccount(textBox1.Text);
                if (activeAccount != null)     // if account found, go to the next step
                {
                    textBox1.Text = "Enter your pin here";
                    screen1();
                    pinTry = 4;
                }
                else     // if account was not found, program stays in the same state
                {
                    textBox1.Text = "Insert your card here";
                    text[4].Text  = "Wrong card";
                }
                break;

            case 1:
                try
                {
                    int input = Convert.ToInt32(IsDigitsOnly(textBox1.Text));
                    if (activeAccount.checkPin(input))     // if entered pin is correct
                    {
                        textBox1.Text         = "";
                        textBox1.PasswordChar = '\0';
                        screen2();
                    }
                    else     // if pin is wrong
                    {
                        pinFail();
                    }
                }
                catch (Exception)     // if pin is wrong
                {
                    pinFail();
                }
                break;

            case 6:
                //Take user input and deposit it into their acount
                try
                {
                    int depositValue = Convert.ToInt32(IsDigitsOnly(textBox1.Text));
                    if (depositValue != 0)     //Checks if deposit value is
                    {
                        int oldBal = activeAccount.getBalance();
                        Thread.Sleep(3000);
                        activeAccount.setBalance(oldBal + depositValue);
                        activeAccount.Unlock();
                        //Go to stage 2
                        textBox1.Text         = "";
                        textBox1.PasswordChar = '\0';

                        screen2();
                    }
                    else
                    {
                        textBox1.Text = "Invalid value";
                    }
                }
                catch (Exception)     // if user input causes error
                {
                    textBox1.Text = "Invalid value";
                }

                break;

            case 7:
                //read the amount of money provided by user to the taken from account
                int userCash = Convert.ToInt32(IsDigitsOnly(textBox1.Text));
                if (userCash % 5 == 0)     // amount must be devisible by 5
                {
                    if (datarace)
                    {
                        takeCashTrue(userCash);
                    }
                    else
                    {
                        takeCashFalse(userCash);
                    }
                }
                else
                {
                    textBox1.Text = "";
                    label1.Text   = "Enter Amount\n Invalid number";
                }
                break;

            default:
                textBox1.Text = "";
                break;
            }
        }