noobianov
noobianov

Reputation: 83

Is there a way to divide and map a date into months with their sum of daily value?

Im having trouble thinking of a way to create a function that can calculate the monthly spends of all combined projects in a system and return a map with month as its key and the monthly spends as their value. By monthly spends I mean the worked hours of the employees multiplied by their wage.

The class Project has a startdate and an end date with a function that calculates the working days in between. Each project has differen start and end date.

public int getNumWorkingDays() {
    return utils.Calendar.getNumWorkingDays(this.startDate, this.endDate);
}

the project also has an committedHoursPerDay() function that maps the employee to the amount of worked hours per day.

private Map<Employee,Integer> committedHoursPerDay;

How can i create a function that can sum up the spendings based on each month keeping the start and end date in mind for each individual project?

Map<Month,Integer> monthlyBudget;

Upvotes: 0

Views: 534

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 339342

Of course I'll not do your programming job for you, but I can give you some things to consider.

YearMonth

To represent a month, use YearMonth class.

YearMonth yearMonth = YearMonth.of( 2020 , Month.JANUARY ) ;

You can get the YearMonth for any date.

LocalDate ld = LocalDate.of( 2020 , Month.MARCH , 27 ) ;
YearMonth yearMonth = YearMonth.from( ld ) ;

That class can be used as the key of your map. As for the value part of a map's key-value pairing, if you are talking about currency amounts that are fractional, use BigDecimal for accuracy, never the floating-point types, float or double.

Map< YearMonth , BigDecimal > monthlyTotals = new TreeMap<> () ;

Map implementations

As for which Map implementation to choose, you may want one that keeps the keys in chronological order of those YearMonth objects. If so, choose one that implements NavigableMap. Two such implementations are bundled with Java 11: TreeSet and ConcurrentSkipListMap. Use the second if manipulating your map across threads.

Here is a table I made summarizing the features of the various map implementations bundled with Java 11.

Table of map implementations in Java 11, comparing their features

Elapsed days

The class Project has a startdate and an end date with a function that calculates the working days in between

Use ChronoUnit class to count elapsed days. Uses the Half-Open approach where the beginning is inclusive while the ending is exclusive.

LocalDate start = LocalDate.of( 2020 , Month.JANUARY , 23 ) ;
LocalDate stop = LocalDate.of( 2020 , Month.FEBRUARY , 15 ) ;
long days = ChronoUnit.DAYS.between( start , stop ) ;

If you want to omit certain days of the week to count only "working days", then search Stack Overflow to learn more as that has been covered many times. You likely will want to look at the temporal adjusters in the ThreeTen-Extra library.

Upvotes: 3

Related Questions