본문 바로가기

JavaCode(review)

InnerClass Challenge - Playlist -Album에 innerClass 이용

반응형

InnerClass define In Main Class >  

OuterClass.InnerClass object = OuterClass object.new InnerClass();

 

LinkedList Challenge(PlayList) 이용.

Main, Song class는 그대로 , Album clas에 innerClass 이용.

 

수정 Album Class - Inner Class (SongList)반영

package com.company;                        //modify  the palylist challenge
                                            //Album class uses an inner class

import java.util.ArrayList;
import java.util.LinkedList;

public class Album {             //add song to the album, getSong from Album

    private  SongList songs;             //InnerClass object define
    private String albumName;
    private String artist;

    //constructor
    public Album(String albumName, String artist) {
        this.songs = new SongList();           //수정    //Inner class object
        this.albumName = albumName;
        this.artist = artist;
    }

    //method addSong  // 수정   
    				////Inner class에 addSong있어도 지우지 않음/ main class서 이거로 불러야하니
    public boolean addSong(String title, double duration) {

        return this.songs.add(new Song(title, duration));   //inner class add method와 연결
    }

    //playList 재생목록에 추가하기 2가지 방법> linkedList 사용

    //trackNumber 있으면 add       //수정
    public boolean addToPlaylist(int trackNumber, LinkedList<Song> playList){
        Song checkedSong = this.songs.findSong(trackNumber);    //inner class와 연결
        System.out.println("there is no track"+trackNumber);
        if(checkedSong != null){
            playList.add(checkedSong);
        }
        System.out.println("this album does not have a trak "+trackNumber);
        return false;
    }
    //title 있으면 add
    public boolean addToPlayList(String title, LinkedList<Song> playList){  //playlist linkedList선언
        Song checkedSong = this.songs.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;
    }

    //SongList - inner class
    private class SongList{
        //field
        private ArrayList<Song> songs;

        //constructor
        public SongList() {
            this.songs = new ArrayList<Song>(); //class Song에 arraylist
        }
        //add songs
        public boolean add(Song song){
            if(songs.contains(song)){
                return false;
            }
            this.songs.add(song);
            return true;
        }

        //add songs to the playlist /title로 찾기, track으로 찾기
        public 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;
        }
        public Song findSong(int trackNumber){
            int index = trackNumber -1;
            if(index >= 0 && (index<= this.songs.size())){         //index가 0~사이즈 해당범위라면
                return  songs.get(0);         //song을 playList에 추가위한 검색
            }
            return null;
        }

    }



}

나머지 class 그대로

결과값도 동일.

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 ;
    }
}


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());
        }
    }
}