Guest User

Online Radio Database

a guest
Mar 12th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package _11OnlineRadioDatabase;/**
  2.  * Created by IntelliJ IDEA.
  3.  * User: LAPD
  4.  * Date: 12.3.2018 г.
  5.  * Time: 20:56 ч.
  6.  */
  7.  
  8. import _11OnlineRadioDatabase.exceptions.*;
  9.  
  10. import java.io.BufferedReader;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.text.ParseException;
  14. import java.text.SimpleDateFormat;
  15. import java.time.LocalDateTime;
  16. import java.time.format.DateTimeFormatter;
  17. import java.util.Calendar;
  18. import java.util.Date;
  19.  
  20. public class Main {
  21.     public static void main(String[] args) throws IOException, ParseException {
  22.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  23.  
  24.         int songsCount = Integer.parseInt(reader.readLine());
  25.  
  26.         int songsAdded = 0;
  27.  
  28.         SimpleDateFormat format = new SimpleDateFormat("H:m:s");
  29.         Date startDuration = format.parse("0:0:0");
  30.         Calendar playlistDuration = Calendar.getInstance();
  31.         playlistDuration.setTime(startDuration);
  32.  
  33.  
  34.         for (int i = 0; i < songsCount; i++) {
  35.  
  36.             try {
  37.                 String[] songArgs = reader
  38.                         .readLine()
  39.                         .split(";");
  40.                 if (songArgs.length != 3) {
  41.                     throw new InvalidSongException();
  42.                 }
  43.  
  44.                 String artistName = songArgs[0];
  45.                 if (artistName.length() < 3 || artistName.length() > 20) {
  46.                     throw new InvalidArtistNameException();
  47.                 }
  48.  
  49.                 String songName = songArgs[1];
  50.                 if (songName.length() < 3 || songName.length() > 20) {
  51.                     throw new InvalidSongNameException();
  52.                 }
  53.  
  54.                 String[] duration = songArgs[2].split(":");
  55.                 if (duration.length != 2) {
  56.                     throw new InvalidSongLengthException();
  57.                 }
  58.  
  59.                 int minutes = Integer.parseInt(duration[0]);
  60.                 if (minutes < 0 || minutes > 14) {
  61.                     throw new InvalidSongMinutesException();
  62.                 }
  63.  
  64.                 int seconds = Integer.parseInt(duration[1]);
  65.                 if (seconds < 0 || seconds > 59) {
  66.                     throw new InvalidSongSecondsException();
  67.                 }
  68.  
  69.                 System.out.println("Song added.");
  70.  
  71.                 songsAdded++;
  72.                 playlistDuration.add(Calendar.SECOND, seconds);
  73.                 playlistDuration.add(Calendar.MINUTE, minutes);
  74.  
  75.             } catch (InvalidArtistNameException e) {
  76.                 System.out.println("Artist name should be between 3 and 20 symbols.");
  77.  
  78.             } catch (InvalidSongNameException e) {
  79.                 System.out.println("Song name should be between 3 and 30 symbols.");
  80.  
  81.             } catch (InvalidSongMinutesException e) {
  82.                 System.out.println("Song minutes should be between 0 and 14.");
  83.  
  84.             } catch (InvalidSongSecondsException e) {
  85.                 System.out.println("Song seconds should be between 0 and 59.");
  86.  
  87.             } catch (InvalidSongLengthException e) {
  88.                 System.out.println("Invalid song length.");
  89.  
  90.             } catch (InvalidSongException e) {
  91.                 System.out.println("Invalid song.");
  92.             }
  93.         }
  94.  
  95.         System.out.println("Songs added: " + songsAdded);
  96.  
  97.         int h = playlistDuration.get(Calendar.HOUR);
  98.         int m = playlistDuration.get(Calendar.MINUTE);
  99.         int s = playlistDuration.get(Calendar.SECOND);
  100.         System.out.printf("Playlist length: %sh %sm %ss%n", h, m, s);
  101.     }
  102. }
Add Comment
Please, Sign In to add comment