Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.lang.reflect.Array;
- import java.util.*;
- class UnsupportedFormatException extends Exception {
- String msg;
- public UnsupportedFormatException(String msg) {
- this.msg = msg;
- }
- @Override
- public String getMessage() {
- return msg;
- }
- }
- class InvalidTimeException extends Exception {
- /* Using Exception's default getMessage() */
- public InvalidTimeException(String msg) {
- super(msg);
- }
- }
- enum TimeFormat {
- FORMAT_24, FORMAT_AMPM
- }
- class Time implements Comparable<Time> {
- int hours;
- int minutes;
- public Time(String time) throws UnsupportedFormatException, InvalidTimeException {
- time = time.replace(".", ":");
- String parts[] = time.split(":");
- if (parts.length < 2) {
- throw new UnsupportedFormatException(time);
- }
- int temp = Integer.parseInt(parts[0]);
- if (temp < 0 || temp > 24) {
- throw new InvalidTimeException(time);
- }
- this.hours = temp;
- temp = Integer.parseInt(parts[1]);
- if (temp < 0 || temp > 60) {
- throw new InvalidTimeException(time);
- }
- this.minutes = temp;
- }
- public String formatAMPM() {
- String ampm;
- /* 00:00 to 11:59 is AM */
- if (hours == 0) {
- hours += 12;
- ampm = "AM";
- } else if (hours < 12) {
- ampm = "AM";
- /* 12:00 to 23:59 is AM */
- } else if (hours == 12) {
- ampm = "PM";
- } else {
- hours -= 12;
- ampm = "PM";
- }
- return String.format("%2d:%02d %s", hours, minutes, ampm);
- }
- public String format24() {
- return String.format("%2d:%02d", hours, minutes);
- }
- @Override
- public String toString() {
- return format24();
- }
- @Override
- public int compareTo(Time that) {
- /* It's always in the context of this */
- if (this.hours < that.hours) {
- return -1;
- } else if (this.hours > that.hours) {
- return 1;
- } else if (this.hours == that.hours) {
- if (this.minutes < that.minutes) {
- return -1;
- } else if (this.minutes > that.minutes) {
- return 1;
- } else {
- return 0;
- }
- }
- return 0;
- /* Simpler Way */
- /*return Integer.compare(this.hours * 60 + this.minutes, that.hours * 60 + that.minutes);*/
- }
- }
- class TimeTable {
- ArrayList<Time> times;
- public TimeTable() {
- times = new ArrayList<>();
- }
- public void readTimes(InputStream inputStream) throws UnsupportedFormatException, InvalidTimeException {
- Scanner input = new Scanner(inputStream);
- while (input.hasNext()) {
- Time time = new Time(input.next());
- times.add(time);
- }
- }
- public void writeTimes(OutputStream outputStream, TimeFormat format) {
- PrintWriter pw = new PrintWriter(outputStream);
- Collections.sort(times);
- if (format.equals(TimeFormat.FORMAT_24)) {
- for (Time time : times) {
- pw.println(time.format24());
- }
- } else {
- for (Time time : times) {
- pw.println(time.formatAMPM());
- }
- }
- /*
- 4: Don't close a printWriter because
- if you close the System.out then you can't print at all.
- It's better to you flush it.
- */
- pw.flush();
- }
- }
- public class TimesTest {
- public static void main(String[] args) {
- TimeTable timeTable = new TimeTable();
- try {
- timeTable.readTimes(System.in);
- } catch (UnsupportedFormatException e) {
- System.out.println("UnsupportedFormatException: " + e.getMessage());
- } catch (InvalidTimeException e) {
- System.out.println("InvalidTimeException: " + e.getMessage());
- }
- System.out.println("24 HOUR FORMAT");
- timeTable.writeTimes(System.out, TimeFormat.FORMAT_24);
- System.out.println("AM/PM FORMAT");
- timeTable.writeTimes(System.out, TimeFormat.FORMAT_AMPM);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment