Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.io.PrintStream;
- import java.io.PrintWriter;
- import java.util.*;
- class Time {
- private int hours;
- private int minutes;
- private String timeOfDay;
- public Time(int hours, int minutes) {
- this.hours = hours;
- this.minutes = minutes;
- this.timeOfDay="AM";
- }
- public int getHours() {
- return hours;
- }
- public int getMinutes() {
- return minutes;
- }
- public static Time createTime(String s) throws UnsupportedFormatException, InvalidTimeException {
- int hours,minutes;
- if(s.contains(".")){
- String[] parts= s.split("\\.");
- hours= Integer.parseInt(parts[0]);
- minutes= Integer.parseInt(parts[1]);
- }
- else if(s.contains(":")){
- String[] parts= s.split(":");
- hours= Integer.parseInt(parts[0]);
- minutes= Integer.parseInt(parts[1]);
- }
- else{
- throw new UnsupportedFormatException(s);
- }
- if(hours>23||hours<0||minutes<0||minutes>59)
- throw new InvalidTimeException(s);
- return new Time(hours,minutes);
- }
- public String getTimeOfDay() {
- return timeOfDay;
- }
- public String changeToFormat24(){
- int new_hours;
- if(hours>12){
- new_hours=hours-12;
- timeOfDay="PM";
- }
- else if(hours==12){
- new_hours=12;
- timeOfDay="PM";
- }
- else if(hours==0){
- new_hours=hours+12;
- }
- else{
- new_hours=hours;
- }
- return String.format("%2d:%02d %s",new_hours,minutes,timeOfDay);
- }
- @Override
- public String toString() {
- return String.format("%2d:%02d",hours,minutes);
- }
- }
- class InvalidTimeException extends Exception{
- public InvalidTimeException(String message) {
- super(message);
- }
- }
- class UnsupportedFormatException extends Exception{
- public UnsupportedFormatException(String message) {
- super(message);
- }
- }
- 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);
- }
- }
- enum TimeFormat {
- FORMAT_24, FORMAT_AMPM
- }
- class TimeTable {
- private List<Time> times;
- public TimeTable() {
- times= new ArrayList<>();
- }
- public void readTimes(InputStream in) throws InvalidTimeException, UnsupportedFormatException {
- Scanner scanner = new Scanner(in);
- while(scanner.hasNextLine()){
- String line = scanner.nextLine();
- String[] parts = line.split("\\s+");
- for(String p : parts){
- Time time= Time.createTime(p);
- times.add(time);
- }
- }
- }
- public void writeTimes(PrintStream out, TimeFormat timeFormat) {
- PrintWriter printWriter = new PrintWriter(out);
- if(timeFormat.equals(TimeFormat.FORMAT_24))
- times.stream().sorted(Comparator.comparing(Time::getHours).thenComparing(Time::getMinutes))
- .forEach(i-> printWriter.println(i));
- else{
- times.stream().sorted(Comparator.comparing(Time::getHours)
- .thenComparing(Time::getMinutes))
- .forEach(i-> printWriter.println(i.changeToFormat24()));
- }
- printWriter.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement