TheCrispyToast
TheCrispyToast

Reputation: 31

Variables to list box?

taking a VB class this term and I've been stumped on a problem I'm trying to figure out. We were asked to create a price calculator for movie titles at a movie rental place. Extra credit was storing them in a list and being able to print the list. I've gotten that far and now I want to go a step further and actually add titles to that list with an attached price. I figured the easiest way to do this would probably be with arrays but I don't have much experience working with arrays.

I was thinking something along the lines of storing each title(as its added) as well as the price in a variable to give a "Movie Title - $2.93" format in every line of the list box. For the sake of this problem I'm going to just post my full source code and that might make it easier to see what I'm trying to accomplish. ANY help would be MUCH appreciated. Thanks Stack overflow community! A screenshot of my project can be viewed here: http://puu.sh/54SgI.jpg

Public Class Form1
    'globablly declared because I might use them outside of btnAdd_Click event
    Const decDiscount As Double = 0.9 '1-.10 discount = .9
    Const decDVD As Decimal = 2D
    Const decBlueray As Decimal = 2.5D
    Const decDVDNew As Decimal = 3.25D
    Const decBluerayNew As Decimal = 3.5D

Dim intCount As Integer Dim decCost, decTotal As Decimal Dim decDayTotal As Decimal Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AcceptButton = btnAdd End Sub Private Sub chkDiscount_Click(sender As Object, e As EventArgs) Handles chkDiscount.Click If chkDiscount.CheckState = 1 Then chkDiscount.Enabled = False End If End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 'Display error when no title entered If txtAdd.Text = "" Then MessageBox.Show("Please enter a movie title and select the appropriate item details.", "Complete details", MessageBoxButtons.OK, MessageBoxIcon.Error) Else listMovies.Items.Add(txtAdd.Text) listMovies.SelectedIndex = listMovies.SelectedIndex + 1 End If 'update list 'clear txtbox txtAdd.Text = "" 'Decision Statements to calculate correct price If radDVD.Checked = True Then decCost = CDec(decDVD.ToString("c")) If chkNew.Checked = True Then decCost = CDec(decDVDNew.ToString("c")) End If ElseIf radBlueray.Checked = True Then decCost = CDec(decBlueray.ToString("c")) If chkNew.Checked = True Then decCost = CDec(decBlueray.ToString("c")) End If End If If chkDiscount.Checked = True Then decCost = CDec((decCost * decDiscount).ToString("c")) End If 'display cost txtCost.Text = CStr(CDec(decCost)) 'calc total decTotal = CDec(decTotal + decCost) 'display total txtTotal.Text = CStr(CDec(decTotal)) 'clear chkNew every item added to list chkNew.CheckState = 0 End Sub 'Public so summary message box can access variable Public Sub btnFinish_Click(sender As Object, e As EventArgs) Handles btnFinish.Click 'Add +1 to counter & update txtCounter intCount = CInt(Val(intCount) + 1) 'add to day total decDayTotal = CDec(Val(decDayTotal) + decTotal) 'Set Everything back to empty/enabled chkDiscount.Enabled = True chkDiscount.CheckState = 0 chkNew.CheckState = 0 txtAdd.Text = "" txtCost.Text = "" txtTotal.Text = "" decTotal = 0 decCost = 0 'Instead of clearing radios each time, a more desirable result would be to have DVD always set back to the default checked radio radDVD.Checked = True radBlueray.Checked = False listMovies.Items.Clear() End Sub Private Sub btnSummary_Click(sender As Object, e As EventArgs) Handles btnSummary.Click If decTotal > 0 Then MessageBox.Show("Please finish your current order before viewing a daily summary.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Else MessageBox.Show(("Your total cutomer count is: " & intCount) + Environment.NewLine + ("Your total sales today is: $" & decDayTotal), "Daily Summary", MessageBoxButtons.OK) End If End Sub Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click listMovies.Items.Remove(listMovies.SelectedItem) End Sub

Upvotes: 3

Views: 1916

Answers (2)

ProtoNoob
ProtoNoob

Reputation: 301

A good thing to think about when doing assignments like this (particularly when learning your first language) is to think of the algorithm (the step you need to get to your goal).

1st, determine all the steps you need to get to your goal.

2nd, and I think this is the more import point for your question, figure out what order the steps need to be in (or better yet, what order they are most efficient in).

In your case I think that you are kind of ice skating up hill by adding the name of the movie to the list first, and then trying to add the price to the line later. Unless that kind of functionality was requested as part of the assignment I would require the user to enter both the name AND the price before accepting either (just like you do with the name currently). Like thus:

If txtAdd.Text <> "" AND txtCost.Text <> "" Then  'requiring both fields to not be null
    ''add moive code
Else
    ''MessageBox.Show("Yadda Yadda Yadda")
End If

I agree with Plutonix that creating a class, while overkill in your case, is a good idea, as it will give you practice for when it WILL be appropriate. Once you have that a class of Movie, you can then create lists of Movie(s) like this:

Dim MovieList as new List(of Movie)

So then, each time you press the btnAdd button, you can pass the values to a movie AND add it to the list.

Dim m As Movie
Dim MovieList as new List(of Movie)

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    'Display error when no title entered
    If txtAdd.Text <> "" And txtCost.Text <> "" Then
        myMovie = New Movie(txtAdd.Text, txtCost.Text)
        myMovieList.Add(myMovie)
        listMovies.Items.Clear()

        For Each X As Movie In myMovieList
            listMovies.Items.Add(X.DisplayMovie)
        Next
    Else
        MessageBox.Show("Please enter a movie title and select the appropriate item details.", "Complete details", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    'Other Code
End Sub

Note the line ListMovies.Items.Add(X.DisplayMovie) I added a function to the class Movie (seen below) so that it will do the formatting as you suggested.

 Public Function DisplayMovie()
    Return Title & " - $" & Cost
 End Function

This will get you much of the way. Try to extrapolate what Plutonix and myself have explained to further refine your code. For instance, try encapsulating your adjusted price calculation in its own function so you can call it from anywhere.

Upvotes: 0

I wont go very far here because you need to do the work. But, I would start with a class:

Public Class Movie

Public Title As String = ""
Public Cost As Decimal

' prevents you from adding a movie without critical info
Public Sub New(ByVal t As String, ByVal c As Decimal)
    Title = t
    Cost = c

End Sub

End Class

This would hold the info on one movie title rental, and keep it together (and can be added to in order to print exactly as you showed) . The plan (to the extent I understand what you are after) would be to create one of these for each movie rented and add it to a List(Of Movie) this is more appropriate than a Dictionary in this case.

To create a movie:

Dim m As New Movie(theTitle, theCost)

Things I would do:

  • You did a good job of declaring numerics as numbers. Fix the code that converts it to string and back to numeric. (edit your post)
  • You can use the Movie Class to populate the "Shopping Cart" listbox alone; at which point, listMovies.Items would BE the extra credit List. But it wouldnt hurt to use/learn about List (Of T). (BTW, does 'print' mean to paper, on a printer?)
  • What are you doing with chkDiscount? If they check it, you disable it (and never enable). Did you mean to disable the New Releases check? In THAT case, arent they really a pair of radios too?
  • Either way, CheckChanged is a better event for evaluating and there is no reason to manually set the check state for the user that happens by itself.

Check out List(of T) and HTH

Upvotes: 0

Related Questions