Advertisement
NebojshaTodorovic

TimeTable-НП

Nov 14th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.57 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.io.PrintWriter;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collections;
  7. import java.util.Scanner;
  8.  
  9. public class TimesTest {
  10.  
  11.     public static void main(String[] args) {
  12.         TimeTable timeTable = new TimeTable();
  13.         try {
  14.             timeTable.readTimes(System.in);
  15.         } catch (UnsupportedFormatException e) {
  16.             System.out.println("UnsupportedFormatException: " + e.getMessage());
  17.         } catch (InvalidTimeException e) {
  18.             System.out.println("InvalidTimeException: " + e.getMessage());
  19.         }
  20.         System.out.println("24 HOUR FORMAT");
  21.         timeTable.writeTimes(System.out, TimeFormat.FORMAT_24);
  22.         System.out.println("AM/PM FORMAT");
  23.         timeTable.writeTimes(System.out, TimeFormat.FORMAT_AMPM);
  24.     }
  25.  
  26. }
  27.  
  28. enum TimeFormat {
  29.     FORMAT_24, FORMAT_AMPM
  30. }
  31. class TimeTable {
  32.  
  33.     ArrayList<Time> list;
  34.     public TimeTable() {
  35.         list=new ArrayList<>();
  36.     }
  37.     public void readTimes(InputStream inputStream) throws UnsupportedFormatException,InvalidTimeException{
  38.         Scanner cin=new Scanner(inputStream);
  39.         while(cin.hasNextLine()){
  40.             String line=cin.nextLine();
  41.             String []s=line.split("\\s+");
  42.             boolean nvalid,valid1;
  43.            try {
  44.                for (int i = 0; i < s.length; i++) {
  45.                    nvalid = false;
  46.                    valid1 = true;
  47.                    boolean tocka=false;
  48.                    String tmp = s[i];
  49.                    for (int j = 0; j < tmp.length(); j++) {
  50.                        if (tmp.charAt(j) == ':' || tmp.charAt(j) == '.') {
  51.                            nvalid = true;
  52.                        }
  53.                        if (!(Character.isDigit(tmp.charAt(j))) && tmp.charAt(j) != ':' && tmp.charAt(j) != '.') {
  54.                            valid1 = false;
  55.                        }
  56.                        if(tmp.charAt(j)=='.')
  57.                            tocka=true;
  58.                    }
  59.                    if (!nvalid ) {
  60.                        throw new UnsupportedFormatException(tmp);
  61.                    }
  62.                    String []tmp1;
  63.                    if(!tocka)
  64.                    tmp1=tmp.split("\\:");
  65.                    else
  66.                        tmp1=tmp.split("\\.");
  67.                    if(Integer.parseInt(tmp1[0])<0||Integer.parseInt(tmp1[0])>23)
  68.                        throw new InvalidTimeException(tmp);
  69.                    if(Integer.parseInt(tmp1[1])<0||Integer.parseInt(tmp1[1])>59)
  70.                        throw new InvalidTimeException(tmp);
  71.                   list.add(new Time(Integer.parseInt(tmp1[0]),Integer.parseInt(tmp1[1])));
  72.                }
  73.            }
  74.            catch (UnsupportedFormatException e){throw e;}
  75.            catch(InvalidTimeException exc){throw exc;}
  76.         }
  77.     }
  78.     public void writeTimes(OutputStream outputStream, TimeFormat format){
  79.         ArrayList<Time>t1;
  80.         t1=new ArrayList<>();
  81.         for(int i=0;i<list.size();i++){
  82.             t1.add((Time)list.get(i));
  83.         }
  84.         Collections.sort(t1);
  85.         String rez;
  86.         rez="";
  87.         PrintWriter out=new PrintWriter(outputStream);
  88.         if(format.equals(TimeFormat.FORMAT_24)){
  89.             for(int i=0;i<t1.size();++i){
  90.                 /*if(t1.get(i).getH()<10)
  91.                     rez+="0"+t1.get(i).getH()+":";
  92.                 else
  93.                     rez+=t1.get(i).getH()+":";
  94.  
  95.                 if(t1.get(i).getM()<10)
  96.                     rez+="0"+t1.get(i).getM()+"\n";
  97.                 else
  98.                     rez+=t1.get(i).getM()+"\n";*/
  99.                 rez+=t1.get(i).format24()+"\n";
  100.  
  101.             }
  102.         }
  103.         if(format.equals(TimeFormat.FORMAT_AMPM)){
  104.             for(int i=0;i<t1.size();i++){
  105.                 rez+=t1.get(i).formatAMPM()+"\n";
  106.             }
  107.         }
  108.         out.print(rez);
  109.         out.flush();
  110.     }
  111.  
  112. }
  113. class Time implements Comparable<Time>{
  114.     int h,m;
  115.  
  116.     public Time(int h, int m) {
  117.         this.h = h;
  118.         this.m = m;
  119.     }
  120.  
  121.     public int getM() {
  122.         return m;
  123.     }
  124.  
  125.     public void setM(int m) {
  126.         this.m = m;
  127.     }
  128.  
  129.     public int getH() {
  130.         return h;
  131.     }
  132.  
  133.     public void setH(int h) {
  134.         this.h = h;
  135.     }
  136.     public String formatAMPM() {
  137.         String ampm;
  138.         if (h == 0) {
  139.             h +=12;
  140.             ampm = "AM";
  141.         }
  142.         else if (h == 12) {
  143.             ampm = "PM";
  144.         }
  145.         else if (h < 12) {
  146.             ampm = "AM";
  147.         }
  148.         else {
  149.             h -= 12;
  150.             ampm = "PM";
  151.         }
  152.         return String.format("%2d:%02d %s", h,m,ampm);
  153.     }
  154.  
  155.     public String format24() {
  156.         return String.format("%2d:%02d", h,m);
  157.     }
  158.     @Override
  159.     public int compareTo(Time time) {
  160.         if(this.h!=time.h){
  161.             if(this.h<time.h)
  162.                 return -1;
  163.             else
  164.                 return 1;
  165.         }
  166.         else if(this.m!=time.m){
  167.             if(this.m<time.m)
  168.                 return -1;
  169.             else
  170.                 return 1;
  171.         }
  172.         else
  173.             return 0;
  174.     }
  175. }
  176. class UnsupportedFormatException extends Exception{
  177.     String s;
  178.     public UnsupportedFormatException(String s1){
  179.         super(s1);
  180.         s=s1;
  181.     }
  182.     @Override
  183.     public String getMessage(){return s;}
  184. }
  185. class InvalidTimeException extends Exception{
  186.     String s;
  187.     public InvalidTimeException(String s1){
  188.         super(s1);
  189.         s=s1;
  190.     }
  191.     @Override
  192.     public String getMessage(){return s;}
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement