Advertisement
galinyotsev123

ProgBasicsJavaBook4.2ComplexConditions01OnTimeForTheExam

Jan 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class E01OnTimeForTheExam {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int examHour = Integer.parseInt(scanner.nextLine());
  8. int examMins = Integer.parseInt(scanner.nextLine());
  9. int arrivalHour = Integer.parseInt(scanner.nextLine());
  10. int arrivalMin = Integer.parseInt(scanner.nextLine());
  11.  
  12. String late = "late";
  13. String onTime = "On time";
  14. String early = "Early";
  15.  
  16. int examTime = (examHour * 60) + examMins;
  17. int arrivalTime = (arrivalHour * 60) + arrivalMin;
  18. int totalMinDifference = arrivalTime - examTime;
  19.  
  20. String studentArrival = late;
  21. if (totalMinDifference < -30) {
  22. studentArrival = early;
  23. } else if (totalMinDifference <= 0) {
  24. studentArrival = onTime;
  25. }
  26.  
  27. String result = "";
  28. if (totalMinDifference != 0) {
  29. int hoursDifference = Math.abs(totalMinDifference / 60);
  30. int minsDifference = Math.abs(totalMinDifference % 60);
  31.  
  32. if (hoursDifference > 0) {
  33. result = String.format("%d:%02d hours", hoursDifference,
  34. minsDifference);
  35. } else {
  36. result = minsDifference + " minutes";
  37. }
  38.  
  39. if (totalMinDifference < 0) {
  40. result += " before the start";
  41. } else {
  42. result += " after the start";
  43. }
  44. }
  45.  
  46. System.out.println(studentArrival);
  47. if (!result.isEmpty()) {
  48. System.out.println(result);
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement