vb
' Created/revised by: Jared Clark on 4-30-14
Option Explicit On
Option Strict On
Option Infer Off
'declare class
Public Class Product
'declare and fill attributes
Private _price As Single
Private _part As String
Public Property Price() As Single
Get
Return _price
End Get
Set(ByVal value As Single)
_price = value
End Set
End Property
Public Property Part() As String
Get
Return _part
End Get
Set(ByVal value As String)
_part = value
End Set
End Property
Public Sub New()
_price = 0
_part = ""
End Sub
Public Sub New(ByVal pr As Single, ByVal pa As String)
Price = pr
Part = pa
End Sub
Public Function getPart() As String
'get part number from index
Dim index As Integer = CInt(mainForm.productListbox.SelectedIndex)
Dim productNum() As String
Dim inFile As IO.StreamReader
If IO.File.Exists("products.txt") Then
inFile = IO.File.OpenText("products.txt")
Do Until inFile.Peek = -1
'split the product description
productNum = inFile.ReadLine.Split(","c)
If productNum(1) = CStr(mainForm.productListbox.SelectedItem) Then
_part = productNum(0)
End If
Loop
Else
MsgBox("File not found. Contact System administrator")
End If
inFile.Close()
Return _part
End Function
Public Function getPrice() As Single
'get the price for the selected product
Dim index As Integer = CInt(mainForm.productListbox.SelectedIndex) + 2
Dim productPrice() As String
Dim inFile As IO.StreamReader
If IO.File.Exists("products.txt") Then
inFile = IO.File.OpenText("products.txt")
Do Until inFile.Peek = -1
'split the product description
productPrice = inFile.ReadLine.Split(","c)
If productPrice(1) = CStr(mainForm.productListbox.SelectedItem) Then
_price = CSng(productPrice(2))
End If
Loop
Else
MsgBox("File not found. Contact system administrator.")
End If
inFile.Close()
Return _price
End Function
'write listbox
Public Sub writeListBox()
Dim productDescription() As String
Dim inFile As IO.StreamReader
If IO.File.Exists("products.txt") Then
inFile = IO.File.OpenText("products.txt")
Do Until inFile.Peek = -1
'split the product description
productDescription = inFile.ReadLine.Split(","c)
mainForm.productListbox.Items.Add(productDescription(1))
Loop
Else
MsgBox("File not found. Contact system administrator.")
End If
'default the selected index to the first item
mainForm.productListbox.SelectedIndex = 0
inFile.Close()
End Sub
Public Sub writeProduct(ByVal N As String, ByVal D As String, ByVal P As Single)
Dim outFile As IO.StreamWriter
If N Like "[A-Z][A-Z]##" Then
'Write to file
outFile = IO.File.AppendText("products.txt")
outFile.WriteLine(N.Trim & "," & D.Trim & "," & P.ToString.Trim)
outFile.Close()
Else
MsgBox("The product number ought to be four digits long. It should begin with
two capital letters and end with two numbers.")
End If
End Sub
End Class