Solved

Help populating a listbox like a register receipt

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
recursion example 16 69
VB.net PaintEventArgs Handling. 10 27
Round up to 100% in .NET 10 44
Loop through Multiple Processes Async 2 17
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
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 …

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now