工资核算程序未显示正确的金额结果

时间:2015-04-25 00:00:17

标签: vb.net

我正在制定薪资计划。第一个基类是奖励,该类应包含2个公共属性salesid和sales。  在类中包含默认构造函数和参数化构造函数。还包括一个使用公式sales * 0.05计算销售人员奖金的getbonus方法(函数)  创建一个名为Premiumbonus的派生类派生的clas getbonus mothod计算奖金如下sales0.05 +(sales-2500))。01还包括这个派生类的默认和参数化构造函数。  如果销售额超过2500,请使用此

我觉得我的数学已关闭有人可以请仔细检查一下吗?我需要一些建议,我也尝试调试和getbonus似乎它保持在0无论我输入什么数字但我得到计算框中的结果。  下面是代码

选项明确开启 选项严格打开 选项推断关闭

    ' base class
    Public Class Bonus
    Public Property SalesId As String
    Public Property Sales As Double

    Public Sub New()
    _Sales = 0
    _SalesId = String.Empty




    End Sub
   Public Sub New(ByVal dblB As Double,
    ByVal strId As String)

    _Sales = dblB
    _SalesId = strId
   End Sub

  Public Overridable Function GetBonus() As Double
    ' returns sales 
    Return _Sales * 0.05
   End Function
  End Class


  ' derived class
   Public Class PremiumBonus
    Inherits Bonus

    Public Sub New()
    MyBase.New()
   End Sub

   Public Sub New(ByVal dblB As Double,
    ByVal strId As String)
    MyBase.New(dblB, strId)
    End Sub

     Public Overrides Function GetBonus() As Double
    Return MyBase.GetBonus + (Sales - 2500) * 0.01
   End Function
  End Class



    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
    ' calculates and displays a bonus

    Dim myBonus As New Bonus
    Dim myPremiumBonus As New PremiumBonus
    Dim Sales As Double

    ' if the sales are over $2500, instantiate a PremiumBonus object
    ' and then calculate the bonus
    ' otherwise, instantiate a Bonus object and then calculate the bonus

    If Sales > 2500 Then
        Double.TryParse(txtSales.Text, myBonus.Sales)
        Sales = myBonus.GetBonus

    Else
        Double.TryParse(txtSales.Text, myPremiumBonus.Sales)
        Sales = myPremiumBonus.GetBonus
    End If

2 个答案:

答案 0 :(得分:0)

根据您的标题:

  

薪资计划未显示正确的金额结果

和部分说明:

  

getbonus似乎保持在0,无论我输入什么数字,但我在计算框中得到结果

我很确定你的问题是"无论输入如何,我的输出为零。出了什么问题?"

我在你的代码中看到了:

Dim Sales As Double
If Sales > 2500 Then
    Double.TryParse(txtSales.Text, myBonus.Sales)
    Sales = myBonus.GetBonus

您应该在评估之前考虑使用值填充Sales,并且在很多地方不使用相同的变量名称可能是一个好主意,因为它会使您的代码更多难以阅读。您可以尝试这样的事情:

Dim myNewBonus As Double
Dim Sales As Double
Double.TryParse(txtSales.Text, Sales)
If Sales > 2500 Then
    myBonus.Sales = Sales
    myNewBonus = myBonus.GetBonus()

如果仍然失败,请尝试使用调试器,或至少部分策略性地放置MessageBox.Show(someValueIShouldCheck.ToString)

答案 1 :(得分:0)

非常感谢我修好了我在错误的地方获得了奖金

 Public Class Bonus
Public Property SalesId As String
Public Property Sales As Double

Public Sub New()
    _Sales = 0
    _SalesId = String.Empty




End Sub
Public Sub New(ByVal dblB As Double,
 ByVal strId As String)

    _Sales = dblB
    _SalesId = strId
  End Sub

Public Overridable Function GetBonus() As Double
    ' returns sales 
    Return _Sales * 0.05
   End Function
 End Class


 ' derived class
 Public Class PremiumBonus
Inherits Bonus

Public Sub New()
    MyBase.New()
End Sub

Public Sub New(ByVal dblB As Double,
  ByVal strId As String)
    MyBase.New(dblB, strId)
End Sub

Public Overrides Function GetBonus() As Double
    Return MyBase.GetBonus + (Sales - 2500) * 0.01
End Function
 End Class

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
    ' calculates and displays a bonus

    Dim myBonus As New Bonus
    Dim myPremiumBonus As New PremiumBonus
    Dim Sales As Double

    ' if the sales are over $2500, instantiate a PremiumBonus object
    ' and then calculate the bonus
    ' otherwise, instantiate a Bonus object and then calculate the bonus

    If Val(txtSales.Text) > 2500 Then
        Double.TryParse(txtSales.Text, myPremiumBonus.Sales)
        Sales = myPremiumBonus.GetBonus

    Else
        Double.TryParse(txtSales.Text, myBonus.Sales)
        Sales = myBonus.GetBonus()
    End If