2013-10-31 47 views
3

這個術語是VB類,我一直在困擾着一個我試圖找出的問題。我們被要求在電影出租的地方爲電影創作一個價格計算器。額外的信貸將它們存儲在列表中並且能夠列出列表。我已經得到了這麼多,現在我想更進一步,實際上以附加價格爲標題添加標題。我認爲最簡單的方法可能是使用數組,但我沒有太多處理數組的經驗。變量列表框?

我正在考慮按照存儲每個標題(作爲其添加的)以及變量中的價格在列表框的每一行中給出「電影標題 - $ 2.93」格式。爲了解決這個問題,我打算髮布我的完整源代碼,這可能會讓我更容易看到我想要完成的任務。任何幫助將非常感激。感謝堆棧溢出社區! 我的項目的屏幕截圖可以在這裏看到: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

+0

你的具體問題是什麼?如果額外的功勞是將它們存儲在列表中,不要使用數組,請使用List(String)或者可能是一個類。你也有很多冗餘代碼:'intCount = CInt(Val(intCount)+ 1)',因爲intCount是一個整數,'intCount + = 1'是你所需要的。和**從來沒有**使用Val(或Cint)看看XXX.TryParse和Convert.ToXXXX – Plutonix

+0

是的,我剛纔清理了一切,謝謝。 我的具體問題涉及字典我認爲,我不知道完成我想要完成的最佳方式。基本上,我想將電影標題添加到列表框以及條目右側的價格以及刪除事件我想從列表中刪除它們以及更新decTotal整數。感謝你目前的幫助! (:@Plutonix – TheCrispyToast

回答

0

我不會走的很遠,因爲在這裏你需要做的工作。但是,我會帶班開始:

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 

這將舉行一個電影標題的租賃信息,並保持在一起(並可以以打印完全按照自己的顯示被添加到)。該計劃(就我理解你的意思而言)將是爲每部租借的電影創建一個,並將其添加到List(Of Movie)這比在這種情況下的字典更合適。

要創建一個電影:

Dim m As New Movie(theTitle, theCost) 

事我會做的事:

  • 你做了聲明數值作爲數字的好工作。修復將其轉換爲字符串並返回數字的代碼。 (編輯您的帖子)
  • 您可以使用電影類來單獨填充「購物車」列表框;在這一點上,listMovies.Items將是額外的信用列表。但它不會傷害使用/瞭解List (Of T)。 (順便說一句,在打印機上'打印'是指打印紙嗎?)
  • 你在做什麼chkDiscount?如果他們檢查它,則禁用它(並且從不啓用)。你是否想要禁用新版本檢查?在這種情況下,他們真的是一對無線電嗎?
  • 無論哪種方式,CheckChanged是一個更好的事件進行評估,並沒有理由手動設置自己發生的用戶的檢查狀態。

退房名單(的T)和HTH

+0

我想要做的是設置一個刪除事件,它從列表中刪除標題(我已經擁有該部分),然後刪除它們添加到列表中時所對應的價格。有不同的價格因素,所以價格是不同的每個List.Items.add()條目,這就是爲什麼我假設一個字典將是一個簡單的方法來做到這一點。對於添加事件我有: lstMovies.Items.Add(txtAdd .Text) lstMovies.SelectedIndex = lstMovies.SelectedIndex + 1 myDict.Add(txtAdd.Text,decCost) @Plutonix – TheCrispyToast

0

一件好事,想想做這樣的作業(特別是學習你的第一語言時)時是考慮算法(你需要的步驟達到你的目標)。

1,確定你需要達到目標的所有步驟。

第二,我認爲這是您的問題的更重要的點,找出步驟需要在什麼順序(或更好的順序,他們是最有效的)。

在你的情況下,我認爲你是一種通過將電影名稱先添加到列表中,然後嘗試將價格添加到行中的方法。除非這種功能被要求作爲分配的一部分,否則我將要求用戶在接受任何一個(就像您當前使用的名稱一樣)之前輸入名稱的價格。像這樣的:

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 

我Plutonix同意,創建一個類,而矯枉過正你的情況,是一個好主意,因爲它會給你練習的時候它會是適當的。一旦你有一個類的電影,然後你可以這樣創建短片(S)的列表:

Dim MovieList as new List(of Movie) 

那麼,每次按btnAdd按鈕時,您可以值傳遞給電影和ADD它的名單。

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 

注行ListMovies.Items.Add(X.DisplayMovie)我添加到類電影(如下圖所示),這樣它會做的格式如你所說的功能。

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

這會給你帶來很大的方便。嘗試推斷Plutonix和我自己解釋的內容以進一步優化您的代碼。例如,嘗試將調整後的價格計算封裝在自己的函數中,以便您可以從任何地方調用它。

+0

OMG,你刪除了一個問題,我寫了一個很大的答案,你可以聯繫我,也許我可以幫助你(關於你的遊戲管理器)https://twitter.com/Waescher – Waescher