|
File Processing Application: Sales Report |
|
|
This example explores various apects of Microsoft Visual
Basic, including its own support for file processing through its My namespace.
|
Imports System.IO
Public Class Exercise
REM We will use this class to hold information about a salesperson
Private Class SalesPerson
Public Name As String
Public Amount As Decimal
End Class
Private Sub BtnOpen_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnOpen.Click
' This variable will be used in a For loop
Dim i As Integer
Dim j As Integer
' This variable will be used to identify each line in the file
Dim strLine As String
' This variable will be used to internally identify the parts in a line
Dim LineOfRecord As String()
' This variable will hold the total sales
Dim TotalSales As Decimal
' This is the name of the file
Dim SalesReport As String
' This variable will hold the highest sale amount
Dim HighestSale As Decimal
' This variable will hold the name of the person with the highest sale
Dim HighestSalesPerson As String
' We will use a TextFieldParser object.
' We are using it because it can scan the file to identify a delimiter
Dim SalesReader As FileIO.TextFieldParser
Dim Slr As SalesPerson
Dim InListAlready As Boolean
Dim SalesPersons As List(Of SalesPerson)
InListAlready = False
SalesPersons = New List(Of SalesPerson)
' Initialize the variables
TotalSales = 0
HighestSale = 0
HighestSalesPerson = ""
SalesReport = "SalesData.txt"
' Initialize the opening of the file
SalesReader = My.Computer.FileSystem.OpenTextFieldParser(SalesReport)
' Indicate that the file uses delimiters
SalesReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
' Indicate the type of delimiters that the file is using
SalesReader.Delimiters = New String() {","}
' Scan the file to the end
While Not SalesReader.EndOfData
' Use exception handling to handle exceptions, just in case
Try
' Get a reference to an object of the list view
Dim LviPerson As ListViewItem
' Read a line in the file
strLine = SalesReader.ReadLine()
' Split the text in the line by delimiting the parts by commas
LineOfRecord = strLine.Split(New [Char]() {","c})
' For each item in the line, ...
For i = 0 To LineOfRecord.Length
' ... create a list view object
LviPerson = New ListViewItem(LineOfRecord(i))
LviPerson.SubItems.Add(LineOfRecord(i + 1))
LviPerson.SubItems.Add(LineOfRecord(i + 2))
LviPerson.SubItems.Add(LineOfRecord(i + 3))
LviPerson.SubItems.Add(LineOfRecord(i + 4))
LviPerson.SubItems.Add((CInt(LineOfRecord(i + 3)) * CDbl(LineOfRecord(i + 4))).ToString("F"))
' Show the (completed) object to the list view
LvwSalesPersons.Items.Add(LviPerson)
' Update the total sales
TotalSales += ((CInt(LineOfRecord(i + 3)) * CDbl(LineOfRecord(i + 4))))
Next
Catch ex As Exception
End Try
End While
If LvwSalesPersons.Items.Count > 0 Then
Dim ss As SalesPerson = Nothing
For k As Integer = 0 To LvwSalesPersons.Items.Count - 1
For Each s In SalesPersons
If s.Name.Equals(LvwSalesPersons.Items(k).SubItems(0).Text) Then REM Already in the list
ss = s
InListAlready = True
Exit For
End If
Next
If InListAlready = False Then
Slr = New SalesPerson
Slr.Name = LvwSalesPersons.Items(k).SubItems(0).Text
Slr.Amount = CDec(LvwSalesPersons.Items(k).SubItems(5).Text)
SalesPersons.Add(Slr)
Else
ss.Amount = ss.Amount + CDec(LvwSalesPersons.Items(k).SubItems(5).Text)
End If
Next
Dim LviSummary As ListViewItem
For j = 0 To SalesPersons.Count - 1
LviSummary = New ListViewItem(SalesPersons(j).Name)
LviSummary.SubItems.Add(SalesPersons(j).Amount.ToString())
lvwSummarySales.Items.Add(LviSummary)
Next
For Each LviSummary In lvwSummarySales.Items
If CDec(LviSummary.SubItems(1).Text) > HighestSale Then
HighestSale = CDec(LviSummary.SubItems(1).Text)
ss.Name = LviSummary.SubItems(0).Text
ss.Amount = HighestSale
End If
Next
' Show the total sales
TxtTotalSales.Text = TotalSales.ToString("F")
' Display the name of the person with the highest sale
TxtHighestSeller.Text = ss.Name REM HighestSalesPerson
' Display the amount of the highest sale
TxtHighestSale.Text = ss.Amount.ToString("F")
End If
End Sub
End Class
|
|