Advertisement
dzocesrce

[NP] Time Table

Apr 15th, 2025
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.PrintStream;
  3. import java.io.PrintWriter;
  4. import java.util.*;
  5.  
  6. class Time {
  7.     private int hours;
  8.     private  int minutes;
  9.     private String timeOfDay;
  10.  
  11.     public Time(int hours, int minutes) {
  12.         this.hours = hours;
  13.         this.minutes = minutes;
  14.         this.timeOfDay="AM";
  15.     }
  16.  
  17.     public int getHours() {
  18.         return hours;
  19.     }
  20.  
  21.     public int getMinutes() {
  22.         return minutes;
  23.     }
  24.  
  25.     public static Time createTime(String s) throws UnsupportedFormatException, InvalidTimeException {
  26.         int hours,minutes;
  27.  
  28.         if(s.contains(".")){
  29.             String[] parts= s.split("\\.");
  30.             hours= Integer.parseInt(parts[0]);
  31.             minutes= Integer.parseInt(parts[1]);
  32.         }
  33.         else if(s.contains(":")){
  34.             String[] parts= s.split(":");
  35.             hours= Integer.parseInt(parts[0]);
  36.             minutes= Integer.parseInt(parts[1]);
  37.         }
  38.         else{
  39.             throw new UnsupportedFormatException(s);
  40.         }
  41.         if(hours>23||hours<0||minutes<0||minutes>59)
  42.             throw new InvalidTimeException(s);
  43.  
  44.         return new Time(hours,minutes);
  45.     }
  46.  
  47.     public String getTimeOfDay() {
  48.         return timeOfDay;
  49.     }
  50.  
  51.     public String changeToFormat24(){
  52.         int new_hours;
  53.         if(hours>12){
  54.             new_hours=hours-12;
  55.             timeOfDay="PM";
  56.         }
  57.         else if(hours==12){
  58.             new_hours=12;
  59.             timeOfDay="PM";
  60.         }
  61.         else if(hours==0){
  62.             new_hours=hours+12;
  63.         }
  64.         else{
  65.             new_hours=hours;
  66.         }
  67.         return String.format("%2d:%02d %s",new_hours,minutes,timeOfDay);
  68.  
  69.  
  70.     }
  71.  
  72.  
  73.     @Override
  74.     public String toString() {
  75.         return String.format("%2d:%02d",hours,minutes);
  76.     }
  77. }
  78. class InvalidTimeException extends Exception{
  79.     public InvalidTimeException(String message) {
  80.         super(message);
  81.     }
  82. }
  83.  
  84. class UnsupportedFormatException extends Exception{
  85.     public UnsupportedFormatException(String message) {
  86.         super(message);
  87.     }
  88. }
  89.  
  90. public class TimesTest {
  91.  
  92.     public static void main(String[] args) {
  93.         TimeTable timeTable = new TimeTable();
  94.         try {
  95.             timeTable.readTimes(System.in);
  96.         } catch (UnsupportedFormatException e) {
  97.             System.out.println("UnsupportedFormatException: " + e.getMessage());
  98.         } catch (InvalidTimeException e) {
  99.             System.out.println("InvalidTimeException: " + e.getMessage());
  100.         }
  101.         System.out.println("24 HOUR FORMAT");
  102.         timeTable.writeTimes(System.out, TimeFormat.FORMAT_24);
  103.         System.out.println("AM/PM FORMAT");
  104.         timeTable.writeTimes(System.out, TimeFormat.FORMAT_AMPM);
  105.     }
  106.  
  107. }
  108.  
  109. enum TimeFormat {
  110.     FORMAT_24, FORMAT_AMPM
  111. }
  112.  
  113. class TimeTable {
  114.     private List<Time> times;
  115.  
  116.     public TimeTable() {
  117.         times= new ArrayList<>();
  118.     }
  119.  
  120.     public void readTimes(InputStream in) throws InvalidTimeException, UnsupportedFormatException {
  121.         Scanner scanner = new Scanner(in);
  122.         while(scanner.hasNextLine()){
  123.             String line = scanner.nextLine();
  124.             String[] parts = line.split("\\s+");
  125.             for(String p : parts){
  126.                 Time time= Time.createTime(p);
  127.                 times.add(time);
  128.             }
  129.         }
  130.  
  131.  
  132.     }
  133.  
  134.     public void writeTimes(PrintStream out, TimeFormat timeFormat) {
  135.         PrintWriter printWriter = new PrintWriter(out);
  136.  
  137.         if(timeFormat.equals(TimeFormat.FORMAT_24))
  138.             times.stream().sorted(Comparator.comparing(Time::getHours).thenComparing(Time::getMinutes))
  139.                     .forEach(i-> printWriter.println(i));
  140.         else{
  141.             times.stream().sorted(Comparator.comparing(Time::getHours)
  142.                             .thenComparing(Time::getMinutes))
  143.                     .forEach(i-> printWriter.println(i.changeToFormat24()));
  144.         }
  145.  
  146.         printWriter.flush();
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement