Answered Elegant solution for declaring how often your paid?

jdraughn

New member
Joined
May 31, 2008
Messages
4
Programming Experience
Beginner
I am creating a program that takes into account pay from a job. I will try to describe the problem I am having using some psuedo code - so dont take syntax too seriously.

I created a structure that has some of the following values:
Name as String
Amount as Double
FrequencyType as MyEnumerator

MyEnumerator looks like this:
EveryTwoWeeks = 1
TwiceAMonth = 2
Weekly = 3

Now my problem is how do I add the date ranges into my structure?
If your pay is weekly or twice a week, then I just need a single date because all other paydays should increment in 1 or 2 week blocks. But if the pay is twice a month I need two dates.

It seems like there has got to be a better way then just having two dates in my structure and knowing that you just need the 2nd date if the FrequencyType is = 2 (TwiceAMonth).

I was thinking that instead of a Structure I should use a class and then I could create a function that returns the date or dates depending on the enumeration type that you pass into it.

What would be the "correct" way of doing this? I think I need a type of enumerator that allows for more then just integer type values. Then I could do something like:
Weekly = WeeklyStructure
TwiceAWeek = TwiceAWeekStructure
TwiceAMonth = TwiceAMonthStructure

WeeklyStructure
Name as String
StartingDate as Date

TwiceAMonthStructure
Name as String
FirstDate as Date
SecondDate as Date


I dunno though, I am having a hard time figuring this out in my head. As you can see I am not the best programmer, especially when my ADD meds wear off. I can't get more then 2 steps into a problem before I lose track in my head.
 
Last edited:
I think you shouldnt use structures; this is not what they are for

Then I think you should define a class that is the base class of two other classes, one representing periodic payment, and the other representing date based payment
For periodic payment it's easy; just spec the number of days or months or years that elapse between payments using a timespan, then have a start date. every call to a GetNextPaymentDate should update an internal date (originally the start date) by adding the timespan to it and returning it

For the other kind of payment, allow for a list(array) of numbered days of when payment will occur, e.g. the first, third and fifteenth of every month. Calling GetNextPaymentDate on this advances an array indexer by one, then creates a date using the year and month of the start date and the day in the array (the first).
VB.NET:
GetNextPaymentDate()
  Dim toReturn As Date = New Date(startDate.Year, startDate.Month, daysarray(indexer))
  indexer += 1
  If indexer > array.Length - 1 Then
    'reset it to 0,
    'add a month to your start date
  End If
  Return toReturn
 
Thanks! I have never implemented my own classes in that way before so it never occured to me. I will work on getting that going. I sure love programming, it's so much fun - like putting together a puzzle where you get to create your own pieces which are like mini puzzles.
 
Back
Top