Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part I - The SimpleMusicTrack class You must design a class named SimpleMusicTra

ID: 641823 • Letter: P

Question

Part I - The SimpleMusicTrack class

You must design a class named SimpleMusicTrack. This class must implement the PlayListTrackinterface given below. You must download this file and import it into your Project03 source code folder before you can start to implement your own SimpleMusicTrack class.

/**

* PlayListTrack

*/

import java.util.Scanner;

public interface PlayListTrack {

     public String getName();

     public void setName(String name);

     public String getArtist();

     public void setArtist(String artist);

     public String getAlbum();

     public void setAlbum(String album);

     public boolean getNextTrack(Scanner infile);

         // Attempts to read a playlist track entry from a Scanner object

         // Sets the values in the object to the values given in

         // the file

         // If it successfully loads the track, return true

         // otherwise, return false

}

It is up to you how to represent the member variables of your SimpleMusicTrack class, but it must implement all of the methods given in the MusicTrack interface. Note in particular that it must implement the getNextTrack(Scanner in) method that reads a single entry from a Scanner object (used with the input datafile).

In addition, your SimpleMusicTrack class must implement the following methods not provided in the interface, but are inherited from the Object class:

equals(Object obj)

Explanation / Answer

import java.util.Scanner;
public class SimpleMusicTrack {


public class SimpleMusicTrack implements MusicTrack{
private String name;
private String artist;
private String albumName;

public SimpleMusicTrack(){}
//Convenience constructor for unit testing
public SimpleMusicTrack(String name, String artist, String album){
this.name=name;
this.artist=artist;
this.albumName=album;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name=name;
}
public String getArtist() {
return this.artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getAlbum() {
return this.albumName;
}
public void setAlbum(String album) {
this.albumName=album;
}
public boolean getNextTrack(Scanner infile) {
if(infile==null)
return false;
while(infile.hasNext()){
this.setName(infile.nextLine());
this.setArtist(infile.nextLine());
this.setAlbum(infile.nextLine());
return true;
}
return false;
}

public boolean equals(Object obj){
if(obj instanceof SimpleMusicTrack){
return EqualsBuilder.reflectionEquals(this, obj);
}
return false;
}

public String toString(){
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}

------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public interface MusicTrack {
String getName();
void setName (String name);
String getArtist();
void setArtist(String artist);
String getAlbum();
void setAlbum(String album);
boolean getNextTrack (Scanner inFile);
}

---------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SimplePlayList implements PlayList {
private Scanner musicTrack;
private List<MusicTrack> currentAndNextTrack;

public SimplePlayList(Scanner musicTrack) {
this.currentAndNextTrack = new ArrayList<MusicTrack>();
this.musicTrack = musicTrack;
}

public PlayListTrack getNextTrack() {
MusicTrack currentMusicTrackToPlay;
SimpleMusicTrack musicTrack = new SimpleMusicTrack();
boolean successfulTrackCreation = ((MusicTrack) musicTrack)
.getNextTrack(this.musicTrack);
// Beginning of the PlayList return null and set the next track at the
// zero index of the collection
if (this.currentAndNextTrack.size() == 0) {
if (successfulTrackCreation)
this.currentAndNextTrack.add((MusicTrack) musicTrack);
return null;
} else {
// gets the the music track at index zero and set as current track
// set next track at zero index of collection and return the
// original current track
currentMusicTrackToPlay = this.currentAndNextTrack.get(0);
if (successfulTrackCreation)
this.currentAndNextTrack.set(0, musicTrack);
else {
// display the rest tracks added in by the user
if (this.currentAndNextTrack.size() > 1) {
this.currentAndNextTrack.set(0,
this.currentAndNextTrack.get(1));
this.currentAndNextTrack.remove(1);
} else
this.currentAndNextTrack.remove(0);
}
}
return currentMusicTrackToPlay;
}

public PlayListTrack peekAtNextTrack() {
if (this.currentAndNextTrack.size() > 0)
return (PlayListTrack) currentAndNextTrack.get(0);
else
return null;
}

public void addTrack(MusicTrack musicTrack) {
if (musicTrack != null)
this.currentAndNextTrack.add(musicTrack);
}

public boolean isEmpty() {
return this.currentAndNextTrack.size() == 0;
}

public boolean equals(Object obj) {
if (obj instanceof SimplePlayList) {
return EqualsBuilder.reflectionEquals(this, obj);
}
return false;
}

public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}

@Override
public PlayListTrack getNextTrack() {
// TODO Auto-generated method stub
return null;
}

@Override
public PlayListTrack peekAtNextTrack1() {
// TODO Auto-generated method stub
return null;
}

@Override
public void addTrack(PlayListTrack track) {
// TODO Auto-generated method stub

}
}

---------------------------------------------------------------------------------------------------------

public interface PlayList {

public PlayListTrack getNextTrack();

// Removes track from PlayList and returns it to the caller
// Should return a null value if the PlayList is empty
public PlayListTrack peekAtNextTrack();

// Returns next entry to the caller, but leaves it in the list
public void addTrack(PlayListTrack track);

// Adds this track to the play list in the appropriate order
public boolean isEmpty();
// Returns true if the play list is empty
}

---------------------------------------------------------------------------------------------------------

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FinalProject {

public static final String INVENTORY_LIST_LOCATION = "/";

public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter database filename: ");
Scanner musicList = null;
try {
musicList = new Scanner(new File("\proj03_input.txt"));
String input = "P";
PlayList playList = new SimplePlayList(musicList);
while (!"Q".equalsIgnoreCase(input)) {
if ("A".equalsIgnoreCase(input)) {
displayAddTrackOption(userInput, playList);
input = "";
} else {
displayNextSong(playList);
input = userInput.nextLine();
}
}

displayRemainingTracks(playList);
} catch (FileNotFoundException e) {
System.out.println("Sorry, could not find your file");
}
}

private static void displayRemainingTracks(PlayList playList) {
System.out
.println("Tracks remaining in play list ------------------------------------------------------------");
if (!playList.isEmpty()) {
boolean hasAnotherTrack = true;
int lineNumber = 1;
while (hasAnotherTrack) {
MusicTrack currentTrackToPlay = (MusicTrack) playList
.getNextTrack();
if (currentTrackToPlay != null) {
System.out.printf("%d - %s / %s / %s ", lineNumber,
currentTrackToPlay.getName(),
currentTrackToPlay.getArtist(),
currentTrackToPlay.getAlbum());
lineNumber++;
} else
hasAnotherTrack = false;
}
} else
System.out.println("No tracks remaining");
}

private static void displayAddTrackOption(Scanner userInput,
PlayList playList) {
String title, artist, album, confirmation;
System.out.print("Track name: ");
title = userInput.nextLine();
System.out.print("Artist name: ");
artist = userInput.nextLine();
System.out.print("Album name: ");
album = userInput.nextLine();
System.out.println("New Track: " + title);
System.out.println("Artist: " + artist);
System.out.println("Album: " + album);
System.out.print("Are you sure you want to add this track [y/n]? ");
confirmation = userInput.nextLine();
if ("Y".equalsIgnoreCase(confirmation))
playList.addTrack((PlayListTrack) new SimpleMusicTrack());
}

private static void displayNextSong(PlayList playList) {
MusicTrack currentMusicTrackToPlay;
MusicTrack nextMusicTrackToPlay;
currentMusicTrackToPlay = (MusicTrack) playList.getNextTrack();
nextMusicTrackToPlay = (MusicTrack) playList.peekAtNextTrack();
if (currentMusicTrackToPlay != null)
System.out.println("Currently playing: "
+ currentMusicTrackToPlay.getName() + " / "
+ currentMusicTrackToPlay.getArtist());
else
System.out.println("Currently playing: No Song Playing");
if (nextMusicTrackToPlay != null)
System.out.println("Next track to play: "
+ nextMusicTrackToPlay.getName() + " / "
+ nextMusicTrackToPlay.getArtist());
else
System.out.println("Play list is empty

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote