Tuesday, December 8, 2015

WPF Form (Assignment 12)

Here is XAML

<Window x:Class="Assignment12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Assignment12"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label x:Name="label" Content="Enter the base Amount" HorizontalAlignment="Left" Margin="49,0,0,0" VerticalAlignment="Top" Height="27" Width="194"/>
        <TextBox x:Name="txtAmount" HorizontalAlignment="Left" Height="23" Margin="210,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="134"/>
        <RadioButton x:Name="rdbTenPercent" Content="Ten Percent" HorizontalAlignment="Left" Margin="63,45,0,0" VerticalAlignment="Top"/>
        <RadioButton x:Name="rdbFifteenPercent" Content="fifteen Percent" HorizontalAlignment="Left" Margin="63,65,0,0" VerticalAlignment="Top"/>
        <RadioButton x:Name="rdbTwentyPercent" Content="Twenty Percent" HorizontalAlignment="Left" Margin="63,85,0,0" VerticalAlignment="Top"/>
        <RadioButton x:Name="rdbOther" Content="Other" HorizontalAlignment="Left" Margin="63,105,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtOther" HorizontalAlignment="Left" Height="23" Margin="210,103,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="btnCalculate" Content="Calculate" HorizontalAlignment="Left" Margin="63,141,0,0" VerticalAlignment="Top" Width="75" Click="btnCalculate_Click"/>
        <Label x:Name="lblResult" Content="Label" HorizontalAlignment="Left" Margin="210,126,0,0" VerticalAlignment="Top" Height="97" Width="154"/>
        <Button x:Name="btnClear" Content="Clear" HorizontalAlignment="Left" Margin="63,175,0,0" VerticalAlignment="Top" Width="75" Click="btnClear_Click"/>
        <Button x:Name="btnExit" Content="Exit" HorizontalAlignment="Left" Margin="63,208,0,0" VerticalAlignment="Top" Width="75" Click="btnExit_Click"/>

    </Grid>
</Window>

Here is the Tip Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment12
{
    class Tip
    {
       public double Amount { get; set; }
        public double TipPercent { get; set; }

        private const double TAX = .092;

        public double CalculateTip()
        {
            return Amount * TipPercent;
        }

        public double CalculateTax()
        {
            return Amount * TAX;
        }

        public double CalculateTotal()
        {
            return Amount + (Amount * TipPercent)
                + (Amount * TAX);
        }

        public override string ToString()
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Amount: " + Amount.ToString("$#,###.00"));
            sb.AppendLine("Tip: " + CalculateTip().ToString("$#,###.00"));
            sb.AppendLine("Tax: " + CalculateTax().ToString("$#,###.00"));
            sb.AppendLine("Total: " + CalculateTotal().ToString("$#,###.00"));
            return sb.ToString();
        }
    }
}

Here is the code behind the Form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Assignment12
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, RoutedEventArgs e)
        {
            Calculate();
        }

        private void Calculate()
        {
            Tip t = new Tip();
            double amount;
            bool goodAmount = double.TryParse(txtAmount.Text, out amount);
            if (goodAmount)
            {
                t.Amount = amount;
            }
            else
            {
                MessageBox.Show("Enter a valid Amount");
                txtAmount.Clear();
                txtAmount.Focus();
                return;
            }
            t.TipPercent = GetTipPercent();

            lblResult.Content = t.ToString();

        }

        private double GetTipPercent()
        {
            double tipPercent = 0;
            if (rdbTenPercent.IsChecked == true)
                tipPercent = .1;
            if (rdbFifteenPercent.IsChecked == true)
                tipPercent = .15;
            if (rdbTwentyPercent.IsChecked == true)
                tipPercent = .2;
            if (rdbOther.IsChecked == true)
            {
                bool goodPercent = double.TryParse(txtOther.Text, out tipPercent);
                if (tipPercent > 1)
                    tipPercent /= 100;
            }
            return tipPercent;

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            Clear();
        }

        private void Clear()
        {
            txtAmount.Clear();
            txtOther.Clear();
            rdbTenPercent.IsChecked = false;
            rdbFifteenPercent.IsChecked = false;
            rdbTwentyPercent.IsChecked = false;
            rdbOther.IsChecked = false;
            lblResult.Content = "";
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            //this.Close();
            Application.Current.Shutdown();
        }
    }
}

No comments:

Post a Comment