Advertisement
Guest User

main

a guest
Jan 20th, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.50 KB | None | 0 0
  1. package main;
  2.  
  3. import java.awt.Toolkit;
  4. import java.awt.datatransfer.Clipboard;
  5. import java.awt.datatransfer.StringSelection;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.io.PrintWriter;
  12. import java.util.Vector;
  13.  
  14. import javax.swing.JFileChooser;
  15.  
  16. public class Main {
  17.  
  18.     public static void main(String[] args) throws IOException, InterruptedException {
  19.  
  20.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  21.         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  22.         String opcion = "0";
  23.         String exit = "q";
  24.         Vector<Usuario> usuarios = new Vector<Usuario>();
  25.         Utiles util = new Utiles();
  26.         int opcionuno = 0;
  27.         Usuario user = new Usuario("FIGT1@ALU.UA.ES", "819575", "ua.ES", false);
  28.         Usuario user2 = new Usuario("Mari", "Pass", "ee", true);
  29.         usuarios.add(user);
  30.         usuarios.add(user2);
  31.  
  32.         /*
  33.          * Para leer String s = br.readLine();
  34.          *
  35.          * Para copiar al portapapeles StringSelection data = new
  36.          * StringSelection("This text is going to be copied into the clipboard"
  37.          * ); clipboard.setContents(data, data);
  38.          *
  39.          */
  40.  
  41.         do {
  42.             opcion = "";
  43.  
  44.             System.out.println("Menú:");
  45.             System.out.println("1) LISTAR      USUARIOS");
  46.             System.out.println("2) SELECCIONAR     USUARIO");
  47.             System.out.println("3) ADD         USUARIO");
  48.             System.out.println("4) GUARDAR     USUARIOS");
  49.             System.out.println("5) CARGAR      USUARIOS");
  50.  
  51.             System.out.println("q) SALIR");
  52.  
  53.             System.out.print("Opcion: ");
  54.             opcion = br.readLine();
  55.  
  56.             if (opcion.equals("1")) {
  57.                 opcionuno++;
  58.                 util.limpia();
  59.                 System.out.println("--- LISTA DE USUARIOS   ---");
  60.                 System.out.println();
  61.                 System.out.println("i) ID DE USUARIO   PASSWORD SITIO  VIP");
  62.                 System.out.println();
  63.  
  64.                 for (int i = 0; i < usuarios.size(); i++) {
  65.                     System.out.print((i + 1) + ") ");
  66.                     usuarios.elementAt(i).lista();
  67.                 }
  68.                 System.out.println();
  69.  
  70.                 System.out.println("¿Limpiar pantalla? (y)es/(n)o");
  71.                 String siono = br.readLine();
  72.                 if (siono.equals("y"))
  73.                     util.limpia();
  74.                 else {
  75.                     util.espacio();
  76.                 }
  77.             }
  78.  
  79.             if (opcion.equals("2")) {
  80.  
  81.                 int fails = 0;
  82.  
  83.                 if (opcionuno == 0) {
  84.                     opcionuno++;
  85.                     util.limpia();
  86.                     System.out.println("--- LISTA DE USUARIOS   ---");
  87.                     System.out.println();
  88.                     System.out.println("i) ID DE USUARIO   PASSWORD SITIO  VIP");
  89.                     System.out.println();
  90.  
  91.                     for (int i = 0; i < usuarios.size(); i++) {
  92.                         System.out.print((i + 1) + ") ");
  93.                         usuarios.elementAt(i).lista();
  94.                     }
  95.                 }
  96.                 System.out.print("Selecciona el usuario: ");
  97.                 String eleccion = br.readLine();
  98.                 // Comprobar que no se puede hacer el parse
  99.                 char[] charArray = eleccion.toCharArray();
  100.                 for (int i = 0; i < charArray.length; i++) {
  101.                     if (charArray[i] > 47 && charArray[i] < 58) {
  102.                     } else
  103.                         fails++;
  104.  
  105.                     if (fails == 0) {
  106.                         int iusu = Integer.parseInt(eleccion);
  107.                         if (iusu <= 0 || iusu > usuarios.size()) {
  108.                             System.out.print("Opcion incorrecta.");
  109.                             Thread.sleep(3000); // 1000 milliseconds is one
  110.                                                 // second.
  111.                             util.limpia();
  112.                         } else {
  113.  
  114.                             System.out.println("Se ha marcado la opción:  " + usuarios.get(iusu - 1).getId() + " |  "
  115.                                     + usuarios.get(iusu - 1).getPass());
  116.  
  117.                             System.out.print("¿Es correcto? (y/n): ");
  118.                             String siono = br.readLine();
  119.                             if (siono.equals("y")) {
  120.  
  121.                                 StringSelection data = new StringSelection(
  122.                                         usuarios.get(iusu - 1).getId() + " " + usuarios.get(iusu - 1).getPass());
  123.                                 clipboard.setContents(data, data);
  124.                                 System.out.print("Se ha copiado al portapales.");
  125.                                 Thread.sleep(1000); // 1000 milliseconds is one
  126.                                                     // second.
  127.                                 System.out.print("Volviendo al menú principal...");
  128.                                 Thread.sleep(3000); // 1000 milliseconds is one
  129.                                                     // second.
  130.                                 util.limpia();
  131.  
  132.                             } else {
  133.                                 util.limpia();
  134.                             }
  135.  
  136.                         }
  137.  
  138.                     } else {
  139.                         System.out.print("Opcion incorrecta.");
  140.                         Thread.sleep(3000); // 1000 milliseconds is one second.
  141.                         util.limpia();
  142.  
  143.                     }
  144.  
  145.                 }
  146.  
  147.             }
  148.             if (opcion.equals("3")) {
  149.  
  150.                 Usuario alta = new Usuario();
  151.                 System.out.print("ID:  ");
  152.                 alta.setId(br.readLine());
  153.                 System.out.print("PASSWORD:  ");
  154.                 alta.setPass(br.readLine());
  155.                 System.out.print("Sitio:  ");
  156.                 alta.setSitio(br.readLine());
  157.                 System.out.print("VIP(y/n): ");
  158.                 String n = br.readLine();
  159.                 if (n.equals("y")) {
  160.                     alta.setVip(true);
  161.                 }
  162.  
  163.                 if (alta.getId().isEmpty() || alta.getPass().isEmpty() || alta.getSitio().isEmpty()) {
  164.  
  165.                     System.out.print("La id, pass y sitio no pueden estar vacías. ");
  166.                     Thread.sleep(1000); // 1000 milliseconds is one second.
  167.                     System.out.print("Volviendo al menú principal...");
  168.                     Thread.sleep(3000); // 1000 milliseconds is one second.
  169.                     util.limpia();
  170.  
  171.                 }
  172.  
  173.                 else {
  174.                     System.out.print("Has introducido :  ");
  175.                     alta.lista();
  176.                     System.out.print("¿Es correcto? (y/n):  ");
  177.                     String t = br.readLine();
  178.                     if (t.equals("y")) {
  179.                         usuarios.addElement(alta);
  180.                     }
  181.                 }
  182.  
  183.             }
  184.  
  185.             if (opcion.equals("4")) {
  186.  
  187.                 /*
  188.                  * System.out.print("Nombre del fichero:  " ); String t=
  189.                  * br.readLine();
  190.                  */
  191.  
  192.                 try {
  193.                     JFileChooser fileChooser = new JFileChooser();
  194.                     if (fileChooser.showSaveDialog(fileChooser) == JFileChooser.APPROVE_OPTION) {
  195.                         File file = fileChooser.getSelectedFile();
  196.                         // save to file
  197.  
  198.                         PrintWriter writer = new PrintWriter(file + ".txt", "UTF-8");
  199.                         for (int i = 0; i < usuarios.size(); i++) {
  200.                             writer.println(usuarios.get(i).linea());
  201.                         }
  202.                         writer.close();
  203.                         System.out.print("Se ha guardado el archivo correctamente.");
  204.                         Thread.sleep(1000); // 1000 milliseconds is one second.
  205.                         System.out.print("Volviendo al menú principal...");
  206.                         Thread.sleep(3000); // 1000 milliseconds is one second.
  207.                         util.limpia();
  208.                     }
  209.                 } catch (IOException e) {
  210.                     System.out.print("No se ha podido guardar el archivo.");
  211.                     Thread.sleep(1000); // 1000 milliseconds is one second.
  212.                     System.out.print("Volviendo al menú principal...");
  213.                     Thread.sleep(3000); // 1000 milliseconds is one second.
  214.                     util.limpia();
  215.  
  216.                 }
  217.  
  218.             }
  219.  
  220.             if (opcion.equals("5")) {
  221.  
  222.                 JFileChooser fileChooser = new JFileChooser();
  223.                 if (fileChooser.showSaveDialog(fileChooser) == JFileChooser.APPROVE_OPTION) {
  224.                     File file = fileChooser.getSelectedFile();
  225.                     if (file.getName().contains(".txt")) {
  226.                         try (BufferedReader bX = new BufferedReader(new FileReader(file))) {
  227.                             StringBuilder sb = new StringBuilder();
  228.                             String line = bX.readLine();
  229.  
  230.                             while (line != null) {
  231.                                 sb.append(line);
  232.                                 sb.append(System.lineSeparator());
  233.                                 line = bX.readLine();
  234.                             }
  235.  
  236.                             String everything = sb.toString();
  237.                             System.out.println("Se ha leido: ");
  238.                             System.out.println(everything);
  239.                             System.out.print("¿Es correcto?: (y/n): ");
  240.                             String t = br.readLine();
  241.                             if (t.equals("y")) {
  242.                                 for (int i = 0; i < (Utiles.countLines(everything) - 1); i++) {
  243.                                     String[] linea = everything.split("\n");
  244.                                     String palabricas = linea[i];
  245.                                     String[] palabras = palabricas.split(" ");
  246.  
  247.                                     if (palabras.length == 4) {
  248.                                         Usuario alta = new Usuario();
  249.                                         alta.setId(palabras[0]);
  250.                                         alta.setPass(palabras[1]);
  251.                                         alta.setSitio(palabras[2]);
  252.                                         if (palabras[3].contains("true")) {
  253.                                             alta.setVip(true);
  254.                                         } else {
  255.                                             alta.setVip(false);
  256.                                         }
  257.  
  258.                                         usuarios.add(alta);
  259.                                     } else {
  260.                                         System.out.print(
  261.                                                 "Formato incorrecto (Debe ser 4 palabras separadas por un espacio en blanco) ");
  262.                                         Thread.sleep(2000); // 1000 milliseconds
  263.                                                             // is one second.
  264.                                         System.out.print("Volviendo al menú principal...");
  265.                                         Thread.sleep(3000); // 1000 milliseconds
  266.                                                             // is one second.
  267.                                         util.limpia();
  268.  
  269.                                     }
  270.                                 }
  271.                             }
  272.  
  273.                         }
  274.                     }
  275.  
  276.                     else {
  277.                         System.out.print("Formato incorrecto de archivo (Debe ser .txt) ");
  278.                         Thread.sleep(1000); // 1000 milliseconds is one second.
  279.                         System.out.print("Volviendo al menú principal...");
  280.                         Thread.sleep(3000); // 1000 milliseconds is one second.
  281.                         util.limpia();
  282.                     }
  283.                 }
  284.             }
  285.  
  286.         } while (!opcion.equals(exit));
  287.  
  288.     }
  289.  
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement