0
votes

I need to make a phone bill program that would allow the user to calculate their phone bill. They would enter their minutes, texts and data allowance as well as how much they've used.

Each extra minute is 30p, each extra text is 10p and data used over the limit is £10.00, all added to the final cost.

The problem is that I cannot get the program to calculate the cost of each minute, I guess that after finding out how the minutes are calculated I will be able to do the same for the texts and the data.

Thanks for the help, much appreciated.

UI

This is the problem

Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub

Private Sub btn1TotalCost_Click(sender As Object, e As EventArgs) Handles btn1TotalCost.Click

    Dim MinutesAllowed As Integer
    Dim MinutesUsed As Integer
    Dim TextsAllowed As Integer
    Dim TextsUsed As Integer
    Dim DataAllowed As Integer
    Dim DataUsed As Integer
    Dim MinutesTotalCost As Double
    Dim TextsTotalCost As Double
    Dim DataTotalCost As Double
    Dim MonthlyCost As Double
    Dim TotalCost As Double

    MinutesAllowed = Val(txtBoxMinutesAllowed.Text)
    MinutesUsed = Val(txtBoxMinutesUsed.Text)
    TextsAllowed = Val(txtBoxTextsAllowed.Text)
    TextsUsed = Val(txtBoxTextsUsed.Text)
    DataAllowed = Val(txtBoxDataAllowed.Text)
    DataUsed = Val(txtBoxDataUsed.Text)
    MinutesTotalCost = Val(txtBoxTotalCost.Text)
    TextsTotalCost = Val(txtBoxTotalCost.Text)
    DataTotalCost = Val(txtBoxTotalCost.Text)
    TotalCost = Val(txtBoxTotalCost.Text)

    'Minutes
    MinutesAllowed = Integer.Parse(txtBoxMinutesAllowed.Text)
    MinutesUsed = Integer.Parse(txtBoxMinutesUsed.Text)
    MonthlyCost = Val(txtBoxMonthlyCost.Text)

    If MinutesAllowed >= MinutesUsed Then
        MinutesTotalCost = 0
    Else
        MinutesTotalCost = (MinutesUsed - MinutesAllowed) * 0.3
    End If
    txtBoxTotalCost.Text = CType(MinutesTotalCost + MonthlyCost, String)

    'Texts
    TextsAllowed = Integer.Parse(txtBoxTextsAllowed.Text)
    TextsUsed = Integer.Parse(txtBoxTextsUsed.Text)
    MonthlyCost = Val(txtBoxMonthlyCost.Text)

    If TextsAllowed >= TextsUsed Then
        TextsTotalCost = 0
    Else
        TextsTotalCost = (TextsUsed - TextsAllowed) * 0.15
    End If
    txtBoxTotalCost.Text = CType(TextsTotalCost + MonthlyCost, String)

    'Data
    DataAllowed = Integer.Parse(txtBoxDataAllowed.Text)
    DataUsed = Integer.Parse(txtBoxDataUsed.Text)
    MonthlyCost = Val(txtBoxMonthlyCost.Text)

    If DataAllowed >= DataUsed Then
        DataTotalCost = 0
    Else
        DataTotalCost = (DataUsed - DataAllowed) + 10.0
    End If
    txtBoxTotalCost.Text = CType(DataTotalCost + MonthlyCost, String)

End Sub

End Class

1
You should turn on Option Strict - you have a number of conversions going on. - Ňɏssa Pøngjǣrdenlarp
Done it now but thanks! - FutureProgrammer

1 Answers

1
votes
MinutesTotalCost = MinutesUsed - MinutesAllowed * 0.3

Multiplication and division operators have precedence over the addition and subtraction operators.
You need to add brackets to the calculation.

MinutesTotalCost = (MinutesUsed - MinutesAllowed) * 0.3

You also overwrite the result of the calculation here:

MinutesTotalCost = txtBoxTotalCost.Text

Edit: Working code for just the minutes

 Dim MinutesAllowed As Integer
 Dim MinutesUsed As Integer
 Dim MinutesTotalCost As Double
 Dim MonthlyCost As Double

 MinutesAllowed = Integer.Parse(txtBoxMinutesAllowed.Text)
 MinutesUsed = Integer.Parse(txtBoxMinutesUsed.Text)
 MonthlyCost = Val(txtBoxMonthlyCost.Text)

 If MinutesAllowed >= MinutesUsed Then
     MinutesTotalCost = 0
 Else
     MinutesTotalCost = (MinutesUsed - MinutesAllowed) * 0.3         
 End If
 txtBoxTotalCost.Text = CType(MinutesTotalCost + MonthlyCost, String)

Val() returns a double so you need to use Integer.Parse() for int.
Double needs to be converted to string for the textbox.

Edit 2:
Since MonthlyCostdoesn't change you need the line

MonthlyCost = Val(txtBoxMonthlyCost.Text)

only once at the beginning.
You are always overwriting the value of txtBoxTotalCost. Just make a single calculation with all costs i.e.

 txtBoxTotalCost.Text = CType(MinutesTotalCost + TextsTotalCost + DataTotalCost + MonthlyCost, String)