Create a program that implements a playlist for songs
Create a Song class having Title and Duration for a song.
The program will have an Album class containing a list of songs.
The albums will be stored in an ArrayList
Songs from different albums can be added to the playlist and will appear in the list in the order they are added.
Once the songs have been added to the playlist, create a menu of options to:-
Quit,Skip forward to the next song, skip backwards to a previous song. Replay the current song.
List the songs in the playlist
A song must exist in an album before it can be added to the playlist (so you can only play songs that you own).
Hint: To replay a song, consider what happened when we went back and forth from a city before we started tracking the direction we were going.
As an optional extra, provide an option to remove the current song from the playlist
hint: listiterator.remove()
Song.java
public class Song {
private String title;
private String duration; public Song(String title, String duration) {
this.title = title;
this.duration = duration;
} public String getTitle() {
return title;
} public String getDuration() {
return duration;
} public static Song createSong(String title, String duration){
return new Song(title, duration);
}
}
Album.java
import java.util.ArrayList;public class Album {
private String name;
private ArrayList<Song> albums; public Album(String name) {
this.name = name;
albums = new ArrayList<Song>();
} public static Album createAlbum(String name) {
return new Album(name);
} public String getName() {
return name;
} public void addSong(String title, String duration) {
albums.add(Song.createSong(title, duration));
} public void printSongs() {
System.out.println("The song of Album: ");
for (Song s : albums) {
System.out.println("No." + (albums.indexOf(s) + 1) + " song is " + s.getTitle() + " duration is " + s.getDuration());
}
} public void printAlbums(boolean b) {
}
}
PlayList.java
import java.util.LinkedList;
import java.util.ListIterator;public class PlayList {
private String name;
private LinkedList<Album> playLists;
public PlayList(String name) {
this.name = name;
playLists = new LinkedList<Album>(); } public static PlayList createPlayList(String name) {
return new PlayList(name);
} public String getName() {
return name;
} public LinkedList<Album> getPlayLists() {
return playLists;
} public void addAlbum(Album album) {
ListIterator<Album> albumListIterator = playLists.listIterator(); while (albumListIterator.hasNext()) {
int comparison = albumListIterator.next().getName().compareTo(album.getName());
if (comparison == 0) {
System.out.println("Album - " + album.getName() + " - is already added in play lists");
return;
} else if (comparison > 0) {
albumListIterator.previous();
albumListIterator.add(album);
return;
}
}
albumListIterator.add(album);
} public void printAlbums(boolean includesSong) {
System.out.println("==============================================");
System.out.println("The list of Album: ");
for (Album s : playLists) {
System.out.println("No." + (playLists.indexOf(s) + 1) + " Album is " + s.getName());
if(includesSong){
s.printSongs();
}
}
System.out.println("==============================================");
}}
PlayListMain.java
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;public class PlayListMain {
public static void main(String[] args) {
Album album1 = Album.createAlbum("Apple Day");
album1.addSong("A", "1:11");
album1.addSong("AA", "2:22");
album1.addSong("AAA", "3:33"); Album album2 = Album.createAlbum("Banana Day");
album2.addSong("B", "1:11");
album2.addSong("BB", "2:22");
album2.addSong("BBB", "3:33"); PlayList playList = PlayList.createPlayList("Emotion");
playList.addAlbum(album1);
playList.addAlbum(album2);
playList.printAlbums(false);
visit(playList); } private static void visit(PlayList playList) {
Scanner scanner = new Scanner(System.in);
boolean quit = false;
LinkedList<Album> albumList = playList.getPlayLists();
ListIterator<Album> albumListIterator = albumList.listIterator();
boolean changeDirection = false; if (albumList.isEmpty()) {
System.out.println("None album in the play list");
return;
} else {
System.out.println("Start to play album!\nWe are at the origin point now.");
printMenu();
} while (!quit) {
System.out.print("\n" + "Enter number to action:");
int action = scanner.nextInt();
scanner.nextLine();
switch (action) {
case 0:
System.out.println("Play list over!");
quit = true;
break;
case 1:
if (changeDirection) {
if (albumListIterator.hasNext()) {
albumListIterator.next();
}
changeDirection = false;
}
if (albumListIterator.hasNext()) {
System.out.println("Playing Album No." + (albumListIterator.nextIndex() + 1));
albumListIterator.next().printSongs();
} else {
System.out.println("No more next Album!");
changeDirection = true;
}
break;
case 2:
if (!changeDirection) {
if (albumListIterator.hasPrevious()) {
albumListIterator.previous();
}
changeDirection = true;
}
if (albumListIterator.hasPrevious()) {
System.out.println("Playing Album No." + (albumListIterator.previousIndex() + 1));
albumListIterator.previous().printSongs();
} else {
System.out.println("No more previous Album!");
changeDirection = false;
}
break;
case 3:
if (!changeDirection) {
if (albumListIterator.hasPrevious()) {
System.out.println("Playing Album No." + (albumListIterator.previousIndex() + 1));
albumListIterator.previous().printSongs();
} else {
System.out.println("No more previous Album!");
changeDirection = false;
}
} else {
if (albumListIterator.hasNext()) {
System.out.println("Playing Album No." + (albumListIterator.nextIndex() + 1));
albumListIterator.next().printSongs();
} else {
System.out.println("No more next Album!");
changeDirection = true;
}
}
break;
case 4:
printMenu();
break;
}
}
} private static void printMenu() {
System.out.println("Available actions:");
System.out.println("0 - Quit!\n" +
"1 - Go to next Album.\n" +
"2 - Go to previous Album.\n" +
"3 - Repeat current Album.\n" +
"4 - Print menu of actions again.\n" +
"-------------------------------");
}}
一樣是看完題目,自己先寫,沒有看答案
應該跟 Tim 要求的有些出入
畢竟他題目的指示,越後面的課程越來越難懂
檢查 song 有沒有進入 album 這也沒教阿 不曉得怎處理
Tim Version:
Song_TimVersion
public class Song_TimVersion { private String title;
private int duration; public Song_TimVersion(String title, int duration) {
this.title = title;
this.duration = duration;
} public static Song_TimVersion CreateSong(String title, int duration) {
return new Song_TimVersion(title, duration);
} public String getTitle() {
return title;
} @Override
public String toString() {
return "song title is : '" + title + "' , and duration is " + duration;
}
}
Album_TimVersion
import java.util.ArrayList;
import java.util.LinkedList;public class Album_TimVersion { private String name;
private String artist;
private ArrayList<Song_TimVersion> songs; public Album_TimVersion(String name, String artist) {
this.name = name;
this.artist = artist;
songs = new ArrayList<Song_TimVersion>();
} public Boolean addSong(String title, int duration) {
if (findSong(title) == null) {
songs.add(Song_TimVersion.CreateSong(title, duration));
}
return true;
} private Song_TimVersion findSong(String title) {
for (Song_TimVersion checkSong : songs) {
if (checkSong.getTitle().equals(title)) {
return checkSong;
}
}
return null;
} public boolean addToPlaylist(int trackNumber, LinkedList<Song_TimVersion> playlist) {
int index = trackNumber - 1;
if ((index >= 0) && (index < songs.size())) {
playlist.add(songs.get(index));
return true;
}
System.out.println("This album does not have a track " + trackNumber);
return false;
} public boolean addToPlaylist(String title, LinkedList<Song_TimVersion> playlist) {
Song_TimVersion song = findSong(title);
if (song != null) {
playlist.add(song);
return true; }
System.out.println("This album does not have song: " + title);
return false;
}
}
MainSong_TimVersion
import java.util.*;public class MainSong_TimVersion { private static ArrayList<Album_TimVersion> albums = new ArrayList<Album_TimVersion>(); public static void main(String[] args) { Album_TimVersion album = new Album_TimVersion("Alphabet song", "Elementary Bang");
album.addSong("AAA", 111);
album.addSong("BBB", 222);
album.addSong("CCC", 333);
album.addSong("DDD", 444);
album.addSong("EEE", 555);
albums.add(album); album = new Album_TimVersion("Number song", "Kindergarten Bang");
album.addSong("111", 666);
album.addSong("222", 777);
album.addSong("333", 888);
album.addSong("444", 999);
album.addSong("555", 1000);
albums.add(album); LinkedList<Song_TimVersion> playList = new LinkedList<Song_TimVersion>();
albums.get(0).addToPlaylist(1, playList);
albums.get(0).addToPlaylist(6, playList); // not exist
albums.get(0).addToPlaylist("BBB", playList);
albums.get(0).addToPlaylist("FFF", playList); // not exist albums.get(1).addToPlaylist(1, playList);
albums.get(1).addToPlaylist(6, playList); // not exist
albums.get(1).addToPlaylist("222", playList);
albums.get(1).addToPlaylist("666", playList); // not exist play(playList); } private static void play(LinkedList<Song_TimVersion> playList) {
Scanner sc = new Scanner(System.in);
boolean quit = false;
boolean forward = true;
ListIterator<Song_TimVersion> listIterator = playList.listIterator();
if (playList.size() > 0) {
System.out.println("Now playing " + listIterator.next().toString());
} else {
System.out.println("No songs in play list.");
} while (!quit) {
int action = sc.nextInt();
sc.nextLine(); switch (action) {
case 0:
System.out.println("System out!");
quit = true;
break;
case 1:
if (!forward) {
if (listIterator.hasNext()) {
listIterator.next();
}
forward = true;
}
if (listIterator.hasNext()) {
System.out.println("Now playing " + listIterator.next().toString());
} else {
System.out.println("We are reached the end of play list.");
forward = false;
}
break;
case 2:
if (forward) {
if (listIterator.hasPrevious()) {
listIterator.previous();
}
forward = false;
}
if (listIterator.hasPrevious()) {
System.out.println("Now playing " + listIterator.previous().toString());
} else {
System.out.println("We are at the start of play list.");
forward = true;
}
break;
case 3:
if (forward) {
if (listIterator.hasPrevious()) {
System.out.println("Now playing " + listIterator.previous().toString());
forward = false;
} else {
System.out.println("We are at the start of play list.");
}
} else {
if (listIterator.hasNext()) {
System.out.println("Now playing " + listIterator.next().toString());
forward = true;
} else {
System.out.println("We are reached the end of play list.");
}
}
break;
case 4:
printList(playList);
break;
case 5:
printMenu();
break;
case 6:
if (playList.size() > 0) {
listIterator.remove();
if (listIterator.hasNext()) {
System.out.println("Now playing " + listIterator.next().toString());
} else {
if (listIterator.hasPrevious()){
System.out.println("Now playing " + listIterator.previous().toString());
}
}
}else{
System.out.println("No songs in play list.");
}
break;
} }
} private static void printList(LinkedList<Song_TimVersion> playList) {
Iterator<Song_TimVersion> iterator = playList.iterator();
System.out.println("====================================");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("====================================");
} private static void printMenu() {
System.out.println("Available actions:\npress");
System.out.println("0 - to quit\n" +
"1 - to play next song\n" +
"2 - to play previous song\n" +
"3 - to replay the current song\n" +
"4 - list songs in the playlist\n" +
"5 - print available actions.\n" +
"6 - delete current song from playlist"); }
}