locked
Problem With loop continuing till a certain point is reached RRS feed

  • Question

  • The assignment is as follows: 

    1. Create an Item Class. It should be able to store one string, one integer and one double.
    2. Create an ItemList Class. This class should contain an array of Items. You may hard code the number of items in the initial array or use ReDim to increase the size of the array before you insert new items into the array. This class should allow for expanding the size of the array if necessary and must include a function to generate a receipt as a string for all of the items in the Item array.
    3. Create an application interface that asks the user to enter 3 values: text string (Part Name), a quantity, and a sale's' price.
    4. Once the 3 values are all entered, the user can press a button. Store these values in the array of Item class objects in the ItemList. Clear the text boxes for the next item.
    5. When the user is done entering N items into the ItemList, they may push a second button in the UI to save a receipt. The string of text generated by the button push should call the function in the ItemList class that creates a receipt as a string and save that string to a file with the name Receipt_DATE.txt where DATE is todays date as YYYY-MM-DD.

    Ive gotten everything done but generating the array to a text document. the problem im having is the loop doesnt stop when the number of items in the cart is met, so it crashes. heres the code i have so far: 

    Imports System.IO
    Imports System.Text


    Public Class Form1

        Private ShoppingCart As New ItemList
        Private Counter As Integer = 0

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
        End Sub
        Dim Items() As Item
        Dim current As Integer = 0
        Function GetInteger(s As String) As Integer
            Dim v As Integer
            If Integer.TryParse(s, v) Then Return v
            Return 0
        End Function
        Function GetDouble(s As String) As Double
            Dim v As Double
            If Double.TryParse(s, v) Then Return v
            Return 0D
        End Function

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

            Dim ThisItem As New Item

            Counter = Counter + 1
            Label7.Text = Counter

            If Counter > 9 Then
                MessageBox.Show("Cart is Full!")
                Counter = Counter - 1
            End If

            If TextBox1.Text = "" Then
                MessageBox.Show("Error:" & vbCrLf & "Must fill in all boxes")
                Counter = Counter - 1
            End If

            If (IsNumeric(TextBox2.Text)) = False Then
                MessageBox.Show("Error:" & vbCrLf & "Entry for quantity is not a number")
                Counter = Counter - 1
            End If

            If (IsNumeric(TextBox3.Text)) = False Then
                MessageBox.Show("Error" & vbCrLf & "Entry for price is not a number")
                Counter = Counter - 1
            End If

            With ThisItem
                .Quantity = GetInteger(TextBox2.Text)
                .PartName = TextBox1.Text
                .Price = GetDouble(TextBox3.Text)
            End With
            ShoppingCart.Items(Counter) = ThisItem

            TextBox1.Clear()
            TextBox2.Clear()
            TextBox3.Clear()
        End Sub

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

            Dim sbItem As StringBuilder = New StringBuilder()

            For i As Integer = 0 To ShoppingCart.Items.Count
                Do Until i = ShoppingCart.Items.Count

                    Dim TempItem As Item = ShoppingCart.Items(i)
                    sbItem.AppendFormat(" {0},{1},{2}{3}", TempItem.PartName, TempItem.Quantity, TempItem.Price, ControlChars.NewLine)
                Loop
            Next
            Dim ReceiptContents As String = sbItem.ToString()
            Dim Writer As StreamWriter = File.CreateText("Hw 6/bin/Debug/Receipt_" & Date.Now.ToString("YYYYMMDD") & ".txt")
            Writer.WriteLine(ReceiptContents)
            Writer.Close()

        End Sub
    End Class

    The loop in question is in the last part under "button2_click"

    Is "  Do Until i = ShoppingCart.Items.Count " not the right way to go about it?

    Monday, March 26, 2018 6:59 PM

All replies

  • Use Count-1.  the array is zero based.

    Lloyd Sheen

    Monday, March 26, 2018 7:20 PM

  • The loop in question is in the last part under "button2_click"

    Is "  Do Until i = ShoppingCart.Items.Count " not the right way to go about it?

    Hi

    I would have done something like this. This example needs a multiline TextBox1 on Form1.

    Option Strict On
    Option Explicit On
    
    Public Class Form1
      Private ShoppingCart As New List(Of Item)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i As Integer = 10 To 15
          Dim item As New Item With {.PartName = "Part" & i.ToString, .Price = CDec(i * 2.5), .Quantity = i * 800}
          ShoppingCart.Add(item)
        Next
    
        Dim sbItem As Text.StringBuilder = New Text.StringBuilder()
    
        For Each TempItem As Item In ShoppingCart
          sbItem.AppendFormat(" {0},{1},{2}{3}", TempItem.PartName, TempItem.Quantity, TempItem.Price, ControlChars.NewLine)
        Next
    
        TextBox1.Text = sbItem.ToString
      End Sub
    End Class
    Class Item
      Property PartName As String
      Property Quantity As Integer
      Property Price As Decimal
    End Class


    Regards Les, Livingston, Scotland

    Monday, March 26, 2018 7:23 PM
  • Use Count-1.  the array is zero based.

    Lloyd Sheen

    HI Lloyd,

    Because of a strange decision in version VB7 (2002) both can be used (to keep it in line with the older functions. Initially an empty array has 1 item more. 


    Success
    Cor

    Tuesday, March 27, 2018 12:00 AM