这个学期学习VB课程,我一直在努力解决一个我想弄清楚的问题。我们被要求在电影租赁场所为电影片头创建一个价格计算器。额外的功劳是将它们存储在列表中并能够打印列表。我已经走得那么远了,现在我想更进一步,实际上以附加价格为该列表添加标题。我认为最简单的方法可能是使用数组,但我没有多少使用数组的经验。
我正在考虑存储每个标题(如其添加的)以及变量中的价格,以在列表框的每一行中给出“电影标题 - $ 2.93”格式。为了解决这个问题,我将发布完整的源代码,这可能会让我更容易看到我想要完成的任务。任何帮助将非常感激。谢谢Stack溢出社区! 我的项目的屏幕截图可以在这里查看: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 :(得分: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
答案 1 :(得分:0)
在做这样的作业时(特别是在学习第一语言时)考虑的好处是要考虑算法(达到目标所需的步骤)。
1,确定实现目标所需的所有步骤。
第二,我认为这是你的问题的更重要的一点,弄清楚步骤需要的顺序(或者更好的是,他们最有效的顺序)。
在你的情况下,我认为你是通过首先将电影的名称添加到列表中,然后尝试将价格添加到线上来滑冰。除非作为作业的一部分请求了这种功能,否则我会要求用户在接受之前输入名称 AND 的价格(就像你当前使用的名称一样)。像这样:
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的观点,即创建一个类,虽然你的情况有点过分,但这是一个好主意,因为它会让你在适当的时候练习。一旦你拥有了一类电影,你就可以创建这样的电影列表:
Dim MovieList as new List(of Movie)
那么,每次按btnAdd按钮,您都可以将值传递给电影并将其添加到列表中。
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)我在类Movie中添加了一个函数(如下所示),以便它按照你的建议进行格式化。
Public Function DisplayMovie()
Return Title & " - $" & Cost
End Function
这将为您提供很多帮助。尝试推断Plutonix和我自己解释的内容,以进一步优化您的代码。例如,尝试将调整后的价格计算封装在自己的函数中,以便可以从任何地方调用它。