ublic Class Form1 Private Sub writeCustomers() 'write to file Dim sw As System.IO.StreamWriter sw = System.IO.File.CreateText("Customers.txt") 'write number customers sw.WriteLine(numCustomers) 'write customers For i As Integer = 0 To numCustomers - 1 sw.WriteLine(mkstrng(Customers(i))) Next 'close file sw.Close() End Sub Private Sub ReadyNextCustomer() tbName.Clear() tbPhone.Clear() tbAddress.Clear() tbBalance.Clear() tbName.Select() End Sub Private Sub loadCustomers() 'read from file Dim sr As System.IO.StreamReader sr = System.IO.File.OpenText("Customers.txt") 'read number customers Dim n As Integer = sr.ReadLine 'read customers ReDim Customers(0) Dim custStr As String = "" Dim fields() As String numCustomers = 0 For i As Integer = 1 To n ReDim Preserve Customers(numCustomers + 1) custStr = sr.ReadLine fields = custStr.Split(","c) With Customers(numCustomers) .Name = fields(0) .Phone = fields(1) .Address = fields(2) .InitialBalance = fields(3) .Balance = fields(4) End With numCustomers += 1 Next 'close file sr.Close() End Sub Private Sub showCustomers() Dim fmtStr As String = "{0,-12}{1,6}" 'go through array and display each customer lbCustomers.Items.Clear() lbCustomers1.Items.Clear() lbCustomers2.Items.Clear() lbCustomers3.Items.Clear() For i As Integer = 0 To numCustomers - 1 With Customers(i) Dim dispStr As String = String.Format(fmtStr, .Name, FormatCurrency(.Balance)) lbCustomers.Items.Add(dispStr) lbCustomers1.Items.Add(dispStr) lbCustomers2.Items.Add(dispStr) lbCustomers3.Items.Add(dispStr) End With Next End Sub Private Function makeStatement(ByVal c As Customer, ByVal month As Integer) As statement Dim s As statement s.ChargeList = New List(Of Charge) s.paymentlist = New List(Of payment) s.SCustomer = c Dim sn As String = c.Name Dim m As Integer = month Dim i As Integer For i = 0 To numCharges - 1 If sn = Charges(i).customername Then If Charges(i).ChargeDate.Month = m Then s.ChargeList.Add(Charges(i)) End If End If Next For i = 0 To numpayments - 1 If sn = payments(i).CustomerName Then If payments(i).PaymentDate.Month = m Then s.paymentlist.Add(payments(i)) End If End If Next Return s End Function Private Sub showStatment(ByVal s As statement) Dim fmtStr As String = "{0,-20}{1,-20}" Dim fmtstr1 As String = "{0,15}" Dim fmtstr2 As String = "{0,-12}{1,-10}{2,-10}" lbStatement.Items.Add(String.Format(fmtStr, "Customer", selectedCustomer.Name)) lbStatement.Items.Add(String.Format(fmtStr, "Address", selectedCustomer.Address)) Dim ib As Double If selectedCustomer.InitialBalance = "" Then ib = 0 Else ib = selectedCustomer.InitialBalance End If lbStatement.Items.Add(String.Format(fmtStr, "Initial Balance", FormatCurrency(ib))) lbStatement.Items.Add(String.Format(fmtStr, "Ending Balance", FormatCurrency(selectedCustomer.Balance))) lbStatement.Items.Add("") lbStatement.Items.Add(String.Format(fmtstr1, "Charges")) For Each c As Charge In s.ChargeList Dim a As String = FormatCurrency(FormatCurrency(c.ChargeAmount)) lbStatement.Items.Add(String.Format(fmtstr2, c.ItemName, a, c.ChargeDate.ToShortDateString)) Next lbStatement.Items.Add("") lbStatement.Items.Add(String.Format(fmtstr1, "Payments")) For Each p As payment In s.paymentlist Dim a As String = FormatCurrency(FormatCurrency(p.PaymentAmount)) lbStatement.Items.Add(String.Format(fmtStr, a, p.PaymentDate.ToShortDateString)) Next End Sub Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click Dim newItem As Item With newItem .itemName = tbItem.Text .ItemCost = tbPrice.Text End With 'clear text boxes tbItem.Clear() tbPrice.Clear() tbItem.Select() 'add new item to array NumItems += 1 ReDim Preserve StoreItems(NumItems) StoreItems(NumItems - 1) = newItem 'save items saveItems() 'display store items showitems() End Sub Private Sub saveItems() Dim sveItem As Item Dim sw As System.IO.StreamWriter sw = System.IO.File.CreateText("items.txt") sw.WriteLine(NumItems) For i As Integer = 0 To NumItems - 1 sveItem = StoreItems(i) sw.WriteLine(mkStr(sveItem)) Next sw.Close() End Sub Private Sub showitems() lbitems.Items.Clear() For i As Integer = 0 To NumItems - 1 With StoreItems(i) Dim n As String = .itemName Dim c As String = FormatCurrency(.ItemCost) Dim dispItem As String = String.Format("{0,-12}{1,7}", n, c) lbitems.Items.Add(dispItem) End With Next End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'read customer file and populate customers array loadCustomers() showCustomers() 'read charges and show them readcharges() showcharges() 'read items and show them loaditems() showitems() 'read payments and show them loadPayments() showPayments() End Sub Private Sub loaditems() Dim sr As System.IO.StreamReader sr = System.IO.File.OpenText("Items.txt") NumItems = sr.ReadLine Dim nxtItem As Item, x As String, fields() As String ReDim StoreItems(NumItems) For i As Integer = 0 To NumItems - 1 x = sr.ReadLine fields = x.Split(","c) With nxtItem .itemName = fields(0) .ItemCost = fields(1) End With StoreItems(i) = nxtItem Next sr.Close() showitems() End Sub Private Sub loadpayments() Dim sr As System.IO.StreamReader sr = System.IO.File.OpenText("Payments.txt") Dim nxtPayment As payment, x As String, fields() As String 'read number of payments numpayments = sr.ReadLine ReDim payments(numpayments) For i As Integer = 0 To numpayments - 1 x = sr.ReadLine fields = x.Split(","c) With nxtPayment .CustomerName = fields(0) .PaymentAmount = fields(1) .PaymentDate = fields(2) End With payments(i) = nxtPayment Next sr.Close() End Sub Private Sub showpayments() Dim fmtstr As String = "{0,-15}{1,-10}{2,-10}" lbPayments.Items.Clear() For i As Integer = 0 To numpayments - 1 With payments(i) Dim displayStr As String displayStr = String.Format(fmtstr, .CustomerName, .PaymentAmount, .PaymentDate.ToShortDateString, .PaymentAmount, .PaymentDate) lbPayments.Items.Add(displayStr) End With Next End Sub Private Sub btnAddCharge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCharge.Click Dim newCharge As Charge With newCharge 'get item Dim itemIndex As Integer = lbitems.SelectedIndex If itemIndex >= 0 And itemIndex <= NumItems - 1 Then .ItemName = StoreItems(itemIndex).itemName .ChargeAmount = StoreItems(itemIndex).ItemCost Else MsgBox("Select an item") Exit Sub End If 'get customer Dim customerIndex As Integer = lbCustomers1.SelectedIndex If customerIndex >= 0 And customerIndex <= numCustomers - 1 Then .customername = Customers(customerIndex).Name .ChargeDate = Today.ToShortDateString ‘add item cost to customer balance ** 'save customers to file writeCustomers() showCustomers() 'show new balance tbSaleBalance.Text = Customers(customerIndex).Balance Else MsgBox("Select a customer") Exit Sub End If End With numCharges += 1 ReDim Preserve Charges(numCharges) Charges(numCharges - 1) = newCharge 'save charges savecharges() 'show charges showcharges() End Sub Private Sub savecharges() 'array charges -> text file charges.txt Dim sw As System.IO.StreamWriter sw = System.IO.File.CreateText("Charges.txt") sw.WriteLine(numCharges) Dim i As Integer For i = 0 To numCharges - 1 With Charges(i) ‘make charges(i) into comma delimited string and ‘write to file ** End With Next sw.Close() End Sub Private Sub readcharges() 'text file charges.txt -> array charges Dim sr As System.IO.StreamReader sr = System.IO.File.OpenText("Charges.txt") numCharges = sr.ReadLine Dim i As Integer ReDim Charges(numCharges) For i = 0 To numCharges - 1 With Charges(i) Dim fields() As String = sr.ReadLine.Split(","c) ‘populate structure variables with fields ** End With Next sr.Close() End Sub Private Sub showcharges() Dim i As Integer lbCharges.Items.Clear() Dim fmtStr As String = "{0,-15}{1,-15}{2,-8}{3,8}" For i = 0 To numCharges - 1 With Charges(i) Dim c As String = .customername Dim itm As String = .ItemName Dim a As String = FormatCurrency(.ChargeAmount) Dim d As String = .ChargeDate.ToShortDateString Dim s As String = String.Format(fmtStr, c, itm, a, d) lbCharges.Items.Add(s) End With Next End Sub Private Sub lbCustomers_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbCustomers.SelectedIndexChanged Dim custIdx As Integer = lbCustomers.SelectedIndex 'MsgBox(custIdx & "," & numCustomers) 'display customer in text boxes With Customers(custIdx) tbName.Text = .Name tbPhone.Text = .Phone tbAddress.Text = .Address tbBalance.Text = .Balance End With 'remove customer from Customers For k As Integer = custIdx To numCustomers - 2 Customers(k) = Customers(k + 1) Next numCustomers -= 1 ReDim Preserve Customers(numCustomers) showCustomers() writeCustomers() End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 'Create new customer numCustomers += 1 ReDim Preserve Customers(numCustomers - 1) 'populate field variables of new customer With Customers(numCustomers - 1) .Name = tbName.Text .Phone = tbPhone.Text .Address = tbAddress.Text .Balance = 0 'add name to listbox lbCustomers.Items.Add(.Name) End With writeCustomers() 'ready for next customer ReadyNextCustomer() End Sub Private Sub btnCharges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) readcharges() showcharges() End Sub Private Sub lbCustomers2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbCustomers2.SelectedIndexChanged Dim payingCustomer As Customer 'get customer and fill textboxes payingCustomer = Customers(lbCustomers2.SelectedIndex) With payingCustomer tbAccountName.Text = .Name tbAccountBalance.Text = .Balance tbAccountAddress.Text = .Address End With 'set label on statement selectedCustomer = lbCustomers.SelectedItem lblStatement.Text &= " - " & selectedCustomer.Name End Sub Private Sub btnPayOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPayOK.Click Dim curPayment As payment 'fill payment fields with textboxes With curPayment ** End With 'adjust customer balance Customers(lbCustomers2.SelectedIndex).Balance -= CDbl(tbPayment.Text) ‘Redimension Payments array and add curPayment ‘save payments to text file and show in listbox End Sub Private Sub savePayments() Dim nxtPayment As payment, payStr As String Dim sw As System.IO.StreamWriter sw = System.IO.File.CreateText("Payments.txt") 'write number payments ** 'for each payment For i As Integer = 0 To numpayments - 1 'build comma delimited string ** Next sw.Close() End Sub Private Sub lbMonths_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbMonths.SelectedIndexChanged selectedmonth = lbMonths.SelectedIndex + 1 textMonth = lbMonths.SelectedItem lblStatement.Text = "Statement - " & selectedCustomer.Name & " - " & lbMonths.SelectedItem Dim s As statement = makeStatement(selectedCustomer, selectedmonth) showStatment(s) End Sub Private Sub lbCustomers3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbCustomers3.SelectedIndexChanged selectedCustomer = Customers(lbCustomers3.SelectedIndex) End Sub Private Sub lbitems_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbitems.SelectedIndexChanged tbSaleItem.Text = StoreItems(lbitems.SelectedIndex).itemName tbSalePrice.Text = StoreItems(lbitems.SelectedIndex).ItemCost End Sub Private Sub lbCustomers1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbCustomers1.SelectedIndexChanged tbSaleCustomer.Text = Customers(lbCustomers1.SelectedIndex).Name tbSaleBalance.Text = Customers(lbCustomers1.SelectedIndex).Balance End Sub Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click pdStatement.Print() End Sub Private Sub pdStatement_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdStatement.PrintPage Dim strFmt As String = "{0,5}{1,-20}{2,-25}{3,-15}", printLine As String Dim m As Integer = lbStatement.Items.Count - 1, i As Integer Dim curY As Integer = 20, curx As Integer = 20 e.Graphics.DrawString(textMonth & " Statement for " & selectedCustomer.Name, New Font("Courier New", 14, _ FontStyle.Bold), Brushes.Black, curx, curY) curY += 30 For i = 0 To m - 1 With lbStatement.Items(i) printLine = lbStatement.Items(i) e.Graphics.DrawString(printLine, New Font("Courier New", 10, _ FontStyle.Bold), Brushes.Black, curx, curY) End With curY += 20 Next End Sub Private Sub pdCustomers_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdCustomers.PrintPage Dim strFmt As String = "{0,-15}{1,-20}{2,-12}", printLine As String Dim i As Integer Dim curY As Integer = 20, curx As Integer = 20 e.Graphics.DrawString("Customer List", New Font("Courier New", 14, _ FontStyle.Bold), Brushes.Black, curx, curY) curY += 30 For i = 0 To numCustomers - 1 With Customers(i) 'ith customer printLine = String.Format(strFmt, .Name, .Address, .Phone) e.Graphics.DrawString(printLine, New Font("Courier New", 10, _ FontStyle.Bold), Brushes.Black, curx, curY) End With curY += 20 Next End Sub Private Sub btnPrintCustomers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintCustomers.Click pdCustomers.Print() End Sub End Class Module Globals #Region "Customers" Public Structure Customer Dim Name As String Dim Phone As String Dim Address As String Dim InitialBalance As String Dim Balance As Double End Structure Public Customers() As Customer Public numCustomers As Integer Public Function mkstrng(ByVal c As Customer) As String Dim s As String With c s = .Name & "," & .Phone & "," & .Address & _ "," & .InitialBalance & "," & .Balance End With Return s End Function Public Function showCust(ByVal c As Customer) As String Dim s As String With c s = .Name & " - " & .Balance End With Return s End Function #End Region #Region "Sales" 'item structure Public Structure Item Dim itemName As String Dim ItemCost As Double End Structure Public StoreItems() As Item Public NumItems As Integer Public Function mkStr(ByVal x As Item) As String With x Return .itemName & "," & .ItemCost End With End Function 'charge structure Structure Charge Dim customername As String Dim ItemName As String Dim ChargeDate As Date Dim ChargeAmount As Double End Structure Public Charges() As Charge Public numCharges As Integer #End Region #Region "Accounts" 'payments Public Structure payment Dim CustomerName As String Dim PaymentAmount As Double Dim PaymentDate As Date End Structure Public payments() As payment Public numpayments As Integer 'statement Public Structure statement Dim SCustomer As Customer Dim statementMonth As String 'charges and payments within statement month 'statement written to text file named SCustomer & statementMonth & .txt Dim ChargeList As List(Of Charge) Dim paymentlist As List(Of payment) End Structure Public statements() As String 'name of file Public numStatements As Integer Public selectedCustomer As Customer Public selectedmonth As Integer Public textMonth As String #End Region End Module