Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Main {
- private static Connection connection = null;
- private static InputStreamReader input = new InputStreamReader(System.in);
- private static BufferedReader reader = new BufferedReader(input);
- private static Statement stmt, stmtAux;
- private static String utilizadorUsername;
- public static void connectDataBase() {
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Driver not found, check if the jar is reachable !");
- e.printStackTrace();
- return;
- }
- System.out.println("JDBC Driver funciona .. tentar a ligacao");
- try {
- connection = DriverManager.getConnection(
- "jdbc:postgresql://127.0.0.1:5432/postgres",
- "postgres",
- "basedados");
- } catch (SQLException e) {
- System.out.println("Ligacao falhou.. erro:");
- e.printStackTrace();
- return;
- }
- if (connection != null) {
- System.out.println("Connect successfully");
- } else {
- System.out.println("Nao conseguimos estabelecer a ligacao");
- }
- }
- public static void createTables(){
- try {
- Statement stmt;
- if (connection.createStatement() == null) {
- connection = DriverManager.getConnection(
- "jdbc:postgresql://127.0.0.1:5432/postgres",
- "postgres",
- "basedados");
- }
- if ((stmt = connection.createStatement()) == null) {
- System.out.println("Erro nao foi possível criar uma statement ou retornou null");
- System.exit(-1);
- }
- if ((stmtAux = connection.createStatement()) == null) {
- System.out.println("Erro nao foi possível criar uma statement ou retornou null");
- System.exit(-1);
- }
- String createTables = "CREATE TABLE artista (\n" +
- "\tn_artista\t BIGINT,\n" +
- "\tnome\t\t VARCHAR(512) NOT NULL,\n" +
- "\tperiodo\t VARCHAR(512) NOT NULL,\n" +
- "\thistoria\t VARCHAR(512) NOT NULL,\n" +
- "\tbanda_elementos VARCHAR(512),\n" +
- "\tsolo_nome\t VARCHAR(512),\n" +
- "\tPRIMARY KEY(n_artista)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE album (\n" +
- "\tn_album\t\t BIGINT,\n" +
- "\tnome\t\t VARCHAR(512) NOT NULL,\n" +
- "\tgenero\t\t VARCHAR(512) NOT NULL,\n" +
- "\tdata_album\t VARCHAR(512) NOT NULL,\n" +
- "\tartista_n_artista BIGINT NOT NULL,\n" +
- "\tPRIMARY KEY(n_album)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE musica (\n" +
- "\tn_musica\t\t BIGINT,\n" +
- "\tnome\t\t VARCHAR(512) NOT NULL,\n" +
- "\tletra\t\t VARCHAR(512) NOT NULL,\n" +
- "\tduracao\t\t SMALLINT NOT NULL,\n" +
- "\tcompositor\t VARCHAR(512) NOT NULL,\n" +
- "\teditora_n_editora BIGINT NOT NULL,\n" +
- "\tPRIMARY KEY(n_musica)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE playlist (\n" +
- "\tn_playlist\t\t BIGINT,\n" +
- "\tnome\t\t VARCHAR(512) NOT NULL,\n" +
- "\tutilizador_username VARCHAR(512) NOT NULL,\n" +
- "\tPRIMARY KEY(n_playlist)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE utilizador (\n" +
- "\tusername VARCHAR(512),\n" +
- "\tpassword VARCHAR(512) NOT NULL,\n" +
- "\tPRIMARY KEY(username)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE editor (\n" +
- "\tutilizador_username VARCHAR(512),\n" +
- "\tPRIMARY KEY(utilizador_username)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE nao_editor (\n" +
- "\tutilizador_username VARCHAR(512),\n" +
- "\tPRIMARY KEY(utilizador_username)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE critica (\n" +
- "\tn_critica\t\t BIGINT,\n" +
- "\tpontuacao\t\t SMALLINT NOT NULL,\n" +
- "\tcritica\t\t VARCHAR(512) NOT NULL,\n" +
- "\tutilizador_username VARCHAR(512) NOT NULL,\n" +
- "\tPRIMARY KEY(n_critica)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE concerto (\n" +
- "\tn_concerto BIGINT,\n" +
- "\tdata\t VARCHAR(512) NOT NULL,\n" +
- "\tlocalidade VARCHAR(512) NOT NULL,\n" +
- "\tPRIMARY KEY(n_concerto)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE ficheiro (\n" +
- "\tuploaded_file\t BOOL NOT NULL,\n" +
- "\tmusica_n_musica BIGINT,\n" +
- "\tPRIMARY KEY(musica_n_musica)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE editora (\n" +
- "\tn_editora BIGINT,\n" +
- "\tnome\t VARCHAR(512) NOT NULL,\n" +
- "\tPRIMARY KEY(n_editora)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE ficheiro_utilizador (\n" +
- "\tficheiro_musica_n_musica BIGINT,\n" +
- "\tutilizador_username\t VARCHAR(512),\n" +
- "\tPRIMARY KEY(ficheiro_musica_n_musica,utilizador_username)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE musica_critica (\n" +
- "\tmusica_n_musica\t BIGINT NOT NULL,\n" +
- "\tcritica_n_critica BIGINT,\n" +
- "\tPRIMARY KEY(critica_n_critica)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE album_editora (\n" +
- "\talbum_n_album\t BIGINT,\n" +
- "\teditora_n_editora BIGINT,\n" +
- "\tPRIMARY KEY(album_n_album,editora_n_editora)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE album_musica (\n" +
- "\talbum_n_album\t BIGINT,\n" +
- "\tmusica_n_musica BIGINT,\n" +
- "\tPRIMARY KEY(album_n_album,musica_n_musica)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE playlist_musica (\n" +
- "\tplaylist_n_playlist BIGINT,\n" +
- "\tmusica_n_musica\t BIGINT,\n" +
- "\tPRIMARY KEY(playlist_n_playlist,musica_n_musica)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE album_critica (\n" +
- "\talbum_n_album\t BIGINT NOT NULL,\n" +
- "\tcritica_n_critica BIGINT,\n" +
- "\tPRIMARY KEY(critica_n_critica)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE concerto_artista (\n" +
- "\tconcerto_n_concerto BIGINT,\n" +
- "\tartista_n_artista\t BIGINT,\n" +
- "\tPRIMARY KEY(concerto_n_concerto,artista_n_artista)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE artista_musica (\n" +
- "\tartista_n_artista BIGINT,\n" +
- "\tmusica_n_musica\t BIGINT,\n" +
- "\tPRIMARY KEY(artista_n_artista,musica_n_musica)\n" +
- ");\n" +
- "\n" +
- "CREATE TABLE playlist_privada_utilizador (\n" +
- "\tplaylist_n_playlist BIGINT,\n" +
- "\tutilizador_username VARCHAR(512),\n" +
- "\tPRIMARY KEY(playlist_n_playlist,utilizador_username)\n" +
- ");";
- stmt.executeUpdate(createTables);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void insertData(){
- try {
- Statement stmt;
- if (connection.createStatement() == null) {
- connection = DriverManager.getConnection(
- "jdbc:postgresql://127.0.0.1:5432/postgres",
- "postgres",
- "basedados");
- }
- if ((stmt = connection.createStatement()) == null) {
- System.out.println("Erro nao foi possível criar uma statement ou retornou null");
- System.exit(-1);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static boolean isEditor(String username) throws SQLException {
- String query = "select utilizador_username from editor where utilizador_username = '" +username+ "';";
- ResultSet res = stmt.executeQuery(query);
- if (res.next()) {
- return true;
- }
- else {
- return false;
- }
- }
- public static void login_register() throws IOException, SQLException {
- String a, name, password;
- while (true) {
- System.out.print("\nType login or register\n> ");
- a = reader.readLine();
- if (a.equals("login")) {
- System.out.print("\nWhat is your username?\n> ");
- name = reader.readLine();
- utilizadorUsername = name;
- System.out.print("\nWhat is your password?\n> ");
- password = reader.readLine();
- String look = "select username, password from utilizador where username ='" + name + "' and password ='" + password + "';";
- ResultSet res = stmt.executeQuery(look);
- if (!res.next()) {
- System.out.println("Wrong information, try again");
- continue;
- }
- else {
- System.out.println("\nStatus Logged | Welcome back to DropMusic");
- break;
- }
- }
- else if (a.equals("register")) {
- System.out.print("\nWhat is your username?\n> ");
- name = reader.readLine();
- utilizadorUsername = name;
- System.out.print("\nWhat is your password? \n> ");
- password = reader.readLine();
- String look = "select username from utilizador where username ='" + name + "';";
- ResultSet res = stmt.executeQuery(look);
- if (res.next()) {
- System.out.println("Username already exists, try again");
- continue;
- }
- else {
- String query = "INSERT INTO public.utilizador(\n" +
- "\tusername, password)\n" +
- "\tVALUES ('" + name + "', '" + password + "');";
- stmt.executeUpdate(query);
- String query2 = "INSERT INTO public.nao_editor(\n" +
- "\tutilizador_username)\n" +
- "\tVALUES ('"+name+"');";
- stmt.executeUpdate(query2);
- System.out.println("\nStatus Subscribed | Welcome to DropMusic");
- break;
- }
- }
- else {
- System.out.println("Something went wrong, try again");
- continue;
- }
- }
- }
- public static void searchSongByAlbum() throws IOException, SQLException {
- System.out.print("\nType album's name:\n> ");
- String album12 = reader.readLine();
- ArrayList <Integer> musicas = new ArrayList();
- String query12 = "select nome\n"+
- "from album\n" +
- "where nome = '" + album12 + "';";
- ResultSet res12 = stmt.executeQuery(query12);
- if (!res12.next()){
- System.out.println("That album doesn't exist");
- }
- else{
- String query121 = "select musica.nome, musica.n_musica\n" +
- "from album, musica, album_musica\n" +
- "where album.nome = '" + album12 + "' and n_musica = musica_n_musica\n"+
- "and n_album = album_n_album;";
- ResultSet res = stmt.executeQuery(query121);
- int i=1;
- System.out.println();
- while (res.next()) {
- System.out.println("> " + i + ". " + res.getString(1));
- musicas.add(Integer.parseInt(res.getString(2)));
- i++;
- }
- printSongInfo(musicas);
- }
- }
- public static void searchSongByGenre() throws IOException, SQLException {
- System.out.print("\nType album's genre:\n> ");
- String genre = reader.readLine();
- ArrayList <Integer> musicas = new ArrayList();
- String query12 = "select nome\n"+
- "from album\n" +
- "where genero = '" + genre + "';";
- ResultSet res12 = stmt.executeQuery(query12);
- if (res12.next()) {
- String query121 = "select musica.nome, musica.n_musica\n" +
- "from album, musica, album_musica\n" +
- "where album.genero = '" + genre + "' and n_musica = musica_n_musica and n_album = album_n_album;";
- ResultSet res121 = stmt.executeQuery(query121);
- int i=1;
- System.out.println();
- while (res121.next()) {
- System.out.println(i + ". " + res121.getString(1));
- musicas.add(Integer.parseInt(res121.getString(2)));
- i++;
- }
- printSongInfo(musicas);
- }
- else {
- System.out.println("That genre doesn't exist");
- }
- }
- public static void searchSongByDate() throws IOException, SQLException {
- System.out.print("\nType album's date:\n> ");
- String data = reader.readLine();
- ArrayList <Integer> musicas = new ArrayList();
- String query12 = "select nome\n"+
- "from album\n" +
- "where data_album = '" + data + "';";
- ResultSet res12 = stmt.executeQuery(query12);
- if (res12.next()) {
- String query121 = "select musica.nome, musica.n_musica\n" +
- "from album, musica, album_musica\n" +
- "where album.data_album = '" + data + "' and n_musica = musica_n_musica and n_album = album_n_album;";
- ResultSet res121 = stmt.executeQuery(query121);
- int i=1;
- System.out.println();
- while (res121.next()) {
- System.out.println(i + ". " + res121.getString(1));
- musicas.add(Integer.parseInt(res121.getString(2)));
- i++;
- }
- printSongInfo(musicas);
- }
- else {
- System.out.println("That date doesn't exist");
- }
- }
- public static void detailsArtist() throws IOException, SQLException {
- System.out.print("\nType artist's name:\n> ");
- String artist3 = reader.readLine();
- String query3 = "select nome, periodo, historia, banda_elementos, solo_nome, concerto.data, concerto.localidade\n"+
- "\tfrom artista, concerto, concerto_artista\n"+
- "\twhere nome='"+artist3+"' and concerto_n_concerto = n_concerto and artista_n_artista = n_artista;";
- ResultSet res = stmt.executeQuery(query3);
- Boolean aux = false;
- while (res.next()) {
- aux = true;
- System.out.println("Name: " + res.getString(1));
- System.out.println("Date: " + res.getString(2));
- System.out.println("History: " + res.getString(3));
- if(res.getString(4).equals(' '))
- System.out.println("Real name: " + res.getString(5));
- else
- System.out.println("Elements: " + res.getString(4));
- System.out.println("Concerts:");
- System.out.println(res.getString(6) + " ; " + res.getString(7));
- }
- if (!aux)
- System.out.println("Artist name doesn't exist");
- }
- public static void makeEditor() throws IOException, SQLException {
- if (isEditor(utilizadorUsername)) {
- listUsernames();
- System.out.print("\nType username:\n> ");
- String user7 = reader.readLine();
- String query71 = "select username\n"+
- "from utilizador ,nao_editor\n" +
- "where utilizador_username = username and username = '" + user7 + "' and utilizador_username = '" + user7 +"';";
- ResultSet res71 = stmt.executeQuery(query71);
- if (!res71.next()) {
- System.out.println("The user is not in the data base or is already an editor");
- }
- else {
- String remove= "DELETE FROM public.nao_editor\n" +
- "\tWHERE utilizador_username='"+user7+"';";
- stmt.executeUpdate(remove);
- String query72 = "INSERT INTO public.editor(\n" +
- "\tutilizador_username)\n" +
- "\tVALUES ('"+user7+"');";
- stmt.executeUpdate(query72);
- System.out.println(user7 + " is now an editor");
- }
- }
- }
- public static void detailsOfAnAlbum(String albumName) throws SQLException {
- //Details of an album
- String queryAlbum= "select album.n_album, album.nome, artista.nome, album.genero, album.data_album "+
- "from album, artista "+
- "where artista.n_artista= album.artista_n_artista AND album.nome='"+albumName+"';";
- ResultSet res = stmt.executeQuery(queryAlbum);
- Boolean aux = true;
- while(res.next()) {
- aux = false;
- int albumNumber= Integer.parseInt(res.getString(1));
- System.out.println("Album: " + res.getString(2));
- System.out.println("> Artist: " + res.getString(3));
- System.out.println("> Genre: " + res.getString(4));
- System.out.println("> Released: " + res.getString(5));
- String queryLabels= "select editora.nome from editora, album, album_editora "+
- "where editora.n_editora= album_editora.editora_n_editora AND album.n_album= album_editora.album_n_album AND album.n_album= "+albumNumber+";";
- ResultSet resultAux= stmtAux.executeQuery(queryLabels);
- System.out.print("> Record Label(s): ");
- while(resultAux.next()){
- System.out.println(resultAux.getString(1)+" ");
- }
- System.out.println();
- String queryReviews= "select critica.pontuacao, critica.critica from critica, album_critica "+
- "where critica.n_critica= album_critica.critica_n_critica "+
- "AND album_critica.album_n_album= "+albumNumber+";";
- resultAux= stmtAux.executeQuery(queryReviews);
- boolean first=true;
- while(resultAux.next()){
- if(first) {
- System.out.println("Album's reviews:");
- first = false;
- }
- System.out.println("Score: "+resultAux.getString(1));
- System.out.println("Review: "+resultAux.getString(2));
- }
- String queryMusicas="select musica.nome, musica.compositor, musica.duracao "+
- "from album, musica, album_musica "+
- "where album.n_album=album_musica.album_n_album AND musica.n_musica= album_musica.musica_n_musica AND album.n_album= '"+albumNumber+"';";
- resultAux= stmtAux.executeQuery(queryMusicas);
- first=true;
- while(resultAux.next()) {
- if(first) {
- System.out.println("List of album's songs");
- first = false;
- }
- System.out.println("> Song: " + resultAux.getString(1) + ", Writer: " + resultAux.getString(2) + ", Duration: " + resultAux.getString(3) + "sec");
- }
- }
- if (aux)
- System.out.println("Album name doesn't exist");
- }
- public static void searchSongByArtist(String artistName) throws SQLException, IOException {
- //Search song by artist name
- String searchArtistNumber= "select artista.n_artista from artista where artista.nome= '"+artistName+"';";
- ResultSet res = stmt.executeQuery(searchArtistNumber);
- ResultSet resAux;
- String querySearchArtist;
- Boolean first=true, exists=false;
- ArrayList<Integer> songsFound= new ArrayList<>();
- int opcaoUtilizador=1; //não esquecer de subtrair 1
- while(res.next()) {
- exists=true;
- if(first){
- first=false;
- }
- int artistNumber= Integer.parseInt(res.getString(1));
- querySearchArtist= "select musica.n_musica, musica.nome " +
- "from musica, artista, artista_musica "+
- "where artista.n_artista= "+artistNumber+" AND (artista_musica.musica_n_musica =musica.n_musica AND artista_musica.artista_n_artista=artista.n_artista)"+
- "union select musica.n_musica, musica.nome "+
- "from musica, album, album_musica "+
- "where musica.n_musica=album_musica.musica_n_musica AND album.n_album=album_musica.album_n_album AND album.artista_n_artista= "+artistNumber+";";
- resAux = stmtAux.executeQuery(querySearchArtist);
- System.out.println();
- while(resAux.next()) {
- System.out.println("> "+opcaoUtilizador+". "+resAux.getString(2));
- opcaoUtilizador++;
- songsFound.add(Integer.parseInt(resAux.getString(1)));
- }
- }
- if(!exists){
- System.out.println("Artist name doesn't exist");
- }
- else{
- printSongInfo(songsFound);
- }
- }
- public static void searchSongByScore() throws SQLException, IOException {
- int pontuacao=8;
- Boolean first=true, exists=false;
- ArrayList<Integer> songsFound= new ArrayList<>();
- int opcaoUtilizador=1;
- String query="select musica.n_musica, musica.nome" +
- " from musica"+
- " where "+pontuacao+"= (select avg((critica.pontuacao)) "+"" +
- "from critica, musica, musica_critica "+
- " where musica_critica.musica_n_musica= musica.n_musica AND musica_critica.critica_n_critica= critica.n_critica);";
- ResultSet res = stmt.executeQuery(query);
- while(res.next()) {
- exists=true;
- if(first){
- System.out.println("Songs found searching score "+pontuacao);
- first=false;
- }
- System.out.println(opcaoUtilizador+". "+res.getString(2));
- songsFound.add(Integer.parseInt(res.getString(1)));
- }
- if(!exists){
- System.out.println("There were no songs found with that score");
- }
- else{
- printSongInfo(songsFound);
- }
- }
- public static int searchSongByName(String songName, String operation) throws SQLException, IOException {
- String querySearchNumber= "select musica.n_musica from musica "+
- "where musica.nome= '"+songName+"';";
- ResultSet res= stmt.executeQuery(querySearchNumber);
- int option=1;
- ArrayList<Integer> songsFound= new ArrayList<>();
- boolean exists=false;
- while (res.next()) {
- exists=true;
- int songNumber= Integer.parseInt(res.getString(1));
- System.out.println(option+". "+songName+"\nBy: ");
- songsFound.add(songNumber);
- String queryArtists= "select artista.nome from artista, musica, artista_musica "+
- "where artista.n_artista= artista_musica.artista_n_artista AND musica.n_musica= artista_musica.musica_n_musica AND musica.n_musica="+songNumber+";";
- ResultSet resAux= stmtAux.executeQuery(queryArtists);
- while(resAux.next()){
- System.out.println(resAux.getString(1));
- }
- }
- if(!exists){
- System.out.println("Song name doesn't exist");
- }
- if(operation.equals("add playlist")){
- System.out.print("Option: ");
- option = Integer.parseInt(reader.readLine());
- System.out.println(option);
- if (option <= songsFound.size() && option>0)
- return songsFound.get(option - 1);
- else return-1;
- }
- else{
- printSongInfo(songsFound);
- return -1;
- }
- }
- public static String [] writeReview() throws IOException {
- String review;
- String scoreAux = null;
- int score;
- while (true) {
- System.out.print("\nType album's score (0 to 10):\n> ");
- scoreAux = reader.readLine();
- try {
- score = Integer.parseInt(scoreAux);
- if (score >= 0 && score <= 10) {
- break;
- } else {
- System.out.println("That input is invalid");
- continue;
- }
- } catch (NumberFormatException e) {
- System.out.println("That input is invalid");
- continue;
- }
- }
- System.out.print("\nWrite review (300 characters):\n> ");
- String reviewAux = reader.readLine();
- if (reviewAux.length() > 300)
- review = reviewAux.substring(0, 300);
- else
- review = reviewAux;
- String [] result = new String[2];
- result[0] = review;
- result[1] = String.valueOf(score);
- return result;
- }
- public static void reviewAlbum() throws IOException, SQLException {
- System.out.print("\nType album's name:\n> ");
- String album = reader.readLine();
- String query= "select n_album, album.nome, artista.nome\n"+
- "from album, artista\n"+
- "where n_artista=artista_n_artista and album.nome = '" + album + "';";
- ResultSet resAux= stmt.executeQuery(query);
- Boolean auxb = true;
- int i=1;
- ArrayList <int []> albuns = new ArrayList ();
- while (resAux.next()) {
- if(auxb)
- System.out.println();
- auxb = false;
- System.out.println(i + ". " + resAux.getString(2) + " by " + resAux.getString(3));
- i++;
- int [] aux = new int [2];
- aux[0] = i;
- aux[1] = resAux.getInt(1);
- albuns.add(aux);
- }
- if(auxb){
- System.out.println("Album name doesn't exist");
- }
- else{
- System.out.print(">");
- Scanner sc = new Scanner(System.in);
- int op = sc.nextInt();
- int id_a = 0;
- for (int j=0; j<albuns.size(); j++){
- if (op+1 == albuns.get(j)[0]){
- id_a = albuns.get(j)[1];
- }
- }
- String [] aux = writeReview();
- String review = aux[0];
- String score = aux[1];
- //defenir a id da critica que tem de ser unica
- String querySelect = "select max(n_critica)\n"+
- "from critica;";
- ResultSet resSelect = stmt.executeQuery(querySelect);
- int id_c = 0;
- if(resSelect.next()) {
- id_c = resSelect.getInt(1) + 1;
- }
- //inserir a critica
- String query1 = "INSERT INTO public.critica(\n" +
- "\tn_critica, pontuacao, critica, utilizador_username)\n" +
- "\tVALUES ('" + id_c + "','" + score + "','" + review + "','" + utilizadorUsername + "');";
- stmt.executeUpdate(query1);
- String query2 = "INSERT INTO public.album_critica(\n" +
- "\talbum_n_album, critica_n_critica)\n" +
- "\tVALUES ('" + id_a +"','" + id_c + "');";
- stmt.executeUpdate(query2);
- System.out.println("Review added");
- }
- }
- public static void reviewSong() throws IOException, SQLException {
- System.out.print("\nType song's name:\n> ");
- String song = reader.readLine();
- String query= "select musica.n_musica, musica.nome, artista.nome\n"+
- "from artista, musica, artista_musica\n"+
- "where n_artista = artista_n_artista and n_musica = musica_n_musica and musica.nome = '" + song + "'"+
- "union select musica.n_musica, musica.nome, artista.nome "+
- "from musica, album, album_musica, artista "+
- "where album.artista_n_artista= artista.n_artista AND musica.n_musica=album_musica.musica_n_musica AND album.n_album=album_musica.album_n_album "+
- "AND musica.nome= '"+song+"';";
- ResultSet resAux= stmt.executeQuery(query);
- Boolean auxb = true;
- int i=1;
- ArrayList <int []> songs = new ArrayList ();
- while (resAux.next()) {
- if(auxb) {
- System.out.println();
- }
- auxb = false;
- System.out.println(i + ". " + resAux.getString(2) + " by " + resAux.getString(3));
- i++;
- int [] aux = new int [2];
- aux[0] = i;
- aux[1] = resAux.getInt(1);
- songs.add(aux);
- }
- if(auxb){
- System.out.println("Song name doesn't exist");
- }
- else{
- System.out.print(">");
- Scanner sc = new Scanner(System.in);
- int op = sc.nextInt();
- int id_a = 0;
- for (int j=0; j<songs.size(); j++){
- System.out.println(songs.get(j)[0] + ";" + songs.get(j)[1]);
- if (op+1 == songs.get(j)[0])
- id_a = songs.get(j)[1];
- }
- String [] aux = writeReview();
- String review = aux[0];
- String score = aux[1];
- //defenir a id da critica que tem de ser unica
- String querySelect = "select max(n_critica)\n"+
- "from critica;";
- ResultSet resSelect = stmt.executeQuery(querySelect);
- int id_c = 0;
- if(resSelect.next()) {
- id_c = resSelect.getInt(1) + 1;
- }
- //inserir a critica
- String query1 = "INSERT INTO public.critica(\n" +
- "\tn_critica, pontuacao, critica, utilizador_username)\n" +
- "\tVALUES ('" + id_c + "','" + score + "','" + review + "','" + utilizadorUsername + "');";
- stmt.executeUpdate(query1);
- String query2 = "INSERT INTO public.musica_critica(\n" +
- "\tmusica_n_musica, critica_n_critica)\n" +
- "\tVALUES ('" + id_a + "','" + id_c + "');";
- stmt.executeUpdate(query2);
- System.out.println("Review added");
- }
- }
- public static void printSongInfo(ArrayList<Integer> songsFound) throws IOException, SQLException {
- while(true) {
- System.out.print("Option: ");
- int option = Integer.parseInt(reader.readLine());
- if (option <= songsFound.size() && option>0) {
- int songNumber = songsFound.get(option - 1);
- System.out.println();
- String querySongInfo = "select musica.nome, musica.compositor, musica.duracao, editora.nome, musica.letra " +
- "from musica, editora " +
- "where editora.n_editora= musica.editora_n_editora AND musica.n_musica =" + songNumber + ";";
- ResultSet res = stmt.executeQuery(querySongInfo);
- if (res.next()) {
- System.out.println("Song: " + res.getString(1) + "\n> Writer: " + res.getString(2) + "\n> Duration: " + res.getString(3) + "sec\n" +
- "> Record Label: " + res.getString(4) + "\n> Lyrics: " + res.getString(5));
- }
- String queryReviews="select critica.pontuacao, critica.critica from critica, musica_critica "+
- "where critica.n_critica= musica_critica.critica_n_critica "+
- "AND musica_critica.musica_n_musica= "+songNumber+";";
- res = stmt.executeQuery(queryReviews);
- Boolean first=true;
- while(res.next()){
- if(first) {
- System.out.println("Song's reviews:");
- first = false;
- }
- System.out.println("> Score: "+res.getString(1));
- System.out.println("> Review: "+res.getString(2));
- }
- break;
- } else {
- System.out.println("Invalid option, please try again");
- }
- }
- }
- public static void createPlaylist() throws SQLException, IOException {
- //Condição, uma playlist tem uma ou mais músicas
- String playlistName, option;
- System.out.print("\nPlease, choose an option:\n1. Public\n2. Private\n>");
- option = reader.readLine();
- System.out.print("\nPlaylist name\n>");
- playlistName= reader.readLine();
- String queryVerify="select playlist.nome from playlist where utilizador_username= '"+utilizadorUsername+"'";
- ResultSet res= stmt.executeQuery(queryVerify);
- if(res.next())
- System.out.println("You already have a playlist with that name");
- else {
- String playlistNumberAux= "select max(playlist.n_playlist) from playlist;";
- res= stmt.executeQuery(playlistNumberAux);
- int playlistNumber;
- if(res.next()) playlistNumber= Integer.parseInt(res.getString(1))+1;
- else playlistNumber=1;
- String query = "INSERT INTO public.playlist(\n" +
- "\tn_playlist, nome, utilizador_username)\n" +
- "\tVALUES (" + playlistNumber + ", '" + playlistName + "','" + utilizadorUsername + "');";
- stmt.executeUpdate(query);
- if (option.equals("2")) {
- String queryPrivate = "INSERT INTO public.playlist_privada_utilizador(\n" +
- "\tplaylist_n_playlist, utilizador_username)\n" +
- "\tVALUES (" + playlistNumber + ", '" + utilizadorUsername + "');";
- stmt.executeUpdate(queryPrivate);
- }
- System.out.println("Please insert a song name to add to the new playlist");
- String songName= reader.readLine();
- int songNumber=searchSongByName(songName,"add playlist");
- String queryInsertMusic = "INSERT INTO public.playlist_musica(\n" +
- "\tplaylist_n_playlist, musica_n_musica)\n" +
- "\tVALUES (" + playlistNumber + ", " + songNumber + ");";
- stmt.executeUpdate(queryInsertMusic);
- }
- }
- public static int getOwnPlaylistNumber(String playlistName) throws SQLException {
- String query= "select playlist.n_playlist from playlist where playlist.utilizador_username='"+utilizadorUsername+"' AND playlist.nome='"+playlistName+"';";
- ResultSet res= stmt.executeQuery(query);
- //vai ser único
- if(res.next()){
- return Integer.parseInt(res.getString(1));
- }
- else return -1;
- }
- public static void showPlaylists() throws SQLException {
- boolean first=true;
- String query="select DISTINCT playlist.n_playlist, playlist.nome, playlist.utilizador_username "+
- "from playlist, playlist_privada_utilizador "+
- "where (playlist.n_playlist NOT IN (select playlist.n_playlist "+
- "from playlist, playlist_privada_utilizador where playlist_privada_utilizador.playlist_n_playlist=playlist.n_playlist))"+
- " OR (playlist_privada_utilizador.playlist_n_playlist=playlist.n_playlist AND playlist_privada_utilizador.utilizador_username= '"+utilizadorUsername+"');";
- ResultSet res= stmt.executeQuery(query);
- while(res.next()){
- if(first) {
- System.out.println("Playlists found in DropMusic:");
- first = false;
- }
- System.out.println("> '"+res.getString(2)+"' created by "+res.getString(3));
- int playlistNumber= Integer.parseInt(res.getString(1));
- String querySongs= "select musica.nome "+
- "from musica, playlist_musica "+
- "where musica.n_musica= playlist_musica.musica_n_musica AND playlist_musica.playlist_n_playlist= "+playlistNumber+";";
- ResultSet resAux= stmtAux.executeQuery(querySongs);
- while(resAux.next()){
- System.out.println(resAux.getString(1));
- }
- }
- if(first) System.out.println("No playlists found in DropMusic");
- }
- public static void addSongToPlaylist() throws SQLException, IOException {
- System.out.print("\nPlaylist name\n>");
- String playlistName= reader.readLine();
- int playlistNumber= getOwnPlaylistNumber(playlistName);
- if(playlistNumber!=-1){
- System.out.print("Please insert a song name to add to the new playlist\n>");
- String songName= reader.readLine();
- int songNumber=searchSongByName(songName, "add playlist");
- if(songNumber!=-1) {
- String queryInsertMusic = "INSERT INTO public.playlist_musica(\n" +
- "\tplaylist_n_playlist, musica_n_musica)\n" +
- "\tVALUES (" + playlistNumber + ", " + songNumber + ");";
- stmt.executeUpdate(queryInsertMusic);
- }
- }
- else System.out.println("Playlist not found or you're not the owner");
- }
- public static void sharePlaylist() throws IOException, SQLException {
- System.out.print("\nPlease enter your playlist name\n>");
- String playlistName= reader.readLine();
- int playlistNumber= getOwnPlaylistNumber(playlistName);
- if(playlistNumber!=-1){
- listUsernames();
- System.out.print("Enter the username\n>");
- String username= reader.readLine();
- String look = "select username from utilizador ;";
- ResultSet res = stmt.executeQuery(look);
- if (!res.next()) {
- System.out.println("Username not found");
- }
- else {
- String queryAddUser= "INSERT INTO public.playlist_privada_utilizador(\n" +
- "\tplaylist_n_playlist, utilizador_username)\n" +
- "\tVALUES ("+playlistNumber+",'"+username+"');";
- stmt.executeUpdate(queryAddUser);
- System.out.println("Playlist was successfully shared with " + username);
- }
- }
- else System.out.println("Playlist not found or you're not the owner");
- }
- public static void insertArtist() throws IOException, SQLException {
- System.out.print("\nType artist's name:\n> ");
- String artist811 = reader.readLine();
- System.out.print("\nType artist's history(description):\n> ");
- String description811 = reader.readLine();
- System.out.print("\nType artist's working period:\n> ");
- String period811 = reader.readLine();
- System.out.print("\nIs it a band(1) or solo artist(2)?\n>");
- Scanner sc = new Scanner(System.in);
- String op = sc.next();
- String element = "";
- String queryAux = "select max(n_artista)\n"+
- "from artista";
- ResultSet r = stmt.executeQuery(queryAux);
- int id = 0;
- if(r.next()) {
- id = r.getInt(1) + 1;
- }
- switch (op) {
- case "1":
- System.out.print("\nWrite the band´s elements names.\nSeparate the elements names by ','\n>");
- element = reader.readLine();
- String query61 = "INSERT INTO public.artista(\n" +
- "\tn_artista, nome, periodo, historia, banda_elementos, solo_nome)\n" +
- "\tVALUES ('" + id + "', '" + artist811 + "', '" + period811 + "', '" + description811 + "', '" + element + "', ' ');";
- stmt.executeUpdate(query61);
- break;
- case "2":
- System.out.print("\nWrite solos artist's real name\n>");
- element = reader.readLine();
- String query6 = "INSERT INTO public.artista(\n" +
- "\tn_artista, nome, periodo, historia, banda_elementos, solo_nome)\n" +
- "\tVALUES ('" + id + "', '" + artist811 + "', '" + period811 + "', '" + description811 + "', ' ', '" + element + "');";
- stmt.executeUpdate(query6);
- break;
- }
- System.out.println("To insert an Artist you have to also insert a song associated with that artist");
- insertSong(id);
- System.out.println("Artist was added to the date base");
- }
- public static void insertSong(int id_art) throws IOException, SQLException {
- System.out.print("\nType artist's name:\n> ");
- String artist = reader.readLine();
- System.out.print("\nNew information:");
- System.out.print("\nType song's name:\n> ");
- String song = reader.readLine();
- System.out.print("Type song's duration:\n> ");
- String duration = reader.readLine();
- System.out.print("Type song's composer:\n> ");
- String composer = reader.readLine();
- System.out.print("Type song's lyrics:\n> ");
- String lyrics = reader.readLine();
- String queryRL = "select n_editora, nome\n"+
- "from editora";
- ResultSet rRL =stmt.executeQuery(queryRL);
- int x=1;
- ArrayList <int []> editoras = new ArrayList<>();
- System.out.print("Add Record Label:\n");
- while (rRL.next()){
- System.out.println("> " + x + ". " + rRL.getString(2));
- x++;
- int [] aux = new int [2];
- aux[0] = x;
- aux[1] = rRL.getInt(1);
- editoras.add(aux);
- }
- Scanner sc = new Scanner(System.in);
- int opRL = sc.nextInt();
- int id_e = 0;
- for(int k=0; k<editoras.size(); k++){
- if(opRL+1==editoras.get(k)[0])
- id_e = editoras.get(k)[1];
- }
- String queryAux = "select max(n_musica)\n"+
- "from musica";
- ResultSet r = stmt.executeQuery(queryAux);
- int id = 0;
- if(r.next()) {
- id = r.getInt(1) + 1;
- }
- String query = "INSERT INTO public.musica(\n" +
- "\tn_musica, nome, letra, duracao, compositor, editora_n_editora)\n" +
- "\tVALUES ('" + id + "','" + song + "','" + lyrics + "','" + duration + "','" + composer + "'," + id_e + ");";
- stmt.executeUpdate(query);
- if (id_art!=0){
- String query1 = "INSERT INTO public.artista_musica(\n" +
- "\tartista_n_artista, musica_n_musica)\n" +
- "\tVALUES ('" + id_art + "','" + id + "');";
- stmt.executeUpdate(query1);
- }
- else{
- String querySelect = "select n_artista, nome, historia\n"+
- "from artista\n"+
- "where nome = '" + artist + "'";
- ResultSet rSelect = stmt.executeQuery(querySelect);
- Boolean a = true;
- int i=1;
- ArrayList <int []> art = new ArrayList<>();
- while(rSelect.next()){
- a = false;
- System.out.println(i + ". " + rSelect.getString(2) + ", History: " + rSelect.getString(3));
- i++;
- int [] aux = new int[2];
- aux[0] = i;
- aux[1] = rSelect.getInt(1);
- art.add(aux);
- }
- if(a){
- System.out.println("Artist name doesn't exist");
- }
- else{
- System.out.print("Add to:\n>");
- int op = sc.nextInt();
- for(int j=0; j<art.size(); j++){
- if(op+1==art.get(j)[0]){
- id_art = art.get(j)[1];
- }
- }
- String query2 = "INSERT INTO public.artista_musica(\n" +
- "\tartista_n_artista, musica_n_musica)\n" +
- "\tVALUES ('" + id_art + "','" + id + "');";
- stmt.executeUpdate(query2);
- }
- }
- String query21 = "INSERT INTO public.ficheiro(\n" +
- "\tuploaded_file, musica_n_musica)\n" +
- "\tVALUES (false, +'" + id + "');";
- stmt.executeUpdate(query21);
- System.out.println("Song was added to the database");
- }
- public static void insertAlbum() throws IOException, SQLException {
- System.out.print("\nType artist's name:\n> ");
- String artist = reader.readLine();
- System.out.print("\nNew information:\n");
- System.out.print("Type album's name:\n> ");
- String album = reader.readLine();
- System.out.print("Type album's genre:\n> ");
- String genre = reader.readLine();
- System.out.print("Type album's date:\n> ");
- String date = reader.readLine();
- String queryAux = "select max(n_album)\n"+
- "from album";
- ResultSet r = stmt.executeQuery(queryAux);
- int id = 0;
- if(r.next()) {
- id = r.getInt(1) + 1;
- }
- System.out.print("Add to:\n>");
- ArrayList <int []> art = auxDetailsArtist(artist);
- int id_art = 0;
- if(!art.isEmpty()){
- Scanner sc = new Scanner(System.in);
- int op = sc.nextInt();
- for(int j=0; j<art.size(); j++){
- if(op+1==art.get(j)[0])
- id_art = art.get(j)[1];
- }
- }
- String query62 = "INSERT INTO public.album(\n" +
- "\tn_album, nome, genero, data_album, artista_n_artista)\n" +
- "\tVALUES (" + id +",'" + album + "','" + genre + "','" + date + "','" + id_art + "');";
- stmt.executeUpdate(query62);
- String queryRL = "select n_editora, nome\n"+
- "from editora";
- ResultSet rRL =stmt.executeQuery(queryRL);
- int i=1;
- ArrayList <int []> editoras = new ArrayList<>();
- System.out.print("Add Record Label:\n");
- while (rRL.next()){
- System.out.println("> " + i + ". " + rRL.getString(2));
- i++;
- int [] aux = new int [2];
- aux[0] = i;
- aux[1] = rRL.getInt(1);
- editoras.add(aux);
- }
- Scanner sc = new Scanner(System.in);
- int opRL = sc.nextInt();
- int id_e = 0;
- for(int k=0; k<editoras.size(); k++){
- if(opRL+1==editoras.get(k)[0])
- id_e = editoras.get(k)[1];
- }
- String queryR = "INSERT INTO public.album_editora(\n" +
- "\talbum_n_album, editora_n_editora)\n" +
- "\tVALUES (" + id + ","+ id_e + ");";
- stmt.executeUpdate(queryR);
- System.out.println("To insert an Album you have to also insert a song associated with that album");
- insertSong(id_art);
- System.out.println("Album was added to the database");
- }
- public static void changeArtist() throws IOException, SQLException {
- System.out.print("\nType artist's name:\n> ");
- String artist = reader.readLine();
- ArrayList <int []> art = auxDetailsArtist(artist);
- if(!art.isEmpty()) {
- System.out.print("Option: ");
- Scanner sc = new Scanner(System.in);
- int op = sc.nextInt();
- System.out.print("\nType artist's new name:\n> ");
- String newartist = reader.readLine();
- System.out.print("\nType artist's new history (description):\n> ");
- String newd = reader.readLine();
- System.out.print("\nType artist's new working period:\n> ");
- String newp = reader.readLine();
- int id_art = 0;
- for(int j=0; j<art.size(); j++){
- if(op+1==art.get(j)[0]){
- id_art = art.get(j)[1];
- }
- }
- System.out.print("\nIs it a band(1) or solo artist(2)?\n>");
- String op1 = sc.next();
- String element = "";
- switch (op1) {
- case "1":
- System.out.print("\nWrite the band´s elements names.\nSeparate the elements names by ','\n>");
- element = reader.readLine();
- String query61 = "UPDATE public.artista\n" +
- "\tSET n_artista='" + id_art + "', nome='" + newartist + "',periodo='" + newp + "',historia='" + newd + "',banda_elementos='" + element + "', solo_nome=' '\n"+
- "\tWHERE n_artista='" + id_art + "';";
- stmt.executeUpdate(query61);
- break;
- case "2":
- System.out.print("\nWrite solos artist's real name\n>");
- element = reader.readLine();
- String query6 = "UPDATE public.artista\n" +
- "\tSET n_artista='" + id_art + "', nome='" + newartist + "',periodo='" + newp + "',historia='" + newd + "',banda_elementos=' ', solo_nome='" + element +"'\n"+
- "\tWHERE n_artista='" + id_art + "';";
- stmt.executeUpdate(query6);
- break;
- }
- }
- }
- public static void changeAlbum() throws IOException, SQLException {
- System.out.print("\nType album's name:\n> ");
- String album = reader.readLine();
- String querySelect = "select n_artista, artista.nome, n_album, album.nome\n"+
- "from artista, album\n"+
- "where album.nome = '" + album + "' and n_artista = artista_n_artista";
- ResultSet rSelect = stmt.executeQuery(querySelect);
- Boolean ab = false;
- int i=1;
- ArrayList <int []> a = new ArrayList<>();
- while(rSelect.next()){
- ab = true;
- System.out.println("> " + i + ". " + rSelect.getString(4) + ", By " + rSelect.getString(2));
- i++;
- int [] aux = new int[3];
- aux[0] = i;
- aux[1] = rSelect.getInt(1);
- aux[2] = rSelect.getInt(3);
- a.add(aux);
- }
- if(!ab) {
- System.out.println("Album name doesn't exist");
- }
- if(!a.isEmpty()) {
- System.out.print("Option: ");
- Scanner sc = new Scanner(System.in);
- int op = sc.nextInt();
- System.out.print("\nType album's new name:\n> ");
- String newalbum = reader.readLine();
- System.out.print("\nType album's new description:\n> ");
- String newdesc = reader.readLine();
- System.out.print("\nType album's new genre:\n> ");
- String newgenre = reader.readLine();
- System.out.print("\nType album's new date:\n> ");
- String newdate = reader.readLine();
- int id_a = 0, id = 0;
- for(int j=0; j<a.size(); j++){
- if(op+1==a.get(j)[0]){
- id_a = a.get(j)[1];
- id = a.get(j)[2];
- }
- }
- String query61 = "UPDATE public.album\n" +
- "\tSET n_album='" + id + "', nome='" + newalbum + "',genero='" + newgenre + "',data_album='" + newdate + "',artista_n_artista='" + id_a + "'\n"+
- "\tWHERE n_album='" + id + "';";
- stmt.executeUpdate(query61);
- String queryRL = "select n_editora, nome\n"+
- "from editora";
- ResultSet rRL =stmt.executeQuery(queryRL);
- int x=1;
- ArrayList <int []> editoras = new ArrayList<>();
- System.out.print("Add Record Label:\n");
- while (rRL.next()){
- System.out.println("> " + x + ". " + rRL.getString(2));
- x++;
- int [] aux = new int [2];
- aux[0] = x;
- aux[1] = rRL.getInt(1);
- editoras.add(aux);
- }
- int opRL = sc.nextInt();
- int id_e = 0;
- for(int k=0; k<editoras.size(); k++){
- if(opRL+1==editoras.get(k)[0])
- id_e = editoras.get(k)[1];
- }
- String queryEditora = "UPDATE public.album_editora\n" +
- "\tSET album_n_album=" +id + ", editora_n_editora=" + id_e + "\n" +
- "\tWHERE editora_n_editora = " +id_e + ";";
- stmt.executeUpdate(queryEditora);
- }
- }
- public static void changeSong() throws IOException, SQLException {
- System.out.print("\nType song's name:\n> ");
- String song = reader.readLine();
- String querySelect= "select musica.n_musica, musica.nome, artista.nome\n"+
- "from artista, musica, artista_musica\n"+
- "where n_artista = artista_n_artista and n_musica = musica_n_musica and musica.nome = '" + song + "'"+
- "union select musica.n_musica, musica.nome, artista.nome "+
- "from musica, album, album_musica, artista "+
- "where album.artista_n_artista= artista.n_artista AND musica.n_musica=album_musica.musica_n_musica AND album.n_album=album_musica.album_n_album "+
- "AND musica.nome= '"+song+"';";
- ResultSet rSelect = stmt.executeQuery(querySelect);
- Boolean ab = false;
- int i=1;
- ArrayList <int []> a = new ArrayList<>();
- while(rSelect.next()){
- ab = true;
- System.out.println("> " + i + ". " + rSelect.getString(2) + ", By " + rSelect.getString(3));
- i++;
- int [] aux = new int[2];
- aux[0] = i;
- aux[1] = rSelect.getInt(1);
- a.add(aux);
- }
- if(!ab) {
- System.out.println("Song name doesn't exist");
- }
- if(!a.isEmpty()) {
- System.out.print("Option: ");
- Scanner sc = new Scanner(System.in);
- int op = sc.nextInt();
- System.out.print("\nType song's new name:\n> ");
- String newsong = reader.readLine();
- System.out.print("\nType song's new duration:\n> ");
- String newdur = reader.readLine();
- System.out.print("\nType song's new composer:\n> ");
- String newcomp = reader.readLine();
- System.out.print("\nType song's new lyrics:\n> ");
- String newlyrics = reader.readLine();
- String queryRL = "select n_editora, nome\n"+
- "from editora";
- ResultSet rRL =stmt.executeQuery(queryRL);
- int x=1;
- ArrayList <int []> editoras = new ArrayList<>();
- System.out.print("Add Record Label:\n");
- while (rRL.next()){
- System.out.println("> " + x + ". " + rRL.getString(2));
- x++;
- int [] aux = new int [2];
- aux[0] = x;
- aux[1] = rRL.getInt(1);
- editoras.add(aux);
- }
- int opRL = sc.nextInt();
- int id_e = 0;
- for(int k=0; k<editoras.size(); k++){
- if(opRL+1==editoras.get(k)[0])
- id_e = editoras.get(k)[1];
- }
- int id_s = 0;
- for(int j=0; j<a.size(); j++){
- if(op+1==a.get(j)[0]){
- id_s = a.get(j)[1];
- }
- }
- String query61 = "UPDATE public.musica\n" +
- "\tSET n_musica=" + id_s + ", nome='" + newsong + "',letra='" + newlyrics + "',duracao='" + newdur + "',compositor='" + newcomp + "' , editora_n_editora =" + id_e + "\n"+
- "\tWHERE n_musica='" + id_s + "';";
- stmt.executeUpdate(query61);
- }
- }
- public static ArrayList<int[]> auxDetailsArtist(String artist) throws SQLException {
- String querySelect = "select n_artista, nome, historia\n"+
- "from artista\n"+
- "where nome = '" + artist + "'";
- ResultSet rSelect = stmt.executeQuery(querySelect);
- Boolean a = false;
- int i=1;
- ArrayList <int []> art = new ArrayList<>();
- while(rSelect.next()){
- a = true;
- System.out.println("> " + i + ". " + rSelect.getString(2) + ", History: " + rSelect.getString(3));
- i++;
- int [] aux = new int[2];
- aux[0] = i;
- aux[1] = rSelect.getInt(1);
- art.add(aux);
- }
- if(!a) {
- System.out.println("Artist name doesn't exist");
- return art;
- }
- return art;
- }
- public static void uploadSongFile() throws IOException, SQLException {
- System.out.println("Please insert a song name to upload");
- String songName= reader.readLine();
- int songNumber=searchSongByName(songName, "add playlist");
- if(songNumber!=-1) {
- String uploadCheck= "select uploaded_file from ficheiro where musica_n_musica="+songNumber+";";
- ResultSet res=stmt.executeQuery(uploadCheck);
- if(res.next()) System.out.println("Sorry, someone already uploaded a file for that song");
- else {
- String query = "INSERT INTO public.ficheiro(\n" +
- "\tuploaded_file, musica_n_musica)\n" +
- "\tVALUES (" + true + ", " + songNumber + ");";
- stmt.executeUpdate(query);
- String query1 = "INSERT INTO public.ficheiro_utilizador(\n" +
- "\tficheiro_musica_n_musica, utilizador_username)\n" +
- "\tVALUES (" + songNumber + ", '" + utilizadorUsername + "');";
- stmt.executeUpdate(query1);
- System.out.println("Song uploaded");
- }
- }
- }
- public static void shareSongFile() throws SQLException, IOException {
- String query= "select musica.n_musica, musica.nome from musica, ficheiro, ficheiro_utilizador "+
- "where ficheiro.uploaded_file=true AND musica.n_musica= ficheiro.musica_n_musica "+
- "AND ficheiro_utilizador.ficheiro_musica_n_musica=ficheiro.musica_n_musica "+
- "AND ficheiro_utilizador.utilizador_username='"+utilizadorUsername+"';";
- ResultSet res= stmt.executeQuery(query);
- int option=1;
- ArrayList<Integer> songsFound= new ArrayList<>();
- boolean first=true;
- while(res.next()){
- if(first){
- System.out.println("List of songs you have permission to share, please choose a number");
- first=false;
- }
- System.out.println(option+". "+res.getString(2));
- option++;
- songsFound.add(Integer.parseInt(res.getString(1)));
- }
- if(first) System.out.println("You don't have permission to share a song file with that name");
- else {
- String aux = reader.readLine();
- if(Integer.parseInt(aux)<=songsFound.size() && Integer.parseInt(aux)>0) {
- int songNumber = songsFound.get(Integer.parseInt(aux) - 1);
- listUsernames();
- System.out.println("Please enter a username");
- aux = reader.readLine();
- String usernameCheck = "select username from utilizador where username='" + aux + "';";
- res = stmt.executeQuery(usernameCheck);
- if (res.next()) {
- String auxx = "select ficheiro_utilizador.utilizador_username " +
- "from musica, ficheiro, ficheiro_utilizador " +
- "where ficheiro.uploaded_file=true AND musica.n_musica= ficheiro.musica_n_musica " +
- "AND ficheiro_utilizador.ficheiro_musica_n_musica=ficheiro.musica_n_musica " +
- "AND ficheiro_utilizador.utilizador_username='" + aux + "';";
- ResultSet resAux = stmt.executeQuery(auxx);
- if (resAux.next()) System.out.println("Song already shared with that user");
- else {
- String query1 = "INSERT INTO public.ficheiro_utilizador(\n" +
- "\tficheiro_musica_n_musica, utilizador_username)\n" +
- "\tVALUES (" + songNumber + ", '" + aux + "');";
- stmt.executeUpdate(query1);
- }
- } else System.out.println("Invalid username");
- } else System.out.println("Invalid option");
- }
- }
- public static void printMySongsFiles() throws SQLException {
- String querySongs= "select musica.nome " +
- "from musica, ficheiro, ficheiro_utilizador " +
- "where ficheiro.uploaded_file=true AND musica.n_musica= ficheiro.musica_n_musica " +
- "AND ficheiro_utilizador.ficheiro_musica_n_musica=ficheiro.musica_n_musica " +
- "AND ficheiro_utilizador.utilizador_username='" + utilizadorUsername + "';";
- ResultSet res = stmt.executeQuery(querySongs);
- boolean first=true;
- while(res.next()){
- if(first){
- System.out.println("Your songs and songs shared with you");
- first=false;
- }
- System.out.println("> "+res.getString(1));
- }
- }
- public static void listUsernames() throws SQLException {
- String query= "select utilizador.username from utilizador;";
- ResultSet res= stmt.executeQuery(query);
- boolean first=true;
- while(res.next()){
- if(first){
- System.out.println("Usernames found in DropMusic");
- first=false;
- }
- System.out.println("> "+res.getString(1));
- }
- }
- public static void main(String[] args) {
- connectDataBase();
- //createTables();
- //insertData();
- try {
- if (connection.createStatement() == null) {
- connection = DriverManager.getConnection(
- "jdbc:postgresql://127.0.0.1:5432/postgres",
- "postgres",
- "potato");
- }
- if ((stmt = connection.createStatement()) == null) {
- System.out.println("Erro nao foi possível criar uma statement ou retornou null");
- System.exit(-1);
- }
- if ((stmtAux = connection.createStatement()) == null) {
- System.out.println("Erro nao foi possível criar uma statement ou retornou null");
- System.exit(-1);
- }
- //MENU
- try {
- login_register();
- while (true) {
- System.out.println("\n1- Search song");
- System.out.println("2- Details of an album");
- System.out.println("3- Details of an artist");
- System.out.println("4- Write a review");
- System.out.println("5- Manage song files");
- System.out.println("6- Playlists");
- if (isEditor(utilizadorUsername)) {
- System.out.println("7- Manage information");
- System.out.println("8- Make an user a new editor");
- }
- System.out.println("0- Logout");
- System.out.print("> ");
- Scanner sc = new Scanner(System.in);
- String num = sc.next();
- switch (num) {
- case "1":
- System.out.println("\n1- Trough artist");
- System.out.println("2- Trough album");
- System.out.println("3- By song name");
- System.out.println("4- By genre");
- System.out.println("5- By date");
- System.out.print("6- By score\n>");
- String aux1 = sc.next();
- switch (aux1) {
- case "1":
- System.out.print("\nType artist's name:\n> ");
- String artist13 = reader.readLine();
- searchSongByArtist(artist13);
- continue;
- case "2":
- searchSongByAlbum();
- continue;
- case "3":
- System.out.print("\nType song's name:\n> ");
- String song13 = reader.readLine();
- searchSongByName(song13, "");
- continue;
- case "4":
- searchSongByGenre();
- continue;
- case "5":
- searchSongByDate();
- continue;
- case "6":
- searchSongByScore();
- continue;
- }
- case "2":
- System.out.print("\nType album's name:\n> ");
- String album2 = reader.readLine();
- detailsOfAnAlbum(album2);
- continue;
- case "3":
- detailsArtist();
- continue;
- case "4":
- System.out.println("\n1- Album");
- System.out.print("2- Song\n>");
- String aux4 = sc.next();
- switch (aux4){
- case "1":
- reviewAlbum();
- continue;
- case "2":
- reviewSong();
- continue;
- default:
- continue;
- }
- case "5":
- System.out.println("\n1- Upload song");
- System.out.println("\n2- Share song");
- System.out.print("3- Show my songs\n>");
- String aux5 = sc.next();
- switch (aux5){
- case "1":
- uploadSongFile();
- continue;
- case "2":
- shareSongFile();
- continue;
- case "3":
- printMySongsFiles();
- continue;
- default:
- continue;
- }
- case "6":
- System.out.println("\n1- Create playlist");
- System.out.println("2- Add user to playlist");
- System.out.println("3- Add song to playlist");
- System.out.print("4- Print all available playlists\n>");
- String aux6 = sc.next();
- switch (aux6) {
- case "1":
- createPlaylist();
- continue;
- case "2":
- sharePlaylist();
- continue;
- case "3":
- addSongToPlaylist();
- continue;
- case "4":
- showPlaylists();
- continue;
- default:
- continue;
- }
- case "7":
- if (true) {
- System.out.println("\n1- Insert");
- System.out.print("2- Change\n> ");
- String aux8 = sc.next();
- switch (aux8) {
- case "1":
- System.out.println("\n1- Artist");
- System.out.println("2- Album");
- System.out.print("3- Song\n> ");
- String aux81 = sc.next();
- switch (aux81) {
- case "1":
- insertArtist();
- continue;
- case "2":
- insertAlbum();
- continue;
- case "3":
- insertSong(0);
- continue;
- default:
- continue;
- }
- case "2":
- System.out.println("\n1- Artist");
- System.out.println("2- Album");
- System.out.print("3- Song\n> ");
- String aux82 = sc.next();
- switch (aux82) {
- case "1":
- changeArtist();
- continue;
- case "2":
- changeAlbum();
- continue;
- case "3":
- changeSong();
- continue;
- default:
- continue;
- }
- default:
- continue;
- }
- } else {
- continue;
- }
- case "8":
- makeEditor();
- continue;
- case "0":
- System.out.println("Goodbye ❤");
- System.exit(0);
- default:
- continue;
- }
- }
- }
- catch (IOException e) {
- System.out.println("Input null");
- main(args);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement