Advertisement
Guest User

empleado

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