none
Program that calculates total books along side with voucher and discountrate RRS feed

  • Question

  • I need help with my program. it needs the title, cost 20.99, voucher 30, discountrate.

    For example:

    If books <=3 or Books >=10 Then

    Discountrate- 0.05

    And i also need:

    If voucher="Yes" or"yes" Then

    Voucher needs to be applied with $30

    and if not then just the total.

    I tried to do that but it kept giving me $0.0 in my final total

    Thank you

    Tuesday, February 6, 2018 11:43 AM

Answers

  • Moderator note: always post code in a code block as done below.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Dim Title As String
            Dim Books As Integer
            Dim Costperbook As Double
            Dim Totalcost As Double
            Dim Voucher As String
            Dim Discount As Single
            Dim DiscountRate As Double
    
    
    
            Title = txtTitle.Text
            Books = txtBooks.Text
            DiscountRate = Totalcost - (Totalcost * 0.05)
            DiscountRate = Totalcost - (Totalcost * 0.75)
            DiscountRate = Totalcost - (Totalcost * 0.1)
    
            Totalcost = Books * 15.99
            txtTotal.Text = Total
            Costperbook = 15.99
            Voucher = 25
            txtTitle.Text = InputBox("Please Enter your book name")
            txtBooks.Text = InputBox("Please Enter Amount you'd like to buy")
            txtCostperbook.Text = InputBox("Please Enter the cost of the book", & 15.99 &)
            txtVoucher.Text = InputBox("Please Say yes or no")
    
            If Voucher = "yes" Then
                Totalcost = ((Costperbook * Books) - DiscountRate - Voucher)
                txtVoucher.Text = Format(Voucher, "Currency")
            End If
    
            If Voucher = "no" Then
                    Totalcost = Books * Costperbook
                    txtVoucher.Text = Format(Voucher, "Currency")
                End If
    
                If Books <= 5 Or Books >= 10 Then
                    DiscountRate = 0.05
                End If
    
                If Books <= 11 Or Books >= 50 Then
                    DiscountRate = 0.075
                End If
    
                If Books <= 51 Or Books >= 80 Then
                    DiscountRate = 0.1
                End If
                Books = Inputbox("You cannot order less than 1 book or  more than 80", "Enter Amount")
                Do While Books <= 1 Or Books >= 80
                    MsgBox("Your entry is out of range please choose amount from 1-80")
                    Books = Inputbox("You cannot order less than 1 book or  more than 80", "Enter Amount")
                Loop
                txtBooks.Text = Books
    
    
    
        End Sub
    
    End Class

    Tuesday, February 6, 2018 1:12 PM
  • Hi

    Here is some code that *may* help. It only uses the default control names rather than what I am assuming is your control names. There is no error checking.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Dim s As String = NumericUpDown1.Value.ToString & " of " & TextBox1.Text & vbCrLf
    	Dim tc As Double = (CDbl(TextBox2.Text) * NumericUpDown1.Value) / 100 * (100 - NumericUpDown2.Value)
    	If CheckBox1.Checked Then tc -= 30
    
    	s &= "Discount = " & NumericUpDown2.Value.ToString & "%" & vbCrLf
    	s &= "Voucher = "
    
    	If CheckBox1.Checked Then
    	  s &= "  Yes" & vbCrLf
    	Else
    	  s &= "  No" & vbCrLf
    	End If
    	s &= tc.ToString("C")
    	MessageBox.Show(s, "Order")
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Maniiii Sunday, February 11, 2018 1:10 AM
    Tuesday, February 6, 2018 2:45 PM
  • Hi

    Here is some code that *may* help. It only uses the default control names rather than what I am assuming is your control names. There is no error checking.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Dim s As String = NumericUpDown1.Value.ToString & " of " & TextBox1.Text & vbCrLf
    	Dim tc As Double = (CDbl(TextBox2.Text) * NumericUpDown1.Value) / 100 * (100 - NumericUpDown2.Value)
    	If CheckBox1.Checked Then tc -= 30
    
    	s &= "Discount = " & NumericUpDown2.Value.ToString & "%" & vbCrLf
    	s &= "Voucher = "
    
    	If CheckBox1.Checked Then
    	  s &= "  Yes" & vbCrLf
    	Else
    	  s &= "  No" & vbCrLf
    	End If
    	s &= tc.ToString("C")
    	MessageBox.Show(s, "Order")
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, February 6, 2018 2:48 PM
    • Marked as answer by Maniiii Sunday, February 11, 2018 1:10 AM
    Tuesday, February 6, 2018 2:45 PM
  • Hi

    Sorry for delay. Forum is acting up as though monkeys were running it.

    *

    Here is adjusted code to try out. It uses the 3 discount rates from your code. Again, no error checking. For example, 1 book cost 10 voucher bings it down so you pay customer for buying the book.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Dim s As String = NumericUpDown1.Value.ToString & " of " & TextBox1.Text & vbCrLf
    
    	Dim tc As Double = (CDbl(TextBox2.Text) * NumericUpDown1.Value) * (1 - DiscountRate)
    	If CheckBox1.Checked Then tc -= 25
    
    	s &= "Discount = " & Label1.Text & "% " & vbCrLf
    	s &= "Voucher = "
    
    	If CheckBox1.Checked Then
    	  s &= "  Yes" & vbCrLf
    	Else
    	  s &= "  No" & vbCrLf
    	End If
    	s &= "Total:  " & tc.ToString("C")
    	MessageBox.Show(s, "Order")
      End Sub
      Dim DiscountRate As Decimal = 0D
      Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    	DiscountRate = 0D
    	Dim v As Decimal = NumericUpDown1.Value
    	If v > 4 AndAlso v < 11 Then DiscountRate = 0.05D
    	If v > 10 AndAlso v < 51 Then DiscountRate = 0.075D
    	If v > 50 AndAlso v < 81 Then DiscountRate = 0.1D
    	If v > 80 Then
    	  MessageBox.Show("Maximum Books is 80")
    	End If
    	Label1.Text = DiscountRate.ToString("#.0%")
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Maniiii Sunday, February 11, 2018 1:10 AM
    Tuesday, February 6, 2018 5:33 PM
  • Hi Maniiii,

    I guess you would be a student and it would be a school assignment.

    Putting that aside, I'm afraid you forget other conditions.
    What is the discount rate, when a user types more than 10 books?

    This is my sample:
         
    [Designer: for showing control name]
         
    [Code]
    ' --- Variables ---
    Private IsIgnoreEvent As Boolean = True
    ' --- Form: Load
    Private Sub frm_Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.lbl_DiscountRate.Text = ""
        Me.lbl_Total_without_Discount.Text = ""
        Me.lbl_Total_with_Discount.Text = ""
        Me.lbl_Voucher.Text = ""
    End Sub
    ' --- after Form shown
    Private Sub frm_Main_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        Me.IsIgnoreEvent = False
    End Sub
    ' ---
    Private Sub Value_Changed(sender As Object, e As EventArgs) Handles _
        txt_Price.TextChanged, nud_Quantity.TextChanged, chk_Voucher.CheckedChanged
        ' ---
        If (Me.IsIgnoreEvent = False) Then
            If (Me.txt_Price.Text = "") Then
                Me.txt_Price.Text = "1"
            End If
            Call Me.prc_Compute()
        End If
    End Sub
    ' ---
    Private Sub prc_Compute()
        ' --- Discount Rate
        Dim discountRate As Single
        Select Case Me.nud_Quantity.Value
            Case < 5    ' -- 1 ~ 4
                discountRate = 0.0
            Case <= 10  ' -- 5 ~ 10
                discountRate = 0.075
            Case <= 20  ' -- 11 ~ 20
                discountRate = 0.1
            Case <= 50  ' -- 21 ~ 50
                discountRate = 0.2
            Case Else   ' -- over 51
                discountRate = 0.3
        End Select
        Me.lbl_DiscountRate.Text = Format(discountRate, "0.0 %")
        ' --- Voucher
        Dim voucher As Single
        If (Me.chk_Voucher.Checked) Then
            voucher = 25.0
        Else
            voucher = 0.0
        End If
        Me.lbl_Voucher.Text = Format(voucher, "0.00")
        ' --- Total
        Me.lbl_Total_without_Discount.Text =
            Format(CSng(Me.txt_Price.Text) * Me.nud_Quantity.Value, "#,0.00")
        Me.lbl_Total_with_Discount.Text =
            Format((CSng(lbl_Total_without_Discount.Text) * (1 - discountRate)) - voucher, "#,0.00")
    End Sub

    Regards,

    Ashidacchi

    • Marked as answer by Maniiii Sunday, February 11, 2018 1:10 AM
    Wednesday, February 7, 2018 3:27 AM

All replies

  • Forgot to add that it needs to display in a msgbox

    thank you

    Tuesday, February 6, 2018 11:47 AM
  • Forgot to add that it needs to display in a msgbox

    thank you

    Hi

    You also forgot to include your code so far ...........


    Regards Les, Livingston, Scotland

    Tuesday, February 6, 2018 11:50 AM
  • Forgot to add that it needs to display in a msgbox

    thank you

    Hi

    You also forgot to include your code so far ...........


    Regards Les, Livingston, Scotland



    Success Cor

    Tuesday, February 6, 2018 12:03 PM
  • Moderator note: always post code in a code block as done below.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Dim Title As String
            Dim Books As Integer
            Dim Costperbook As Double
            Dim Totalcost As Double
            Dim Voucher As String
            Dim Discount As Single
            Dim DiscountRate As Double
    
    
    
            Title = txtTitle.Text
            Books = txtBooks.Text
            DiscountRate = Totalcost - (Totalcost * 0.05)
            DiscountRate = Totalcost - (Totalcost * 0.75)
            DiscountRate = Totalcost - (Totalcost * 0.1)
    
            Totalcost = Books * 15.99
            txtTotal.Text = Total
            Costperbook = 15.99
            Voucher = 25
            txtTitle.Text = InputBox("Please Enter your book name")
            txtBooks.Text = InputBox("Please Enter Amount you'd like to buy")
            txtCostperbook.Text = InputBox("Please Enter the cost of the book", & 15.99 &)
            txtVoucher.Text = InputBox("Please Say yes or no")
    
            If Voucher = "yes" Then
                Totalcost = ((Costperbook * Books) - DiscountRate - Voucher)
                txtVoucher.Text = Format(Voucher, "Currency")
            End If
    
            If Voucher = "no" Then
                    Totalcost = Books * Costperbook
                    txtVoucher.Text = Format(Voucher, "Currency")
                End If
    
                If Books <= 5 Or Books >= 10 Then
                    DiscountRate = 0.05
                End If
    
                If Books <= 11 Or Books >= 50 Then
                    DiscountRate = 0.075
                End If
    
                If Books <= 51 Or Books >= 80 Then
                    DiscountRate = 0.1
                End If
                Books = Inputbox("You cannot order less than 1 book or  more than 80", "Enter Amount")
                Do While Books <= 1 Or Books >= 80
                    MsgBox("Your entry is out of range please choose amount from 1-80")
                    Books = Inputbox("You cannot order less than 1 book or  more than 80", "Enter Amount")
                Loop
                txtBooks.Text = Books
    
    
    
        End Sub
    
    End Class

    Tuesday, February 6, 2018 1:12 PM
  • Hi

    Here is some code that *may* help. It only uses the default control names rather than what I am assuming is your control names. There is no error checking.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Dim s As String = NumericUpDown1.Value.ToString & " of " & TextBox1.Text & vbCrLf
    	Dim tc As Double = (CDbl(TextBox2.Text) * NumericUpDown1.Value) / 100 * (100 - NumericUpDown2.Value)
    	If CheckBox1.Checked Then tc -= 30
    
    	s &= "Discount = " & NumericUpDown2.Value.ToString & "%" & vbCrLf
    	s &= "Voucher = "
    
    	If CheckBox1.Checked Then
    	  s &= "  Yes" & vbCrLf
    	Else
    	  s &= "  No" & vbCrLf
    	End If
    	s &= tc.ToString("C")
    	MessageBox.Show(s, "Order")
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Maniiii Sunday, February 11, 2018 1:10 AM
    Tuesday, February 6, 2018 2:45 PM
  • Hi

    Here is some code that *may* help. It only uses the default control names rather than what I am assuming is your control names. There is no error checking.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Dim s As String = NumericUpDown1.Value.ToString & " of " & TextBox1.Text & vbCrLf
    	Dim tc As Double = (CDbl(TextBox2.Text) * NumericUpDown1.Value) / 100 * (100 - NumericUpDown2.Value)
    	If CheckBox1.Checked Then tc -= 30
    
    	s &= "Discount = " & NumericUpDown2.Value.ToString & "%" & vbCrLf
    	s &= "Voucher = "
    
    	If CheckBox1.Checked Then
    	  s &= "  Yes" & vbCrLf
    	Else
    	  s &= "  No" & vbCrLf
    	End If
    	s &= tc.ToString("C")
    	MessageBox.Show(s, "Order")
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, February 6, 2018 2:48 PM
    • Marked as answer by Maniiii Sunday, February 11, 2018 1:10 AM
    Tuesday, February 6, 2018 2:45 PM

  • What i want is that if a user types between 5 or 10 books the discount is 7.5% and when yes in voucher boxthen -25 from the total cost.

    Tuesday, February 6, 2018 4:29 PM
  • Hi

    OK then, just adjust the code I posted accordingly.


    Regards Les, Livingston, Scotland

    Tuesday, February 6, 2018 4:42 PM
  • Hi

    Sorry for delay. Forum is acting up as though monkeys were running it.

    *

    Here is adjusted code to try out. It uses the 3 discount rates from your code. Again, no error checking. For example, 1 book cost 10 voucher bings it down so you pay customer for buying the book.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Dim s As String = NumericUpDown1.Value.ToString & " of " & TextBox1.Text & vbCrLf
    
    	Dim tc As Double = (CDbl(TextBox2.Text) * NumericUpDown1.Value) * (1 - DiscountRate)
    	If CheckBox1.Checked Then tc -= 25
    
    	s &= "Discount = " & Label1.Text & "% " & vbCrLf
    	s &= "Voucher = "
    
    	If CheckBox1.Checked Then
    	  s &= "  Yes" & vbCrLf
    	Else
    	  s &= "  No" & vbCrLf
    	End If
    	s &= "Total:  " & tc.ToString("C")
    	MessageBox.Show(s, "Order")
      End Sub
      Dim DiscountRate As Decimal = 0D
      Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    	DiscountRate = 0D
    	Dim v As Decimal = NumericUpDown1.Value
    	If v > 4 AndAlso v < 11 Then DiscountRate = 0.05D
    	If v > 10 AndAlso v < 51 Then DiscountRate = 0.075D
    	If v > 50 AndAlso v < 81 Then DiscountRate = 0.1D
    	If v > 80 Then
    	  MessageBox.Show("Maximum Books is 80")
    	End If
    	Label1.Text = DiscountRate.ToString("#.0%")
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Maniiii Sunday, February 11, 2018 1:10 AM
    Tuesday, February 6, 2018 5:33 PM
  • Hi Maniiii,

    I guess you would be a student and it would be a school assignment.

    Putting that aside, I'm afraid you forget other conditions.
    What is the discount rate, when a user types more than 10 books?

    This is my sample:
         
    [Designer: for showing control name]
         
    [Code]
    ' --- Variables ---
    Private IsIgnoreEvent As Boolean = True
    ' --- Form: Load
    Private Sub frm_Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.lbl_DiscountRate.Text = ""
        Me.lbl_Total_without_Discount.Text = ""
        Me.lbl_Total_with_Discount.Text = ""
        Me.lbl_Voucher.Text = ""
    End Sub
    ' --- after Form shown
    Private Sub frm_Main_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        Me.IsIgnoreEvent = False
    End Sub
    ' ---
    Private Sub Value_Changed(sender As Object, e As EventArgs) Handles _
        txt_Price.TextChanged, nud_Quantity.TextChanged, chk_Voucher.CheckedChanged
        ' ---
        If (Me.IsIgnoreEvent = False) Then
            If (Me.txt_Price.Text = "") Then
                Me.txt_Price.Text = "1"
            End If
            Call Me.prc_Compute()
        End If
    End Sub
    ' ---
    Private Sub prc_Compute()
        ' --- Discount Rate
        Dim discountRate As Single
        Select Case Me.nud_Quantity.Value
            Case < 5    ' -- 1 ~ 4
                discountRate = 0.0
            Case <= 10  ' -- 5 ~ 10
                discountRate = 0.075
            Case <= 20  ' -- 11 ~ 20
                discountRate = 0.1
            Case <= 50  ' -- 21 ~ 50
                discountRate = 0.2
            Case Else   ' -- over 51
                discountRate = 0.3
        End Select
        Me.lbl_DiscountRate.Text = Format(discountRate, "0.0 %")
        ' --- Voucher
        Dim voucher As Single
        If (Me.chk_Voucher.Checked) Then
            voucher = 25.0
        Else
            voucher = 0.0
        End If
        Me.lbl_Voucher.Text = Format(voucher, "0.00")
        ' --- Total
        Me.lbl_Total_without_Discount.Text =
            Format(CSng(Me.txt_Price.Text) * Me.nud_Quantity.Value, "#,0.00")
        Me.lbl_Total_with_Discount.Text =
            Format((CSng(lbl_Total_without_Discount.Text) * (1 - discountRate)) - voucher, "#,0.00")
    End Sub

    Regards,

    Ashidacchi

    • Marked as answer by Maniiii Sunday, February 11, 2018 1:10 AM
    Wednesday, February 7, 2018 3:27 AM
  • Hi Maniiii,

    Many people wanted to help you and provided answers.
    Has your question been answered? If your issue has been resolved, please mark on post which is helpful.

    Regards,

    Ashidacchi

    Thursday, February 8, 2018 3:44 AM