Monday, September 21, 2015

Format Number As Money, How?

Sometimes we need convert entered numbers as money format. Thanks to javascript we can do it simultaneously. Firstly we define textbox to enter number, and give this textbox id to handle it in javascript code.

@Html.TextBoxFor(model => model.Para, new { @class = "form-control", 
@ID = "paraId", @Value = String.Format("{0:C}", Model.Para) }) 

We define id as paraId.Also if model is not null, we format it as money by using 'String.Format("{0:C}, Model.Para")' .

User enters number in textbox,it  triggers the javascript code.



 $(function () {
            $("#paraId").on("change", function (e) {

                var num = $('#paraId').val();
                num = num.toString().replace(/\$|\,/g, '');
                if (isNaN(num))
                    num = "0";
                sign = (num == (num = Math.abs(num)));
                num = Math.floor(num * 100 + 0.50000000001);
                cents = num % 100;
                num = Math.floor(num / 100).toString();
                if (cents < 10)
                    cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3) ; i++)
          num = num.substring(0, num.length - (4 * i + 3)) + '.' +
          num.substring(num.length - (4 * i + 3));
          document.getElementById("paraId").value = (((sign) ? '' : '-') + num + ',' + cents);

            });
        });


Firstly, we get entered value by  '$('#paraId').val()'. Then We use math methods abs, floor, After we find money format, We set value of result like 'document.getElementById("paraId").value = (((sign) ? '' : '-') + num + ',' + cents)'.





Sunday, July 12, 2015

Plotting a Chart of Numbers

We will design a program that asks user to enter 9 numbers, all less than 50, and plot a horizontal and vertical chart showing the values of these numbers. Also enter width value for row size.


User Enters 9 Number that less than 50  and Width


After entered these value. Our program looks like below.

Output of the program

Now shall we speak how to develope this algorithmç Firstly our program must read 9 numbers using scanf. The horizontal chart simply displays the value of each number followed by as many *' chars as the value of the number. The vertical chart displays the same information with '*' chars being printed vertically. The value of the number is printed at the bottom of the chart as illustrated.

The width of the charts can be 1,3,5 or 7.In this example 3 is chosen.

Firstly we should write main class.



int main()

{
 int i, max = 0, A[9], width; // Define variables
 printf("Enter 9 number (less than 50)");

 for (i = 0; i<9; i++) 
 {
            scanf("%d", &A[i]);   //Entered numbers assigned to Array

     if (A[i]>max) max = A[i];//store max value among the entered values
 }

 printf("\nEnter width(1,3,5 or 7):");
 scanf("%d", &width);  // read entered width value inside widht variable 
 printf("max is:%d\n", max);
 horizontal(max, A, width);  call method of horizontal with parameters

 vertical(max, A, width);    call vertical method
    getch(); // these for preventing to closing console
 return 0;
}


Now we develope Horizontal method . For parameters we send entered max value, Array of entered integers and width.





int horizontal(int n, int B[9], int w)
{
 int i, j, k, m, t;
 printf("HORIZONTAL CHART:\n", 0);
 for (i = 0; i<9; i++)
 {
 //root of chart
  printf("%7c", 0);//7 char for empty.
  printf("+");
  for (k = 0; k<n; k++)//for root..
  {
   printf("-");
  }
  printf("+\n");
 //end of root 
 
 //begin of body
  for (j = 0; j<w; j++)//loop according to width ( rows size)
   
   if (j == ((w + 1) / 2) - 1)//display entered value in middle of the begin of the shape 
   {
    printf("%4c", 0);
    printf("%3d", B[i]);
    printf("|");

   }
   else                   
   {
    printf("%7c", 0); // for 7 empty chars
    printf("|");
   }

   for (m = 0; m<B[i]; m++)  //loop according to entered numbers (9 integers ) and plot '*' char (column size)
   {
    printf("*");

   }
   for (t = B[i]; t<n; t++)
    printf(" "); // after '*' chars, write empyt char to end of chart
   printf("|");
   printf("\n");
  }
 //end of body 

 }
 
 ////begin floor
 printf("%7c", 0);
 printf("+");
 for (i = 0; i<n; i++)
 {
  printf("-");
 }
 printf("+\n");
 //end floor
 return(0);
}
Now you can build vertical chart yourself. Please review above algorithm and try to understand the logic. Good luck..

Wednesday, June 17, 2015

Shopping Cart with Form Web Application with Asp.Net (Part 1)

In this project, we will design a website that displays information and allow user to add products into shopping cart. We willl design four pages that hold product information.

Firstly we will add a class that's name is Product.cs.(Add New Item -> Class).This file contains information about the products.Let Visual Studio create the App_Code folder for Product.cs.

Define a constructor for the Product class  which takes all of its member variables as parameter.

Product class should be like below.


public class Product
{
    public int ProductID;
    public string Type;
    public string Vendor;
    public string Model;
    public string ImageUrl;

    public int globalvar;
    public  static ArrayList proCopy;
    public static int id;
    public Product(int ProductID, string Type, string Vendor, string Model, string ImageUrl)
    {
        this.ProductID = ProductID;
        this.Type = Type;
        this.Vendor = Vendor;
        this.Model = Model;
        this.ImageUrl = ImageUrl;
        
    }
    public Product()
    { 
    
    }

    public void AddIndex(int n)
    {
        id = n;
    
    }

    public int getIndex()
    {
        return id;
    }
    public void copyArray(System.Collections.ArrayList array)
    {
        proCopy = array;
        
    }
    public System.Collections.ArrayList getArray()
    {
        return proCopy;
    }
   
}
Design Default.aspx
In Default.aspx page check if the user logged in or not.We use cookie check.The cookie should contain only first name and last name of the user. If no such cookie is defined, then should be displayed a message then link to the Login.aspx.
Fig. 1 – Default.aspx when user is not logged in.


When Login button is clicked, first name and last name will be written into a cookie named UserInfo for one month and will redirect to the left of Default.aspx.
 In Login.aspx.cs click method.

 protected void btnLogin_Click(object sender, EventArgs e)
    {
        //Create new cookie
        HttpCookie cookie = new HttpCookie("UserInfo");
        
        // add name and lastname to the cookie
        cookie["UserInfo"] = txtFirstName.Text+" "+txtLastName.Text;
        if (txtFirstName.Text == null || txtLastName.Text == null)
        {
            //lblLogin.Text = "Please specify valid Name and LastName";
        }
        else
        {
          //Define cookie for one month

            cookie.Expires = DateTime.Now.AddDays(30);
            Response.Cookies.Add(cookie);

            Response.Redirect("Default.aspx");
        }
    }
In Default.aspx.cs file we get cookie data with Request.Cookies["UserInfo"]. If cookie is null, redirect to login page.If cookie store data of user info, allow the see product information.

 protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["UserInfo"];




        if (cookie == null )  // if no cookies
        {
            lblExistingCookie.Text = "You are not login  Please login ";
            hyperlink1.Visible = true;
            lnkBtnCart. Visible = false;
            btnLogout.Visible = false;
            linkSamC2.Visible = false;
            linkSamE2.Visible = false;
            linkToshiba.Visible = false;
            linkLenova.Visible = false;
            linkAcer.Visible = false;
            lblHyper.Visible = false;

        }
        else
        {
            
            //  lblExistingCookie.Visible = true;
            hyperlink1.Visible = false;
            lblExistingCookie.Text = "Welcome " + cookie["UserInfo"] + "!";
            lnkBtnCart. Visible = true;
            btnLogout.Visible = true;
            linkSamC2.Visible = true;
            linkSamE2.Visible = true;
            linkToshiba.Visible = true;
            linkLenova.Visible = true;
            linkAcer.Visible = true;
            lblHyper.Visible = true;

        }

        if (!Page.IsPostBack) // first load create five product
        {


            //create Product objects
            
            Product part1 = new Product(1,"Phone","Samsung","C270","samsung.jpg");
            Product part2 = new Product(2,"Phone","Samsung","E250i","samsungE2.jpg");
            Product part3 = new Product(3,"Laptop","Toshiba","L500","toshiba.jpg");
            Product part4 = new Product(4,"Laptop","Lenova","G550","lenova.jpg");
            Product part5 = new Product(5,"Netbook","Acer","FO200","acer.jpg");
        
            //Add objects to the session state

            Session["Product1"] = part1; 
            Session["Product2"] = part2;
            Session["Product3"] = part3;
            Session["Product4"] = part4;
            Session["Product5"] = part5;
        }
        if(btnLogout.Visible == true)
        {
            linkSamC2.Visible = true;
            linkSamE2.Visible = true;
            linkToshiba.Visible = true;
            linkLenova.Visible = true;
            linkAcer.Visible = true;
            lblHyper.Visible = true;

           
        }
    }
Default.aspx file when user entered with name and last name
We Design Login.aspx and Default.aspx page. Then we continue to design ProductInfo.aspx page that display information of product.Then design Cart.aspx. It will done for display all products that are added to shopping cart. See You..