如何改进此代码以使其更简洁高效?

时间:2015-10-18 12:23:05

标签: vb.net optimization

Visual Basic

这是我编写的程序代码的最后一部分。最后一部分(如下所示)显示收据。但是,它有很多重复的代码,所以我认为它可以通过一些更好的代码来改进。但是,我不知道该怎么做。

那么,你能不能改进代码,使它更简洁,更简洁(可能有一个循环?)并写出改进的代码。如果您还能解释改进的代码,我将非常感激。

以下是我希望您改进的Visual Basic代码:

    Dim itemName (4) As String
    Dim priceOfItem(4) As Decimal 
    Dim amountOrdered(4) As Decimal 
    'the "completePriceOfItem" array is simply the "priceOfItem" multipled by the "amountOrdered" but ,as this is only a... 
    '...part of my program, this has already been processed and assigned to the aray.
    Dim completePriceOfItem(4) As Decimal
    'setting  up variables
    Dim numberOfItems As Integer = 0

    'final section of program where it displays the receipt
    Console.WriteLine("Receipt:")
    Console.WriteLine
    If numberOfItems = 1 Then
        Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total")
    ElseIf numberOfItems = 2 Then
        Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total")
        Console.WriteLine(itemName(1) & ": " & Format (priceOfItem(1), "Currency") & " each" & ", " & amountOrdered(1) & " bought" & ", " & Format (completePriceOfItem(1), "Currency") & " in total")
    ElseIf numberOfItems = 3 Then
        Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total")
        Console.WriteLine(itemName(1) & ": " & Format (priceOfItem(1), "Currency") & " each" & ", " & amountOrdered(1) & " bought" & ", " & Format (completePriceOfItem(1), "Currency") & " in total")
        Console.WriteLine(itemName(2) & ": " & Format (priceOfItem(2), "Currency") & " each" & ", " & amountOrdered(2) & " bought" & ", " & Format (completePriceOfItem(2), "Currency") & " in total")
    ElseIf numberOfItems = 4 Then
        Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total")
        Console.WriteLine(itemName(1) & ": " & Format (priceOfItem(1), "Currency") & " each" & ", " & amountOrdered(1) & " bought" & ", " & Format (completePriceOfItem(1), "Currency") & " in total")
        Console.WriteLine(itemName(2) & ": " & Format (priceOfItem(2), "Currency") & " each" & ", " & amountOrdered(2) & " bought" & ", " & Format (completePriceOfItem(2), "Currency") & " in total")
        Console.WriteLine(itemName(3) & ": " & Format (priceOfItem(3), "Currency") & " each" & ", " & amountOrdered(3) & " bought" & ", " & Format (completePriceOfItem(3), "Currency") & " in total") 
    ElseIf numberOfItems = 5 Then
        Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total")
        Console.WriteLine(itemName(1) & ": " & Format (priceOfItem(1), "Currency") & " each" & ", " & amountOrdered(1) & " bought" & ", " & Format (completePriceOfItem(1), "Currency") & " in total")
        Console.WriteLine(itemName(2) & ": " & Format (priceOfItem(2), "Currency") & " each" & ", " & amountOrdered(2) & " bought" & ", " & Format (completePriceOfItem(2), "Currency") & " in total")
        Console.WriteLine(itemName(3) & ": " & Format (priceOfItem(3), "Currency") & " each" & ", " & amountOrdered(3) & " bought" & ", " & Format (completePriceOfItem(3), "Currency") & " in total") 
        Console.WriteLine(itemName(4) & ": " & Format (priceOfItem(4), "Currency") & " each" & ", " & amountOrdered(4) & " bought" & ", " & Format (completePriceOfItem(4), "Currency") & " in total")
    End If
    Console.ReadLine

2 个答案:

答案 0 :(得分:1)

以下是我如何更好地制作此代码。

我创建了几个课程。一个用于Order,另一个用于OrderLine

Public Class Order

    Public Property Lines As List(Of OrderLine)

    Public Sub New()
        Lines = New List(Of OrderLine)()

    End Sub

End Class

Public Class OrderLine

    Public Property itemName As String


    Public Property priceOfItem As Decimal

    Public Property amountOrdered As Decimal

    Public ReadOnly Property CompletePriceOfItem() As Decimal
        Get
            Return priceOfItem * amountOrdered
        End Get
    End Property

End Class

然后我会像这样更改Main代码:

Dim Orders As List(Of Order) = New List(Of [Order])()

'Create a new Order, and add OrderLine to it
Dim o As New Order()
Dim ol As OrderLine

ol = New OrderLine()
ol.itemName = "Item1"
ol.priceOfItem = 10.99
ol.amountOrdered = 3

o.Lines.Add(ol)

Orders.Add(o)

'final section of program where it displays the receipt
Console.WriteLine("Receipt:")
Console.WriteLine()

For Each ord As Order In Orders

    For Each ordline As OrderLine In ord.Lines
        Console.WriteLine(ordline.itemName & ": " & Format(ordline.priceOfItem, "Currency") & " each" & ", " & ordline.amountOrdered & " bought" & ", " & Format(ordline.completePriceOfItem, "Currency") & " in total")

    Next
Next

Console.ReadLine()

因此,我使用List(Of...)代替硬编码项目数量,而不是根据需要增长。

此外,使用OrderLine类不需要单独的数组。

最后,我没有为CompletePriceOfItem创建一个单独的数组,而是让课程'属性处理计算。

现在这只是一个粗略的概念,但我认为你会得到这个想法。

干杯

答案 1 :(得分:0)

您可以使用简单的for循环。将if逻辑替换为以下内容......

For index As Integer = 0 To numberOfItems - 1
    Console.WriteLine(itemName(index) & ": " & Format (priceOfItem(index), "Currency") & " each" & ", " & amountOrdered(index) & " bought" & ", " & Format (completePriceOfItem(index), "Currency") & " in total")
Next

变量index每个循环都会递增,这意味着您可以使用变量名“0”替换12index等以正确的索引提取数据。

循环将运行numberOfItems变量中定义的次数。

相关问题