0

I'm writing a Java program that automatically generates a certain amount of "units" for the user to do (Actual purpose not important in this question).

I'm struggling to find a good way to determine how many of these "units" to give the user for the next week.

Intentions of the calculation :

  • Gives "units" of a specific week
  • Calculates units for the week based on the actual day of the week (Mon-Fri)
  • Accounts for all of the units that month (Ex. If it is the last week of the month, give all possible units that entire week)
  • If estimating, should over-estimate (Due to the program's nature, under-estimating would be far worse.)
  • Units cannot be decimals (Hence the cast to int)

Variables I have :

  • The amount of "units" to do that month

I have tried to do this in a few different ways, and so far, the best way I have is this :

 public static int getRemainingUnitsThisWeek() {
    return (int) Math.round(((double) getUnitsThisMonth() / (((30 - (double) DateTime.now().getDayOfMonth()+1) / 7) < 1 ? 1 :
            ((30 - (double) DateTime.now().getDayOfMonth()+1) / 7))) / (double)DateTime.now().getDayOfWeek());
 }

The problem that I end up (specifically, I'm still not happy with how it does it) with is that in the "units" given on the last week of the month is still divided by the day of the week (Which in turn gives you a much lower number each day - even though the user needs to receive all the "units" by the end of the month).

Any questions you might have, just ask!

PS. The library I am using in the DateTime.now() function is joda-time.

Joel Gallant
  • 315
  • 4
  • 21
  • Not especially, at least not for me. I don't see any answers yet from others either. – Hovercraft Full Of Eels Aug 29 '12 at 02:19
  • I don't have the time to rewrite it right now. I'm pretty new here, would it be better to edit the question or just repost it with different wording? – Joel Gallant Aug 29 '12 at 02:28
  • It's up to you what you do, but if you don't get answers, consider clarifying your question so that it's easier to understand. We are volunteers and while you may not have much time, you're asking free help from us, so it really is your responsibility to put in the effort to frame your question so that it is as clear as possible. We only ask that you put as much effort into asking your question as you'd like someone to put in answering, and that's not asking too much. – Hovercraft Full Of Eels Aug 29 '12 at 02:36

2 Answers2

0

Your description is confusing, so I'm not sure I can answer it directly. Instead, I will answer a different question, which may help:

I have a bunch of units to do, and the rest of the month to do them. How many should I do today?

I don't know about Joda, but using the standard Date API, you can use this answer to get a Date representing the end of the month, a similar technique to get a Date representing the end of today, and new Date() to get the current time. Then subtract their getTime()s. You can then apportion your units now that you know how many milliseconds to the end of the day and the end of the month.

units_today = total_units_to_do * milliseconds_to_end_of_today /
  milliseconds_to_end_of_month;
Community
  • 1
  • 1
Keith Randall
  • 22,985
  • 2
  • 35
  • 54
0

I don't know java, so I won't attempt to write code, but I suggest you start by calculating daysLeftInMonth and daysLeftInWeek. Once you get those working perfectly, you have two simple cases: either the month will end before the week does, or else it won't.

In the first case the rest of the algorithm is trivial. In the second, calculate how many units there should be per day (rounding up), then multiply by the days left in the week.

Beta
  • 96,650
  • 16
  • 149
  • 150