반응형
class song
package com.company;
public class Song { //having title and duration for a song
private String title;
private double duration; //class field
//constructor
public Song(String title, double duration) {
this.title = title;
this.duration = duration;
}
//getter
public String getTitle() {
return title;
}
public double getDuration() {
return duration;
}
//toString > optional : quickly print out / 그대로 출력
@Override
public String toString() {
return this.title + ":"+ this.duration ;
}
}
Class Album
package com.company;
import java.util.ArrayList;
import java.util.LinkedList;
public class Album { //add song to the album, getSong from Album
private ArrayList<Song> songs; //arraylist song define
private String albumName;
private String artist;
//constructor
public Album(String albumName, String artist) {
this.songs = new ArrayList<>(); //arrayList initialization
this.albumName = albumName;
this.artist = artist;
}
//method addSong
public boolean addSong(String title, double duration){
if(findSong(title)==null){
this.songs.add(new Song(title, duration)); //song ArrayList에 저장
return true;
}
return false;
}
private Song findSong(String title){
for(Song checkedSong : this.songs){ //shortcut 방법 < i=0;i<size();i++의
if(checkedSong.getTitle().equals(title)){ //checkedSong이 arrayList에 있으면
return checkedSong;
}
}
return null;
}
//playList 재생목록에 추가하기 2가지 방법> linkedList 사용
//trackNumber 있으면 add
public boolean addToPlaylist(int trackNumber, LinkedList<Song> playList){
int index = trackNumber -1;
if(index >= 0 && (index<= this.songs.size())){ //index가 0~사이즈 해당범위라면
playList.add(this.songs.get(index)); //song을 playList에 추가
return true;
}
System.out.println("there is no track"+trackNumber);
return false;
}
//title 있으면 add
public boolean addToPlayList(String title, LinkedList<Song> playList){
Song checkedSong = findSong(title);
if(checkedSong != null){
playList.add(checkedSong); //song을 playList에 추가
return true;
}
System.out.println("the title "+title+"is not in this album.");
return false;
}
Main- printOut
package com.company;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
public class Main { // playList 재생목록 실행해보자.
private static ArrayList<Album> Ablums = new ArrayList<>(); // album ArrayList 정의
public static void main(String[] args) {
//앨범setting
Album album1 = new Album("Nep","Yubin"); // object설정 및 call constructor
album1.addSong("one",3.4); //album에 song 입력/ song ArrayList에 저장
album1.addSong("two", 2.4);
album1.addSong("three",5.2);
Ablums.add(album1); // Album ArrayList에 album1 추가
Album album2 = new Album("Sep","Jina"); //object설정
album2.addSong("four",3.4); //앨범 song ArrayList에 저장
album2.addSong("five",4.3);
album2.addSong("six",2.4);
Ablums.add(album2); // Album ArrayList에 album2 추가
//재생목록 setting
LinkedList<Song> playList = new LinkedList<Song>(); //object설정
Ablums.get(0).addToPlayList("one",playList); //앨범arrayList의 앨범1 재생목록에 추가
Ablums.get(0).addToPlayList("two",playList);
Ablums.get(0).addToPlayList("zam",playList);//없는 song으로 추가 안됨.
Ablums.get(1).addToPlaylist(1,playList); //앨범2 재생목록에 추가
Ablums.get(1).addToPlaylist(2,playList);
Ablums.get(1).addToPlaylist(8,playList);//없는 song으로 추가 안됨.
//재생목록 재생
play(playList);
}
//method play
private static void play(LinkedList<Song> playList){
ListIterator<Song> listIterator = playList.listIterator(); //literator이용 반복문
if(playList.size() ==0 ){
System.out.println("No songs in playList");
}else{
System.out.println(listIterator.next().toString() + " is playing");
}
}
}
OutPut
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=49934:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\NewProject\LinkedListChallenge\out\production\LinkedListChallenge com.company.Main
the title zam is not in this album.
there is no track8
one:3.4 is playing //첫번째값 출력
Process finished with exit code 0
Main에 console 입력하여 case 별 출력
package com.company;
import java.util.*;
public class Main { // playList 재생목록 실행해보자.
private static ArrayList<Album> Ablums = new ArrayList<>(); // album ArrayList 정의
public static void main(String[] args) {
//앨범setting
Album album1 = new Album("Nep","Yubin"); // object설정 및 call constructor
album1.addSong("one",3.4); //album에 song 입력/ song ArrayList에 저장
album1.addSong("two", 2.4);
album1.addSong("three",5.2);
Ablums.add(album1); // Album ArrayList에 album1 추가
Album album2 = new Album("Sep","Jina"); //object설정
album2.addSong("four",3.4); //앨범 song ArrayList에 저장
album2.addSong("five",4.3);
album2.addSong("six",2.4);
Ablums.add(album2); // Album ArrayList에 album2 추가
//재생목록 setting
LinkedList<Song> playList = new LinkedList<Song>(); //object설정
Ablums.get(0).addToPlayList("one",playList); //앨범arrayList의 앨범1 재생목록에 추가
Ablums.get(0).addToPlayList("two",playList);
Ablums.get(0).addToPlayList("zam",playList);//없는 song으로 추가 안됨.
Ablums.get(1).addToPlaylist(1,playList); //앨범2 재생목록에 추가
Ablums.get(1).addToPlaylist(2,playList);
Ablums.get(1).addToPlaylist(8,playList);//없는 song으로 추가 안됨.
//재생목록 재생
play(playList);
}
//method play ////case 실행들 추가 // console 입력
private static void play(LinkedList<Song> playList){
Scanner scanner = new Scanner(System.in);
boolean quit = false;
boolean forward = true;
ListIterator<Song> listIterator = playList.listIterator(); //literator이용 반복문
if(playList.size() ==0 ){
System.out.println("No songs in playList");
}else{
System.out.println(listIterator.next().toString() + " is playing");
printMenu();
}
while(!quit){
int action = scanner.nextInt();
scanner.nextLine();
switch (action){
case 0 :
System.out.println("Playlist done");
quit = true;
break;
case 1 : //play next song ---> 방향
if(!forward){
if(listIterator.hasNext()){
listIterator.next();
}forward = true;
} //forward일때
if(listIterator.hasNext()){
System.out.println("Now playing "+ listIterator.next().toString());
} else{
System.out.println("we have reached the end of the playList");
forward=false;
}
break;
case 2 : //play previous song
if(forward){
if(listIterator.hasPrevious()){
listIterator.previous();
}forward=false;
} //forward아닐떄
if(listIterator.hasPrevious()) {
System.out.println("Now playing " + listIterator.previous().toString());
}else{
System.out.println("we are at the start of the playlist");
forward = true;
}
break;
case 3 : //relay the current song
if(forward){
if(listIterator.hasPrevious()){
System.out.println("Now replaying "+listIterator.previous().toString());
forward = false;
}else{
System.out.println("we are at the start of the list");
}
}//forward아니면 <----------방향으로 재생 중
else{
if(listIterator.hasNext()){
System.out.println("Now playing "+listIterator.next().toString());
forward = true;
}else{
System.out.println("we have reached the end of the playlist");
}
}
break;
case 4 :
printList(playList);
break;
case 5 :
printMenu();
break;
case 6 : //delete
if(playList.size()>0){
listIterator.remove(); //current 값 제거
System.out.println("deleted.");
if(listIterator.hasNext()){
System.out.println("playing "+listIterator.next());
}else if(listIterator.hasPrevious()){ //다음곡 없고, 이전곡만 존재
System.out.println("Now playing "+listIterator.previous());
}
}
break;
}
}
}
private static void printMenu(){
System.out.println("press action:");
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"+
"4 - show list in the playlist\n"+
"5 - show actions\n"+
"6 - delete current song and playing song");
}
private static void printList(LinkedList<Song> playList){
Iterator<Song> iterator = playList.iterator(); //iterator 반복문이용
System.out.println("==================");
while(iterator.hasNext()){
System.out.println(iterator.next().toString());
}
}
}
'JavaCode(review)' 카테고리의 다른 글
InnerClass Challenge - Playlist -Album에 innerClass 이용 (0) | 2020.06.19 |
---|---|
Interface Challenge (0) | 2020.06.16 |
LinkedList 1. alphabetical order(알파벳 순으로 값추가) / 2. visit method - console 이용 case (0) | 2020.06.11 |
LinkedList 개념코딩 (0) | 2020.06.11 |
Bank Application - Autoboxing & Unboxing Challenge (0) | 2020.06.03 |