Solved

Help populating a listbox like a register receipt

Posted on 2013-12-10
4
951 Views
Last Modified: 2013-12-10
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
Comment
Question by:Barry62
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 39709934
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
 
LVL 8

Author Comment

by:Barry62
ID: 39709939
Is there a way to insert items into the middle of the list?
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 39709991
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
 
LVL 8

Author Closing Comment

by:Barry62
ID: 39710015
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question