Tuesday, January 11, 2011

Sample Code for Assignment 2

This sample program collects basic information about a computer sale, loads that information into a computer class, saves the computer class in a Session variable and then opens a second page, which calls the class from the Session variable and displays its contents.

Here is a picture of the form running:


Here is a picture of the second page:


Here is the source for Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Computer Sale</title>
<link href="Computer.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">

<h1>Select Your LapTop components</h1>
<p>Choose your size</p>
<asp:RadioButtonList ID="rdoSize" runat="server" cssclass="myList">
<asp:ListItem Value="400">15 Inch</asp:ListItem>
<asp:ListItem Value="500">17 Inch</asp:ListItem>
</asp:RadioButtonList>
<p>Choose Ram</p>
<asp:RadioButtonList ID="rdoRam" runat="server" CssClass="myList">
<asp:ListItem Value="100">3 gigabytes</asp:ListItem>
<asp:ListItem Value="150">4 gigabytes</asp:ListItem>
<asp:ListItem Value="250">8 gigabytes</asp:ListItem>
</asp:RadioButtonList>
<p>Choose Processor</p>
<asp:RadioButtonList ID="rdoProcessor" runat="server">
<asp:ListItem Value="100">I3</asp:ListItem>
<asp:ListItem Value="150">I5</asp:ListItem>
<asp:ListItem Value="200">I7</asp:ListItem>
</asp:RadioButtonList>

<br /><asp:Button ID="Button1" runat="server" Text="Submit"
onclick="Button1_Click" />
</form>
</body>
</html>

Here is the code behind for Default.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/*this is the code for the default aspx page. It gets the values from the radio buttons on the form and then initilizes the Computer class and assigns the selected values to the class properties. It saves the class to a session variable and then redirects the user to a second page*/

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
//this code executes when the button is clicked
//declare and instantiate the Computer class
Computer comp = new Computer();

//assign the selected values to class properties
comp.Size = rdoSize.SelectedItem.ToString();
comp.SizePrice = double.Parse(rdoSize.SelectedValue.ToString());
comp.Ram = rdoRam.SelectedItem.ToString();
comp.RamPrice = double.Parse(rdoRam.SelectedValue.ToString());
comp.Processor = rdoProcessor.SelectedItem.ToString();
comp.ProcessorPrice = double.Parse(rdoProcessor.SelectedValue.ToString());

//Save the object to a session variable
Session["myOrder"] = comp;

//redirect to the second page
Response.Redirect("finalize.aspx");

}
}

Here is the Computer.cs class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

///
/// Summary description for Computer
///

public class Computer
{
/*this class stores all the values associated
* with the purchased computer and a method
* to calculate the price*/

//empty constructor
public Computer()
{
//
// TODO: Add constructor logic here
//
}

//private fields and public properties
private string size;
public string Size
{
//get allows a user of the class to
// see the value
get { return size; }
//set allows a user of the class to
//change the value
set
{

size = value;
}
}
private double sizePrice;

public double SizePrice
{
get { return sizePrice; }
set { sizePrice = value; }
}


private string ram;

public string Ram
{
get { return ram; }
set { ram = value; }
}
private double ramPrice;

public double RamPrice
{
get { return ramPrice; }
set { ramPrice = value; }
}
private string processor;

public string Processor
{
get { return processor; }
set { processor = value; }
}
private double processorPrice;

public double ProcessorPrice
{
get { return processorPrice; }
set { processorPrice = value; }
}

//public method to calculate price
public double CalculatePrice()
{
//uses the properties rather than the
//private fields in case any validation
//was done in the property
return SizePrice + RamPrice + ProcessorPrice;
}
}

Here is the source for Finalize.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="finalize.aspx.cs" Inherits="finalize" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Your Order</h1>
<asp:Label ID="lblSize" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="lblRam" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="lblProcessor" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="lblPrice" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>

Here is the code behind for Finalize.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class finalize : System.Web.UI.Page
{
/*this class is attached to the second web page
* it uses the page load event to load the saved
* class from the session variable and populate
* the labels on the form with the class contents*/

protected void Page_Load(object sender, EventArgs e)
{
//if somebody accidentally navigates to this page
//without going through the form
//redirect them to the form on default.aspx
if ((Session["myOrder"] != null))
{
//declare an object of the computer type
//assign it the object stored in the session
//variable. It has to be cast to the type
//computer because it is stored simply as an
//object
Computer comp2 = (Computer)Session["myOrder"];
//assign values from the class properties
//to the text property of the labels
lblSize.Text = comp2.Size;
lblRam.Text = comp2.Ram;
lblProcessor.Text = comp2.Processor;
//call the calcualteprice method and
//assign its results to lblprice
lblPrice.Text = comp2.CalculatePrice().ToString("C");
}
else
{
Response.Redirect("Default.aspx");
}

}
}

Here is the Computer.css stylesheet
body
{
font-size:large;
}

h1
{
color:Navy;
}

.myList
{
color:Green;
}

No comments:

Post a Comment