4 Replies - 1396 Views - Last Post: 26 March 2011 - 10:47 AM Rate Topic: -----

#1 Msqueen082   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-February 11

Logic using files and parallel lists

Posted 25 March 2011 - 05:53 PM

Hello,,,
Could sure appreciate some help and a point in the right direction with some logic using files and parallel list.. I have to create a summary of a current clients portfolio using the client id I have in a textbox. I've everything to work except the summary of the client. I have several sub functions that I can use that I've made up in my code. They are frmMain_Load that have a file and a string holding the client txtfile to a file
Dim clientFile As System.IO.StreamReader 'file of client data
      Dim clientFileName As String = "faclient.txt" '

And I have two functions Private Sub GetStockData(ByVal sCode As String, ByRef sName As String,ByRef sPrice As Double) and Private Sub GetNextTran(ByRef tid As String, ByRef tcode As String, ByRef tdate As Date, ByRef ttype As String, ByRef tshares As Integer, ByRef tprice As Double). I have to add to a label,a summary tha shows the stock code, stock name, number of shares currently held, current share price and value(shares times current price). The number of shares currently help will be the result of adding the shares bought and subracting the shares sole for that stock. If the number of shares help turns out to be zero(client sold all the shares they bought), do not display the line. The program should not assume there is an upper limit to the number of different stocks a client can hold. I know is alot..But here is what I have so far..
 
Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click
        'get a summary of client portfolio and value
        Dim summaryCodeList As New List(Of String) 'holds scode 
        Dim sharesList As New List(Of Integer) 'holds tshares

        Dim tid As String = ""
        Dim tcode As String = ""
        Dim tdate As Date
        Dim ttype As String = ""
        Dim tshares As Integer
        Dim tprice As Double
        Dim sname As String = ""
        Dim sprice As Double
        Dim total As Double

        'position the file at its beginning
        Call ResetTranFile()

        lblDisp.Text = "code".PadRight(9) & "Name".PadRight(15) & "shares".PadRight(10) & "price".PadRight(11) & "value" & vbCrLf

        Do While tranFile.Peek <> -1
            Call GetNextTran(tid, tcode, tdate, ttype, tshares, tprice)
            If tid = txtTargetID.Text Then
                If summaryCodeList.Contains(tcode) Then
                    If ttype = "B" Then
                        sharesList.Item(summaryCodeList.IndexOf(tcode)) += tshares
                    Else
                        sharesList.Item(summaryCodeList.IndexOf(tcode)) -= tshares
                    End If
                Else
                    summaryCodeList.Add(tcode)
                    sharesList.Add(tshares)

                    If ttype = "B" Then
                        sharesList.Add(tshares)
                        summaryCodeList.Add(tcode)
                    End If
                End If
            End If

            'For Each i As Integer In summaryCodeList
            '    Call GetStockData(summaryCodeList.Item(i), sname, sprice)
            '    value = sharesList.Item(summaryCodeList.IndexOf) * sprice
            'Next

            lblDisp.Text += tcode.PadRight(5) & sname.PadRight(19) & tshares.ToString.PadRight(10) & _
              FormatCurrency(sprice).PadRight(11) & FormatCurrency(total) & vbCrLf
        Loop

        lblDisp.Text += "Total portfolio value as of " & tdate & "  =  " & FormatCurrency(total).ToString
    End Sub


I am probably not doing it right, but it is that last thing I have to do for this project. could sure use some help.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Logic using files and parallel lists

#2 Elda   User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: Logic using files and parallel lists

Posted 25 March 2011 - 06:37 PM

Would you please point out what's the problem?
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: Logic using files and parallel lists

Posted 25 March 2011 - 07:00 PM

OK, I'm going to admit I've not studied your code completely but List (Of Integer) has extended methods (ie, count, sum, average, etc...) If you can't assign these with something like lblTotalShares.Text = Shares.Count then it may be that you are trying to access it outside its scope. If this is the case, try moving the declaration (dim Shares as New List (of Integer)) to form level. For example:

Public Class Form1
Dim Shares As New List (Of Integer) ' This is now global to all routines contained in the form. you can access it inside any sub or function that you have in this form.


I apologize if I'm way off base with your issue but I don't completely understand what problem is occurring from your description.
Was This Post Helpful? 1
  • +
  • -

#4 Msqueen082   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-February 11

Re: Logic using files and parallel lists

Posted 25 March 2011 - 07:24 PM

No that is ok...I probably overexplain myself. Im just overwhelmed with this summary part. I kind of adjusted it and my labels not at least shoe values. But :dozingoff: not the values I want of course...
To put it simple..the label should display only stocks that they have left. Like
code name shares price value
Frd Ford Moter Corp. 300 $275.00 $82,500.00
ENRON Enron Energy 200 $0.45 $ 90.00
DRYMF Dreyfus mutual fund 100 $25.20 $2,520.00

The number of shares currently held will be the esult of adding the shares bought (ttyp "B") and subtracting the shares sold if it is (ttype "S")
And then the value is shares * price.
Here is my whole code ..
Option Strict On
Option Explicit On

Public Class frmMain
    Dim clientLastNameList As New List(Of String)
    Dim clientFirstNameList As New List(Of String)
    Dim clientIDList As New List(Of Integer)  'list of all ids
    Dim clientPhoneList As New List(Of String) 'list of phone #s

    Dim tranFile As System.IO.StreamReader 'file of transaction data
    Dim tranFileName As String = "fatrans.txt"

    Private Sub btnGetClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetClient.Click
        'search lists for target client id, display name, phone if found

        Dim ndx As Integer

        'position the file at its beginning
        Call ResetTranFile()

        If clientIDList.Contains(CInt(txtTargetID.Text)) Then
            For ndx = 0 To clientIDList.Count - 1
                If clientIDList.Item(ndx) = CInt(txtTargetID.Text) Then
                    txtClientName.Text = clientFirstNameList.Item(ndx) & ", " & _
                                clientLastNameList.Item(ndx).Trim
                    txtClientPh.Text = clientPhoneList.Item(ndx)
                End If
            Next
        End If
    End Sub

    Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click
        'get a summary of client portfolio and value
        'Dim summaryCodeList As New List(Of String) 'holds scode 
        'Dim sharesList As New List(Of Integer) 'holds tshares

        Dim tid As String = ""
        Dim tcode As String = ""
        Dim tdate As Date
        Dim ttype As String = ""
        Dim tshares As Integer
        Dim tprice As Double
        Dim sname As String = ""
        Dim scode As String = ""
        Dim sprice As Double
        Dim total As Double

        'position the file at its beginning
        Call ResetTranFile()

        If txtTargetID.Text = "" Or txtYear.Text = "" Then
            MessageBox.Show("Enter a valid client id")
            Exit Sub
        End If

        lblDisp.Text = "code".PadRight(9) & "Name".PadRight(23) & "shares".PadRight(10) & "price".PadRight(11) & "value" & vbCrLf

        Do While tranFile.Peek <> -1

            Call GetNextTran(tid, tcode, tdate, ttype, tshares, tprice)

            If txtTargetID.Text = tid Then
                Call GetStockData(tcode, sname, sprice)
            End If

            If ttype = "B" Then
                sprice += tprice
            ElseIf ttype = "S" Then
                sprice -= tprice
            End If

            total = sprice * tshares
            lblDisp.Text += tcode.PadRight(9) & sname.PadRight(23) & tshares.ToString.PadRight(10) & _
              FormatCurrency(sprice).PadRight(11) & FormatCurrency(total) & vbCrLf
        Loop

        lblDisp.Text += "Total portfolio value as of " & tdate & "  =  " & FormatCurrency(total).ToString
    End Sub

 

    Private Sub btnGainLoss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYTD.Click
        'display transactions from target year for current client

        Dim sname As String = ""
        Dim sprice As double
        Dim tid As String = txtTargetID.Text
        Dim tcode As String = ""
        Dim tdate As Date
        Dim ttype As String = ""
        Dim tshares As Integer
        Dim tprice As Double

        'position the file at its beginning
        Call ResetTranFile()

        If Not IsNumeric(txtTargetID.Text) Or txtTargetID.Text = "" Then
            MessageBox.Show("Enter a valid client id")
            Exit Sub
        End If

        If CDbl(txtYear.Text) > Today.Year Or CDbl(txtYear.Text) <= Today.Year - 100 Then
            MessageBox.Show("Enter a valid year")
            Exit Sub
        End If

        If txtClientName.Text = "" Then
            MessageBox.Show("No client selected")
            Exit Sub
        End If

        'print label headings
        lblDisp.Text = "Date".PadRight(12) & "Name".PadRight(20) & "Type".PadRight(7) & _
        "Shares".PadRight(9) & "Price" & vbCrLf

        Do While tranFile.Peek <> -1
            Call GetNextTran(tid, tcode, tdate, ttype, tshares, tprice)
            If txtTargetID.Text = tid And CDbl(txtYear.Text) = tdate.Year Then
                Call GetStockData(tcode, sname, sprice)

                lblDisp.Text += tdate.ToShortDateString.PadRight(11) & sname.PadRight(21) & ttype.PadRight(7) & _
                tshares.ToString.PadRight(9) & FormatCurrency(tprice) & vbCrLf

            End If
        Loop
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' load parallel arrays of client data from file
        Dim clientFile As System.IO.StreamReader 'file of client data
        Dim clientFileName As String = "faclient.txt" '

        Dim record As String
        Dim id As Integer
        Dim last As String
        Dim first As String
        Dim phoneNum As String

        'position the file at its beginning
        Call ResetTranFile()

        'check to see if transaction file exists
        If Not System.IO.File.Exists(tranFileName) Then
            MessageBox.Show("cannot find transaction file")
            Me.Close()
            Exit Sub
        End If

        'check to see if client file exists
        If Not System.IO.File.Exists(clientFileName) Then
            MessageBox.Show("cannot find client file")
            Me.Close()
            Exit Sub
        End If

        clientFile = New System.IO.StreamReader(clientFileName)

        'load the id, names and phone into parallel lists
        Do Until clientFile.Peek = -1
            If clientFile.Peek <> -1 Then   'check to see if next record exists
                record = clientFile.ReadLine

                id = CInt(record.Substring(0, 3))
                last = record.Substring(4, 10).Trim
                first = record.Substring(15, 10).Trim
                phoneNum = record.Substring(26, 10).Trim

                'add code here to read each record on the client file and
                clientIDList.Add(CInt(id))
                clientLastNameList.Add(last)
                clientFirstNameList.Add(first)
                clientPhoneList.Add(phoneNum)
            End If
        Loop
    End Sub

    '===============GetStockData============
    'given - the stock code for the target stock
    'task  - read thru stock file, find target stock code in stock file
    'return- stock name and price for target stock code via reference parameters
    '        if target code not found, return price of 0 and name UNKNOWN
    Private Sub GetStockData(ByVal sCode As String, _
                             ByRef sName As String, _
                             ByRef sPrice As Double)
        Dim stockFile As System.IO.StreamReader 'file of stock data
        Dim stockFileName As String = "fastock.txt"
        Dim stockFound As Boolean
        Dim holdrecord As String
        Dim holdcode As String   'code found on stock file record

        'check to see if stock file exists
        If Not System.IO.File.Exists(stockFileName) Then
            MessageBox.Show("cannot find stock file")
            Me.Close()
            Exit Sub
        End If

        'open stock file and see if it has at least one record
        stockFile = New System.IO.StreamReader(stockFileName)
        If stockFile.Peek = -1 Then
            MessageBox.Show("stock file is empty")
            Me.Close()
            Exit Sub
        End If

        'search file for stock code
        stockFound = False
        Do While stockFile.Peek <> -1 And Not stockFound
            holdrecord = stockFile.ReadLine()
            holdcode = holdrecord.Substring(0, 5)
            If holdcode.Trim = sCode.Trim Then  'stock found
                stockFound = True
                sName = holdrecord.Substring(6, 20)
                sPrice = CDbl(holdrecord.Substring(27, 7))
            End If
        Loop
        If Not stockFound Then
            sName = "UNKNOWN"
            sPrice = 0.0
        End If
        stockFile.Close()
    End Sub
    '=======ResetTranFile=========
    'given - assuming the transaction file exists
    'task  - position file reader at begining of file
    'return- nothing
    Private Sub ResetTranFile()
        tranFile = New System.IO.StreamReader(tranFileName)

    End Sub

    '===========GetNextTran=================
    'given - assuming the transaction file exists
    'task  - attempt to read the data from the next transaction 
    'return- data from next transaction record (if found) is returned via six parameters
    '        else fourth parameter is set to X indicating end of file is reached
    Private Sub GetNextTran(ByRef tid As String, ByRef tcode As String, _
                              ByRef tdate As Date, ByRef ttype As String, _
                              ByRef tshares As Integer, ByRef tprice As Double)
        'if another tran record exists, it is read, parsed, and returned
        'else a value of "X" is returned for the tran type code
        Dim holdrecord As String
        If tranFile.Peek <> -1 Then   'check to see if next record exists
            'unpack record into fields
            holdrecord = tranFile.ReadLine()
            tid = holdrecord.Substring(0, 3)
            tcode = holdrecord.Substring(4, 5).Trim
            tdate = CDate(holdrecord.Substring(10, 10))
            ttype = holdrecord.Substring(21, 1)
            tshares = CInt(holdrecord.Substring(23, 5))
            tprice = CDbl(holdrecord.Substring(29, 7))
        Else
            tid = ""
            tcode = ""
            tdate = #12:00:00 AM#
            ttype = "X"  'denotes that next record does not exist
            tshares = 0
            tprice = 0
        End If

    End Sub

    Private Sub txtTarget_textChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTargetID.TextChanged

        'txtTargetID.Clear()
        'lblDisp.ResetText()

    End Sub
End Class



Thank you for you help..
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Logic using files and parallel lists

Posted 26 March 2011 - 10:47 AM

Also you may want to consider moving away from parallel arrays/collection, and use objects.
Parallel Arrat vs Array with Obejects
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1