Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.awt.Toolkit;
- import java.awt.datatransfer.Clipboard;
- import java.awt.datatransfer.StringSelection;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.Vector;
- import javax.swing.JFileChooser;
- public class Main {
- public static void main(String[] args) throws IOException, InterruptedException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
- String opcion = "0";
- String exit = "q";
- Vector<Usuario> usuarios = new Vector<Usuario>();
- Utiles util = new Utiles();
- int opcionuno = 0;
- Usuario user = new Usuario("FIGT1@ALU.UA.ES", "819575", "ua.ES", false);
- Usuario user2 = new Usuario("Mari", "Pass", "ee", true);
- usuarios.add(user);
- usuarios.add(user2);
- /*
- * Para leer String s = br.readLine();
- *
- * Para copiar al portapapeles StringSelection data = new
- * StringSelection("This text is going to be copied into the clipboard"
- * ); clipboard.setContents(data, data);
- *
- */
- do {
- opcion = "";
- System.out.println("Menú:");
- System.out.println("1) LISTAR USUARIOS");
- System.out.println("2) SELECCIONAR USUARIO");
- System.out.println("3) ADD USUARIO");
- System.out.println("4) GUARDAR USUARIOS");
- System.out.println("5) CARGAR USUARIOS");
- System.out.println("q) SALIR");
- System.out.print("Opcion: ");
- opcion = br.readLine();
- if (opcion.equals("1")) {
- opcionuno++;
- util.limpia();
- System.out.println("--- LISTA DE USUARIOS ---");
- System.out.println();
- System.out.println("i) ID DE USUARIO PASSWORD SITIO VIP");
- System.out.println();
- for (int i = 0; i < usuarios.size(); i++) {
- System.out.print((i + 1) + ") ");
- usuarios.elementAt(i).lista();
- }
- System.out.println();
- System.out.println("¿Limpiar pantalla? (y)es/(n)o");
- String siono = br.readLine();
- if (siono.equals("y"))
- util.limpia();
- else {
- util.espacio();
- }
- }
- if (opcion.equals("2")) {
- int fails = 0;
- if (opcionuno == 0) {
- opcionuno++;
- util.limpia();
- System.out.println("--- LISTA DE USUARIOS ---");
- System.out.println();
- System.out.println("i) ID DE USUARIO PASSWORD SITIO VIP");
- System.out.println();
- for (int i = 0; i < usuarios.size(); i++) {
- System.out.print((i + 1) + ") ");
- usuarios.elementAt(i).lista();
- }
- }
- System.out.print("Selecciona el usuario: ");
- String eleccion = br.readLine();
- // Comprobar que no se puede hacer el parse
- char[] charArray = eleccion.toCharArray();
- for (int i = 0; i < charArray.length; i++) {
- if (charArray[i] > 47 && charArray[i] < 58) {
- } else
- fails++;
- if (fails == 0) {
- int iusu = Integer.parseInt(eleccion);
- if (iusu <= 0 || iusu > usuarios.size()) {
- System.out.print("Opcion incorrecta.");
- Thread.sleep(3000); // 1000 milliseconds is one
- // second.
- util.limpia();
- } else {
- System.out.println("Se ha marcado la opción: " + usuarios.get(iusu - 1).getId() + " | "
- + usuarios.get(iusu - 1).getPass());
- System.out.print("¿Es correcto? (y/n): ");
- String siono = br.readLine();
- if (siono.equals("y")) {
- StringSelection data = new StringSelection(
- usuarios.get(iusu - 1).getId() + " " + usuarios.get(iusu - 1).getPass());
- clipboard.setContents(data, data);
- System.out.print("Se ha copiado al portapales.");
- Thread.sleep(1000); // 1000 milliseconds is one
- // second.
- System.out.print("Volviendo al menú principal...");
- Thread.sleep(3000); // 1000 milliseconds is one
- // second.
- util.limpia();
- } else {
- util.limpia();
- }
- }
- } else {
- System.out.print("Opcion incorrecta.");
- Thread.sleep(3000); // 1000 milliseconds is one second.
- util.limpia();
- }
- }
- }
- if (opcion.equals("3")) {
- Usuario alta = new Usuario();
- System.out.print("ID: ");
- alta.setId(br.readLine());
- System.out.print("PASSWORD: ");
- alta.setPass(br.readLine());
- System.out.print("Sitio: ");
- alta.setSitio(br.readLine());
- System.out.print("VIP(y/n): ");
- String n = br.readLine();
- if (n.equals("y")) {
- alta.setVip(true);
- }
- if (alta.getId().isEmpty() || alta.getPass().isEmpty() || alta.getSitio().isEmpty()) {
- System.out.print("La id, pass y sitio no pueden estar vacías. ");
- Thread.sleep(1000); // 1000 milliseconds is one second.
- System.out.print("Volviendo al menú principal...");
- Thread.sleep(3000); // 1000 milliseconds is one second.
- util.limpia();
- }
- else {
- System.out.print("Has introducido : ");
- alta.lista();
- System.out.print("¿Es correcto? (y/n): ");
- String t = br.readLine();
- if (t.equals("y")) {
- usuarios.addElement(alta);
- }
- }
- }
- if (opcion.equals("4")) {
- /*
- * System.out.print("Nombre del fichero: " ); String t=
- * br.readLine();
- */
- try {
- JFileChooser fileChooser = new JFileChooser();
- if (fileChooser.showSaveDialog(fileChooser) == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- // save to file
- PrintWriter writer = new PrintWriter(file + ".txt", "UTF-8");
- for (int i = 0; i < usuarios.size(); i++) {
- writer.println(usuarios.get(i).linea());
- }
- writer.close();
- System.out.print("Se ha guardado el archivo correctamente.");
- Thread.sleep(1000); // 1000 milliseconds is one second.
- System.out.print("Volviendo al menú principal...");
- Thread.sleep(3000); // 1000 milliseconds is one second.
- util.limpia();
- }
- } catch (IOException e) {
- System.out.print("No se ha podido guardar el archivo.");
- Thread.sleep(1000); // 1000 milliseconds is one second.
- System.out.print("Volviendo al menú principal...");
- Thread.sleep(3000); // 1000 milliseconds is one second.
- util.limpia();
- }
- }
- if (opcion.equals("5")) {
- JFileChooser fileChooser = new JFileChooser();
- if (fileChooser.showSaveDialog(fileChooser) == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- if (file.getName().contains(".txt")) {
- try (BufferedReader bX = new BufferedReader(new FileReader(file))) {
- StringBuilder sb = new StringBuilder();
- String line = bX.readLine();
- while (line != null) {
- sb.append(line);
- sb.append(System.lineSeparator());
- line = bX.readLine();
- }
- String everything = sb.toString();
- System.out.println("Se ha leido: ");
- System.out.println(everything);
- System.out.print("¿Es correcto?: (y/n): ");
- String t = br.readLine();
- if (t.equals("y")) {
- for (int i = 0; i < (Utiles.countLines(everything) - 1); i++) {
- String[] linea = everything.split("\n");
- String palabricas = linea[i];
- String[] palabras = palabricas.split(" ");
- if (palabras.length == 4) {
- Usuario alta = new Usuario();
- alta.setId(palabras[0]);
- alta.setPass(palabras[1]);
- alta.setSitio(palabras[2]);
- if (palabras[3].contains("true")) {
- alta.setVip(true);
- } else {
- alta.setVip(false);
- }
- usuarios.add(alta);
- } else {
- System.out.print(
- "Formato incorrecto (Debe ser 4 palabras separadas por un espacio en blanco) ");
- Thread.sleep(2000); // 1000 milliseconds
- // is one second.
- System.out.print("Volviendo al menú principal...");
- Thread.sleep(3000); // 1000 milliseconds
- // is one second.
- util.limpia();
- }
- }
- }
- }
- }
- else {
- System.out.print("Formato incorrecto de archivo (Debe ser .txt) ");
- Thread.sleep(1000); // 1000 milliseconds is one second.
- System.out.print("Volviendo al menú principal...");
- Thread.sleep(3000); // 1000 milliseconds is one second.
- util.limpia();
- }
- }
- }
- } while (!opcion.equals(exit));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement