Skip to content

Instantly share code, notes, and snippets.

@schleary
Last active February 14, 2020 21:11
Show Gist options
  • Save schleary/735998c9a727beeda523 to your computer and use it in GitHub Desktop.
Save schleary/735998c9a727beeda523 to your computer and use it in GitHub Desktop.
Mimics a simple music player
Blue Suede Shoes
Elvis Presley
Elvis Presley: Legacy Edition
With A Little Help From My Friends
The Beatles
Sgt. Pepper's Lonely Hearts Club Band
Seven Nation Army
The White Stripes
Elephant
Long Line Of Cars
Cake
Comfort Eagle
Head Like A Hole
Nine Inch Nails
Pretty Hate Machine
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 playlist in the appropriate order
public boolean isEmpty();
// Returns true if the playlist is empty
}
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
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* @author Holly Leary
*
*/
public class Project01 {
static PlayListTrack track = new SimpleMusicTrack();
static Scanner infile = new Scanner(System.in);
static SimplePlayList playList;
public String selection = "null";
public int trackInPlayCounter = 0;
/**
* Manage playlists
*/
public Project01() {
playList = new SimplePlayList(track);
getFile();
while (!(selection.equals("Q"))) {
try{
userInterface();
} catch (Exception e){
System.out.println("Ivalid entry. System shutting down.");
System.exit(0);
}
}
}
/**
* Gets file from user; converts to tracks in a playlist
*/
public static void getFile() {
System.out.print("Input filename:");
String filename = infile.nextLine();
File inputFile = new File(filename);
Scanner file;
try {
file = new Scanner(inputFile);
track.getNextTrack(file);
} catch (FileNotFoundException e) {
file = null;
file.close();
throw new IllegalArgumentException("You must enter a valid file.");
}
while (file.hasNext() == true) {
PlayListTrack track = new SimpleMusicTrack();
track.getNextTrack(file);
playList.addTrack(track);
}
}
public void selectionP(){
track = playList.getNextTrack();
this.trackInPlayCounter += 1;
}
public void selectionA(){
System.out.println("Please enter track name:");
String name = infile.nextLine();
System.out.println("Enter artist name:");
String artist = infile.nextLine();
System.out.println("Enter album name:");
String album = infile.nextLine();
PlayListTrack newTrack = new SimpleMusicTrack(name, artist,
album);
System.out.println("New track: " + newTrack.getName());
System.out.println("Artist: " + newTrack.getArtist());
System.out.println("Album: " + newTrack.getAlbum());
System.out.println("Are you sure you want to add this track [y/n]?");
String validity = infile.nextLine();
if ((validity.equals("y") || validity.equals("Y"))) {
playList.addTrack(newTrack);
} else {
this.selection = "no selection";
}
}
public void selectionQ(){
System.out.println("Tracks remaining in playlist: ");
playList.remainingTracks();
System.out.println();
this.selection = "Q";
System.exit(0);
}
public void getSelection(){
if (playList.songsinPlaylist() >= 1){
System.out.println("[P]lay next track");
}
System.out.println("[A]dd a new track");
System.out.println("[Q]uit");
selection = infile.nextLine();
}
/**
* User playlist interface
*/
public void userInterface() {
if ((selection != "P") && (selection != "p") && (selection != "A") && (selection != "a") && (selection != "Q") && (selection != "q") && (selection != null)){
selection = null;
userInterface();
} else {
if (this.trackInPlayCounter == 0) {
System.out.println("Currently playing: No Song Playing");
} else if (playList.songsinPlaylist()+ 1 == 0){
System.out.println("Currently playing: There are no songs remaining in playlist");
} else {
System.out.println("Currently playing: " + track.toString());
}
if (playList.songsinPlaylist() >= 1){
System.out.println("Next track to play: " + playList.peekAtNextTrack());
} else {
System.out.println("Next track to play: No songs remaining in playlist");
}
getSelection();
//If user selects "P"
if ((selection.equals("p")) || (selection.equals("P"))) {
selectionP();
//If user selects "A"
} else if ((selection.equals("a")) || (selection.equals("A"))) {
selectionA();
//If user selects "Q"
} else if ((selection.equals("q")) || (selection.equals("Q"))) {
selectionQ();
}
}
}
/**
* main
*
* @param args
*/
public static void main(String[] args) {
Project01 startProject01 = new Project01();
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
/**
* @author Holly Leary
*
*/
public class SimpleMusicTrack implements PlayListTrack {
private String album = null;
private String artist = null;
private String name = null;
public SimpleMusicTrack() {
}
/**
* @param name
* @param artist
* @param album
*/
public SimpleMusicTrack(String name, String artist, String album) {
setName(name);
setArtist(artist);
setAlbum(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.album;
}
public void setAlbum(String album) {
this.album = album;
}
/**
* @param Scanner infile
* @return boolean
*
* Attempts to read playlist track entry from Scanner object
* Sets values in object to values given in file
* Returns value regarding success of track loading
*/
public boolean getNextTrack(Scanner infile) {
try {
this.name = infile.nextLine();
this.artist = infile.nextLine();
this.album = infile.nextLine();
return this.equals(infile);
} catch (Exception e) {
return false;
}
}
/**
* @param obj
* @return boolean: are two tracks the same, or is there a failure?
*/
public boolean equals(Object obj) {
try {
if (obj instanceof SimpleMusicTrack) {
if ((this.name == ((SimpleMusicTrack) obj).getName())
&& (this.artist == ((SimpleMusicTrack) obj).getArtist())
&& (this.album == ((SimpleMusicTrack) obj).getAlbum())) {
return true;
}
}
return false;
} catch (Exception e){
return false;
}
}
/**
* @return String in 'Artist / Song' format
*/
public String toString() {
return "'" + getArtist() + " / " + getName() + "'";
}
}
import java.io.FileNotFoundException;
import java.util.ArrayList;
/**
*
* @author Holly Leary
*
*/
public class SimplePlayList implements PlayList {
public ArrayList<PlayListTrack> list = new ArrayList<PlayListTrack>();
public SimplePlayList(PlayListTrack track) {
addTrack(track);
}
/**
* @return PlayListTrack to caller; removes track from playlist
* Should return a null value if the PlayList is empty
*/
public PlayListTrack getNextTrack() {
PlayListTrack track;
track = this.list.get(0);
this.list.remove(0);
return track;
}
/**
* @returns next PLayListTrack entry to the caller, but leaves it in the list
*/
public PlayListTrack peekAtNextTrack() {
return this.list.get(0);
}
/**
* Prints and removes remaining tracks from playlist
*/
public void remainingTracks() {
if (list.size() == 0) {
System.out.println("No tracks remaining");
} else {
for (int i = 0; i < list.size(); i++) {
System.out.print(i + 1 + ".) ");
System.out.println(this.list.get(i).getArtist() + " / "
+ this.list.get(i).getName() + " / "
+ this.list.get(i).getAlbum());
}
list.clear();
}
}
/**
* @param track
* : adds this track to playlist in appropriate order
*/
public void addTrack(PlayListTrack track) {
this.list.add(track);
}
/**
* @return boolean: is playlist empty?
*/
public boolean isEmpty() {
if (this.list.size() >= 1) {
return true;
} else {
return false;
}
}
public int songsinPlaylist(){
int quantity = list.size();
return quantity;
}
}
@taraeicher
Copy link

taraeicher commented Oct 11, 2018

Hello, Holly. I'm an instructor for CSE 2123 (Data Structures Using Java) at OSU. This is the solution to a homework problem that we still use for that course. To discourage plagiarism, please remove this repository or make it private.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment