본문 바로가기
Java

[Java] 미니미니 프로젝트 - Mp3 Player

by 리슈다 2025. 2. 1.

국비 수업 지금까지 들으면서 배운 것들 활용해서 미니미니 프로젝트를 하라고 하셔서..

이미 저장해놓은 음악 정보에서 곡과 가수 검색해서 꺼내오는 기능,

랜덤한 곡 재생 혹은 랜덤한 플레이리스트 만들어주는 기능,

그리고 새로운 곡 추가 기능을 구현한 MP3를 만들어보려고 한다.


 

 

public class Mp3DB {
    // 저장된 음악 DB
    // 곡 제목, 가수, 장르
    String[][] songList =
            {{"space", "더 폴스", "밴드"}, {"High tide", "더 폴스", "밴드"}, {"Oddities", "더 폴스", "밴드"}, {"cares", "더 폴스", "밴드"}, {"Good Morning Sunshine", "더 폴스", "밴드"},
                    {"입춘", "한로로", "밴드"}, {"정류장", "한로로", "밴드"}, {"거울", "한로로", "밴드"}, {"생존법", "한로로", "밴드"}, {"화해", "한로로", "밴드"},
                    {"seasons", "wave to earth", "밴드"}, {"ride", "wave to earth", "밴드"}, {"light", "wave to earth", "밴드"}, {"homesick", "wave to earth", "밴드"},
                    {"사랑으로", "wave to earth", "밴드"}, {"calla", "wave to earth", "밴드"},
                    {"call out my name", "the weeknd", "팝"}, {"die for you", "the weeknd", "팝"}, {"If I ain't got you", "Alicia Keys", "팝"}, {"All of me", "John Legend", "팝"}};
}

Mp3DB에는 음악 정보들 2차원 배열로 저장했다.

제목, 가수, 장르 순서대로 저장해두었다.

 

public class Mp3DTO {
    // Mp3 정보
    private String title;
    private String artist;
    private String genre;

    public Mp3DTO(String title, String artist, String genre){
        this.title = title;
        this.artist = artist;
        this.genre = genre;
    }
    public Mp3DTO(){}

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }
}

Mp3DTO.. 사실 DTO가 무슨 역할인지는 아직 정확힌 모르는데 일단 getter/setter 메서드 있고 정보 저장 및 반환하는 역할은 하는 걸로 알아서 일단 구현은 해뒀다.

 

import java.util.Scanner;
public class Mp3DAO {
    // 실행 코드들
    Mp3DB db = new Mp3DB();
    Mp3DTO songList[] = new Mp3DTO[db.songList.length];
    int songCnt;
    public void Mp3set(){
        songCnt = songList.length;
        for(int i = 0; i < songCnt; i++){
            songList[i] = new Mp3DTO(db.songList[i][0], db.songList[i][1], db.songList[i][2]);
        }
    }

    public void Mp3Info(){
        while(true){
            Scanner sc = new Scanner(System.in);
            System.out.println("================MP3================");
            System.out.println("항목을 선택하세요[종료 : Q]");
            System.out.println("1. 곡 검색");
            System.out.println("2. 가수 검색");
            System.out.println("3. 랜덤 재생");
            System.out.printf("번호를 선택하세요: ");
            String search = sc.nextLine();

            if(search.isEmpty()){
                System.out.println("Enter를 입력하셨습니다. 번호를 입력하세요");
            } else if(search.equalsIgnoreCase("q")){
                System.out.println("시스템을 종료합니다");
                System.exit(0);
            } else if(search.equals("1") || search.equals("2") || search.equals("3")){
                if(search.equals("1")){
                    searchTitle();
                } else if(search.equals("2")){
                    searchArtist();
                } else if(search.equals("3")){
                    randomPlay();
                }
            } else {
                System.out.println("올바른 입력이 아닙니다. 다시 입력하세요:");
                continue;
            }

        }
    }

    public void searchTitle(){
        System.out.println("************************************");
        while(true) {
            boolean chk = false;
            System.out.println("곡을 검색합니다.");
            System.out.printf("곡 제목을 입력하세요[메인 메뉴로 돌아가기 : Q]: ");
            Scanner sc = new Scanner(System.in);
            String search = sc.nextLine();
            if(search.equalsIgnoreCase("q")){
                System.out.println("메인 메뉴로 돌아갑니다.");
                break;
            } else if(search.isEmpty()){
                System.out.println("Enter입력 확인. 다시 입력하세요.");
            } else{
                for(int i = 0; i < songList.length; i++){
                    if(search.equalsIgnoreCase(db.songList[i][0])){
                        System.out.println("------------------------------------");
                        /*System.out.println("검색한 노래 정보입니다");
                        System.out.println("곡 제목: \t" + songList[i].getTitle());
                        System.out.println("가수: \t\t" + songList[i].getArtist());
                        System.out.println("장르: \t\t" + songList[i].getGenre());*/
                        System.out.println("검색한 노래 정보입니다");
                        System.out.printf("%-5s : %s%n", "제목", songList[i].getTitle());
                        System.out.printf("%-5s : %s%n", "가수", songList[i].getArtist());
                        System.out.printf("%-5s : %s%n", "장르", songList[i].getGenre());
                        System.out.println("------------------------------------");
                        chk = true;
                    }
                }
                if(chk == false){
                    System.out.println("찾으시는 곡이 저장 목록에 없습니다.");
                    System.out.println("------------------------------------");
                }
            }
        }

    }

    public void searchArtist(){
        boolean first = true;
        System.out.println("************************************");
        while(true) {
            boolean chk = false;
            System.out.println("가수를 검색합니다.");
            System.out.printf("가수 입력하세요[메인 메뉴로 돌아가기 : Q]: ");
            Scanner sc = new Scanner(System.in);
            String search = sc.nextLine();
            if(search.equalsIgnoreCase("q")){
                System.out.println("메인 메뉴로 돌아갑니다.");
                break;
            } else if(search.isEmpty()){
                System.out.println("Enter입력 확인. 다시 입력하세요.");
            } else{
                for(int i = 0; i < songList.length; i++){
                    if(search.equalsIgnoreCase(db.songList[i][1])){
                        if(first){
                            System.out.println("------------------------------------");
                            System.out.println("검색한 가수의 곡 리스트입니다");
                            first = false;
                        }
                        System.out.println(songList[i].getTitle());
                        chk = true;
                    }
                }
                System.out.println("------------------------------------");
                if(chk == false){
                    System.out.println("찾으시는 가수가 저장 목록에 없습니다.");
                    System.out.println("------------------------------------");
                }
            }
        }
    }

    public void randomPlay(){
        System.out.println("랜덤 재생");
    }
}

실행 코드들 들어간 Mp3DAO

지금까진 저장된 곡들에서 곡 제목과 가수 검색해서 가져오는 기능만 구현

랜덤 재생 기능이랑 곡 추가는 아직 구현 안 해놨다.

수업 때 random()을 활용한 로또 번호 추첨을 쓴 적이 있었는데 이걸 어떻게 활용해야할까.?

 

public class Mp3Main {
    public static void main(String[] args){
        Mp3DAO obj = new Mp3DAO();
        obj.Mp3set();
        obj.Mp3Info();
    }
}

Mp3Main