Advertisement
plamen911

Problem 5. Online Radio Database - Song.java

Mar 3rd, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package org.lynxlake._05InheritanceExercises._05OnlineRadioDatabase.models;
  2.  
  3. import org.lynxlake._05InheritanceExercises._05OnlineRadioDatabase.exceptions.*;
  4.  
  5. public class Song {
  6.     private String artistName;
  7.     private String songName;
  8.     private int seconds;
  9.     private int minutes;
  10.  
  11.     public Song(String artistName, String songName, String duration) {
  12.         this.setArtistName(artistName);
  13.         this.setSongName(songName);
  14.         this.setDuration(duration);
  15.     }
  16.  
  17.     public int getSeconds() {
  18.         return seconds;
  19.     }
  20.  
  21.     public int getMinutes() {
  22.         return minutes;
  23.     }
  24.  
  25.     private void setArtistName(String artistName) {
  26.         if (artistName == null || artistName.trim().length() < 3 || artistName.trim().length() > 20) {
  27.             throw new InvalidArtistNameException();
  28.         }
  29.         this.artistName = artistName;
  30.     }
  31.  
  32.     private void setSongName(String songName) {
  33.         if (songName == null || songName.trim().length() < 3 || songName.trim().length() > 30) {
  34.             throw new InvalidSongNameException();
  35.         }
  36.         this.songName = songName;
  37.     }
  38.  
  39.     private void setSeconds(int seconds) {
  40.         if (seconds < 0 || seconds > 59) {
  41.             throw new InvalidSongSecondsException();
  42.         }
  43.         this.seconds = seconds;
  44.     }
  45.  
  46.     private void setMinutes(int minutes) {
  47.         if (minutes < 0 || minutes > 14) {
  48.             throw new InvalidSongMinutesException();
  49.         }
  50.         this.minutes = minutes;
  51.     }
  52.  
  53.     private void setDuration(String duration) {
  54.         String[] durationParams = duration.split(":");
  55.         int minutes;
  56.         int seconds;
  57.         try {
  58.             minutes = Integer.parseInt(durationParams[0]);
  59.             seconds = Integer.parseInt(durationParams[1]);
  60.         } catch (NumberFormatException ex) {
  61.             throw new InvalidSongLengthException();
  62.         }
  63.         this.setMinutes(minutes);
  64.         this.setSeconds(seconds);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement