Solved

Help populating a listbox like a register receipt

Posted on 2013-12-10
4
921 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 28
Delphi: barcode reading on android platform 1 29
REXEX help Part 2 2 23
get row value in vb.net 4 14
A short article about a problem I had getting the GPS LocationListener working.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 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