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.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- *
- * @author Raul
- *
- */
- public class Methods {
- Menus m;
- Musicians mu = new Musicians();
- AccesDB a = new AccesDB();
- private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- public void loginMenu() throws ClassNotFoundException, SQLException, IOException{
- a.openConnection();
- m = new Menus();
- String[] i = { "Login","Login as a guest", "Exit"};
- m.setN(i);
- int op = 0;
- while(true){
- op = m.menu();
- switch(op){
- case 1: menuLevel();continue;
- case 2: continue;
- case 3: break;
- default: continue;
- }
- break;
- }
- }
- private int login() throws IOException, SQLException, ClassNotFoundException {
- a.openConnection();
- String login,pass;
- int level = 0;
- while(true){
- login = getInputText("login");
- pass = getInputText("password");
- a.setUsers(login);
- a.rs = a.st.executeQuery("SELECT * from users where user ='" + login +
- "' and password ='" + pass +"';");
- if(a.rs.first()){
- a.rs = a.st.executeQuery("SELECT * from users where user ='" + login +
- "' and password ='" + pass +"';");
- while(a.rs.next()){
- level = a.rs.getInt(3); a.rs.close();
- break;
- }
- break;
- }else{
- println("Incorrect User or Password, try again"); continue;
- }
- }
- return level;
- }
- public void menuLevel() throws IOException, SQLException, ClassNotFoundException{
- int op = 0;
- while(true){
- op = login();
- switch(op){
- case 1: System.out.println("hola"); break;
- case 2: System.out.println("hola"); break;
- case 3: representantMenu(); break;
- case 4: ; break;
- case 5: AdminMenu(); break;
- }
- break;
- }
- }
- public void representantMenu() throws ClassNotFoundException, SQLException, IOException{
- Menus m = new Menus();
- String[] i = {"See all groups","See all musics", "Modify my group","Logout"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: listBands(1);continue;
- case 2: listMusics(1,0);continue;
- case 3: modifyGroupMenu();continue;
- case 4: break;
- default: continue;
- }
- break;
- }
- }
- public void AdminMenu() throws ClassNotFoundException, SQLException, IOException{
- Menus m = new Menus();
- String[] i = {"Admin concerts","Admin bands","Admin musicians","Admin workers","Admin users","Logout"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: concertsMenu();continue;
- case 2: AdminGroupMenu();continue;
- case 3: AdminMusicMenu();continue;
- case 4: AdminWorkersMenu();continue;
- case 5: continue;
- case 6: break;
- default: continue;
- }
- break;
- }
- }
- public void AdminWorkersMenu() throws ClassNotFoundException, SQLException, IOException{
- Menus m = new Menus();
- String[] i = {"See all Workers","See a worker","Modify Workers","Insert Workers","Delete Workers","Back"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: listWorkers(1);continue;
- case 2: listWorkers(2);continue;
- case 3: AdminModifyW();continue;
- case 4: AdminInsertsW();continue;
- case 5: AdminDeletesW();continue;
- case 6: break;
- default: continue;
- }
- break;
- }
- }
- public void AdminUsersMenu() throws ClassNotFoundException, SQLException, IOException{
- Menus m = new Menus();
- String[] i = {"See all users","Modify user","Insert user","Grant level acces","Make representative","Back"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: listUsers();continue;
- case 2: AdminModifyU();continue;
- case 3: AdminModifyW();continue;
- case 4: AdminInsertsW();continue;
- case 5: AdminDeletesW();continue;
- case 6: break;
- default: continue;
- }
- break;
- }
- }
- public void listUsers() throws SQLException, ClassNotFoundException{
- if(comproveExist("users")){
- a.openConnection();
- a.rs = a.st.executeQuery("SELECT * FROM users");
- while(a.rs.next()){
- int level = a.rs.getInt("level");
- String name = a.rs.getString("user");
- String pass = a.rs.getString("password");
- println("\n");
- println("\n -----------------" + "ID_USER: "+ name + " \n Password: " + pass + "\n Level: " + level + "\n -----------------");
- println("\n");
- }
- a.rs.close();
- a.closeConnection();
- }else{
- println("No users availables");
- }
- }
- public void listWorkers(int v) throws SQLException, ClassNotFoundException{
- if(comproveExist("Workers")){
- a.openConnection();
- if(v == 1){
- a.rs = a.st.executeQuery("SELECT id_worker,name,surname,phone,TRUNCATE(DATEDIFF(CURDATE(), birth)/365.25, 0)," +
- "function,salary FROM Workers");
- }else if (v == 2){
- while(true){
- int f = getInputInt("the id fo the worker you want to see");
- if(comproveExistWhere("workers","id_worker",f)){
- a.rs =a.st.executeQuery("SELECT id_worker,name,surname,phone,TRUNCATE(DATEDIFF(CURDATE(), birth)/365.25, 0)," +
- "function,salary FROM Workers where id_worker = " + f); break;
- }else{
- println("This worker doesn't exist, try again"); continue;
- }
- }
- }
- while(a.rs.next()){
- int id_music = a.rs.getInt("id_worker");
- String name = a.rs.getString("NAME");
- String surname = a.rs.getString("SURNAME");
- int phone = a.rs.getInt("PHONE");
- int old = a.rs.getInt("TRUNCATE(DATEDIFF(CURDATE(), birth)/365.25, 0)");
- int salary =a.rs.getInt("salary");
- String funct = a.rs.getString("function");
- println("\n");
- println("ID_WORKER: "+ id_music + "\n -----------------" + "\n NAME: " + name + " \n SURNAME: " + surname + "\n PHONE: " +
- phone + "\n YEARS OLD: " + old + "\n SALARY: " + salary + "\n FUNCTION " + funct + "\n -----------------");
- println("\n");
- }
- a.rs.close();
- a.closeConnection();
- }else{
- println("No workers availables to see");
- }
- }
- public void AdminMusicMenu() throws ClassNotFoundException, SQLException, IOException{
- Menus m = new Menus();
- String[] i = {"See all Musicians","See a Musician","Insert Musician","Delete Musician","Back"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: listMusics(1,0);continue;
- case 2: listMusics(3,0);continue;
- case 3: AdminInsertsM();continue;
- case 4: AdminDeletesM();continue;
- case 5: break;
- default: continue;
- }
- break;
- }
- }
- public void AdminDeletesM() throws ClassNotFoundException, SQLException{
- listMusics(1,0);
- int b = 0;
- int id = getInputInt("ID of musician that you want to delete");
- a.openConnection();
- a.rs = a.st.executeQuery("Select * from bandmusic where id_music =" + id);
- if(a.rs.first()){
- while(a.rs.next()){
- b = a.rs.getInt("id_band");
- }
- a.openConnection();
- a.st.executeUpdate("DELETE from bandmusic where id_music =" + id);
- a.st.executeUpdate("DELETE from musicians where id_music =" + id);
- a.st.executeUpdate("Update bands set num_comp = (num_comp - 1) where id_band = " + b );
- }else{
- a.st.executeUpdate("DELETE from musicians where id_music =" + id);
- }
- }
- public void AdminDeletesW() throws ClassNotFoundException, SQLException{
- listWorkers(1);
- while(true){
- int id = getInputInt("ID of Worker that you want to delete");
- if(comproveExistWhere("workers","id_worker",id)){
- a.openConnection();
- a.st.executeUpdate("DELETE from workers where id_worker =" + id);
- break;
- }else{
- println("This worker don't exist, try again"); continue;
- }
- }
- }
- public void AdminInsertsW() throws ClassNotFoundException, SQLException, IOException{
- a.openConnection();
- String name;
- String surname;
- int phone;
- String age;
- String function;
- int salary;
- name = getInputText("Name");
- surname = getInputText("Surname");
- phone = getInputInt("Phone");
- age = comproveDate("Born date(YYYY-MM-DD)");
- function = getInputText("FUNCTION");
- salary = getInputInt("Salary");
- a.st.executeUpdate("Insert into workers (name,surname,phone,birth,salary,FUNCTION,user) values ('" + name + "','" + surname + "','"
- + phone + "','" + age + "'," + salary + " , '" + function + "' , NULL)");
- }
- public void AdminInsertsM() throws ClassNotFoundException, SQLException, IOException{
- a.openConnection();
- String name;
- String surname;
- int phone;
- String age;
- String instrument;
- name = getInputText("Name");
- surname = getInputText("Surname");
- phone = getInputInt("Phone");
- age = comproveDate("Born date(YYYY-MM-DD)");
- instrument = getInputText("Instrument");
- a.st.executeUpdate("Insert into musicians (name,surname,phone,birth,instrument,user) values ('" + name + "','" + surname + "','"
- + phone + "','" + age + "','" + instrument + "',NULL)");
- while(true){
- println("Want to add this musician into a band?");
- String s = getInputText("Yes or No").toLowerCase();
- if(s.equals("yes")){
- while(true){
- a.openConnection();
- int b = getInputInt("the id of the band where you want to add the musician");
- if(comproveExistWhere("bands","id_band",b)){
- a.openConnection();
- a.rs = a.st.executeQuery("select id_music from musicians where name = '" + name + "' and surname = '" + surname + "' and phone = '" + phone + "'");
- while(a.rs.next()){
- int id_music = a.rs.getInt(1);
- a.st.executeUpdate("Insert into bandmusic values (" + b + "," + id_music + ")");
- a.st.executeUpdate("Update bands set num_comp = (num_comp + 1) where id_band = " + b);
- break;
- }
- break;
- }else{
- println("This id of band don't exist. Try again"); continue;
- }
- }
- break;
- }else if(s.equals("no")){
- break;
- }else{
- println("Enter only YES or NO"); continue;
- }
- }
- }
- public void AdminGroupMenu() throws ClassNotFoundException, SQLException, IOException {
- Menus m = new Menus();
- String[] i = {"See all groups","See a group", "Modify info of a group","Insert a new Member"
- ,"Delete a member","Back"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: listBands(1);continue;
- case 2: listBands(3);continue;
- case 3: modifyInfoGroup('a');continue;
- case 4: MenuInsertMember();continue;
- case 5: MenuDeleteMember();continue;
- case 6: break;
- default: continue;
- }
- break;
- }
- }
- public void concertsMenu() throws ClassNotFoundException, SQLException, IOException{
- Menus m = new Menus();
- String[] i = {"Add concert","Modify Concert","Delete Concert","See all concerts","Back"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: Inserts('c');continue;
- case 2: MenuModifyConcerts();continue;
- case 3: Deletes('c');continue;
- case 4: listConcerts();continue;
- case 5: break;
- default: continue;
- }
- break;
- }
- }
- public void MenuModifyConcerts() throws ClassNotFoundException, SQLException, IOException{
- if(comproveExist("concerts")){
- Menus m = new Menus();
- String[] i = {"Modify Band","Modify Hall","Modify Price","Modify Date and Hour","Modify Attendance","Back"};
- m.setN(i);
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: ModifysConcert('b');continue;
- case 2: ModifysConcert('h');continue;
- case 3: ModifysConcert('p');continue;
- case 4: ModifysConcert('d');continue;
- case 5: ModifysConcert('a');continue;
- case 6: break;
- default: continue;
- }
- break;
- }
- }else{
- println("Don't are any concert to modify, turning back");
- }
- }
- public void AdminModifyW() throws ClassNotFoundException, SQLException, IOException{
- if(comproveExist("workers")){
- Menus m = new Menus();
- String[] i = {"Modify Name","Modify surname","Modify phone ","Modify birth","Modify function","Modify Salary","Back"};
- m.setN(i);
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: ModifysWorkers('n');continue;
- case 2: ModifysWorkers('s');continue;
- case 3: ModifysWorkers('p');continue;
- case 4: ModifysWorkers('b');continue;
- case 5: ModifysWorkers('f');continue;
- case 6: ModifysWorkers('$');continue;
- case 7: break;
- default: continue;
- }
- break;
- }
- }else{
- println("Don't are any concert to modify, turning back");
- }
- }
- public void ModifysWorkers(char m) throws ClassNotFoundException, SQLException, IOException {
- a.openConnection();
- listWorkers(1);
- while(true){
- int s = getInputInt("the id of the member you want to modify");
- if(comproveExistWhere("workers","id_worker",s)){
- if(m == 'n'){
- String name = getInputText("the new name");
- a.st.executeUpdate("update workers set name = '" + name + "' where id_worker = " + s);
- }else if(m == 's'){
- String surname = getInputText("the new surname");
- a.st.executeUpdate("update workers set surname = '" + surname + "' where id_worker = " + s);
- }else if(m == 'p'){
- int phone = getInputInt("the new phone");
- a.st.executeUpdate("update workers set phone =" + phone + " where id_worker = " + s);
- }else if(m == 'b'){
- String birth = comproveDate("New birth date(YYYY-MM-DD)");
- a.st.executeUpdate("update workers set birth ='" + birth +"' where id_worker = " + s);
- }else if(m == 'f'){
- String funct = getInputText("the new function");
- a.st.executeUpdate("update workers set function = '" + funct + "' where id_worker = " + s);
- }else if(m == '$'){
- int salary = getInputInt("the new salary");
- a.st.executeUpdate("update workers set salary = " + salary + " where id_worker = " + s);
- }
- }else{
- println("This worker don't exist, try again"); continue;
- }
- break;
- }
- }
- public void AdminInsertB(char o) throws IOException, SQLException, ClassNotFoundException{
- if(o == 'n'){
- a.openConnection();
- int b = getInputInt("the id of the group where you want to add the member");
- String name = getInputText("Name");
- String surname = getInputText("Surname");
- int phone = getInputInt("Phone");
- String age = comproveDate("Born date(YYYY-MM-DD)");
- String instrument = getInputText("Instrument");
- a.st.executeUpdate("Insert into musicians (name,surname,phone,birth,instrument,user) values ('" + name + "','" + surname + "','"
- + phone + "','" + age + "','" + instrument + "',NULL)");
- a.rs = a.st.executeQuery("select id_music from musicians where name = '" + name + "' and surname = '" + surname + "' and phone = '" + phone + "'");
- while(a.rs.next()){
- int id_music = a.rs.getInt(1);
- a.st.executeUpdate("Insert into bandmusic values (" + b + "," + id_music + ")");
- a.st.executeUpdate("Update bands set num_comp = (num_comp + 1) where id_band = " + b);
- break;
- }
- }else if (o =='e'){
- a.openConnection();
- listMusics(1,0);
- int b = getInputInt("the id of the group where you want to add the member");
- int m = getInputInt("The id of the music that you want to add to the band");
- a.openConnection();
- a.rs = a.st.executeQuery("select * from bandmusic where id_band =" + b + " and id_music = " + m);
- if(a.rs.first()){
- println("This musician is already part of this group.");
- }else{
- a.st.executeUpdate("Insert into bandmusic values (" + b + "," + m + ")");
- a.st.executeUpdate("Update bands set num_comp = (num_comp + 1) where id_band = " + b);
- }
- }
- }
- public void AdminDelete1(char o) throws ClassNotFoundException, SQLException{
- a.openConnection();
- if(o == 'd'){
- int b = 0;
- listMusics(1,0);
- a.openConnection();
- int id = getInputInt("ID from member you want to delete");
- a.rs = a.st.executeQuery("select id_band from bandmusic where id_music = " + id);
- while(a.rs.next()){
- b = a.rs.getInt(1);
- }
- a.openConnection();
- a.st.executeUpdate("DELETE from bandmusic where id_music =" + id);
- a.st.executeUpdate("DELETE from musicians where id_music =" + id);
- a.st.executeUpdate("Update bands set num_comp = (num_comp - 1) where id_band = " + b);
- }else if(o == 'n'){
- a.openConnection();
- int b = 0;
- int id = getInputInt("ID from member you want to delete");
- a.rs = a.st.executeQuery("select id_band from bandmusic where id_music = " + id);
- while(a.rs.next()){
- b = a.rs.getInt(1);
- }
- a.st.executeUpdate("Update bands set num_comp = (num_comp - 1) where id_band = " + b);
- a.st.executeUpdate("DELETE from bandmusic where id_music =" + id);
- }
- }
- public void MenuInsertMember() throws ClassNotFoundException, IOException, SQLException{
- Menus m = new Menus();
- String[] i = {"Insert New Member","Add Existing Music to a group","Back"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: AdminInsertB('n');continue;
- case 2: AdminInsertB('e');continue;
- case 3: break;
- default: continue;
- }
- break;
- }
- }
- public void MenuDeleteMember() throws ClassNotFoundException, IOException, SQLException{
- Menus m = new Menus();
- String[] i = {"Delete a member of a group and all his information","Delete a member from a group but not his information","Back"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: AdminDelete1('d');continue;
- case 2: AdminDelete1('n');continue;
- case 3: break;
- default: continue;
- }
- break;
- }
- }
- public void ModifysConcert(char m) throws ClassNotFoundException, SQLException, IOException{
- listConcerts();
- int concert;
- while(true){
- a.openConnection();
- concert = getInputInt("the id of the concert that you want to modify");
- a.rs = a.st.executeQuery("Select * from concerts where id_concert = " + concert);
- if(a.rs.first()){
- if(m == 'b'){
- while(true){
- a.openConnection();
- int band = getInputInt("the id of the band");
- a.rs = a.st.executeQuery("select * from bands where id_Band =" + band);
- if(a.rs.first()){
- a.openConnection();
- a.st.executeUpdate("update concerts set id_band = " + band + " where id_Concert = " + concert);
- break;
- }else{
- println("The id that you entered is not corret or don't exist. Try again"); continue;
- }
- }
- }else if(m == 'h'){
- while(true){
- a.openConnection();
- String hall = getInputText("the id of hall");
- a.rs = a.st.executeQuery("Select * from halls where id_hall = '" + hall + "'");
- if(a.rs.first()){
- a.openConnection();
- a.st.executeUpdate("update concerts set id_hall = '" + hall + "' where id_concert =" + concert);
- break;
- }else{
- println("The id that you entered is not corret or don't exist. Try again"); continue;
- }
- }
- }else if(m == 'p'){
- a.openConnection();
- int price = getInputInt("the price that you want");
- a.st.executeUpdate("update concerts set price = " + price + " where id_concert = " + concert);
- if(price == 0){
- a.openConnection();
- a.st.executeUpdate("update concerts set free = true where id_concert = " + concert);
- }else{
- a.openConnection();
- a.st.executeUpdate("update concerts set free = false where id_concert = " + concert);
- }
- }else if(m == 'd'){
- a.openConnection();
- String date = comproveDate("Insert the new date(YYYY-MM-DD)");
- String hour = comproveHour("Insert the new time(HH:MM)");
- a.st.executeUpdate("update concerts set date = '" + date +"', time = '" + hour + "' where id_concert = " + concert);
- }else if(m == 'a'){
- while(true){
- a.openConnection();
- int aten = getInputInt("How many tickets you want to reserve");
- int cap = 0;
- a.rs =a.st.executeQuery("Select * from halls h, concerts c where h.id_hall = c.id_hall and id_concert =" + concert);
- while(a.rs.next()){
- cap = a.rs.getInt(2);
- break;
- }
- a.openConnection();
- int aten1 = 0 ;
- a.rs = a.st.executeQuery("Select attendance from concerts where id_concert =" + concert);
- while(a.rs.next()){
- aten1= a.rs.getInt(1);
- break;
- }
- if((aten1 + aten) > cap){
- println("There are not enough tickets available. Put a smaller amount or Put 0 to exit."); continue;
- }else if(aten == 0){
- break;
- }else{
- a.openConnection();
- a.st.executeUpdate("Update concerts set attendance = (attendance + " + aten + ") where id_concert = " + concert);
- break;
- }
- }
- }
- break;
- }else{
- println("Incorrect id of concert. Please try again"); continue;
- }
- }
- }
- public void listConcerts() throws ClassNotFoundException, SQLException{
- if(comproveExist("concerts")){
- a.openConnection();
- a.rs = a.st.executeQuery("Select * from concerts");
- while(a.rs.next()){
- int id_concert = a.rs.getInt("ID_CONCERT");
- int id_band = a.rs.getInt("ID_BAND");
- String id_hall = a.rs.getString("id_hall");
- int price = a.rs.getInt("price");
- String date = a.rs.getString("date");
- String time = a.rs.getString("time");
- int attendance = a.rs.getInt("ATTENDANCE");
- println("\n");
- println("ID_CONCERT: " + id_concert + "\n ----------------- "+ "\n ID_BAND: " + id_band + "\n HALL: " + id_hall
- + "\n PRICE: " + price + "\n DATE: " + date + "\n TIME: " + time + " \n ATTENDANCE: " + attendance + "\n -----------------");
- println("\n");
- }
- a.rs.close();
- a.closeConnection();
- }else{
- println("No concerts to show");
- }
- }
- public void modifyGroupMenu() throws ClassNotFoundException, SQLException, IOException {
- Menus m = new Menus();
- String[] i = {"See my group","See members of my group", "Modify info of my group","Modify info of a member",
- "Insert a new Member","Delete a member","Back"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: listBands(2);continue;
- case 2: listMusics(2,getIdBand());continue;
- case 3: modifyInfoGroup('r');continue;
- case 4: modifyInfoMusician();continue;
- case 5: Inserts('m');continue;
- case 6: Deletes('m');continue;
- case 7: break;
- default: continue;
- }
- break;
- }
- }
- /**
- *
- * @param s
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- public void modifyInfoGroup(char s) throws ClassNotFoundException, SQLException{
- listBands(2);
- Menus m = new Menus();
- String[] i = {"Modify Name","Modify Style","Modify Representative","Back"};
- m.setN(i);
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: ModifysBand('n',s);continue;
- case 2: ModifysBand('s',s);continue;
- case 3: ModifysBand('r',s);continue;
- case 4: break;
- default: continue;
- }
- break;
- }
- }
- public void modifyInfoMusician() throws ClassNotFoundException, SQLException, IOException{
- Menus m = new Menus();
- String[] i = {"Modify name","Modify surname","Modify phone","Modify birth date","Back"};
- int op = 0;
- while(true){
- m.setN(i);
- op = m.menu();
- switch(op){
- case 1: ModifysMusic('n');continue;
- case 2: ModifysMusic('s');continue;
- case 3: ModifysMusic('p');continue;
- case 4: ModifysMusic('b');continue;
- case 5: break;
- default: continue;
- }
- break;
- }
- }
- public void listBands(int v) throws SQLException, ClassNotFoundException{
- a.openConnection();
- if(v == 1){
- a.rs = a.st.executeQuery("SELECT * FROM bands");
- }else if (v == 2){
- int b = getIdBand();
- a.rs = a.st.executeQuery("select * from bands where id_band =" + b );
- }else if (v == 3){
- while(true){
- int b = getInputInt("the id of the group you want to see");
- if(comproveExistWhere("bands","id_band",b)){
- listMusics(2,b);
- a.openConnection();
- a.rs = a.st.executeQuery("select * from bands where id_band =" + b);
- break;
- }else{
- println("The id that you entered don't exist. Try again"); continue;
- }
- }
- }
- while(a.rs.next()){
- int id_band = a.rs.getInt("ID_BAND");
- String name = a.rs.getString("NAME");
- int representative = a.rs.getInt("REPRESENTATIVE");
- int num_comp = a.rs.getInt("NUM_COMP");
- String style = a.rs.getString("STYLE");
- println("\n");
- println("ID_BAND: " + id_band + "\n ----------------- "+ "\n NAME: " + name + "\n REPRESENTATIVE: " + representative
- + "\n Number of members: " + num_comp + "\n Style: " + style + "\n -----------------");
- println("\n");
- }
- a.rs.close();
- a.closeConnection();
- }
- public void listMusics(int v,int b) throws ClassNotFoundException, SQLException{
- a.openConnection();
- if(v == 1){
- a.rs = a.st.executeQuery("SELECT id_music,name,surname,phone,TRUNCATE(DATEDIFF(CURDATE(), birth)/365.25, 0)," +
- "instrument FROM musicians");
- }else if (v == 2){
- a.rs =a.st.executeQuery("SELECT m.id_music,name,surname,phone,TRUNCATE(DATEDIFF(CURDATE(), birth)/365.25, 0)," +
- "instrument FROM musicians m,bandmusic bm where m.id_music = bm.id_music and bm.id_band =" + b );
- }else if (v == 3) {
- int m =getInputInt("Enter the id of the Musician that want to see");
- if(comproveExistWhere("musicians","id_music",m)){
- a.rs =a.st.executeQuery("SELECT id_music,name,surname,phone,TRUNCATE(DATEDIFF(CURDATE(), birth)/365.25, 0)," +
- "instrument FROM musicians m where id_music =" + m );
- }else{
- println("That musician don't exist");
- }
- }
- while(a.rs.next()){
- int id_music = a.rs.getInt("ID_MUSIC");
- String name = a.rs.getString("NAME");
- String surname = a.rs.getString("SURNAME");
- int phone = a.rs.getInt("PHONE");
- int old = a.rs.getInt("TRUNCATE(DATEDIFF(CURDATE(), birth)/365.25, 0)");
- String instrument =a.rs.getString("INSTRUMENT");
- println("\n");
- println("ID_MUSIC: "+ id_music + "\n -----------------" + "\n NAME: " + name + " \n SURNAME: " + surname + "\n PHONE: " +
- phone + "\n YEARS OLD: " + old + "\n INSTRUMENT: " + instrument + "\n -----------------");
- println("\n");
- }
- a.rs.close();
- a.closeConnection();
- }
- private int getIdBand() throws ClassNotFoundException, SQLException{
- a.openConnection();
- int id = 0;
- String user = a.getUsers();
- a.rs = a.st.executeQuery("Select b.id_band from bands b, musicians m where" +
- " m.id_music = b.representative and m.user = '" + user + "';");
- while (a.rs.next()){
- id = a.rs.getInt("ID_BAND");
- }
- return id;
- }
- public void ModifysBand(char m,char s) throws ClassNotFoundException, SQLException{
- if(s == 'r'){
- a.openConnection();
- if(m == 'n'){
- String name = getInputText("the new name");
- a.st.executeUpdate("update bands set name = '" + name + "' where id_band = " + getIdBand());
- }else if(m == 's'){
- String style = getInputText("the new style");
- a.st.executeUpdate("update bands set style = '" + style + "' where id_band =" + getIdBand());
- }else if(m == 'r'){
- println("Only admins are allowed to do it");
- }
- }else if(s == 'a'){
- a.openConnection();
- while(true){
- int b = getInputInt("the id of the group that you want to modify");
- if(comproveExistWhere("bands","id_band",b)){
- if(m == 'n'){
- String name = getInputText("the new name");
- a.st.executeUpdate("update bands set name = '" + name + "' where id_band = " + b);
- break;
- }else if(m == 's'){
- String style = getInputText("the new style");
- a.st.executeUpdate("update bands set style = '" + style + "' where id_band =" + b);
- break;
- }else if(m == 'r'){
- listMusics(2,b);
- int r = 0;
- String u = null;
- int id = getInputInt("the id of the new presentative");
- a.openConnection();
- a.rs = a.st.executeQuery("select representative from bands where id_band = " + b);
- while(a.rs.next()){
- r = a.rs.getInt(1);
- }
- a.openConnection();
- a.rs = a.st.executeQuery("select user from musicians where id_music = " + r );
- while(a.rs.next()){
- u = a.rs.getString(1);
- }
- a.openConnection();
- a.st.executeUpdate("update musicians set user = null where id_music =" + r);
- a.openConnection();
- a.st.executeUpdate("Delete from users where user = '" + u + "'");
- a.openConnection();
- a.rs = a.st.executeQuery("Select * from bandmusic,bands b where b.id_band = " + b + " and id_music = " + id);
- if(a.rs.first()){
- a.openConnection();
- a.st.executeUpdate("update bands set REPRESENTATIVE = " + id +" where id_band =" + b);
- String v = CreateUser(3);
- a.openConnection();
- a.st.executeUpdate("update musicians set user = '" + v + "' where id_music = " + id);
- }
- break;
- }
- }else{
- println("That id of band don't exist");continue;
- }
- }
- }
- }
- public String CreateUser(int l) throws SQLException, ClassNotFoundException{
- String u = getInputText("the user");
- String p = getInputText("the password");
- a.openConnection();
- a.st.executeUpdate("Insert into users values ('" + u + "' , '" + p + "'," + l + ")");
- return u;
- }
- public void ModifysMusic(char m) throws ClassNotFoundException, SQLException, IOException {
- a.openConnection();
- int s = getInputInt("the id of the member you want to modify");
- if(m == 'n'){
- String name = getInputText("the new name");
- a.st.executeUpdate("update musicians set name = '" + name + "' where id_music = " + s);
- }else if(m == 's'){
- String surname = getInputText("the new surname");
- a.st.executeUpdate("update musicians set surname = '" + surname + "' where id_music = " + s);
- }else if(m == 'p'){
- int phone = getInputInt("the new phone");
- a.st.executeUpdate("update musicians set phone =" + phone + " where id_music = " + s);
- }else if(m == 'b'){
- String birth = comproveDate("New birth date(YYYY-MM-DD)");
- a.st.executeUpdate("update musicians set birth ='" + birth +"' where id_music = " + s);
- }
- }
- public void Inserts(char o) throws IOException, SQLException, ClassNotFoundException{
- a.openConnection();
- String name;
- String surname;
- int phone;
- String age;
- String instrument;
- if(o == 'm'){
- name = getInputText("Name");
- surname = getInputText("Surname");
- phone = getInputInt("Phone");
- age = comproveDate("Born date(YYYY-MM-DD)");
- instrument = getInputText("Instrument");
- a.st.executeUpdate("Insert into musicians (name,surname,phone,birth,instrument,user) values ('" + name + "','" + surname + "','"
- + phone + "','" + age + "','" + instrument + "',NULL)");
- a.rs = a.st.executeQuery("select id_music from musicians where name = '" + name + "' and surname = '" + surname + "' and phone = '" + phone + "'");
- while(a.rs.next()){
- int id_music = a.rs.getInt(1);
- a.st.executeUpdate("Insert into bandmusic values (" + getIdBand() + "," + id_music + ")");
- a.st.executeUpdate("Update bands set num_comp = (num_comp + 1) where id_band = " + getIdBand());
- break;
- }
- }else if( o == 'c'){
- int id_band = getInputInt("ID of the band");
- String id_hall = getInputText("ID of the hall");
- int price = getInputInt("Price of the concert");
- String date = comproveDate("Date of the concert(YYYY-MM-DD)");
- String time = comproveHour("time when the concert starts(HH:MM)");
- boolean free;
- if(price > 0){
- free = false;
- }else{
- free = true;
- }
- a.st.executeUpdate("insert into concerts(id_band,id_hall,free,price,date,time) values (" + id_band +",'" + id_hall
- + "'," + free +"," + price + ",'" + date + "','" + time + "')" );
- }
- a.closeConnection();
- }
- private void Deletes(char o) throws SQLException, ClassNotFoundException{
- a.openConnection();
- if(o == 'm'){
- listMusics(2,getIdBand());
- a.openConnection();
- while(true){
- int id = getInputInt("ID from member you want to delete");
- if(comproveExistWhere("musicians","id_music", id)){
- if(comproveExistWhere2("bandmusic","id_music",id,"id_band",getIdBand())){
- a.openConnection();
- a.st.executeUpdate("Update bands set num_comp = (num_comp - 1) where id_band = " + getIdBand());
- a.st.executeUpdate("DELETE from bandmusic where id_music =" + id);
- break;
- }else{
- println("That musiciant aren't part of your group, try again"); continue;
- }
- }else{
- println("That member don't exist, try again"); continue;
- }
- }
- }else if(o == 'c'){
- while(true){
- int id = getInputInt("ID from concert you want to delete");
- if(comproveExistWhere("concerts","id_concert",id)){
- a.st.executeUpdate("DELETE from concerts where id_concert =" + id);
- break;
- }else{
- println("That concert don't exist, try again.");continue;
- }
- }
- }
- }
- private String comproveDate(String g) throws IOException{
- while (true) {
- println(g);
- String f = br.readLine().toLowerCase();
- Pattern pat = Pattern.compile("^([0-9]{4}-(0[0-9]|10|11|12)-([0-2][0-9]|[3][0-1]))");
- Matcher m = pat.matcher(f);
- if (!m.matches()) {
- println("Error try again(YYYY-MM-DD)"); continue;
- } else {
- return f;
- }
- }
- }
- private String comproveHour(String g) throws IOException{
- while (true) {
- println(g);
- String f = br.readLine().toLowerCase();
- Pattern pat = Pattern.compile("^([0-2][0-9]:[1-6][0-9])");
- Matcher m = pat.matcher(f);
- if (!m.matches()) {
- println("Error try again(HH:MM)"); continue;
- } else {
- return f;
- }
- }
- }
- private String getInputText(String quest){
- String aux = "";
- while(true){
- System.out.println("Enter "+ quest + ":");
- try {
- aux = br.readLine();
- if (aux.isEmpty()){
- continue;
- }else{
- break;
- }
- } catch (IOException e) {
- e.printStackTrace();;
- }
- }
- return aux;
- }
- private int getInputInt(String quest){
- int aux;
- String s;
- while(true){
- System.out.println("Enter "+ quest + ":");
- try {
- s = br.readLine();
- if (s.isEmpty()){
- continue;
- }else{
- aux = Integer.parseInt(s);
- break;
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (NumberFormatException e1){
- println("Insert only a number");
- continue;
- }
- }
- return aux;
- }
- public boolean comproveExist(String t) throws SQLException, ClassNotFoundException{
- a.openConnection();
- a.rs = a.st.executeQuery("Select * from " + t);
- if(a.rs.first()){
- return true;
- }else{
- return false;
- }
- }
- public boolean comproveExistWhere(String t,String c,int c1) throws SQLException, ClassNotFoundException{
- a.openConnection();
- a.rs = a.st.executeQuery("Select * from " + t + " where " + c +" = " + c1 );
- if(a.rs.first()){
- return true;
- }else{
- return false;
- }
- }
- public boolean comproveExistWhere2(String t,String c,int c1, String d, int d1) throws SQLException, ClassNotFoundException{
- a.openConnection();
- a.rs = a.st.executeQuery("Select * from " + t + " where " + c +" = " + c1 + " and " + d + " = " + d1);
- if(a.rs.first()){
- return true;
- }else{
- return false;
- }
- }
- private void println(String f){
- System.out.println(f);
- }
- }
Add Comment
Please, Sign In to add comment