Advertisement
Guest User

empleados

a guest
Jan 26th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.02 KB | None | 0 0
  1. package yomusico;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Scanner;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. /**
  13.  *
  14.  * @author cristian
  15.  */
  16. public class Yomusico {
  17.     static Scanner teclado = new Scanner(System.in);
  18.     private static Object sentencia;
  19.        public static void main(String[] args) {
  20.        
  21.         Connection con = null;
  22.         Statement sentencia = null;
  23.         String empleado = "";
  24.         try {
  25.  
  26.             Class.forName("com.mysql.jdbc.Driver");
  27.             con = DriverManager.getConnection(
  28.                     "jdbc:mysql://localhost/yomusico",
  29.                     "root", "1234");
  30.             sentencia = con.createStatement();
  31.         } catch (ClassNotFoundException ex) {
  32.             System.out.println("Error carga driver");
  33.         } catch (SQLException ex) {
  34.             System.out.println("Error SQL");
  35.         }
  36.  
  37.         //Funcion
  38.         menu();
  39.         try {
  40.             con.close();
  41.         } catch (SQLException ex) {
  42.             Logger.getLogger(Yomusico.class.getName()).log(Level.SEVERE, null, ex);
  43.         }
  44.     }
  45.  
  46.     static void menu() {
  47.         Integer op;
  48.         do {
  49.             System.out.println("-----------------------------------\n"
  50.                     + "||Menu Principal                 ||\n"
  51.                     + "-----------------------------------\n"
  52.                     + "|1.Empleados                      |\n"
  53.                     + "|2.Tiendas                        |\n"
  54.                     + "|3.Pedidos                        |\n"
  55.                     + "|4.Clientes                       |\n"
  56.                     + "|5.Instrumentos                   |\n"
  57.                     + "|0.Salir                          |\n"
  58.                     + "-----------------------------------");
  59.             op = teclado.nextInt();
  60.             if (op == null) {
  61.                 op = 100;
  62.             }
  63.             switch (op) {
  64.                 case 1:
  65.                     Empleados();
  66.                     break;
  67.                 case 2:
  68.                     Tiendas();
  69.                     break;
  70.                 case 3:
  71.                     Pedidos();
  72.                     break;
  73.                 case 4:
  74.                     Clientes();
  75.                     break;
  76.                 case 5:
  77.                     Instrumentos();
  78.                     break;
  79.                 case 0:
  80.                     System.out.println("Saliendo...\n\nHasta Pronto");
  81.                     break;
  82.                 default:
  83.                     System.out.println("Opcion no valida");
  84.             }
  85.         } while (op != 0);
  86.     }
  87. //    TABLA EMPLEADOS
  88.  
  89.    static void Empleados() {
  90.         Integer op;
  91.         do {
  92.             System.out.println("-----------------------------------\n"
  93.                     + "||Menu Principal                 ||\n"
  94.                     + "-----------------------------------\n"
  95.                     + "|1.Mostrar Empleados                        |\n"
  96.                     + "|2.Tiendas                        |\n"
  97.                     + "|3.Pedidos                       |\n"
  98.                     + "|4.Clientes                       |\n"
  99.                     + "|5.Instrumentos                  |\n"
  100.                     + "|0.Salir                          |\n"
  101.                     + "-----------------------------------");
  102.             op = teclado.nextInt();
  103.             if (op == null) {
  104.                 op = 100;
  105.             }
  106.             switch (op) {
  107.                 case 1:
  108.                     mostrarEmpleados();
  109.                     break;
  110.                 case 2:
  111.                     añadirEmpleados();
  112.                     break;
  113.                 case 3:
  114.                     eliminarEmpleados();
  115.                     break;
  116.                 case 4:
  117.                     modificarEmpleados();
  118.                 case 0:
  119.                     System.out.println("Volviendo...\n");
  120.                     break;
  121.                 default:
  122.                     System.out.println("Opcion no valida");
  123.             }
  124.         } while (op != 0);
  125.     }
  126.  
  127.     static void mostrarEmpleados(Statement sentencia, String empleado) {
  128.         try {
  129.             Scanner sc = new Scanner(System.in);
  130.             System.out.println("Introduce el nombre del Empleado a buscar:");
  131.             empleado = sc.nextLine();
  132.             String sentenciaSQL = "SELECT * FROM Empleados where nombre =\'" + empleado + "\'";
  133.             ResultSet rs = executeQuery(sentenciaSQL);
  134.  
  135.             while (rs.next()) {
  136.                 String res = rs.getString("numemp") + "\t "
  137.                         + rs.getString("nombre") + "\t"
  138.                         + rs.getString("edad") + "\t"
  139.                         + rs.getString("oficina") + "\t"
  140.                         + rs.getString("titulo") + "\t"
  141.                         + rs.getString("contrato") + "\t"
  142.                         + rs.getString("jefe") + "\t"
  143.                         + rs.getString("cuota") + "\t"
  144.                         + rs.getString("ventas");
  145.                 System.out.println(res);
  146.             }
  147.  
  148.             if (!existeEmpleado((Statement) sentencia, empleado)) {
  149.                 System.out.println("No existe este Empleado.");
  150.             } else {
  151.                mostrarEmpleados();
  152.             }
  153.  
  154.         } catch (SQLException ex) {
  155.             System.out.println("Error SQL");
  156.         }
  157.     }
  158.  
  159.     static boolean existeEmpleado(Statement sentencia, String empleado) {
  160.         boolean existe = false;
  161.         String s = "SELECT * FROM Empleado WHERE nombre=\'" + empleado + "\'";
  162.         ResultSet rs;
  163.         try {
  164.             rs = sentencia.executeQuery(s);
  165.             if (rs.next()) {
  166.                 existe = true;
  167.             } else {
  168.                 existe = false;
  169.             }
  170.         } catch (SQLException ex) {
  171.  
  172.         }
  173.         return existe;
  174.     }
  175.  
  176.     static void añadirEmpleados() {
  177.         System.out.println("Registrar Empleado");
  178.         String consulta;
  179.         System.out.println("Introduce la referencia");
  180.         int ref = teclado.nextInt();
  181.  
  182.         //comprobar si el suelo existe
  183.         if (!repetido("Suelos", "ref", ref)) {
  184.             System.out.println("Introduce el numemp (número id): ");
  185.             int num = teclado.nextInt();
  186.             System.out.println("Introduce el Nombre del empleado: ");
  187.             String nom = teclado.nextLine();
  188.             System.out.println("Introduce la edad del empleado: ");
  189.             int edad = teclado.nextInt();
  190.             System.out.println("Introduce la tienda donde trabaja el Empleado a introducir: ");
  191.             String tien = teclado.nextLine();
  192.             System.out.println("Introduce el trabajo que realiza el empleado: ");
  193.             String ejer = teclado.nextLine();
  194.             System.out.println("Introduce la fecha del contrato de cuando se realizó: ");
  195.             int contrato = teclado.nextInt();
  196.             System.out.println("Introduce la cuota de dicho Empleado: ");
  197.             String cuota = teclado.nextLine();
  198.             System.out.println("Introduce las ventas realizada por el Empleado: ");
  199.             String ventas = teclado.nextLine();
  200.             consulta = "INSERT INTO Empleados VALUE ('" + num + "','" + nom + "','" + edad + "','" + tien + "','" + ejer + "','" + contrato + "' ,'" + cuota + "','" + ventas + "');";
  201.             System.out.println("Suelo registrado");
  202.         } else {
  203.             System.out.println("El Empleado ya existe.");
  204.         }
  205.  
  206.     }
  207.  
  208.     static void eliminarEmpleados() {
  209.         String consulta;
  210.         System.out.println("Introduzca el numemp del Empleado que deseas eliminar: ");
  211.         String numemp = teclado.nextLine();
  212.         if (existe("Empleados", "numemp", numemp)) {
  213.             System.out.println("Los Empleados con la referencia " + numemp + " son:");
  214.             sueloCampo("numep", numemp);
  215.             System.out.println("Desea continuar? (s/n)");
  216.             String sn = teclado.nextLine();
  217.             if (sn.equals("s")) {
  218.                 consulta = "DELETE FROM Empleados WHERE numemp = '" + numemp + "';";
  219.                 System.out.println("Empleado eliminado con exito.");
  220.             } else {
  221.                 System.out.println("Volviendo...\n");
  222.             }
  223.         } else {
  224.             System.out.println("No existen Empleados con este " + numemp);
  225.         }
  226.     }
  227.  
  228.     private static boolean repetido(String suelos, String ref, int ref0) {
  229.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  230.     }
  231.  
  232.     private static boolean existe(String suelos, String ref, String ref0) {
  233.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  234.     }
  235.  
  236.     private static void sueloCampo(String numep, String numemp) {
  237.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  238.     }
  239.     static void modificarEmpleado() {
  240.         Integer op;
  241.         System.out.println("Introduzca la referencia");
  242.         String iden = teclado.nextLine();
  243.         if (existe("Suelos", "ref", iden)) {
  244.             System.out.println("Los suelos con referencia: " + iden + " son:");
  245.             sueloCampo("ref", iden);
  246.             System.out.println("\nSe modificaran todos los suelos mostrados\nDesea continuar? (s/n)");
  247.             String sn = teclado.nextLine();
  248.             if (sn.equals("s")) {
  249.                 do {
  250.                     System.out.println("-----------------------------------\n"
  251.                             + "||Modifición de Empleados               ||\n"
  252.                             + "-----------------------------------\n"
  253.                             + "|1.Modificar Nombre                |\n"
  254.                             + "|2.Modificar Edad               |\n"
  255.                             + "|3.Modificar Tienda                 |\n"
  256.                             + "|4.Modificar Trabajo que ejerce                |\n"
  257.                             + "|5.Modificar Contrato            |\n"
  258.                             + "|6.Modificar Cuota            |\n"
  259.                             + "|7.Modificar Ventas            |\n"
  260.                             + "|0.Volver                         |\n"
  261.                             + "-----------------------------------");
  262.                     op = teclado.nextInt();
  263.                     if (op == null) {
  264.                         op = 100;
  265.                     }                    switch (op) {
  266.                         case 1:
  267.                             //Nombre
  268.                             System.out.println("Introduzca el nuevo Nombre: ");
  269.                             String nombre = teclado.nextLine();
  270.  
  271.                             break;
  272.                         case 2:
  273.                             //Edad
  274.                             System.out.println("Introduzca la nueva edad: ");
  275.                             int edad = teclado.nextInt();
  276.                             break;
  277.                         case 3:
  278.                             //tienda
  279.                             System.out.println("Introduzca la nueva tienda: ");
  280.                             int tienda = teclado.nextInt();
  281.                            
  282.                             break;
  283.                         case 4:
  284.                             //EJERCE
  285.                             System.out.println("Introduzca el nuevo trabajo: ");
  286.                             String ejerce = teclado.nextLine();
  287.                            
  288.                             break;
  289.                         case 5:
  290.                             //CONTRATO
  291.                             System.out.println("Introduzca el nuevo contrato: ");
  292.                             int contrato = teclado.nextInt();
  293.                             break;
  294.                         case 6:
  295.                             //CUOTA
  296.                             System.out.println("Introduzca la nueva cuota: ");
  297.                             int cuota = teclado.nextInt();
  298.                             break;
  299.                         case 7:
  300.                             //VENTAS
  301.                             System.out.println("Introduzca las nuevas ventas: ");
  302.                             int ventas = teclado.nextInt();
  303.                             break;
  304.                         case 0:
  305.                             System.out.println("Volviendo...\n");
  306.                             break;
  307.                         default:
  308.                             System.out.println("Opcion no valida");
  309.                     }
  310.                     if (op != 100 && op != 0) {
  311.                         System.out.println("Pulse 'Intro' para continuar");
  312.                         teclado.nextLine();
  313.                     }
  314.                 } while (op != 0);
  315.             } else {
  316.                 System.out.println("Volviendo...\n");
  317.             }
  318.         } else {
  319.             System.out.println("No exite este Empleado. ");
  320.         }
  321.     }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement