Advertisement
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.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Scanner;
- 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 {
- ArrayList<Time> list;
- public TimeTable() {
- list=new ArrayList<>();
- }
- public void readTimes(InputStream inputStream) throws UnsupportedFormatException,InvalidTimeException{
- Scanner cin=new Scanner(inputStream);
- while(cin.hasNextLine()){
- String line=cin.nextLine();
- String []s=line.split("\\s+");
- boolean nvalid,valid1;
- try {
- for (int i = 0; i < s.length; i++) {
- nvalid = false;
- valid1 = true;
- boolean tocka=false;
- String tmp = s[i];
- for (int j = 0; j < tmp.length(); j++) {
- if (tmp.charAt(j) == ':' || tmp.charAt(j) == '.') {
- nvalid = true;
- }
- if (!(Character.isDigit(tmp.charAt(j))) && tmp.charAt(j) != ':' && tmp.charAt(j) != '.') {
- valid1 = false;
- }
- if(tmp.charAt(j)=='.')
- tocka=true;
- }
- if (!nvalid ) {
- throw new UnsupportedFormatException(tmp);
- }
- String []tmp1;
- if(!tocka)
- tmp1=tmp.split("\\:");
- else
- tmp1=tmp.split("\\.");
- if(Integer.parseInt(tmp1[0])<0||Integer.parseInt(tmp1[0])>23)
- throw new InvalidTimeException(tmp);
- if(Integer.parseInt(tmp1[1])<0||Integer.parseInt(tmp1[1])>59)
- throw new InvalidTimeException(tmp);
- list.add(new Time(Integer.parseInt(tmp1[0]),Integer.parseInt(tmp1[1])));
- }
- }
- catch (UnsupportedFormatException e){throw e;}
- catch(InvalidTimeException exc){throw exc;}
- }
- }
- public void writeTimes(OutputStream outputStream, TimeFormat format){
- ArrayList<Time>t1;
- t1=new ArrayList<>();
- for(int i=0;i<list.size();i++){
- t1.add((Time)list.get(i));
- }
- Collections.sort(t1);
- String rez;
- rez="";
- PrintWriter out=new PrintWriter(outputStream);
- if(format.equals(TimeFormat.FORMAT_24)){
- for(int i=0;i<t1.size();++i){
- /*if(t1.get(i).getH()<10)
- rez+="0"+t1.get(i).getH()+":";
- else
- rez+=t1.get(i).getH()+":";
- if(t1.get(i).getM()<10)
- rez+="0"+t1.get(i).getM()+"\n";
- else
- rez+=t1.get(i).getM()+"\n";*/
- rez+=t1.get(i).format24()+"\n";
- }
- }
- if(format.equals(TimeFormat.FORMAT_AMPM)){
- for(int i=0;i<t1.size();i++){
- rez+=t1.get(i).formatAMPM()+"\n";
- }
- }
- out.print(rez);
- out.flush();
- }
- }
- class Time implements Comparable<Time>{
- int h,m;
- public Time(int h, int m) {
- this.h = h;
- this.m = m;
- }
- public int getM() {
- return m;
- }
- public void setM(int m) {
- this.m = m;
- }
- public int getH() {
- return h;
- }
- public void setH(int h) {
- this.h = h;
- }
- public String formatAMPM() {
- String ampm;
- if (h == 0) {
- h +=12;
- ampm = "AM";
- }
- else if (h == 12) {
- ampm = "PM";
- }
- else if (h < 12) {
- ampm = "AM";
- }
- else {
- h -= 12;
- ampm = "PM";
- }
- return String.format("%2d:%02d %s", h,m,ampm);
- }
- public String format24() {
- return String.format("%2d:%02d", h,m);
- }
- @Override
- public int compareTo(Time time) {
- if(this.h!=time.h){
- if(this.h<time.h)
- return -1;
- else
- return 1;
- }
- else if(this.m!=time.m){
- if(this.m<time.m)
- return -1;
- else
- return 1;
- }
- else
- return 0;
- }
- }
- class UnsupportedFormatException extends Exception{
- String s;
- public UnsupportedFormatException(String s1){
- super(s1);
- s=s1;
- }
- @Override
- public String getMessage(){return s;}
- }
- class InvalidTimeException extends Exception{
- String s;
- public InvalidTimeException(String s1){
- super(s1);
- s=s1;
- }
- @Override
- public String getMessage(){return s;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement