3 분 소요

내 블로그 첫 페이지에도 있는, 내 뮤직리스트를 c++로 표현한 것이다.
당연한 얘기겠지만 실제로 듣는 노래는 이것보다 많다. 단지 1달 정도로 사지방에서 짬내서 만든 거고,
코딩을 아얘 모르는 상태에서 만든 것이기 때문에 노래 수도 많지 않고, 코드들도 살짝 raw하다.
(물론 한동안 안했고, 관심도 없었기에 지금 실력도 그때와 비슷하거나 오히려 못하다..)
하지만 내가 동기한테 계속 묻고 물어서 만든 코드이고, 필터링 기능도 있어서, 아주 없어 보이진 않는다.
밑에는 내가 쓴 코드이다. 엄청 길긴 하지만, 왜인지는 모르겠는데 그냥 이 코드를 여기 포스트에도 보여주고 싶었다.


My-Music-List.cpp

#include <iostream>
#include <random>

using namespace std;

void print(string line) {
    printf("%s\n", line.c_str());
}

class MyMusic {
    private :
        string title;
        string artist;
        unsigned int rating;
    
    public :
        void setInfo(string title, string artist, unsigned int rating) {
            this->title = title; // private 안의 변수와 public 안 변수가 이름이 같을 때, this포인터 사용 
            this->artist = artist; // 하지만 변수 이름이 다르다면 굳이 할 필요가 없다.
            this->rating = rating; // ex) title_로 해놓고 title = title_하면 된다.
        }
        
        void showSong() {
            cout << "This song is " << title << " by " << artist << "." << endl;
            cout << "My rating is " << rating << "!" << endl;
        } // 노래, 아티스트, 점수 보여주기
        
        string getArtist() {
            return artist;
        } // 아티스트 필터 getter
        
        int getRating() {
            return rating;
        } // 점수 필터 getter
        
        void setArtist(string artist) {
            this->artist = artist;
        } // 아티스트 필터 setter
        
        void setRating(unsigned int rating) {
            this->rating = rating; // 점수 필터 setter
        }
};


class EveryMusicList {
    private :
        MyMusic m[100];
        int music_list_size = 0;
        
        void addMusic(string title, string artist, int rating) {
            m[music_list_size].setInfo(title, artist, rating);
            music_list_size += 1;
            
        }
    
    public : 
        void setMusicList() {
            addMusic("AHHA", "Nate Ruess", 8);
            addMusic("Runaway", "Kanye West", 9);
            addMusic("Grace Kelly", "MIKA", 4);
            addMusic("Gansta's Paradise", "Coolio", 6);
            addMusic("Sunday Candy", "Chance the Rapper", 6);
            addMusic("Fuck You", "Cee Lo Green", 6);
            addMusic("Summertime Sadness", "Lana Del Rey", 6);
            addMusic("In Your Eyes", "The Weeknd", 8);
            addMusic("Boulevard of Broken Dreams", "Green Day", 3);
            addMusic("Snow(Hey oh)", "Red Hot Chili Peppers", 5);
            addMusic("Kids", "MGMT", 5);
            addMusic("Erase your Social", "Lil Uzi Vert", 7);
            addMusic("I Miss You", "blink-182", 6);
            addMusic("There's a good reason these tables are numbered", "Panic! at the disco", 7);
            addMusic("Numb", "Linkin Park", 5);
            addMusic("Finesse", "Bruno Mars", 6);
            addMusic("Pursuit of Happiness", "Kid Cudi", 8);
            addMusic("La La Land", "Demi Lovato", 4);
            addMusic("She Knows", "J.Cole", 6);
            addMusic("Taco Tuesday", "Migos", 4);
            addMusic("Less than Zero", "The Weeknd", 8);
            addMusic("Bittersweet Poetry", "Kanye West", 9);
            addMusic("Through the Wire", "Kanye West", 8);
            addMusic("Two Words", "Kanye West", 7);
            addMusic("Is there Someone Else?", "The Weeknd", 8);
            addMusic("She doesn't get it", "The Format", 5);
            addMusic("The First Single", "The Format", 8);
            addMusic("Famous Last Words", "My Chemical Romance", 9);
            addMusic("My Shot", "Hamilton: The Musical", 10);
            addMusic("Cool for the Summer", "Demi Lovato", 7);
            addMusic("Helena", "My Chemical Romance", 10);
            addMusic("Scott Mescudi Vs. The World", "Kid Cudi", 8);
            addMusic("Man on the Moon", "Kid Cudi", 9);
            addMusic("Dear Jealousy", "MIKA", 5);
            addMusic("Headlights", "Eminem", 7);
            addMusic("Armed and Dangerous", "Juice Wrld", 7);
            addMusic("Topanga", "Trippie Redd", 7);
            addMusic("Roses", "OutKast", 6);
        }
        
        void showMusicList() {
            for(int i = 0; i < music_list_size; ++i) {
                m[i].showSong();
            }
        }
        
        void todayRec() {
            random_device rd;   
            mt19937 gen(rd());
            uniform_int_distribution<int> dis(0,music_list_size - 1);
            cout << "**********    Recommendation    **********" << endl;
            m[dis(gen)].showSong();
            cout << "**********    Recommendation    **********" << endl;
        }
        
        void filterArtist(string artist_) {
            print("**********  Artist : " + artist_ + " **********"); // 이런식으로 가능하다..
            for(int i = 0; i < music_list_size; ++i) {
                if(m[i].getArtist() == artist_) {
                    m[i].showSong();
                }
            }
            print("**********  Artist : " + artist_ + " **********");
        }
        
        void filterRating(unsigned int rating_) {
            print("**********      Rating : " + to_string(rating_) + "      **********"); // int를 string으로!
            for(int i = 0; i < music_list_size; ++i) {
                if(m[i].getRating() == rating_) {
                    m[i].showSong();
                }
            }
            print("**********      Rating : " + to_string(rating_) + "      **********");
        }
};




void magicFunction() {
    EveryMusicList my_music_list;
    
    my_music_list.setMusicList();
    my_music_list.todayRec();
    my_music_list.filterArtist("The Weeknd");
    my_music_list.filterRating(5);
}

int main() {
    magicFunction();
    

    return 0;
}

output:

**********    Recommendation    **********
This song is Pursuit of Happiness by Kid Cudi.
My rating is 8!
**********    Recommendation    **********
**********  Artist : The Weeknd **********
This song is In Your Eyes by The Weeknd.
My rating is 8!
This song is Less than Zero by The Weeknd.
My rating is 8!
This song is Is there Someone Else? by The Weeknd.
My rating is 8!
**********  Artist : The Weeknd **********
**********      Rating : 5      **********
This song is Snow(Hey oh) by Red Hot Chili Peppers.
My rating is 5!
This song is Kids by MGMT.
My rating is 5!
This song is Numb by Linkin Park.
My rating is 5!
This song is She doesn't get it by The Format.
My rating is 5!
This song is Dear Jealousy by MIKA.
My rating is 5!
**********      Rating : 5      **********