Public Class Form1
Dim taxrate = 0.06
Private Sub AddBook(ByVal Title As String, ByVal Price As Decimal)
Dim book As ListViewItem = New ListViewItem
book.Text = Title
book.SubItems.Add(Price.ToString)
ListView1.Items.Add(book)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'if there are no items in 'selecteditems' then they haven't selected anything, otherwise do this stuff
If ListView1.SelectedItems.Count > 0 Then
'Make a copy of the item from the book list so it can be added to
'the cart list
Dim book As ListViewItem = ListView1.SelectedItems.Item(0).Clone
'true/false to hold if you need to update quantity vs add new item
Dim updatequant As Boolean = False
'go through each item in the cart.
For Each item As ListViewItem In ListView2.Items
'if the item is already in the cart
If item.Text = book.Text Then
'get the current quantity
Dim tempquant As Integer = Integer.Parse(item.SubItems(2).Text)
'add one to the quantity
tempquant += 1
'and update that item with the new quantity
item.SubItems(2).Text = tempquant.ToString
'change the flag to true so we know that we
'updated the quantity rather than adding a
'new item
updatequant = True
End If
Next
'if we didn't find the item then
If updatequant = False Then
'add a quantity of 1 (as a string)
book.SubItems.Add("1")
'and add it to the cart
ListView2.Items.Add(book)
End If
'update the shipping / tax / totals
UpdateTotal()
Else
'this is if they didn't choose an item in the box
MsgBox("Please choose an item to add to the Cart", , "Add to Cart")
End If
End Sub
Private Sub UpdateTotal()
'start the subtotal at zero
'we replace itemtotal rather than add to it
'so it doesn't need an initial value
Dim subtotal As Decimal = 0
Dim itemtotal As Decimal
'go through each item in the cart
For Each item As ListViewItem In ListView2.Items
'multiply the quantity and price of that item
itemtotal = Decimal.Parse(item.SubItems(1).Text) * Decimal.Parse(item.SubItems(2).Text)
'and add it to the subtotal
subtotal += itemtotal
Next
'calculate tax
Dim tax As Decimal = subtotal * taxrate
'you can put your own shipping calculator here
Dim shipping As Decimal = 5.55 'whatever you choose
'add the subtotal, tax, and shipping to get the total
Dim total As Decimal = subtotal + tax + shipping
'If you want to keep the totals to use later, you can change all these local variables
'to global variables, because if you try to get the data from the textboxes, they'll
'be rounded. I just like having the text boxes beautiful.
Box_ST.Text = subtotal.ToString("c")
Box_T.Text = tax.ToString("c")
Box_Ship.Text = shipping.ToString("c")
Box_Tot.Text = total.ToString("c")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'if there is an item selected
If ListView2.SelectedItems.Count > 0 Then
'if it has a quantity greater than one
If Integer.Parse(ListView2.SelectedItems(0).SubItems(2).Text) > 1 Then
'then reduce the quantity
ListView2.SelectedItems(0).SubItems(2).Text = Integer.Parse(ListView2.SelectedItems(0).SubItems(2).Text) - 1
Else
'otherwise, remove it completely
ListView2.Items.Remove(ListView2.SelectedItems(0))
End If
'then update the totals
UpdateTotal()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'this section should be fairly self explanitory
Dim booktitle As String = InputBox("Book Title")
For Each item As ListViewItem In ListView1.Items
If item.Text = booktitle Then
MsgBox("Sorry, but that book is already in the list")
booktitle = Nothing
End If
Next
If booktitle <> Nothing Then
Dim bookprice As Decimal = InputBox("Price for " & booktitle)
If bookprice <> Nothing Then
AddBook(booktitle, bookprice)
End If
End If
End Sub
End Class