开发者

Need help in BAL Class logic

In my sales detail software i want to calculate TotalCost, Discount, NettotalCost. Once enter Quanity and rate from textbox, all the values should be filled automitically. In my program its able to show the TotalCost and NetTotal but not showing the Discount Value, its always showing 0 only. Here is my code, pls somebody modify it what's wrong here....

public class SalesEntity
{
    private string custid;
    private string custname;
    private string productname;
    private int quantity;
    private float rate;
    private float total;
    private float discount;
    private float NetTotal;

    public string CUSTOMERID
    {
        get
        {
            return custid;
        }
        set
        {
            custid = value;
        }
    }
    public string CUSTOMERNAME
    {
        get
 开发者_StackOverflow中文版       {
            return custname;
        }
        set
        {
            custname = value;
        }
    }
    public string PRODUCTNAME
    {
        get
        {
            return productname;
        }
        set
        {
            productname = value;
        }
    }
    public int QUANTITY
    {
        get
        {
            return quantity;
        }
        set
        {
            quantity = value;
        }
    }
    public float RATE
    {
        get
        {
            return rate;
        }
        set
        {
            rate = value;
        }
    }
    public float TOTAL
    {
        get
        {
            return total;
        }
        set
        {
            total = value; ;

        }
    }
    public float DISCOUNT
    {
        get
        {
            return discount;
        }
        set
        {
            discount = value;
        }
    }
    public float NETTOTAL
    {
        get
        {            
            return NetTotal;
        }
        set
        {
            NetTotal = value;
        }
    }
}
public class SalesBALManager
{  
    public SalesEntity Compute(SalesEntity salesEntity)
    {
        salesEntity.TOTAL = salesEntity.QUANTITY * salesEntity.RATE;
        salesEntity.DISCOUNT = (10 / 100 * salesEntity.TOTAL);
        salesEntity.NETTOTAL = salesEntity.TOTAL - salesEntity.DISCOUNT;
        return salesEntity;

    }
}
protected void TxtRate_TextChanged(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            SalesBALManager obj = new SalesBALManager();
            SalesEntity salesentity = new SalesEntity();

            salesentity.QUANTITY = Convert.ToInt32(TxtQuantity.Text);
            salesentity.RATE = Convert.ToInt32(TxtRate.Text);
            salesentity.CUSTOMERID = TxtCustId.Text;
            salesentity.CUSTOMERNAME = TxtCustName.Text;

            salesentity = obj.Compute(salesentity);

            TxtTotal.Text = salesentity.TOTAL.ToString();
            TxtDiscount.Text = salesentity.DISCOUNT.ToString();            
            TxtNetTotal.Text = salesentity.NETTOTAL.ToString();
        }

    }


You have at least two problems. First, you're using floats when you should be using decimal and second, you're using integer arithmetic when dividing 10 / 100. The result of this is zero when using integer arithmetic. I'd change the floats to decimals and specify 0.1M instead of 10/100. I'd also be more careful with my string formats so that the number of decimal points in the decimal numbers is fixed, e.g., discount.ToString( "0.00" ).


Suspect that 10 / 100 is being calculated using integer division, giving 0, then being cast for the multiplication. Why not just substitute 0.1?


The 10/100 is integer division which returns an integer of 0. Cast them as floats to perform a float division.

((float)10/(float)100)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜