Advertisement
Guest User

Untitled

a guest
May 4th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.05 KB | None | 0 0
  1. import java.io.InputStreamReader;
  2. import java.sql.*;
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7.  
  8. public class Diagnostico {
  9.  
  10.     private final String DATAFILE = "data/disease_data.data";
  11.     private Connection connection = null;
  12.  
  13.     private void showMenu() {
  14.  
  15.         int option = -1;
  16.         do {
  17.             System.out.println("Bienvenido a sistema de diagnóstico\n");
  18.             System.out.println("Selecciona una opción:\n");
  19.             System.out.println("\t1. Creación de base de datos y carga de datos.");
  20.             System.out.println("\t2. Realizar diagnóstico.");
  21.             System.out.println("\t3. Listar síntomas de una enfermedad.");
  22.             System.out.println("\t4. Listar enfermedades y sus códigos asociados.");
  23.             System.out.println("\t5. Listar síntomas existentes en la BD y su tipo semántico.");
  24.             System.out.println("\t6. Mostrar estadísticas de la base de datos.");
  25.             System.out.println("\t7. Salir.");
  26.             try {
  27.                 option = readInt();
  28.                 switch (option) {
  29.                     case 1:
  30.                         crearBD();
  31.                         break;
  32.                     case 2:
  33.                         realizarDiagnostico();
  34.                         break;
  35.                     case 3:
  36.                         listarSintomasEnfermedad();
  37.                         break;
  38.                     case 4:
  39.                         listarEnfermedadesYCodigosAsociados();
  40.                         break;
  41.                     case 5:
  42.                         listarSintomasYTiposSemanticos();
  43.                         break;
  44.                     case 6:
  45.                         mostrarEstadisticasBD();
  46.                         break;
  47.                     case 7:
  48.                         exit();
  49.                         break;
  50.                 }
  51.             } catch (Exception e) {
  52.                 System.err.println("Opción introducida no válida!");
  53.             }
  54.         } while (option != 7);
  55.         exit();
  56.     }
  57.  
  58.     private void exit() {
  59.         System.out.println("Saliendo.. ¡hasta otra!");
  60.         System.exit(0);
  61.     }
  62.  
  63.     /**
  64.      * Método para establecer conexión con la base de datos.
  65.      *
  66.      * @throws Exception Puede lanzar excepción.
  67.      */
  68.     private void conectar() throws Exception {
  69.         if (connection == null) {
  70.             try {
  71.                 connection = DriverManager.getConnection(
  72.                         "jdbc:mysql://localhost:3306/diagnostico",
  73.                         "bddx",
  74.                         "bddx_pwd");
  75.             } catch (Exception e) {
  76.                 throw new Exception("Could not connect to the database.");
  77.             }
  78.         }
  79.     }
  80.  
  81.     private void crearBD() {
  82.         try {
  83.             conectar();
  84.             ArrayList<String> sentenciasSQL = new ArrayList<>();
  85.             sentenciasSQL.add("CREATE DATABASE diagnostico;");
  86.             sentenciasSQL.add("CREATE TABLE disease (disease_id VARCHAR(45), name VARCHAR(255));");
  87.             sentenciasSQL.add("CREATE TABLE disease_code (disease_id VARCHAR(45), code VARCHAR(255), source_id VARCHAR(25))");
  88.             sentenciasSQL.add("CREATE TABLE disease_symptom (disease_id VARCHAR(45), symptom_id VARCHAR(25))");
  89.             sentenciasSQL.add("CREATE TABLE code (code VARCHAR(255), source_id VARCHAR(25))");
  90.             sentenciasSQL.add("CREATE TABLE source (source_id VARCHAR(25), name VARCHAR(255))");
  91.             sentenciasSQL.add("CREATE TABLE symptom (cui VARCHAR(25), name VARCHAR(255))");
  92.             sentenciasSQL.add("CREATE TABLE symptom_semantic_type (cui VARCHAR(25), semantic_type_id INT)");
  93.             sentenciasSQL.add("CREATE TABLE semantic_type (semantic_type_id INT, tui VARCHAR(45))");
  94.             for (String sentencia : sentenciasSQL){
  95.                 PreparedStatement ps = connection.prepareStatement(sentencia);
  96.                 ps.executeUpdate();
  97.             }
  98.  
  99.             //TODO: Volcar datos en las tablas creadas.
  100.  
  101.         } catch (Exception e) {
  102.             e.printStackTrace();
  103.         }
  104.  
  105.     }
  106.  
  107.     private void realizarDiagnostico() {
  108.         // implementar
  109.     }
  110.  
  111.     private void listarSintomasEnfermedad() {
  112.         // implementar
  113.     }
  114.  
  115.     private void listarEnfermedadesYCodigosAsociados() {
  116.         // implementar
  117.     }
  118.  
  119.     private void listarSintomasYTiposSemanticos() {
  120.         // implementar
  121.     }
  122.  
  123.     private void mostrarEstadisticasBD() {
  124.         // implementar
  125.     }
  126.  
  127.     /**
  128.      * Método para leer números enteros de teclado.
  129.      *
  130.      * @return Devuelve el número leído.
  131.      * @throws Exception Puede lanzar excepción.
  132.      */
  133.     private int readInt() throws Exception {
  134.         try {
  135.             System.out.print("> ");
  136.             return Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
  137.         } catch (Exception e) {
  138.             throw new Exception("Not number.");
  139.         }
  140.     }
  141.  
  142.     /**
  143.      * Método para leer cadenas de teclado.
  144.      *
  145.      * @return Devuelve la cadena leída.
  146.      * @throws Exception Puede lanzar excepción.
  147.      */
  148.     private String readString() throws Exception {
  149.         try {
  150.             System.out.print("> ");
  151.             return new BufferedReader(new InputStreamReader(System.in)).readLine();
  152.         } catch (Exception e) {
  153.             throw new Exception("Error reading line.");
  154.         }
  155.     }
  156.  
  157.     /**
  158.      * Método para leer el fichero que contiene los datos.
  159.      *
  160.      * @return Devuelve una lista de String con el contenido.
  161.      * @throws Exception Puede lanzar excepción.
  162.      */
  163.     private LinkedList<String> readData() throws Exception {
  164.         LinkedList<String> data = new LinkedList<String>();
  165.         BufferedReader bL = new BufferedReader(new FileReader(DATAFILE));
  166.         while (bL.ready()) {
  167.             data.add(bL.readLine());
  168.         }
  169.         bL.close();
  170.         return data;
  171.     }
  172.  
  173.     public static void main(String args[]) {
  174.         new Diagnostico().showMenu();
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement