0

Hi Ive been assigned a homework assignment to create the following program: The Cell Phone Company, Cell4U, determines their cell phone costs as follows: A client pays R2.05 for full minute calls and 2c for each extra second. They also pay 40c per SMS, but a client gets 2 free SMS’s for every full 30 call minutes. A client also gets 1 SMS free for every 30 SMS’s sent.

All employees who work for the company get a free cell phone as a perk and their account is also paid to a maximum of R800 per month. The company requires a monthly report of the cell phone bills for all employees as well as the total amount that they spend on cell phone perks for employees.

The program must provide for the following:

• The amount due for the calls and the amount due for the sms-messages must be calculated in a sub procedure. This will necessarily imply that the sub procedure must convert the seconds to minutes and seconds and that these values as well as the number of free sms’s also are made available to the main procedure. • A second sub procedure must be called to calculate the total bill for the employee and to accumulate the total that the company needs to pay out for cell phone perks. This sub procedure must also use a Boolean parameter to indicate to the main procedure whether the company will pay out the full amount for this employee or only R800. • All output must be displayed in the main procedure (button click event). • Any number of employees. An empty employee number will indicate the end of the input (or the user can click on the cancel-button in the input box). When the last data has been entered, the total amount must be displayed as indicated in figure 5. Remember, if an account is more than R800, the company only contributes R800 towards the bill and only R800 must be reflected in the total.

I have written the following but code but returns me with zero values

Option Strict On
Option Explicit On

Public Class TheCellPhoneCompany

Private Sub CalcMinAndSec(ByVal intLengthCallInSec As Integer, ByVal intCallSec As Integer, ByVal intCallMin As Integer, ByVal intNumMsgs As Integer, ByVal intNumFreeMsgs As Integer, ByVal decCallCost As Decimal, _
                          ByVal decSmsCost As Decimal, ByVal RatePerMin As Decimal, ByVal RatePerSec As Decimal, ByVal RatePerSms As Decimal)

    intCallMin = (intLengthCallInSec \ 60I)
    intCallSec = ((intLengthCallInSec Mod 60I))
    intNumFreeMsgs = ((intCallMin - (intCallMin Mod 30)) \ 15) + ((intNumMsgs - (intNumMsgs Mod 30I) \ 30I))
    decCallCost = (intCallMin * RatePerMin) + (intCallSec * RatePerSec)
    decSmsCost = (intNumMsgs - intNumFreeMsgs) * RatePerSms

End Sub

Private Sub CalcTotals(ByVal decCallCost As Decimal, ByVal decSmsCost As Decimal, ByVal decTotCost As Decimal, ByVal decAmtPaid As Decimal, ByVal decFinalAmtPaid As Decimal, ByVal PaidByComp As Decimal)

    Dim blnPaidByComp As Boolean = decTotCost > 800I

    decTotCost = decCallCost + decSmsCost
    If blnPaidByComp = True Then
        decAmtPaid = PaidByComp
    Else
        decAmtPaid = decTotCost
    End If

    decFinalAmtPaid += decAmtPaid

End Sub

Private Sub btnEnterData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterData.Click

    Dim strEmpCode, strLengthCallInSec, strNumOfSms As String
    Dim intNumMsgs, intNumFreeMsgs, intCallMin, intCallSec, intEmpCode, intLengthCallInSec As Integer
    Dim decCallCost, decSmsCost, decTotCost, decAmtPaid, decFinalAmtPaid As Decimal
    Dim intEmpNum As Integer = 1

    Const RatePerMin As Decimal = 2.05D
    Const RatePerSec As Decimal = 0.02D
    Const RatePerSms As Decimal = 0.4D
    Const PaidByComp As Decimal = 800D

    Do

        strEmpCode = InputBox("Please enter employee number", "Employee " & intEmpNum)
        Integer.TryParse(strEmpCode, intEmpCode)
        If intEmpCode <> 0 Then


            strLengthCallInSec = InputBox("Enter total calls for the month in seconds", "Employee " & intEmpNum)
            Integer.TryParse(strLengthCallInSec, intLengthCallInSec)

            If intLengthCallInSec > 0 Then
                strNumOfSms = InputBox("Enter total number of SMS's sent for the month", "Employee " & intEmpNum)
                Integer.TryParse(strNumOfSms, intNumMsgs)

                If intNumMsgs > 0 Then

                Else
                    MessageBox.Show("Invalid value - the number of SMS's must be a positive integer", "Error - please correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                End If

            Else
                MessageBox.Show("Invalid value - the seconds must be a positive integer", "Error - please correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
            End If

        Else
        End If

        Call CalcMinAndSec(intLengthCallInSec, intCallSec, intCallMin, intNumMsgs, intNumFreeMsgs, decCallCost, decSmsCost, RatePerMin, RatePerSec, RatePerSms)

        Call CalcTotals(decCallCost, decSmsCost, decTotCost, decAmtPaid, decFinalAmtPaid, PaidByComp)

        lstCellPhones.Items.Add("Employee: " & intEmpCode)
        lstCellPhones.Items.Add("Calls: " & intCallMin & " minutes and " & intCallSec & " seconds")
        lstCellPhones.Items.Add("Number of SMS messages: " & intNumMsgs & " (" & intNumFreeMsgs & ")")
        lstCellPhones.Items.Add("Cost for calls: R" & decCallCost.ToString("N2"))
        lstCellPhones.Items.Add("Cost for sms messages: R" & decSmsCost.ToString("N2"))
        lstCellPhones.Items.Add("Total cost: R" & decTotCost.ToString("N2"))
        lstCellPhones.Items.Add("Paid by company: R" & decAmtPaid.ToString("N2"))
        lstCellPhones.Items.Add("")

        intEmpNum = intEmpNum + 1


    Loop Until intEmpCode = 0

    lstCellPhones.Items.Add("Total amount paid by company for cell phones = R" & decFinalAmtPaid.ToString("N2"))




End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    lstCellPhones.Items.Clear()

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

    Me.Close()

End Sub
End Class
4

1 に答える 1

4

1) 値を変更するためにサブルーチンに値を渡すことは通常ではありません。代わりに、変数のスコープを変更して、decCallCostすべてのコードに「見える」ようにすると、それを機能させる方法になります。

2) オン・ライン

decSmsCost = (intNumMsgs - intNumFreeMsgs) * RatePerSms

intNumFreeMsgs > intNumMsgs の場合はどうなりますか?

3)通常はそうしませんが、これは宿題であるため、これは悪い考えですが、パラメーターとして渡された変数をサブルーチンに変更できます。

Module Module1

    Sub x(ByRef n As Integer)
        n = 5
    End Sub

    Sub Main()
        Dim a As Integer = 1
        x(a)
        Console.WriteLine(a) ' outputs 5
        Console.ReadLine()
    End Sub

End Module

どこかでそれを見て、何が起こっているのか疑問に思った場合に備えて。

于 2012-09-13T18:03:28.760 に答える