Thursday, January 9, 2014

Assignment1

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="AssignmentOneStyle.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Tip Calculator</h1>
        <p>
            <asp:Label ID="Label1" runat="server" Text="Enter the amount">
            </asp:Label> 
            <asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>

        </p>
        <asp:RadioButtonList ID="rdbPercentage" runat="server">
            <asp:ListItem Text="10%" Value=".1" />
            <asp:ListItem Text="15%" Value=".15" Selected="true"></asp:ListItem>
            <asp:ListItem Text="20%" Value=".2"></asp:ListItem>
            <asp:ListItem Text="other" Value="0"></asp:ListItem>
        </asp:RadioButtonList> 
        <p><asp:TextBox ID="txtOther" runat="server" placeholder="other"></asp:TextBox></p>
        <asp:Button ID="btnCalculate" runat="server" Text="Submit" OnClick="btnCalculate_Click" />
        <p>
            <asp:Label ID="lblTip" runat="server" Text="" CssClass="result"></asp:Label><br />
            <asp:Label ID="lblTotal" runat="server" Text="" CssClass="result"></asp:Label>
        </p>
    </div>
    </form>
</body>
</html>

   

Default.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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        double amount = 0;
        bool goodAmount = double.TryParse(txtAmount.Text, out amount);
        if (!goodAmount)
        {
            Response.Write("<script>alert('Please enter a valid amount');</script>");
            txtAmount.Text = "";
            txtAmount.Focus();
            return;
        }
        double percent=0;
        if(rdbPercentage.SelectedItem.Text != "other")
        { 
            percent = double.Parse(rdbPercentage.SelectedValue.ToString());
        }
        else
        {
            bool goodPercent = double.TryParse(txtOther.Text, out percent);
            if (!goodPercent)
            {
                Response.Write("<script>alert('Please enter a valid Percentage');</script>");
                txtOther.Text = "";
                txtOther.Focus();
                return;
            }
        }
        TipCalculator Tip = new TipCalculator(amount, percent);
        lblTip.Text = Tip.CalculateTip().ToString("$##0.##");
        lblTotal.Text = Tip.CalculateTotal().ToString("$##0.##");
    }
}

CalculateTip.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// Summary description for TipCalculator
/// 
public class TipCalculator
{

    private double amount;
    private double percent;
 public TipCalculator()
 {
        Amount = 0;
        Percent = 0;
 }
    public TipCalculator(double amt, double perc)
    {
        Amount = amt;
        Percent = perc;
    }


    public double Amount
    {
        set { amount = value; }
        get { return amount; }
    }

    public double Percent
    {
        set
        {
            if (value > 1)
            {
                value /= 100;
            }
            percent = value;
        }
        get
        {
            return percent;
        }
    }

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

    public double CalculateTotal()
    {
        return Amount + CalculateTip();
    }
}

AssignmentOne.css

body {
}
h1
{
    background-color:navy;
    color:white;
    border-bottom:5px solid black;
}

.result{
    color:green;
}

No comments:

Post a Comment