The difference between Period units

Report a typo

Find and subtract the smallest Period unit from the sum of all units using the minus() method. Note that you need to implement the getSumMinusMin() method to return the result of the subtraction but this doesn't mean that all logic must be there. You can have more methods. Use the calculateDayCount() method to calculate the number of days in a year for comparison.

Sample Input 1:

1989 6 23
1991 5 22
1994 4 21
1993 3 19
1988 2 18

Sample Output 1:

P7967Y18M85D
Write a program in Java 17
import java.time.*;
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Period> periods = createPeriodList(scanner);

Period result = getSumMinusMin(periods);

System.out.println(result);
}

public static List<Period> createPeriodList(Scanner scanner) {
List<Period> list = new ArrayList<>();
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));

return list;
}

/**
* Calculates days count in a Period unit.
* Assumes there are no leap years and all months have 30 days
*/
private static int calculateDayCount(Period p) {
return p.getYears() * 365 + p.getMonths() * 30 + p.getDays();
}

private static Period getSumMinusMin(List<Period> periods) {

}
}
___

Create a free account to access the full topic