14 Replies - 458 Views - Last Post: 11 December 2018 - 12:09 PM Rate Topic: -----

#1 IndomitableSpirit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 18

Need help with my project

Posted 07 December 2018 - 12:17 PM

I'm trying to build a shopping cart system. When I click on my "Add Book to Cart" button it's suppose to add it to the main form and do calculations. But, nothing happens. I can not figure out why this is happening. I've been stuck on this for days. I'm sure it's something small I'm missing. Please help.


Module BooksModule
    Public frmMainForm As New MainForm
    Public frmAudioBooksForm As New AudioBooksForm
    Public frmPrintBooksForm As New PrintBooksForm

    Public print_BookPrice() As Decimal = {11.95D, 14.5D, 29.95D, 18.5D}
    Public print_BookTitle() As String = {"I Did It your Way (Print)", "The History of Scotland (Print)", "Learn Calculus in One Day (Print)", "Feel the Stress (Print)"}
    Public audio_BookPrice() As Decimal = {29.95D, 14.5D, 12.95D, 11.5D}
    Public audio_BookTitle() As String = {"Learn Calculus in One Day (Audio)", "The History of Scotland (Audio)", "The Science of Body Language (Audio)", "Relaxation Techniques (Audio)"}

    Public g_decSubtotal As Decimal
    Public g_decTax As Decimal
    Public g_decShipping As Decimal
    Public g_decTotal As Decimal

End Module



Public Class MainForm
    Public Const decTax As Decimal = 0.06D
    Public Const decShipping As Decimal = 2D
    Public decBookTotal As Decimal = 0
    Public intBookCounter As Integer



    Sub ReCalculate()
        BookList()
        lblSubTotal.Text = SubtotalCalc.ToString("c2")
        lblTax.Text = TaxCalc.ToString("c2")
        lblShipping.Text = ShippingCalc.ToString("c2")
        lblTotal.Text = TotalCalc.ToString("c2")

    End Sub

    Public Function SubtotalCalc() As Decimal
        g_decSubtotal = decBookTotal
        Return g_decSubtotal
    End Function

    Public Function TaxCalc() As Decimal
        g_decTax = decBookTotal * decTax
        Return g_decTax
    End Function

    Public Function ShippingCalc() As Decimal
        g_decShipping = intBookCounter * decShipping
        Return g_decShipping
    End Function

    Public Function TotalCalc() As Decimal
        g_decTotal = TaxCalc() + ShippingCalc()
        Return g_decTotal
    End Function

    Sub Reset()
        lstProductsSelected.Items.Clear()
        lblSubTotal.Text = String.Empty
        lblTax.Text = String.Empty
        lblShipping.Text = String.Empty
        lblTotal.Text = String.Empty
    End Sub

    Public Sub BookList()

        For Each Book As String In lstProductsSelected.Items
            If frmPrintBooksForm.lstPrintBook.SelectedIndex = 0 Then
                decBookTotal += print_BookPrice(0)
            ElseIf frmPrintBooksForm.lstPrintBook.SelectedIndex = 1 Then
                decBookTotal += print_BookPrice(1)
            ElseIf frmPrintBooksForm.lstPrintBook.SelectedIndex = 2 Then
                decBookTotal += print_BookPrice(2)
            ElseIf frmPrintBooksForm.lstPrintBook.SelectedIndex = 3 Then
                decBookTotal += print_BookPrice(3)
            End If

            If frmAudioBooksForm.lstAudioBook.SelectedIndex = 0 Then
                decBookTotal += audio_BookPrice(0)
            ElseIf frmAudioBooksForm.lstAudioBook.SelectedIndex = 1 Then
                decBookTotal += audio_BookPrice(1)
            ElseIf frmAudioBooksForm.lstAudioBook.SelectedIndex = 2 Then
                decBookTotal += audio_BookPrice(2)
            ElseIf frmAudioBooksForm.lstAudioBook.SelectedIndex = 3 Then
                decBookTotal += audio_BookPrice(3)
            End If
        Next Book

        intBookCounter = lstProductsSelected.Items.Count

    End Sub


    Private Sub mnuFileExit_Click(sender As Object, e As EventArgs) Handles mnuFileExit.Click
        Me.Close()
    End Sub

    Private Sub mnuProductsPrintBooks_Click(sender As Object, e As EventArgs) Handles mnuProductsPrintBooks.Click
        frmPrintBooksForm.Show()
    End Sub

    Private Sub AudioBooksToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AudioBooksToolStripMenuItem.Click
        frmAudioBooksForm.Show()
    End Sub

    Private Sub mnuFileReset_Click(sender As Object, e As EventArgs) Handles mnuFileReset.Click
        Reset()
    End Sub

    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        If lstProductsSelected.SelectedIndex <> -1 Then
            lstProductsSelected.Items.RemoveAt(lstProductsSelected.SelectedIndex)
        End If
        ReCalculate()
    End Sub

End Class




Public Class PrintBooksForm

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Public Sub btnAddPrintBookToCart_Click(sender As Object, e As EventArgs) Handles btnAddPrintBooktoCart.Click
        frmMainForm.ReCalculate()
        frmMainForm.lstProductsSelected.Items.Add(lstPrintBook.SelectedIndex & lstPrintBook.SelectedItem.ToString)

    End Sub

    Private Sub PrintBooksForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i = 0 To UBound(print_BookTitle)
            lstPrintBook.Items.Add(print_BookTitle(i) & " " & print_BookPrice(i).ToString)
        Next i
    End Sub
End Class




Public Class AudioBooksForm

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Public Sub btnAddAudioBookToCart_Click(sender As Object, e As EventArgs) Handles btnAddAudioBookToCart.Click
        frmMainForm.ReCalculate()
        frmMainForm.lstProductsSelected.Items.Add(lstAudioBook.SelectedIndex & lstAudioBook.SelectedItem.ToString)

    End Sub

    Private Sub AudioBooksForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i = 0 To UBound(audio_BookTitle)
            lstAudioBook.Items.Add(audio_BookTitle(i) & " " & audio_BookPrice(i).ToString)
        Next i
    End Sub
End Class



Attached File  ShoppingCartSystem.zip (875.57K)
Number of downloads: 14

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with my project

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5332
  • View blog
  • Posts: 14,210
  • Joined: 18-April 07

Re: Need help with my project

Posted 07 December 2018 - 12:45 PM

Step one, see if your event is firing when you click the button. So I would suggest you do something like put a break point on the first line of your btnAddAudioBookToCart_Click event and see if it is triggering the event. From there you can step through and see where things break down.

Simple debug session can find the problem here.
Was This Post Helpful? 1
  • +
  • -

#3 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,149
  • Joined: 02-November 11

Re: Need help with my project

Posted 07 December 2018 - 12:49 PM

I know this is a school project but there's easier ways to do this using objects. I am gonna be honest with you, I haven't done anything in Forms since I left school and that was like 8 years ago.

When you say nothing is happening, is it not adding anything to lstProductsSelected? or is it just not doing the calculations?

If the latter, it is because you are calculation the amounts before adding the item. Also decBookTotal is going to raise exponentially.

I don't know if you can do this, but I recommend creating a book object with price and title and then adding that to the listbox. I cannot remember if that is a thing that can be done without extending the ListBox... Anyway, then you'd just have to iterate the objects and pull their prices, or even better just tack the price on to the total as you add it.

Pseudo:
Book Class
 String Title - Get/Private Set
 Decimal Price - Get/Private Set

 Public Book(String title, Decimal price)
 {
    Title = title
    Price = price
 }

 Public Override String ToString()
 {
    return string.concat(Title, " - ", Price)
 } 



That is some kind of hybrid of VB and C# but you get the idea, I hope. You could also include calculating their individual tax and then just add the tax to the tax variable as they get added to the list. The override of ToString is so you can format it's output in the List.
Was This Post Helpful? 1
  • +
  • -

#4 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,025
  • Joined: 05-December 13

Re: Need help with my project

Posted 07 December 2018 - 01:35 PM

I don't like posting only a link to help solve the problem, but when you post chunks of code like that, not everyone is going to have time to read through it all for you, unless you specify a specific section you'd like us to check?

At some point in your programming journey, you need to learn how to debug your code. This is the best way to sniff out problematic issues and step through the process by placing break points on sections were you believe something is not executing or working as intended. If you follow the tutorial, you will find it helpful to troubleshooting your own code, now and in future.

TechnoBear, could you not have given a VB example rather than C#, while I can do both and get your example, most people who post in this forum can barely do VB code let alone C#. Just sayin'
Was This Post Helpful? 0
  • +
  • -

#5 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,025
  • Joined: 05-December 13

Re: Need help with my project

Posted 07 December 2018 - 02:03 PM

Side note, I don't get why you are looping when you could just check the selected item(s)?

Public Sub BookList()
        For Each Book As String In lstProductsSelected.Items
49
            If frmPrintBooksForm.lstPrintBook.SelectedIndex = 0 Then



At a second quick glance, Since you didn'#t provide the code for what your selected items/index is doing, I am assuming that you need a selected items/index in order to calculate? The first thing that executes is ::

BookList() from inside this method Sub ReCalculate(). But you don't have a selected index, or do you?

I am guessing one of these are not selected ::

frmPrintBooksForm
lstProductsSelected

From your BookList() Sub when it executes, thus your statement conditions are not met.
Was This Post Helpful? 0
  • +
  • -

#6 IndomitableSpirit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 18

Re: Need help with my project

Posted 08 December 2018 - 11:15 AM

View PostSheepings, on 07 December 2018 - 02:03 PM, said:

Side note, I don't get why you are looping when you could just check the selected item(s)?

Public Sub BookList()
        For Each Book As String In lstProductsSelected.Items
49
            If frmPrintBooksForm.lstPrintBook.SelectedIndex = 0 Then



At a second quick glance, Since you didn'#t provide the code for what your selected items/index is doing, I am assuming that you need a selected items/index in order to calculate? The first thing that executes is ::

BookList() from inside this method Sub ReCalculate(). But you don't have a selected index, or do you?

I am guessing one of these are not selected ::

frmPrintBooksForm
lstProductsSelected

From your BookList() Sub when it executes, thus your statement conditions are not met.


I thought I did. Doesn't the BookList() provide the selected items/index? The BookList()'s array variable corresponds with each index on the list. SelectedIndex(0) = print_BookPrice(0) and so on. Did I code that wrong? Is all that code unnecessary. I don't understand. I only have like 12 weeks of experience with VB. I removed the loop by the way.

View PostTechnoBear, on 07 December 2018 - 12:49 PM, said:

I know this is a school project but there's easier ways to do this using objects. I am gonna be honest with you, I haven't done anything in Forms since I left school and that was like 8 years ago.

When you say nothing is happening, is it not adding anything to lstProductsSelected? or is it just not doing the calculations?

If the latter, it is because you are calculation the amounts before adding the item. Also decBookTotal is going to raise exponentially.

I don't know if you can do this, but I recommend creating a book object with price and title and then adding that to the listbox. I cannot remember if that is a thing that can be done without extending the ListBox... Anyway, then you'd just have to iterate the objects and pull their prices, or even better just tack the price on to the total as you add it.

Pseudo:
Book Class
 String Title - Get/Private Set
 Decimal Price - Get/Private Set

 Public Book(String title, Decimal price)
 {
    Title = title
    Price = price
 }

 Public Override String ToString()
 {
    return string.concat(Title, " - ", Price)
 } 



That is some kind of hybrid of VB and C# but you get the idea, I hope. You could also include calculating their individual tax and then just add the tax to the tax variable as they get added to the list. The override of ToString is so you can format it's output in the List.


A bit of both, but I'm more concerned with it adding to lstProductsSelected. Why is decBookTotal going to raise exponentially? This is my first VB class, so I'm not experienced. We haven't covered objects yet. Not sure how to do it.
Was This Post Helpful? 0
  • +
  • -

#7 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,025
  • Joined: 05-December 13

Re: Need help with my project

Posted 08 December 2018 - 11:58 AM

View PostIndomitableSpirit, on 08 December 2018 - 06:15 PM, said:

I thought I did. Doesn't the BookList() provide the selected items/index?

Does it? You tell me. How many item(s) are selected when that code executes?

View PostSheepings, on 07 December 2018 - 08:35 PM, said:

learn how to debug your code.

Have you read up on the debug tutorial yet? Have you placed break points on that method?

Also, no need to quote a whole post, select the portions you want to quote rather than the whole post or just hit reply and use @ persons name
Was This Post Helpful? 0
  • +
  • -

#8 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,149
  • Joined: 02-November 11

Re: Need help with my project

Posted 10 December 2018 - 06:37 AM

View PostIndomitableSpirit, on 08 December 2018 - 12:15 PM, said:

Why is decBookTotal going to raise exponentially?


When you go to add the totals the value gets added on to the decBookTotal, unless I didn't look close enough, with += which is a-kin to decBookTotal = decBookTotal + value. As decBookTotal is a global variable it will hold it's value so the more times you run the code the higher that number will grow and the more inaccurate it will become.
Was This Post Helpful? 0
  • +
  • -

#9 IndomitableSpirit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 18

Re: Need help with my project

Posted 10 December 2018 - 11:21 AM

@Sheepings

My code works now. It is adding the list to the other form.
Public Sub btnAddAudioBookToCart_Click(sender As Object, e As EventArgs) Handles btnAddAudioBookToCart.Click
        MainForm.lstProductsSelected.Items.Add(Me.lstAudioBook.SelectedItem)
        MainForm.BookList()
    End Sub


Public Sub btnAddPrintBookToCart_Click(sender As Object, e As EventArgs) Handles btnAddPrintBooktoCart.Click
        MainForm.lstProductsSelected.Items.Add(Me.lstPrintBook.SelectedItem)
        MainForm.BookList()
    End Sub



But, none of my math works. I mean, it is not adding to the label box. Any idea?

Public Class MainForm
    Public Const decTax As Decimal = 0.06D
    Public Const decShipping As Decimal = 2D
    Dim decBookTotal As Decimal
    Dim decBookPrice As Decimal

    Public frmAudioBooksForm As New AudioBooksForm
    Public frmPrintBooksForm As New PrintBooksForm

    Dim print_BookPrice1 As Decimal = 11.95D
    Dim print_BookPrice2 As Decimal = 14.5D
    Const print_BookPrice3 As Decimal = 29.95D
    Const print_BookPrice4 As Decimal = 18.5D
    Const audio_BookPrice1 As Decimal = 29.95D
    Const audio_BookPrice2 As Decimal = 14.5D
    Const audio_BookPrice3 As Decimal = 12.95D
    Const audio_BookPrice4 As Decimal = 11.5D

    Public Function SubtotalCalc() As Decimal
        Return decBookTotal
    End Function

    Public Function TaxCalc() As Decimal
        Return SubtotalCalc() * decTax
    End Function

    Public Function ShippingCalc() As Decimal
        Return intBookCounter * decShipping
    End Function

    Public Function TotalCalc() As Decimal
        Return SubtotalCalc() + TaxCalc() + ShippingCalc()
    End Function

    Public Sub BookList()
        If PrintBooksForm.lstPrintBook.SelectedIndex = 0 Then
            decBookPrice = CDec(print_BookPrice1)
            decBookTotal += CDec(decBookPrice)
            TotalCalc()
            ReCalculate()
        End If

        If PrintBooksForm.lstPrintBook.SelectedIndex = 1 Then
            decBookPrice = CDec(print_BookPrice1)
            decBookTotal += CDec(decBookPrice)
            TotalCalc()
            ReCalculate()
        End If

        If PrintBooksForm.lstPrintBook.SelectedIndex = 2 Then
            decBookPrice = CDec(print_BookPrice3)
            decBookTotal += CDec(decBookPrice)
            TotalCalc()
            ReCalculate()
        End If

        If PrintBooksForm.lstPrintBook.SelectedIndex = 3 Then
            decBookPrice = CDec(print_BookPrice4)
            decBookTotal += CDec(decBookPrice)
            lblSubTotal.Text = decBookPrice.ToString("C2")
            ReCalculate()
        End If

        If AudioBooksForm.lstAudioBook.SelectedIndex = 0 Then
            decBookPrice = CDec(print_BookPrice1)
            decBookTotal += CDec(decBookPrice)
            TotalCalc()
            ReCalculate()
        End If

        If AudioBooksForm.lstAudioBook.SelectedIndex = 1 Then
            decBookPrice = CDec(print_BookPrice2)
            decBookTotal += CDec(decBookPrice)
            TotalCalc()
            ReCalculate()
        End If

        If AudioBooksForm.lstAudioBook.SelectedIndex = 2 Then
            decBookPrice = CDec(print_BookPrice3)
            decBookTotal += CDec(decBookPrice)
            TotalCalc()
            ReCalculate()
        End If

        If AudioBooksForm.lstAudioBook.SelectedIndex = 3 Then
            decBookPrice = CDec(print_BookPrice4)
            decBookTotal += CDec(decBookPrice)
            TotalCalc()
            ReCalculate()
        End If

        intBookCounter = Me.lstProductsSelected.Items.Count
    End Sub

    Sub ReCalculate()
        lblSubTotal.Text = SubtotalCalc().ToString("c2")
        lblTax.Text = TaxCalc().ToString("c2")
        lblShipping.Text = ShippingCalc().ToString("c2")
        lblTotal.Text = TotalCalc().ToString("c2")
    End Sub



    Sub Reset()
        lstProductsSelected.Items.Clear()
        lblSubTotal.Text = String.Empty
        lblTax.Text = String.Empty
        lblShipping.Text = String.Empty
        lblTotal.Text = String.Empty
    End Sub




    Private Sub mnuFileExit_Click(sender As Object, e As EventArgs) Handles mnuFileExit.Click
        Me.Close()
    End Sub

    Private Sub mnuProductsPrintBooks_Click(sender As Object, e As EventArgs) Handles mnuProductsPrintBooks.Click
        frmPrintBooksForm.ShowDialog()
    End Sub

    Private Sub AudioBooksToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AudioBooksToolStripMenuItem.Click
        frmAudioBooksForm.ShowDialog()
    End Sub

    Private Sub mnuFileReset_Click(sender As Object, e As EventArgs) Handles mnuFileReset.Click
        Reset()
    End Sub

    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        If lstProductsSelected.SelectedIndex <> -1 Then
            lstProductsSelected.Items.RemoveAt(lstProductsSelected.SelectedIndex)
        End If
        ReCalculate()
    End Sub
End Class



Was This Post Helpful? 0
  • +
  • -

#10 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,149
  • Joined: 02-November 11

Re: Need help with my project

Posted 10 December 2018 - 11:43 AM

As there can only be one selected index you should put
TotalCalc()
ReCalculate()

After the If statements in BookList(), this is purely for readability and redundancy.

As for the math, when you step through debugging, what values are in each variable? Specifically decBookTotal and intBookCounter.
Was This Post Helpful? 0
  • +
  • -

#11 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,025
  • Joined: 05-December 13

Re: Need help with my project

Posted 10 December 2018 - 02:48 PM

Put this at the top of your code files and fix your conversion issues first.
Option Strict On
Option Explicit On


How many errors did you get? That's the result of not properly declaring and converting values appropriately.

You should create a function to do the actual calculations as opposed to bla() + bla2() etc and pass the price of item, tax, quantity, to the function to calculate and return a total from that one function. You have a number of functions, which could be broken into one function. Have you read the debug tutorial on post 7 which I referenced?
Was This Post Helpful? 0
  • +
  • -

#12 IndomitableSpirit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 18

Re: Need help with my project

Posted 11 December 2018 - 11:15 AM

@technobear
What exactly is redundant? Is there a way to shorten that code?

I'm not getting any errors and can't figure out how to get the break points to work.
Was This Post Helpful? 0
  • +
  • -

#13 IndomitableSpirit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 18

Re: Need help with my project

Posted 11 December 2018 - 11:21 AM

decBookTotal is for the total price of each book accumulated. If I choose a book that's $5 and one $10. The decBookTotal should hold everything inside making it $15.

intBookCounter is used to count how many item have been selected. I must multiply the total amount of items * shipping. For example, if I selected 5 books. It should be 5 * 2 (Price of shipping) = $10
Was This Post Helpful? 0
  • +
  • -

#14 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,149
  • Joined: 02-November 11

Re: Need help with my project

Posted 11 December 2018 - 11:38 AM

View PostIndomitableSpirit, on 11 December 2018 - 12:15 PM, said:

@technobear
What exactly is redundant? Is there a way to shorten that code?


The methods I gave, they are used for every call, you could just put them at the end of the group of if statements and run them the once.

View PostIndomitableSpirit, on 11 December 2018 - 12:21 PM, said:

decBookTotal is for the total price of each book accumulated. If I choose a book that's $5 and one $10. The decBookTotal should hold everything inside making it $15.


I was specifically asking what values they hold right now, while debugging, versus what you expect to see them have.

For example, lets say you're expecting decBookTotal to be 15 based on your selections, but when debugging it is actually 5 or even 0. This gives you an idea of where to look to find what isn't happening and should be, or what is happening and shouldn't be.
Was This Post Helpful? 0
  • +
  • -

#15 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: Need help with my project

Posted 11 December 2018 - 12:09 PM

<deleted> Didn't read the entire thread :/

This post has been edited by CharlieMay: 11 December 2018 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1