Filip_Markoski

[NP] TimeTable

Nov 11th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.io.PrintWriter;
  4. import java.lang.reflect.Array;
  5. import java.util.*;
  6.  
  7. class UnsupportedFormatException extends Exception {
  8.     String msg;
  9.  
  10.     public UnsupportedFormatException(String msg) {
  11.         this.msg = msg;
  12.     }
  13.  
  14.     @Override
  15.     public String getMessage() {
  16.         return msg;
  17.     }
  18. }
  19.  
  20. class InvalidTimeException extends Exception {
  21.     /* Using Exception's default getMessage() */
  22.     public InvalidTimeException(String msg) {
  23.         super(msg);
  24.     }
  25. }
  26.  
  27. enum TimeFormat {
  28.     FORMAT_24, FORMAT_AMPM
  29. }
  30.  
  31. class Time implements Comparable<Time> {
  32.     int hours;
  33.     int minutes;
  34.  
  35.     public Time(String time) throws UnsupportedFormatException, InvalidTimeException {
  36.         time = time.replace(".", ":");
  37.  
  38.         String parts[] = time.split(":");
  39.         if (parts.length < 2) {
  40.             throw new UnsupportedFormatException(time);
  41.         }
  42.  
  43.         int temp = Integer.parseInt(parts[0]);
  44.         if (temp < 0 || temp > 24) {
  45.             throw new InvalidTimeException(time);
  46.         }
  47.         this.hours = temp;
  48.  
  49.         temp = Integer.parseInt(parts[1]);
  50.         if (temp < 0 || temp > 60) {
  51.             throw new InvalidTimeException(time);
  52.         }
  53.         this.minutes = temp;
  54.  
  55.     }
  56.  
  57.     public String formatAMPM() {
  58.         String ampm;
  59.         /* 00:00 to 11:59 is AM */
  60.         if (hours == 0) {
  61.             hours += 12;
  62.             ampm = "AM";
  63.         } else if (hours < 12) {
  64.             ampm = "AM";
  65.         /* 12:00 to 23:59 is AM */
  66.         } else if (hours == 12) {
  67.             ampm = "PM";
  68.         } else {
  69.             hours -= 12;
  70.             ampm = "PM";
  71.         }
  72.         return String.format("%2d:%02d %s", hours, minutes, ampm);
  73.     }
  74.  
  75.     public String format24() {
  76.         return String.format("%2d:%02d", hours, minutes);
  77.     }
  78.  
  79.     @Override
  80.     public String toString() {
  81.         return format24();
  82.     }
  83.  
  84.     @Override
  85.     public int compareTo(Time that) {
  86.         /* It's always in the context of this */
  87.         if (this.hours < that.hours) {
  88.             return -1;
  89.         } else if (this.hours > that.hours) {
  90.             return 1;
  91.         } else if (this.hours == that.hours) {
  92.             if (this.minutes < that.minutes) {
  93.                 return -1;
  94.             } else if (this.minutes > that.minutes) {
  95.                 return 1;
  96.             } else {
  97.                 return 0;
  98.             }
  99.         }
  100.         return 0;
  101.  
  102.         /* Simpler Way */
  103.         /*return Integer.compare(this.hours * 60 + this.minutes, that.hours * 60 + that.minutes);*/
  104.     }
  105. }
  106.  
  107. class TimeTable {
  108.     ArrayList<Time> times;
  109.  
  110.     public TimeTable() {
  111.         times = new ArrayList<>();
  112.     }
  113.  
  114.     public void readTimes(InputStream inputStream) throws UnsupportedFormatException, InvalidTimeException {
  115.         Scanner input = new Scanner(inputStream);
  116.         while (input.hasNext()) {
  117.             Time time = new Time(input.next());
  118.             times.add(time);
  119.         }
  120.  
  121.     }
  122.  
  123.     public void writeTimes(OutputStream outputStream, TimeFormat format) {
  124.         PrintWriter pw = new PrintWriter(outputStream);
  125.         Collections.sort(times);
  126.         if (format.equals(TimeFormat.FORMAT_24)) {
  127.             for (Time time : times) {
  128.                 pw.println(time.format24());
  129.             }
  130.         } else {
  131.             for (Time time : times) {
  132.                 pw.println(time.formatAMPM());
  133.             }
  134.         }
  135.         /*
  136.         4: Don't close a printWriter because
  137.         if you close the System.out then you can't print at all.
  138.         It's better to you flush it.
  139.         */
  140.         pw.flush();
  141.     }
  142. }
  143.  
  144. public class TimesTest {
  145.  
  146.     public static void main(String[] args) {
  147.         TimeTable timeTable = new TimeTable();
  148.         try {
  149.             timeTable.readTimes(System.in);
  150.         } catch (UnsupportedFormatException e) {
  151.             System.out.println("UnsupportedFormatException: " + e.getMessage());
  152.         } catch (InvalidTimeException e) {
  153.             System.out.println("InvalidTimeException: " + e.getMessage());
  154.         }
  155.         System.out.println("24 HOUR FORMAT");
  156.         timeTable.writeTimes(System.out, TimeFormat.FORMAT_24);
  157.         System.out.println("AM/PM FORMAT");
  158.         timeTable.writeTimes(System.out, TimeFormat.FORMAT_AMPM);
  159.     }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment