Thursday, November 4, 2010

Code for Tip calculator


Xaml

<Window x:Class="TipCalculatorMark2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="417" Width="525">
<Grid Height="346">
<Label Content="Enter the total meal amount" Height="28" HorizontalAlignment="Left" Margin="50,30,0,0" Name="label1" VerticalAlignment="Top" FontSize="16" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="309,36,0,0" Name="txtMeal" VerticalAlignment="Top" Width="120" Background="#FF1CE9BB" FontSize="16" />
<RadioButton Content="10%" Height="16" HorizontalAlignment="Left" Margin="72,93,0,0" Name="rdoTenPercent" VerticalAlignment="Top" FontSize="16" />
<RadioButton Content="15%" Height="16" HorizontalAlignment="Left" Margin="72,129,0,0" Name="rdoFifteen" VerticalAlignment="Top" FontSize="16" />
<RadioButton Content="20%" Height="16" HorizontalAlignment="Left" Margin="72,167,0,0" Name="rdoTwenty" VerticalAlignment="Top" FontSize="16" />
<RadioButton Content="Other" Height="16" HorizontalAlignment="Left" Margin="72,207,0,0" Name="rdoOther" VerticalAlignment="Top" FontSize="16" />
<TextBox Background="#FF1CE9BB" FontSize="16" Height="23" HorizontalAlignment="Left" Margin="163,207,0,0" Name="txtOther" VerticalAlignment="Top" Width="120" />
<Button Content="GetTip" Height="23" HorizontalAlignment="Left" Margin="63,0,0,41" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click" />
<Label Content="Label" Height="103" HorizontalAlignment="Left" Margin="163,243,0,0" Name="lblResults" VerticalAlignment="Top" Width="276" FontSize="16"/>
</Grid>
</Window>

mainwindow code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 TipCalculatorMark2
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
//Get input
double tipChoice = 0;
double totalMeal = 0;
bool test = double.TryParse(txtMeal.Text, out totalMeal);

if (test == false)
{
MessageBox.Show("Enter a valid number. No $ sign.");
txtMeal.Clear();
return;

}

if (rdoTenPercent.IsChecked==true)
{
tipChoice = .10;
}
if (rdoFifteen.IsChecked == true)
{
tipChoice = .15;
}
if (rdoTwenty.IsChecked == true)
{
tipChoice = .2;
}

if (rdoOther.IsChecked==true)
{
bool test2 = double.TryParse(txtOther.Text, out tipChoice);

if (test2 == false)
{
MessageBox.Show("Enter a valid percentage, no % sign.");
txtOther.Clear();
return;
}
}
Tip t = new Tip(totalMeal, tipChoice);
lblResults.Content = "The Tax on the meal is : "
+ t.CalculateTax().ToString("c") + "\n" +
"the Tip amount is: " + t.CalculateTip().ToString("c")
+ "\nThe total due is: " + t.CalculateTotal().ToString("c");


}
}
}

Tip Class

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

namespace TipCalculatorMark2
{
class Tip
{
//private fields
private double mealAmount;
private double tipPercent;
private const double TAXRATE = .09;

//constructors
public Tip()
{
MealAmount = 0;
TipPercent = 0;
}

public Tip(double total, double percent)
{
MealAmount = total;
TipPercent = percent;

}

//public properties
public double MealAmount
{
get { return mealAmount; }
set { mealAmount = value; }

}

public double TipPercent
{
get { return tipPercent; }
set
{
if (value >= 1)
{
tipPercent = value / 100;
}
else
{
tipPercent = value;
}
}
}

//public methods
public double CalculateTax()
{
return MealAmount * TAXRATE;
}

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

public double CalculateTotal()
{
return MealAmount + CalculateTip() + CalculateTax();
}

}
}

No comments:

Post a Comment