diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/hiphopArtist/HipHopArtist.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/hiphopArtist/HipHopArtist.java new file mode 100644 index 000000000..d04a1e599 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/hiphopArtist/HipHopArtist.java @@ -0,0 +1,143 @@ +package com.codedifferently.lesson16.hiphopArtist; + +import java.util.ArrayList; + +public class HipHopArtist { + private boolean isAlive; + private String stageName; + private int debutYear; + private double netWorth; + private ArrayList albums; + private Genre genre; + + public HipHopArtist( + boolean isAlive, + String stageName, + int debutYear, + double netWorth, + ArrayList albums, + Genre genre) { + this.isAlive = isAlive; + this.stageName = stageName; + this.debutYear = debutYear; + this.netWorth = netWorth; + this.albums = albums; + this.genre = genre; + } + + public String checkLegendStatus() { + return debutYear <= 2000 + ? stageName + " is a legend in the game." + : stageName + " is a modern star."; + } + + public void addAlbum(String album) { + albums.add(album); + System.out.println(album + " was added to " + stageName + "'s discography."); + } + + public String listAlbums() throws NoAlbumsException { + if (albums.isEmpty()) { + throw new NoAlbumsException(stageName + " has no albums listed."); + } + System.out.println("Albums by " + stageName + ":"); + for (String album : albums) { + System.out.println("- " + album); + } + return stageName; + } + + public String getBio() { + return stageName + + " (" + + genre + + ") debuted in " + + debutYear + + ", is " + + (isAlive ? "alive" : "deceased") + + ", and has a net worth of $" + + netWorth + + "."; + } + + public boolean isAlive() { + return isAlive; + } + + public void setAlive(boolean alive) { + isAlive = alive; + } + + public String getStageName() { + return stageName; + } + + public void setStageName(String stageName) { + this.stageName = stageName; + } + + public int getDebutYear() { + return debutYear; + } + + public void setDebutYear(int debutYear) { + this.debutYear = debutYear; + } + + public double getNetWorth() { + return netWorth; + } + + public void setNetWorth(double netWorth) { + this.netWorth = netWorth; + } + + public ArrayList getAlbums() { + return albums; + } + + public void setAlbums(ArrayList albums) { + this.albums = albums; + } + + public Genre getGenre() { + return genre; + } + + public void setGenre(Genre genre) { + this.genre = genre; + } + + public enum Genre { + RAP, + GANGSTA_RAP, + OLD_SCHOOL, + TRAP, + DRILL, + } + + @Override + public String toString() { + return "HipHopArtist{" + + "isAlive=" + + isAlive + + ", stageName='" + + stageName + + '\'' + + ", debutYear=" + + debutYear + + ", netWorth=" + + netWorth + + ", albums=" + + albums + + ", genre=" + + genre + + '}'; + } +} + +class NoAlbumsException extends Exception { + public NoAlbumsException(String message) { + super(message); + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/hiphopArtist/HipHopArtistTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/hiphopArtist/HipHopArtistTest.java new file mode 100644 index 000000000..cbc5524d2 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/hiphopArtist/HipHopArtistTest.java @@ -0,0 +1,162 @@ +package com.codedifferently.lesson16.hiphopArtist; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.codedifferently.lesson16.hiphopArtist.HipHopArtist.Genre; +import java.util.ArrayList; +import org.junit.jupiter.api.Test; + +public class HipHopArtistTest { + + @Test + public void testCheckLegendStatus() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Check legend status + String expected = "Kendrick Lamar is a modern star."; + String actual = artist.checkLegendStatus(); + + // Assert the result + assertEquals(expected, actual); + } + + @Test + public void testAddAlbum() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Add an album + artist.addAlbum("good kid, m.A.A.d city"); + + // Check if the album was added + assertTrue(artist.getAlbums().contains("good kid, m.A.A.d city")); + } + + @Test + public void testIsAlive() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Check if the artist is alive + boolean expected = true; + boolean actual = artist.isAlive(); + + // Assert the result + assertEquals(expected, actual); + } + + @Test + public void testGetStageName() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Get stage name + String expected = "Kendrick Lamar"; + String actual = artist.getStageName(); + + // Assert the result + assertEquals(expected, actual); + } + + @Test + public void testSetAlive() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Set alive status to false + artist.setAlive(false); + + // Check if the artist is alive + boolean expected = false; + boolean actual = artist.isAlive(); + + // Assert the result + assertEquals(expected, actual); + } + + @Test + public void testSetStageName() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Set stage name + artist.setStageName("K.Dot"); + + // Get stage name + String expected = "K.Dot"; + String actual = artist.getStageName(); + + // Assert the result + assertEquals(expected, actual); + } + + @Test + public void testGetDebutYear() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Get debut year + int expected = 2012; + int actual = artist.getDebutYear(); + + // Assert the result + assertEquals(expected, actual); + } + + @Test + public void testSetDebutYear() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Set debut year + artist.setDebutYear(2011); + + // Get debut year + int expected = 2011; + int actual = artist.getDebutYear(); + + // Assert the result + assertEquals(expected, actual); + } + + @Test + public void testGetNetWorth() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Get net worth + double expected = 75_000_000; + double actual = artist.getNetWorth(); + + // Assert the result + assertEquals(expected, actual); + } + + @Test + public void testSetNetWorth() { + // Create a HipHopArtist object + HipHopArtist artist = + new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP); + + // Set net worth + artist.setNetWorth(80_000_000); + + // Get net worth + double expected = 80_000_000; + double actual = artist.getNetWorth(); + + // Assert the result + assertEquals(expected, actual); + } +}