I have two different files, fileA contains employee data (ID and townCode) while file B contains field office data (OfficeID, Town, TownCode, officeCapacity) and a town can contain more than one office. i want to distribute these employees to the offices in the town against their names evenly

What I have tried:

I read the 2 files simulteanously and created another file to hold the last office it was distributed to. My challenge is that the distribution is not even and it picks only 2 offices for the distribution

    For Each line In File.ReadLines(src1)
       Dim STRDATA() As String = line.Split({","c})
       employeeID = STRDATA(0)
       EmpTownCode = STRDATA(1)

       For Each lines In File.ReadLines(src2)
           Dim STRDATA2() As String = lines.Split({","c})

           OfficeID = STRDATA2(0)
           StrTownName = STRDATA2(1)
           StrTownCode = STRDATA2(2)
           OfficeCapacity = STRDATA2(3)
           For Each last In File.ReadLines(src3)
               lastCentre = last.Substring(0, 16)
           Next

           If StrRegTown = StrTownCode Then
               If StrCentreRef = lastCentre.Last Then
                   Exit For
               End If
               FileOpen(intoutfilenum, outfilepath, OpenMode.Append)
               WriteLine(intoutfilenum, EmployeeID, TownCode, OfficeID, OfficeCapacity)
               FileClose(intoutfilenum)
               FileOpen(intoutfilenum2, outfilepath2, OpenMode.Append)
               WriteLine(intoutfilenum2, OfficeID)
               FileClose(intoutfilenum2)
               Exit For
           End If

Dani AI

Generated

A practical path forward is to stop re-reading the offices file for every employee and treat each town as its own rotating pool of offices with remaining capacity. Load the office CSV once into a dictionary keyed by TownCode, keep a small pointer (or queue) per town, then stream the employee CSV and assign each employee to the next available office for that town. This fixes the uneven selection and gives you explicit capacity tracking — exactly the design concerns raised — and it avoids the single‑town lock that pointed out when a town variable is defined outside the employee loop.

Key steps to implement

  • Load offices once: parse OfficeID and numeric capacity, group into Dictionary(Of String, List(Of Office)).
  • Initialize a pointer/queue for each town to implement round‑robin rotation.
  • Stream employees line‑by‑line, look up the town list, iterate from the town pointer until you find an office with Remaining > 0, assign and decrement Remaining, then advance the pointer.
  • If no office in the town has remaining capacity, record the employee as unassigned or overflow.

A concise VB.NET sketch of the assignment loop:

Class Office
    Public Property Id As String
    Public Property Remaining As Integer
End Class

Dim officesByTown As New Dictionary(Of String, List(Of Office))()
' -- load offices once here --

Dim pointerByTown As New Dictionary(Of String, Integer)()

For Each empLine As String In File.ReadLines(employeePath)
    Dim cols = empLine.Split(","c)
    Dim empId = cols(0)
    Dim town = cols(1)

    If Not officesByTown.ContainsKey(town) Then
        ' write unassigned; Continue For
    End If

    Dim list = officesByTown(town)
    Dim p As Integer
    If pointerByTown.ContainsKey(town) Then p = pointerByTown(town) Else p = 0

    Dim assigned As Boolean = False
    For i As Integer = 0 To list.Count - 1
        Dim idx = (p + i) Mod list.Count
        If list(idx).Remaining > 0 Then
            ' record assignment empId -> list(idx).Id
            list(idx).Remaining -= 1
            pointerByTown(town) = (idx + 1) Mod list.Count
            assigned = True
            Exit For
        End If
    Next

    If Not assigned Then
        ' record unassigned / overflow
    End If
Next

Troubleshooting tips

  • Use a proper CSV parser (TextFieldParser or CsvHelper) if values can be quoted or contain commas.
  • Persist the per‑town pointer to a tiny file if you need rotation to continue across runs.
  • Validate capacities are numeric and handle total-capacity < employees.
  • Test on a small dataset and print per-office assignment counts to confirm even distribution.

Recommended Answers

All 5 Replies

The code above doesn't appear to do what you asked. Back to your design (algorithm) to see where it diverged.

Some want members to write the code or app for them. I don't see that here. What I do see is help for a function, a line of code or such but not the entire solution or app.

Remember I can read the comments you made and removed so to discuss the steps you created and to compare them to your code to see where it diverged I'd have to see and read that design document. Some tend to rush into code before they have how it will work step by step. Or "I know what the result should be" and think that's enough. (it's not.)

There are places you can get apps and code written for you such as fiverr.com.

-> Back to what you have above. I don't see that code doing what you asked. Specifically it doesn't seem to keep track of office capacity and when it gets full then what to do when that happens.

commented: I have also tried to read the office file into an array-list, that didn't work too. if office capacity is full, another shift starts. +0

Reply to the discussion rather than comment. By your comments so far you need to work out the steps it takes to get to your goal.

That's called design. Design first, code later.

I don't see where StrRegTown comes from. As this is set outside the outer loop your algorithm is only ever going to match offices in one town. If this isn't what you intend, perhaps you should use EmpTownCode instead.

I also don't understand where StrCentreRef comes from or what part it plays in the algorithm. Again because it is set outside your loop it will restrict your results to only one centre. But perhaps this is what you intend.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.