Hi folks, have a design problem which some of you must have encountered before as I know this scenario crops up in lots of places. It's proving quite tricky to pin down the solution. I'm using OO techniques - it's not really an OO problem as such, but I can't find a general "system design" forum!
The problem in a nutshell is working out the next occurence of a particular day in a time frequency. E.g Weekly, Monday; 31st, Monthly; 1st, Quarterly, pay 2nd month in quarter. If the calculated date is not a working day ie. Saturday, Sunday or Bank Holiday, use the next earliest day.
I have a database table which keeps track of all working days so we can plug in the bank holidays when we know them. And a function we can put in the date and it tells us if it's a working day or not. No problem.
I have an object that stores the payment frequency and required day of week, day of month and/or month of quarter. There is also data to support half yearly payment and annual payment but I've ignored them here for simplicity(!)
There is a method on the object which calculates the next payment due date which I'm working on now. The object itself has the data to work out what the frequency and the desired day is. My difficulty is that the method can be called in 2 situations - if the object has just been updated e.g. on the 15th January, you update it to monthly payment on the 31st of the month, so you want to set the date to 31st January. The other situation is that the customer has just been paid and you need to work out the next date. e.g. you pay them on the 31st January and need to work out the next date when monthly payment on the 31st. In this situation we need to pay them on the 28th February. I had everything worked out but realised I had ignored the above situation... my immediate reaction was if the day required was greater than 28 to check the number of days in the following month ... but what month are you looking at? In scenario 1 it's Januray and in 2 it's February....
I've done tons of work on it and I'm sure I can figure out something. However it's a situation that some of us must have come across before so .... has anyone done this and can give me some pointers/patterns to work with?
The problem in a nutshell is working out the next occurence of a particular day in a time frequency. E.g Weekly, Monday; 31st, Monthly; 1st, Quarterly, pay 2nd month in quarter. If the calculated date is not a working day ie. Saturday, Sunday or Bank Holiday, use the next earliest day.
I have a database table which keeps track of all working days so we can plug in the bank holidays when we know them. And a function we can put in the date and it tells us if it's a working day or not. No problem.
I have an object that stores the payment frequency and required day of week, day of month and/or month of quarter. There is also data to support half yearly payment and annual payment but I've ignored them here for simplicity(!)
There is a method on the object which calculates the next payment due date which I'm working on now. The object itself has the data to work out what the frequency and the desired day is. My difficulty is that the method can be called in 2 situations - if the object has just been updated e.g. on the 15th January, you update it to monthly payment on the 31st of the month, so you want to set the date to 31st January. The other situation is that the customer has just been paid and you need to work out the next date. e.g. you pay them on the 31st January and need to work out the next date when monthly payment on the 31st. In this situation we need to pay them on the 28th February. I had everything worked out but realised I had ignored the above situation... my immediate reaction was if the day required was greater than 28 to check the number of days in the following month ... but what month are you looking at? In scenario 1 it's Januray and in 2 it's February....
I've done tons of work on it and I'm sure I can figure out something. However it's a situation that some of us must have come across before so .... has anyone done this and can give me some pointers/patterns to work with?