Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using RRCAG.Business;
- namespace VehicleSalesQuoteKyleSpecht
- {
- /// <summary>
- /// Represents a Vehicle Sales Quote Form
- /// </summary>
- public partial class VehicleSalesQuoteForm : Form
- {
- private SalesQuote salesQuote;
- private const decimal tax = .13m;
- /// <summary>
- /// Initialzes a Vehicle Sales Quote Form
- /// </summary>
- public VehicleSalesQuoteForm()
- {
- InitializeComponent();
- salesQuote = null;
- lblSummarySalesTax.Text = string.Format("Sales Tax ({0}%):", Math.Round(tax * 100, 0));
- btnCalculate.Click += new EventHandler(btnCalculate_Click);
- txtTradeInValue.TextChanged += new EventHandler(VehicleSalePriceAndTradeIn_TextChanged);
- txtVehicleSalesPrice.TextChanged += new EventHandler(VehicleSalePriceAndTradeIn_TextChanged);
- nudAnnualInterestRate.ValueChanged += new EventHandler(ButtonCheckboxNumYearsAndInterestRate_Changed);
- nudNumOfYears.ValueChanged += new EventHandler(ButtonCheckboxNumYearsAndInterestRate_Changed);
- chkComputerNav.CheckedChanged += new EventHandler(ButtonCheckboxNumYearsAndInterestRate_Changed);
- chkLeatherInterior.CheckedChanged += new EventHandler(ButtonCheckboxNumYearsAndInterestRate_Changed);
- chkStereoSystem.CheckedChanged += new EventHandler(ButtonCheckboxNumYearsAndInterestRate_Changed);
- radCustom.CheckedChanged += new EventHandler(ButtonCheckboxNumYearsAndInterestRate_Changed);
- radPearlized.CheckedChanged += new EventHandler(ButtonCheckboxNumYearsAndInterestRate_Changed);
- radStandard.CheckedChanged += new EventHandler(ButtonCheckboxNumYearsAndInterestRate_Changed);
- btnReset.Click += new EventHandler(btnReset_Click);
- }
- /// <summary>
- /// Handled when the Reset button is clicked.
- /// </summary>
- void btnReset_Click(object sender, EventArgs e)
- {
- DialogResult result = MessageBox.Show("Do you want to reset the form?", "Reset Form", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
- if (result == DialogResult.Yes)
- {
- ResetForm();
- }
- }
- /// <summary>
- /// Handled when a Radio Button, Checkbox, Number of years or Interest Rate is changed.
- /// </summary>
- void ButtonCheckboxNumYearsAndInterestRate_Changed(object sender, EventArgs e)
- {
- if (salesQuote != null)
- {
- btnCalculate.PerformClick();
- }
- }
- /// <summary>
- /// Handled when the Vehicle Sale Price or Trade In Value text is changed..
- /// </summary>
- void VehicleSalePriceAndTradeIn_TextChanged(object sender, EventArgs e)
- {
- TextChangedReset();
- }
- /// <summary>
- /// Handled when the Calculate button is clicked.
- /// </summary>
- void btnCalculate_Click(object sender, EventArgs e)
- {
- decimal vehicleSalesPrice;
- decimal tradeInValue;
- bool vehicleSalesPriceTextBool = decimal.TryParse(txtVehicleSalesPrice.Text, out vehicleSalesPrice);
- bool tradeInTextBool = decimal.TryParse(txtTradeInValue.Text, out tradeInValue);
- if (!vehicleSalesPriceTextBool) //Cannot contain letters or special characters
- {
- errorProvider1.SetError(txtVehicleSalesPrice, "Vehicle price cannot contain letters or special characters.");
- errorProvider1.SetIconPadding(txtVehicleSalesPrice, 3);
- }
- else
- {
- if (vehicleSalesPrice <= 0) //Cannot be less than or equal to 0.
- {
- errorProvider1.SetError(txtVehicleSalesPrice, "Vehicle price cannot be less than or equal to 0.");
- errorProvider1.SetIconPadding(txtVehicleSalesPrice, 3);
- }
- }
- if (!tradeInTextBool) // Cannot contain special characters or letters.
- {
- errorProvider1.SetError(txtTradeInValue, "Trade-in value cannot contain letters or special characters.");
- errorProvider1.SetIconPadding(txtTradeInValue, 3);
- }
- else
- {
- if (tradeInValue < 0) // Cannot be less than 0.
- {
- errorProvider1.SetError(txtTradeInValue, "Trade-in value cannot be less than 0.");
- errorProvider1.SetIconPadding(txtTradeInValue, 3);
- }
- }
- if (vehicleSalesPriceTextBool && vehicleSalesPrice > 0) // Is a number AND greater than 0.
- {
- if (tradeInValue > vehicleSalesPrice) //Trade in value cannot be bigger than vehicle sales price
- {
- errorProvider1.SetError(txtTradeInValue, "Trade-in value cannot exceed the vehicle sale price.");
- errorProvider1.SetIconPadding(txtTradeInValue, 3);
- }
- else //Everything valides we calculate.
- {
- errorProvider1.SetError(txtTradeInValue, "");
- errorProvider1.SetError(txtVehicleSalesPrice, "");
- //decimal tax = .14m;
- salesQuote = new SalesQuote(decimal.Parse(txtVehicleSalesPrice.Text), decimal.Parse(txtTradeInValue.Text), tax, Accessories.None, ExteriorFinish.None);
- AccessoriesCheck();
- FinishCheck();
- lblSummaryVehiclesSalePriceOutput.Text = string.Format("{0:C}", salesQuote.VehicleSalePrice);
- lblSummaryOptionsOutput.Text = string.Format("{0:N}", salesQuote.AccessoryCost + salesQuote.FinishCost);
- lblSummarySubtotalOutput.Text = string.Format("{0:C}", salesQuote.SubTotal);
- lblSummarySalesTaxOutput.Text = string.Format("{0:N}", salesQuote.SalesTax);
- lblSummaryTotalOutput.Text = string.Format("{0:C}", salesQuote.Total);
- lblSummaryTradeInOutput.Text = string.Format("{0:N}", salesQuote.TradeInAmount * -1);
- lblSummaryAmountDueOutput.Text = string.Format("{0:C}", salesQuote.AmountDue);
- lblFinanceMonthlyPaymentOutput.Text = string.Format("{0:C}", salesQuote.GetPayment(nudAnnualInterestRate.Value / 100, (int)nudNumOfYears.Value, salesQuote.AmountDue) / 12);
- }
- }
- }
- /// <summary>
- /// Reset's the form to default input.
- /// </summary>
- public void ResetForm()
- {
- salesQuote = null;
- txtVehicleSalesPrice.Text = "";
- txtTradeInValue.Text = "0";
- lblSummaryAmountDueOutput.Text = "";
- lblSummaryOptionsOutput.Text = "";
- lblSummarySalesTaxOutput.Text = "";
- lblSummarySubtotalOutput.Text = "";
- lblSummaryTotalOutput.Text = "";
- lblSummaryTradeInOutput.Text = "";
- lblSummaryVehiclesSalePriceOutput.Text = "";
- lblFinanceMonthlyPaymentOutput.Text = "";
- chkComputerNav.Checked = false;
- chkLeatherInterior.Checked = false;
- chkStereoSystem.Checked = false;
- radCustom.Checked = false;
- radPearlized.Checked = false;
- radStandard.Checked = true;
- nudAnnualInterestRate.Value = 5m;
- nudNumOfYears.Value = 1m;
- errorProvider1.SetError(txtTradeInValue, "");
- errorProvider1.SetError(txtVehicleSalesPrice, "");
- txtVehicleSalesPrice.Focus();
- }
- /// <summary>
- /// Adds the Accessories Chosen to the Vehicle Sales Quote.
- /// </summary>
- public void AccessoriesCheck()
- {
- if (chkComputerNav.Checked && chkLeatherInterior.Checked && chkStereoSystem.Checked)
- {
- salesQuote.AccessoriesChosen = Accessories.All;
- }
- else if (chkComputerNav.Checked && chkLeatherInterior.Checked)
- {
- salesQuote.AccessoriesChosen = Accessories.LeatherAndNavigation;
- }
- else if (chkStereoSystem.Checked && chkComputerNav.Checked)
- {
- salesQuote.AccessoriesChosen = Accessories.StereoAndNavigation;
- }
- else if (chkStereoSystem.Checked && chkLeatherInterior.Checked)
- {
- salesQuote.AccessoriesChosen = Accessories.StereoAndLeather;
- }
- else if (chkComputerNav.Checked)
- {
- salesQuote.AccessoriesChosen = Accessories.ComputerNavigation;
- }
- else if (chkLeatherInterior.Checked)
- {
- salesQuote.AccessoriesChosen = Accessories.LeatherInterior;
- }
- else if (chkStereoSystem.Checked)
- {
- salesQuote.AccessoriesChosen = Accessories.StereoSystem;
- }
- }
- /// <summary>
- /// Adds the Exterior Finish chosen to the Vehicle Sales Quote.
- /// </summary>
- public void FinishCheck()
- {
- if (radCustom.Checked)
- {
- salesQuote.ExteriorFinishChosen = ExteriorFinish.Custom;
- }
- else if (radPearlized.Checked)
- {
- salesQuote.ExteriorFinishChosen = ExteriorFinish.Pearlized;
- }
- else if (radStandard.Checked)
- {
- salesQuote.ExteriorFinishChosen = ExteriorFinish.Standard;
- }
- }
- /// <summary>
- /// Resets the Summary and Monthly Payment values on the form to initial state.
- /// </summary>
- public void TextChangedReset()
- {
- salesQuote = null;
- lblSummaryAmountDueOutput.Text = "";
- lblSummaryOptionsOutput.Text = "";
- lblSummarySalesTaxOutput.Text = "";
- lblSummarySubtotalOutput.Text = "";
- lblSummaryTotalOutput.Text = "";
- lblSummaryTradeInOutput.Text = "";
- lblSummaryVehiclesSalePriceOutput.Text = "";
- lblFinanceMonthlyPaymentOutput.Text = "";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement