[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1084
  • Last Modified:

Help populating a listbox like a register receipt

I am having trouble with a final project for my vb.net class.  We are to create a POS system that uses a listbox to act like a cash register receipt.  So it needs to add items as buttons are pressed, but also needs to keep a running subtotal and when the order is done, add tax, total, and change due.  My problem is I can't find an easy way to keep the subtotal,tax, total and change lines below all of the items ordered.  

I have dblTotal and dblSubTotal as global variables, and the item prices and tax rate as global constants.

Below is the subroutine I use to add to the receipt every time an item button is pressed:

Private Sub addToReceipt(itemCost As Double, itemName As String, subTot As Double)

        'add store information and subtotal to storage string before first item only
        If lstReceipt.Items.Count = 0 Then
            lstReceipt.Items.Add("Barry's Bagel Bakery - Store #1")
            lstReceipt.Items.Add(FormatDateTime(Today, DateFormat.ShortDate) & " - Checker: Barry Beach")
            lstReceipt.Items.Add(" ")
            lstReceipt.Items.Add("*************************************")
            'lstReceipt.Items.Add("SubTotal: " & subTot.ToString("F2"))

        End If

        'clear last two lines for re-display
        lstReceipt.Items.RemoveAt(lstReceipt.Items.Count - 1)
        lstReceipt.Items.RemoveAt(lstReceipt.Items.Count - 1)

        'add subtotal back in
        lstReceipt.Items.Add(itemName & " " & itemCost.ToString("F2"))
        lstReceipt.Items.Add("*************************************")
        lstReceipt.Items.Add("SubTotal: " & subTot.ToString("F2"))




    End Sub

Open in new window


Here is the subroutine to total the order:

Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click

        Dim dblTotalTax As Double
        dblTotalTax = dblSubTotal * dblTAX_RATE
        dblTotal = dblSubTotal + dblTotalTax

        lstReceipt.Items.Add("Tax: " & dblTotalTax.ToString("F2"))
        lstReceipt.Items.Add("Total: " & dblTotal.ToString("F2"))
        

    End Sub

Open in new window


Here is the subroutine to finalize the order and calculate change:

Private Sub btnFinish_Click(sender As Object, e As EventArgs) Handles btnFinish.Click
        Dim amtPaid As Double = 0
        Dim changeDue As Double
        Double.TryParse(txtAmtPaid.Text, amtPaid)
        If amtPaid = 0 Then
            MessageBox.Show("Please enter the amount paid by the customer", "Barry's Bagel Bakery", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Else
            changeDue = amtPaid - dblTotal
            lblChangeDue.Text = changeDue.ToString("F2")
            lstReceipt.Items.Add("Cash: " & amtPaid.ToString("F2"))
            lstReceipt.Items.Add("Change: " & changeDue.ToString("F2"))
        End If
    End Sub

Open in new window


The image 'bagelshop_1.png' shows what is displayed after I add a few items and total it up.  

The image 'bagelshop_2.png' shows what is displayed when I add another item after totaling it.

The image 'bagelshop_3.png' shows what is displayed after I finish the order and calculate change.

Can anyone help?
bagelshop-1.png
bagelshop-2.png
bagelshop-3.png
0
Barry62
Asked:
Barry62
  • 2
  • 2
1 Solution
 
HooKooDooKuCommented:
The problem I see is that every time you add an item to the receipt, you are blindly removing the last two lines from the receipt and, adding the new item, and reprinting the sub-total.

But that logic obviously fails when other subroutines are called that add more information to the bottom of the receipt and then you go back and add more items to the list.

Seems to me you need to find a way to track how many items are on the receipt so that when you need to add another item, you can properly add the new item below the last item already in the list.
0
 
Barry62Author Commented:
Is there a way to insert items into the middle of the list?
0
 
HooKooDooKuCommented:
Utilize the auto-complete functionality of the IDE if you can.

Simply type in 'lstReceipt.Items.'   and a list of subroutines and properties of the Items object should appear.

When I do that, I can look through the list that pops up and see there is a ...
    Items.Insert( index as Integer, item as object)
... function associated with the Items object of the list box.

But keep in mind that simply inserting an item means you would still have to do something to either erase or update the subtotal and total.  The receipt would be a mess if you already had a total, then inserted an item, updated the sub-total, and left an invalid total visible.
0
 
Barry62Author Commented:
Thanks, I found the Insert method, and I am using a counter for the number of items added to the receipt so I can tell where to insert the new item.  Then I am removing the subtotal line using the findString method, and re-adding it with the new subtotal.

My brain just needed a push. Thanks!
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now