using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace CoffeeShop { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); //All dynamic text labels should be blank when the program loads resetAllLabels(); } //Resets all labels so they are blank when the program loads private void resetAllLabels() { lblSmallCost.Content = ""; lblMediumCost.Content = ""; lblLargeCost.Content = ""; lblTotalCostValue.Content = ""; lblTaxValue.Content = ""; lblGrandTotalValue.Content = ""; } //Set private variables for each coffee size's price private double smallCostPerCup = 2.25; private double mediumCostPerCup = 3.25; private double largeCostPerCup = 4.25; //Set private variable for tax rate private double tax = .095; //These methods handle the check boxes and activate the Quantity controls private void chkSmall_Checked(object sender, RoutedEventArgs e) { txtSmallQuantity.IsEnabled = true; txtSmallQuantity.Focus(); } private void chkSmall_Unchecked(object sender, RoutedEventArgs e) { txtSmallQuantity.IsEnabled = false; txtSmallQuantity.Text = ""; lblSmallCost.Content = ""; calculateGrandTotal(); } private void chkMedium_Checked(object sender, RoutedEventArgs e) { txtMediumQuantity.IsEnabled = true; txtMediumQuantity.Focus(); } private void chkMedium_Unchecked(object sender, RoutedEventArgs e) { txtMediumQuantity.IsEnabled = false; txtMediumQuantity.Text = ""; lblMediumCost.Content = ""; calculateGrandTotal(); } private void chkLarge_Checked(object sender, RoutedEventArgs e) { txtLargeQuantity.IsEnabled = true; txtLargeQuantity.Focus(); } private void chkLarge_Unchecked(object sender, RoutedEventArgs e) { txtLargeQuantity.IsEnabled = false; txtLargeQuantity.Text = ""; lblLargeCost.Content = ""; calculateGrandTotal(); } //Calculates cost based on input and creates a string to display public string calculateCost(double cost, int quantity) { double itemCost = cost * Convert.ToDouble(quantity); return String.Format("{0:c}", itemCost); } private void txtSmallQuantity_TextChanged(object sender, TextChangedEventArgs e) { if (txtSmallQuantity.Text == "") { lblSmallCost.Content = ""; calculateGrandTotal(); } else { try { int quantity = Convert.ToInt32(txtSmallQuantity.Text); lblSmallCost.Content = calculateCost(smallCostPerCup, quantity); calculateGrandTotal(); } catch { lblSmallCost.Content = ""; calculateGrandTotal(); } } } private void txtMediumQuantity_TextChanged(object sender, TextChangedEventArgs e) { if (txtMediumQuantity.Text == "") { lblMediumCost.Content = ""; calculateGrandTotal(); } else { try { int quantity = Convert.ToInt32(txtMediumQuantity.Text); lblMediumCost.Content = calculateCost(mediumCostPerCup, quantity); calculateGrandTotal(); } catch { lblMediumCost.Content = ""; calculateGrandTotal(); } } } private void txtLargeQuantity_TextChanged(object sender, TextChangedEventArgs e) { if (txtLargeQuantity.Text == "") { lblLargeCost.Content = ""; calculateGrandTotal(); } else { try { int quantity = Convert.ToInt32(txtLargeQuantity.Text); lblLargeCost.Content = calculateCost(largeCostPerCup, quantity); calculateGrandTotal(); } catch { lblLargeCost.Content = ""; calculateGrandTotal(); } } } //Calculate the total, tax, and grand total for display private void calculateGrandTotal() { //Initialize variables to hold cost numbers for calculation double smallCost; double mediumCost; double largeCost; //If there is no quantity entered set cost to 0 if (lblSmallCost.Content.ToString() == "") smallCost = 0; else smallCost = Convert.ToInt32(txtSmallQuantity.Text) * smallCostPerCup; if (lblMediumCost.Content.ToString() == "") mediumCost = 0; else mediumCost = Convert.ToInt32(txtMediumQuantity.Text) * mediumCostPerCup; if (lblLargeCost.Content.ToString() == "") largeCost = 0; else largeCost = Convert.ToInt32(txtLargeQuantity.Text) * largeCostPerCup; //Display total cost, remove the label if total cost is $0 double totalCost = smallCost + mediumCost + largeCost; if (totalCost == 0) lblTotalCostValue.Content = ""; else lblTotalCostValue.Content = String.Format("{0:c}", totalCost); //Display tax cost and rate and remove label if total cost is $0 string taxDisplay = String.Format("{0:c}", (totalCost * tax)) + " (" + Convert.ToString(tax * 100) + "%)"; if (totalCost == 0) lblTaxValue.Content = ""; else lblTaxValue.Content = taxDisplay; //Display the grand total and remove label is total cost is $0 double grandTotal = (totalCost * tax) + totalCost; if (totalCost == 0) lblGrandTotalValue.Content = ""; else lblGrandTotalValue.Content = String.Format("{0:c}", grandTotal); } } }