/// <summary> /// Purpose: to move the the cursor to the next text box. /// </summary> /// <param name="sender">TxtBxCrewMembers_KeyPress</param> /// <param name="e">TxtBxTotalGold.Focus</param> private void TxtBxCrewMembers_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { TxtBxTotalGold.Focus(); TxtBxTotalGold.SelectAll(); } }
/// <summary> /// Purpose: Do all the math calculations. /// </summary> private void CalcGold() { #region Var string _userMembers = ""; // capture text for error correction string _userGold = ""; // capture text for error correction int _crewMembers = 0; // user entered crew members int _totalGold = 0; // user entered total gold int _partyGold = 0; // the amount given to everyone initually int _captainShare = 0; // the total the captain will recieve int _firstShare = 0; // the total the first mate will recieve int _crewShare = 0; // the total each crew member will recieve int _pbaShare = 0; // the remainder int _auditorCheck = 0; // to check the accounting #endregion Var // 1) on button press, read data from crewMembers and totalGold // 2) tryParse on both and store them in _crewMembers and _totalGold as int _userMembers = TxtBxCrewMembers.Text; int.TryParse(TxtBxCrewMembers.Text, out _crewMembers); if (_crewMembers <= 0) // any character or -# will go here automatically { MessageBox.Show(string.Format("{0} is an invalad Crew entry, try again.", _userMembers)); TxtBxCrewMembers.Focus(); TxtBxCrewMembers.SelectAll(); return; } if (_crewMembers <= FACTOR_PEOPLE) { MessageBox.Show("You do not have enough Crew Members, hire some more so we can be off."); TxtBxCrewMembers.Focus(); TxtBxCrewMembers.SelectAll(); return; } _userGold = TxtBxTotalGold.Text; int.TryParse(TxtBxTotalGold.Text, out _totalGold); if (_totalGold <= 0) // any character or -# will go here automatically { MessageBox.Show(string.Format("{0} is an invalad Gold entry, try again.", _userGold)); TxtBxTotalGold.Focus(); TxtBxTotalGold.SelectAll(); return; } else if (_totalGold <= (_crewMembers * FACTOR_PARTY)) { MessageBox.Show("You did not earn enough gold for partying, return when you have more plunder."); TxtBxTotalGold.Focus(); TxtBxTotalGold.SelectAll(); return; } // 3) divy out 3 gold to each crew member (3 will be a const) _partyGold = _crewMembers * FACTOR_PARTY; // 4) give pirate captain 12% (will be const double 0.12) _captainShare = (int)(_totalGold * FACTOR_CAPTAIN); _totalGold = _totalGold - _captainShare; // 5) give first mate 8% (will be const double 0.08) _firstShare = (int)(_totalGold * FACTOR_FIRST); _totalGold = _totalGold - _firstShare; // 6) subtract the shares before dividing spliting the gold _totalGold = _totalGold - _partyGold; // 7) divide the gold evenly between the crew members _crewShare = _totalGold / _crewMembers + FACTOR_PARTY; // adding the 3 gold to simplify the process later // 8) find PBA remainder by % _pbaShare = _totalGold % _crewMembers; if (_pbaShare < 0) // had some instances where the PBA was a negative number, this will prevent that. { MessageBox.Show("The Pirate Benavolant Association will not loan you money, YOU GIVE US MONEY, return when you have more plunder"); TxtBxTotalGold.Focus(); TxtBxTotalGold.SelectAll(); return; } // 9) Auditor Check _auditorCheck = _captainShare + _firstShare + (_crewShare * _crewMembers) + _pbaShare; if (_auditorCheck < _totalGold) // because of all the error correction prior this will never happen { MessageBox.Show("The Auditor found descrepancies in your accounting, jail time for you."); ClearTxtBxs(); TxtBxCrewMembers.Focus(); return; } // 10) add up all the totals _captainShare = _captainShare + _crewShare; // because she also gets a share of the loot _firstShare = _firstShare + _crewShare; // because he also gets a share of the loot _crewMembers = _crewMembers - FACTOR_PEOPLE; // subtract 2 so we can display the right amount of crew members // 11) Display the totals in the appropriate textboxes TxtBxJackShare.Text = string.Format("{0} Gold", _captainShare); TxtBxMateShare.Text = string.Format("{0} Gold", _firstShare); TxtBxCrewShare.Text = string.Format("({0} Gold x {1} Crew)", _crewShare, _crewMembers); TxtBxPbaShare.Text = string.Format("{0} Gold", _pbaShare); TxtBxAuditorShare.Text = string.Format("{0} Gold", _auditorCheck); TxtBxCrewMembers.SelectAll(); TxtBxCrewMembers.Focus(); }