Monday, November 1, 2010

Package Class



Window1.xaml
<Window x:Class="PackageCalculator.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="377">
<Grid Height="268">
<Label Height="28" HorizontalAlignment="Left" Margin="20,31,0,0" Name="label1" VerticalAlignment="Top" Width="120">Enter the Weight</Label>
<TextBox Height="23" Margin="0,31,99,0" Name="txtWeight" VerticalAlignment="Top" HorizontalAlignment="Right" Width="120" />
<RadioButton Height="16" HorizontalAlignment="Left" Margin="48,87,0,0" Name="rdoOvernight" VerticalAlignment="Top" Width="120" >Overnight</RadioButton>
<RadioButton HorizontalAlignment="Left" Margin="48,112,0,0" Name="rdoTwoDay" Width="120" Height="16" VerticalAlignment="Top">Two Day</RadioButton>
<RadioButton Height="16" HorizontalAlignment="Left" Margin="50,0,0,106" Name="rdoGround" VerticalAlignment="Bottom" Width="120">Ground</RadioButton>
<Button Height="23" Margin="136,0,0,71" Name="btnShipping" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="75" Click="btnShipping_Click">Get Shipping</Button>
<Label Height="28" Margin="42,0,73,18" Name="lblShippingPrice" VerticalAlignment="Bottom">Label</Label>
</Grid>
</Window>

Here is the Code for Windows1.xaml.cs

using System;
using System.Collections.Generic;
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 PackageCalculator
{
///
/// Interaction logic for Window1.xaml
///

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

private void btnShipping_Click(object sender, RoutedEventArgs e)
{
//get the weight from textbox
double weight = double.Parse(txtWeight.Text);
//initilize the shipmethod variable
string shipMethod = null;
//checking to see which shipping method
//they have selected
if (rdoOvernight.IsChecked == true)
{
shipMethod = rdoOvernight.Content.ToString();
}

if (rdoTwoDay.IsChecked == true)
{
shipMethod = rdoTwoDay.Content.ToString();
}

if (rdoGround.IsChecked == true)
{
shipMethod = rdoGround.Content.ToString();
}

//initialize the Package class with the
//second constructor, passing it the values
Package pack = new Package(weight, shipMethod);
//call the CalculateShippingPrice of the class
//and store the result returned in the variable price
double price = pack.CalculateShippingPrice();
//display the results in the label on the form

lblShippingPrice.Content = "the shipping price is " + price.ToString("c");
}


}
}
Here is the package class

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

namespace PackageCalculator
{
class Package
{

//fields are class level variables
//that describe the class
//private by default, but stating it
//makes it obvious
private double weightInOunces;
private string shippingMethod;
private double shippingPrice;

//default constructor
public Package()
{
WeightInOunces = 0;
ShippingMethod = null;
ShippingPrice = 0;
}

//second constructor
public Package(double weight, string method)
{
WeightInOunces = weight;
ShippingMethod = method;
ShippingPrice = 0;
}


//public properties expose your
//private variables
//you control how they are exposed
//properties don't have ()
//they don't take arguments

public double WeightInOunces
{
set { weightInOunces = value; }
get { return weightInOunces; }
}//end property

public string ShippingMethod
{
set { shippingMethod = value; }
get { return shippingMethod; }
}//end property

public double ShippingPrice
{
set { shippingPrice = value; }
get { return shippingPrice; }
}

//this is a method to calculate shiping price
//it must have a parenthesis
//even if it doesn't have arguments
public double CalculateShippingPrice()
{
if (WeightInOunces <= 8)
{
ShippingPrice = 1;
}
else if (WeightInOunces <= 16)
{
ShippingPrice = 2;
}
else if (WeightInOunces <= 32)
{
ShippingPrice = 5;
}
else
{
ShippingPrice = 10;
}

if (ShippingMethod == "Overnight")
{
//*= means the same as
//ShippingPrice=ShippingPrice * 3
ShippingPrice *= 3;
}

if (ShippingMethod == "Two Day")
{
ShippingPrice *= 2;
}

return ShippingPrice;
}

} //end class
}//end namespace

No comments:

Post a Comment