Advertisement
LynchzDEV

song

Jul 5th, 2023 (edited)
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Song {
  4.  
  5.     private String title;
  6.     private String artist;
  7.     private int duration;
  8.     private boolean isLike;
  9.  
  10.     // Constructor
  11.     public Song(String title, String artist, int duration) {
  12.         this.title = title;
  13.         this.artist = artist;
  14.         this.duration = duration; // in secound
  15.         this.isLike = false;
  16.     }
  17.  
  18.     // Getter Setter
  19.     public String getTitle() {
  20.         return title;
  21.     }
  22.  
  23.     public void setTitle(String title) {
  24.         this.title = title;
  25.     }
  26.  
  27.     public String getArtist() {
  28.         return artist;
  29.     }
  30.  
  31.     public void setArtist(String artist) {
  32.         this.artist = artist;
  33.     }
  34.  
  35.     public int getDuration() {
  36.         return duration;
  37.     }
  38.  
  39.     public void setDuration(int duration) {
  40.         this.duration = duration;
  41.     }
  42.  
  43.     // Add song you Favorite
  44.     public String addToFavorite() {
  45.         if (this.isLike) {
  46.             this.isLike = false;
  47.             return getTitle() + " is disliked!";
  48.         }
  49.         this.isLike = true;
  50.         return getTitle() + " is liked!";
  51.     }
  52.  
  53.     // Format Second To Minute
  54.     public String getFormattedDuration() {
  55.         int minutes = duration / 60;
  56.         int seconds = duration % 60;
  57.         return minutes + " minutes " + seconds + " seconds";
  58.     }
  59.  
  60.     // Check is The same artist
  61.     public boolean isSameArtist(Song otherSong) {
  62.         if (this.artist.equals(otherSong.getArtist())) {
  63.             return true;
  64.         } else {
  65.             return false;
  66.         }
  67.     }
  68.  
  69.     // Check is isFavorite
  70.     public String isFavorite() {
  71.         if (this.isLike) {
  72.             return getTitle() + " is liked";
  73.         }
  74.         return getTitle() + " is disliked";
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         // Create multiple Song objects
  79.         Song song1 = new Song("Shape of You", "Ed Sheeran", 234);
  80.         Song song2 = new Song("Blinding Lights", "The Weeknd", 201);
  81.         Song song3 = new Song("Someone Like You", "Adele", 278);
  82.         Song song4 = new Song("Castle on the Hill", "Ed Sheeran", 273);
  83.  
  84.         // Test the getters for each song
  85.         System.out.println("Song 1 - Title: " + song1.getTitle() + ", Artist: " + song1.getArtist() + ", Duration: "
  86.                 + song1.getFormattedDuration());
  87.         System.out.println("");
  88.         System.out.println("Song 2 - Title: " + song2.getTitle() + ", Artist: " + song2.getArtist() + ", Duration: "
  89.                 + song2.getFormattedDuration());
  90.         System.out.println("");
  91.         System.out.println("Song 3 - Title: " + song3.getTitle() + ", Artist: " + song3.getArtist() + ", Duration: "
  92.                 + song3.getFormattedDuration());
  93.         System.out.println("");
  94.         System.out.println("Song 4 - Title: " + song4.getTitle() + ", Artist: " + song4.getArtist() + ", Duration: "
  95.                 + song4.getFormattedDuration());
  96.  
  97.         // Test setting new values using setters
  98.         song1.setTitle("Thinking Out Loud");
  99.         song1.setArtist("Ed Sheeran");
  100.         song1.setDuration(270);
  101.  
  102.         song2.setTitle("Dance Monkey");
  103.         song2.setArtist("Tones and I");
  104.         song2.setDuration(209);
  105.  
  106.         song3.setTitle("Hello");
  107.         song3.setArtist("Adele");
  108.         song3.setDuration(293);
  109.  
  110.         song4.setTitle("Photograph");
  111.         song4.setArtist("Ed Sheeran");
  112.         song4.setDuration(257);
  113.  
  114.         // Test the getters again
  115.         System.out.println("Song 1 - Title: " + song1.getTitle() + ", Artist: " + song1.getArtist() + ", Duration: "
  116.                 + song1.getFormattedDuration());
  117.         System.out.println("Song 2 - Title: " + song2.getTitle() + ", Artist: " + song2.getArtist() + ", Duration: "
  118.                 + song2.getFormattedDuration());
  119.         System.out.println("Song 3 - Title: " + song3.getTitle() + ", Artist: " + song3.getArtist() + ", Duration: "
  120.                 + song3.getFormattedDuration());
  121.         System.out.println("Song 4 - Title: " + song4.getTitle() + ", Artist: " + song4.getArtist() + ", Duration: "
  122.                 + song4.getFormattedDuration());
  123.  
  124.         // Test adding songs to favorites
  125.         System.out.println(song1.addToFavorite());
  126.         System.out.println(song2.addToFavorite());
  127.         System.out.println(song3.addToFavorite());
  128.         System.out.println(song4.addToFavorite());
  129.  
  130.         // Test isFavorite() method
  131.         System.out.println(song1.isFavorite());
  132.         System.out.println(song2.isFavorite());
  133.         System.out.println(song3.isFavorite());
  134.         System.out.println(song4.isFavorite());
  135.  
  136.         // Test isSameArtist() method
  137.         System.out.println("Song 1 and Song 2 have the same artist: " + song1.isSameArtist(song2));
  138.         System.out.println("Song 1 and Song 3 have the same artist: " + song1.isSameArtist(song3));
  139.         System.out.println("Song 1 and Song 4 have the same artist: " + song1.isSameArtist(song4));
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement