需要帮助BAL类逻辑

时间:2010-02-02 12:26:33

标签: c# user-interface

在我的销售详细信息软件中,我想计算TotalCost,Discount,NettotalCost。从文本框输入Quanity和rate后,所有值都应自动填充。在我的程序中,它能够显示TotalCost和NetTotal但不显示折扣值,它始终只显示0。这是我的代码,请有人在这里修改它有什么问题....

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
        {
            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();
        }

    }

3 个答案:

答案 0 :(得分:2)

你至少有两个问题。首先,当你应该使用小数时,你使用浮点数,第二,你在划分10 / 100时使用整数运算。使用整数运算时,结果为零。我将浮点数更改为小数,并指定0.1M而不是10/100。我对字符串格式也要更加小心,以便十进制数中的小数点数是固定的,例如discount.ToString( "0.00" )

答案 1 :(得分:1)

怀疑正在使用整数除法计算10 / 100,给出0,然后进行乘法运算。为什么不替换0.1

答案 2 :(得分:1)

10/100是整数除法,它返回0的整数。将它们作为浮点运行以执行浮动除法。

((float)10/(float)100)