Exemple #1
0
        /// <summary>
        /// This method records each bet placed by a guy. Any guy can place
        /// multiple bets on any dog even repeatedly on the same dog.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonPlaceBet_Click(object sender, EventArgs e)
        {
            //Whe the guy places a bet we reset all the dogs and
            //==hide the images and set the distance covered by each
            //==dog to 0.
            foreach (var d in dogInRace.DogsInRaceList)
            {
                d.MyPictureBox.Visible = false;
                d.DistanceCoveredByDog = 0;
            }

            int currentBettingAmount = (int)numericBet.Value;
            int currentDogSelected   = 0;

            //We check that a dog has been selected
            if (comboBoxNumericDogPosition.SelectedValue != null)
            {
                currentDogSelected = (int)comboBoxNumericDogPosition.SelectedValue;
            }

            string currentBettingGuyName = labelGuyPlacingBet.Text;
            string guyBettingNowName     = "";

            //We check that there are enough dogs in the race for
            //the person to place a bet
            if (dogInRace.DogsInRaceList.Count() > 1)
            {
                //We check that there is an actual guy placing a bet
                if (currentBettingGuyName != "")
                {
                    //Get the guys who is placing the bet
                    Guy guyBettingNow = guy.guysAtTheRaceList.Where(c => c.GetName() == currentBettingGuyName).FirstOrDefault();

                    int currentInitBalance = guyBettingNow.GetBalance();

                    if (currentBettingAmount <= currentInitBalance && currentDogSelected != 0)
                    {
                        //==We place the bet using the method stored in the guy class. We send the parameters
                        //== Betting amount, the position of the dog selected and the Dog object to that method.

                        bool betPlaced = guyBettingNow.PlaceBet(currentBettingAmount, currentDogSelected, dogInRace, bet);

                        if (betPlaced)
                        {
                            //We populate the guy object in the bet list since it was not transported as a parameter
                            //(The value could have been sent, however we keep it here to call the method JUST for practice).
                            var currentBet = bet.betsList.Where(
                                c => c.dog.DogGatePosition == currentDogSelected &&
                                c.GetAmountBet() == currentBettingAmount &&
                                c.bettingPerson == null
                                ).FirstOrDefault();

                            //==We update the balance of the guy placing the bet
                            currentBet.bettingPerson = guyBettingNow;

                            //==We take the money from the guy's account
                            guyBettingNow.WithdrawFromAccount(currentBettingAmount);

                            //We verify that the current balance is correct
                            int currentEndBalance = guyBettingNow.GetBalance();
                            guyBettingNowName      = guyBettingNow.GetName();
                            labelGuySituation.Text = bet.GetBettingDescription(bet, guyBettingNowName);

                            //==Print the list of bets
                            DisplayBetsPlaced();

                            //==Update of balance labels
                            UpdateLabels();

                            //==After placing the bets we make the dogs visible again.
                            foreach (var d in dogInRace.DogsInRaceList)
                            {
                                d.MyPictureBox.Visible = true;
                            }

                            //We define the starting position of the dogs
                            SetDogsStartingPosition();
                        }
                        else
                        {
                            labelGuySituation.Text      = guyBettingNowName + "Has not placed a bet.";
                            labelGuySituation.ForeColor = Color.Red;
                        }
                    }
                    else
                    {
                        //we identify what is the problem
                        string name = guyBettingNow.GetName();
                        if (currentBettingAmount > currentInitBalance)
                        {
                            labelGuySituation.Text = name + " has not enough money to place a bet.";
                        }
                        if (currentDogSelected == 0)
                        {
                            labelGuySituation.Text = name + " Has not selected a dog to place a bet.";
                        }
                    }
                }
                else
                {
                    labelGuySituation.Text      = "You must select a guy to place a bet. ";
                    labelGuySituation.ForeColor = Color.Red;
                    labelWarning.Text           = "";
                }
            }
            else
            {
                labelWarning.Text      = "you must add at least 2 dogs to the race";
                labelWarning.ForeColor = Color.Red;
            }
        }