Advertisement
vaakata

Class Inheritance Exercises - RadioDatabase

Mar 3rd, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.         int n = Integer.parseInt(reader.readLine());
  11.         RadioDatabase radioDb = new RadioDatabase();
  12.  
  13.  
  14.             for (int i = 0; i < n; i++) {
  15.                 try {
  16.                     String[] inputTokens = reader.readLine().trim().split(";");
  17.                     String artistName = inputTokens[0];
  18.                     String songName = inputTokens[1];
  19.                     String[] timeTokens = inputTokens[2].trim().split(":");
  20.                     Song song = new Song(artistName, songName, timeTokens);
  21.                     radioDb.addSong(song);
  22.  
  23.                 } catch (Exception ex) {
  24.                     System.out.println(ex.getMessage());
  25.                 }
  26.             }
  27.             System.out.println(radioDb);
  28.     }
  29. }
  30.  
  31. class RadioDatabase {
  32.  
  33.     private List<Song> playList;
  34.     private int totalTime;
  35.  
  36.     public RadioDatabase() {
  37.         this.playList = new ArrayList<>();
  38.     }
  39.  
  40.     public void addSong(Song song) throws NoSuchFieldException, IllegalAccessException {
  41.         this.playList.add(song);
  42. //        Field fieldTimeSeconds = song.getClass().getDeclaredField("timeSeconds");
  43. //        fieldTimeSeconds.setAccessible(true);
  44.         totalTime += song.getTime(); //(int) fieldTimeSeconds.get(song);
  45.         System.out.println("Song added.");
  46.     }
  47.  
  48.     private List<Song> getPlayList() {
  49.         return this.playList;
  50.     }
  51.  
  52.  
  53.     private int getTotalTime() {
  54.         return totalTime;
  55.     }
  56.  
  57.     @Override
  58.     public String toString() {
  59.         StringBuilder sb = new StringBuilder();
  60.         int timeSeconds = this.getTotalTime();
  61.         int hours = timeSeconds / 3600;
  62.         timeSeconds %= 3600;
  63.         int minutes = timeSeconds / 60;
  64.         timeSeconds %= 60;
  65.         int seconds = timeSeconds;
  66.         sb.append(String.format("Songs added: %s%n", this.getPlayList().size()));
  67.         sb.append(String.format("Playlist length: %sh %sm %ss", hours, minutes, seconds));
  68.         return sb.toString();
  69.     }
  70.  
  71. }
  72.  
  73. class Song {
  74.     private String artistName;
  75.     private String songName;
  76.     private int timeSeconds;
  77.  
  78.     public Song(String artistName, String songName, String[] timeTokens) {
  79.         this.setArtistName(artistName);
  80.         this.setSongName(songName);
  81.         this.setTime(timeTokens[0], timeTokens[1]);
  82.     }
  83.  
  84.     public int getTime() {
  85.         return this.timeSeconds;
  86.     }
  87.  
  88.     private void setTime(String minutes, String seconds) {
  89.         if (minutes == null || seconds == null) {
  90.             throw new InvalidSongLengthException(InvalidSongLengthException.INVALID_SONG_LENGTH);
  91.         }
  92.  
  93.         int minutesInt = Integer.parseInt(minutes);
  94.         int secondsInt = Integer.parseInt(seconds);
  95.  
  96.         if (minutesInt < 0 || minutesInt > 14) {
  97.             throw new InvalidSongLengthException(InvalidSongLengthException.INVALID_SONG_MINUTES);
  98.         }
  99.  
  100.         if (secondsInt < 0 || secondsInt >= 60) {
  101.             throw new InvalidSongLengthException(InvalidSongLengthException.INVALID_SONG_SECONDS);
  102.         }
  103.         this.timeSeconds = (minutesInt * 60) + secondsInt;
  104.     }
  105.  
  106.  
  107.     private void setArtistName(String artistName) {
  108.         if (artistName == null) {
  109.             throw new InvalidSongException(InvalidSongException.INVALID_SONG);
  110.         }
  111.  
  112.         if (artistName.length() <= 3 || artistName.length() >= 20) {
  113.             throw new InvalidSongException(InvalidSongException.INVALID_ARTIST_NAME);
  114.         }
  115.         this.artistName = artistName;
  116.     }
  117.  
  118.     public void setSongName(String songName) {
  119.         if (songName == null) {
  120.             throw new InvalidSongException(InvalidSongException.INVALID_SONG);
  121.         }
  122.  
  123.         if (songName.length() <= 3 || songName.length() >= 30) {
  124.             throw new InvalidSongException(InvalidSongException.INVALID_SONG_NAME);
  125.         }
  126.         this.songName = songName;
  127.     }
  128. }
  129.  
  130. class InvalidSongException extends IllegalArgumentException {
  131.  
  132.     public static final String INVALID_SONG = "Invalid song.";
  133.     public static final String INVALID_ARTIST_NAME = "Artist name should be between 3 and 20 symbols.";
  134.     public static final String INVALID_SONG_NAME = "Song name should be between 3 and 30 symbols.";
  135.  
  136.     public InvalidSongException(String message) {
  137.         super(message);
  138.     }
  139. }
  140.  
  141. class InvalidSongLengthException extends InvalidSongException{
  142.     public static final String INVALID_SONG_LENGTH = "Invalid song length.";
  143.     public static final String INVALID_SONG_MINUTES = "Song minutes should be between 0 and 14.";
  144.     public static final String INVALID_SONG_SECONDS = "Song seconds should be between 0 and 59.";
  145.  
  146.  
  147.     public InvalidSongLengthException (String message) {
  148.         super(message);
  149.     }
  150. }
  151.  
  152.  
  153.  
  154. /*
  155. Input 1:
  156. 3
  157. ABBA;Mamma Mia;3:35
  158. Nasko Mentata;Shopskata salata;4:123
  159. Nasko Mentata;Shopskata salata;4:12
  160.  
  161. Expected output 1:
  162. Song added.
  163. Song seconds should be between 0 and 59.
  164. Song added.
  165. Songs added: 2
  166. Playlist length: 0h 7m 47s
  167.  
  168.  
  169. Input 2:
  170. 5
  171. Nasko Mentata;Shopskata salata;14:59
  172. Nasko Mentata;Shopskata salata;14:59
  173. Nasko Mentata;Shopskata salata;14:59
  174. Nasko Mentata;Shopskata salata;14:59
  175. Nasko Mentata;Shopskata salata;0:5
  176.  
  177. Expected output 2:
  178. Song added.
  179. Song added.
  180. Song added.
  181. Song added.
  182. Song added.
  183. Songs added: 5
  184. Playlist length: 1h 0m 1s
  185.  
  186.  
  187.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement