We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: returned date
String[] returnedStr = scanner.nextLine().split(" ");
int dayReturned = Integer.parseInt(returnedStr[0]);
int monthReturned = Integer.parseInt(returnedStr[1]);
int yearReturned = Integer.parseInt(returnedStr[2]);
// Input: due date
String[] dueStr = scanner.nextLine().split(" ");
int dayDue = Integer.parseInt(dueStr[0]);
int monthDue = Integer.parseInt(dueStr[1]);
int yearDue = Integer.parseInt(dueStr[2]);
// Calculate fine
int fine = calculateFine(dayReturned, monthReturned, yearReturned, dayDue, monthDue, yearDue);
// Output
System.out.println(fine);
scanner.close();
}
public static int calculateFine(int dayReturned, int monthReturned, int yearReturned, int dayDue, int monthDue, int yearDue) {
if (yearReturned < yearDue || (yearReturned == yearDue && monthReturned < monthDue) || (yearReturned == yearDue && monthReturned == monthDue && dayReturned <= dayDue)) {
return 0; // No fine if returned on or before due date
} else if (yearReturned == yearDue && monthReturned == monthDue) {
// Fine based on days late
return 15 * (dayReturned - dayDue);
} else if (yearReturned == yearDue) {
// Fine based on months late
return 500 * (monthReturned - monthDue);
} else {
// Fixed fine for returning after the due year
return 10000;
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 26: Nested Logic
You are viewing a single comment's thread. Return to all comments →
Java 8 solution