Day 26: Nested Logic
Objective
CodingHumans today's challenge puts your understanding of nested conditional statements to the test. You already have the knowledge to complete this challenge, Lets check your conditiona knowledge
coding humans.
Task
Your local library needs your help! Given the expected and actual return dates for a library book, create a program that calculates the fine (if any). The fee structure is as follows:
1 If the book is returned on or before the expected return date, no fine will be charged (i.e:fine=0) .
2 If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, fine = 15 hackos x (the number of days late).
3 If the book is returned after the expected return month but still within the same calendar year as the expected return date, the fine = 500 hackos x (the number of months late).
4 If the book is returned after the calendar year in which it was expected, there is a fixed fine of 1000 hackos.
Input Style
The first line contains 3 space-separated integers denoting the respective day, month , and year on which the book was actually returned.
The second line contains 3 space-separated integers denoting the respective day ,month, and year on which the book was expected to be returned (due date).
Constraints
1<= D <=31
1<= M <=12
1<=Y <=3000
It is guarented that the dates will be valid Gregorian calendar dates.
Output Stylec
Print a single integer denoting the library fine for the book received as input.
Sample Input
9 6 2015
6 6 2015
Sample Output
45
Explanation
Given the following return dates:
Actual: Da= 9, Ma=6, Ya=2015
Expected: De=6,Me=6,Ye=2015
Because Ye == Ya, we know it is less than a year late.
Because Me == Ma, we know it's less than a month late.
Because De < Da, we know that it was returned late (but still within the same month and year).
Per the library's fee structure, we know that our fine will be 15 hackos x (# days late) . We then print the result of 15 x ( Da x De ) =15 x ( 9-6 ) = 45 as our output.
Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.
Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.
HAVE A GOOD DAY 😁
Solution
( Java )
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int day = sc.nextInt();
int month = sc.nextInt();
int year = sc.nextInt();
int dayExpected = sc.nextInt();
int monthExpected = sc.nextInt();
int yearExpected = sc.nextInt();
int fine = 0;
if(year > yearExpected){
fine = 10000;
}else if(year == yearExpected && month > monthExpected){
fine = (month - monthExpected) * 500;
}else if(year == yearExpected && month == monthExpected && day > dayExpected){
fine = (day - dayExpected) * 15;
}
System.out.println(fine);
}
}
If you have any doubts regarding this problem or need the solution in other programming languages then leave a comment down below .