Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package agenda;
- import java.util.Scanner;
- import java.io.*;
- import sun.security.action.OpenFileInputStreamAction;
- public class Agenda {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- boolean valido=false;
- int op=0,p=0;
- Contacto cont[] = new Contacto[2];
- String nume="abcdefghijklmnopqrstuvwxyz";
- //Crear fichero
- try {
- File fichero = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda.txt");
- if (fichero.exists()) {
- }
- FileWriter ficher = new FileWriter(fichero);
- BufferedWriter bw = new BufferedWriter(ficher);
- //fichero.write("Nombre:Apellidos:Telefono:Correo:2\r\n");
- // bw.newLine();
- ficher.close();
- }
- catch (Exception e) { }
- //Menu mientras opcion sea distinto de 7 = salir
- while (op!=7)
- {
- System.out.println("1-Donar Alta");
- System.out.println("2-Donar Baixa");
- System.out.println("3-Ordenar Llista A-Z");
- System.out.println("4-Modificar Registre");
- System.out.println("5-Llistat");
- System.out.println("6-Cerca Contacte");
- System.out.println("7-Sortir");
- op=reader.nextInt();
- switch(op){
- case 1:
- alta(reader,nume,cont);
- break;
- case 2:
- baixa(reader, cont);
- break;
- case 3:
- ordena();
- break;
- case 4:
- modifica(reader, cont, nume);
- break;
- case 5:
- llistat(cont);
- break;
- case 6:
- cerca(reader, cont);
- break;
- }
- }
- }
- //Funcion Alta
- public static boolean alta(Scanner reader,String nume,Contacto cont[])
- {
- String nombre,apellidos,correo,tel,id;
- String nueva="";
- int c = 0;
- char x=' ';
- boolean v_nombre=true,v_apellido=true,v_tel=true,v_correo=true;
- boolean encontrar =true,encontrar2=true,añadido=false;
- //Bucle mientras los datos no sean correctos
- while (añadido==false)
- {
- reader.nextLine();
- System.out.println("Introduce nombre: ");
- nombre=reader.nextLine();
- System.out.println("Introduce Apellidos: ");
- apellidos=reader.nextLine();
- System.out.println("Introduce Telefono: ");
- tel=reader.nextLine();
- System.out.println("Introduce e-mail: ");
- correo=reader.nextLine();
- System.out.println("Introduce ID: ");
- id=reader.nextLine();
- //Importando de Agenda.Comprobar.*
- v_nombre=agenda.Comprobar.comprobar_nom(nombre,nume,x,encontrar);
- v_apellido=agenda.Comprobar.comprobar_cognoms(apellidos,nume,x,encontrar2);
- v_tel=agenda.Comprobar.comprobar_tel(tel);
- v_correo=agenda.Comprobar.comprobar_correo(correo);
- //Validar si los datos son correctos
- if (v_nombre==true)
- {
- System.out.println("Nombre\t\t✔");
- }else {System.out.println("Nombre\t\t✘ ");}
- if (v_apellido==true) {
- System.out.println("Apellidos\t✔");
- }else {System.out.println("Apellidos\t✘");}
- if (v_tel==true) {
- System.out.println("Telefono\t✔");
- }else {System.out.println("Telefono\t✘");}
- if (v_correo==true) {
- System.out.println("Correo\t\t✔");
- }else {System.out.println("Correo\t\t✘");}
- //Si todo es correcto entonces añade los datos a la clase contacto
- if(v_nombre==true && v_apellido == true && v_correo==true && v_tel==true){
- cont[0]=new Contacto(nombre, apellidos, tel, correo, id);
- //Se añade contacto al fichero Agenda.txt ya existente
- try {
- FileWriter fichero = new FileWriter("Agenda.txt",true);
- BufferedWriter bw = new BufferedWriter(fichero);
- bw.write(cont[c].getNombre()+":"+cont[c].getApellidos()+":"+cont[c].getTelefono()+":"+cont[c].getCorreo()+":"+cont[c].getId()+"\r\n");
- bw.newLine();
- bw.close();
- fichero.close();
- }
- catch (Exception e) {
- }
- //Si añadido es true saldra del while
- añadido=true;
- }else {añadido=false;}
- }
- return añadido;// devuelve boolean añadido
- }
- public static void baixa(Scanner reader,Contacto cont[]){
- String id;
- reader.nextLine();
- System.out.println("Introduce ID a buscar:");
- id =reader.nextLine();
- try
- {
- File fichero = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda.txt");
- FileWriter ficher = new FileWriter(fichero,true);
- File fichero2 = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda2.txt");
- FileWriter ficher2 = new FileWriter(fichero2);
- //boolean renombrar = fichero2.renameTo(fichero);
- BufferedWriter bw = new BufferedWriter(ficher);
- BufferedWriter bw2 = new BufferedWriter(ficher2);
- FileReader read = new FileReader("Agenda.txt");
- BufferedReader bR = new BufferedReader(read);
- String texto="";
- String buffer;
- String borrar="";
- System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
- while ((buffer = bR.readLine())!=null)
- {
- String separador[]=buffer.split(":");
- bR.readLine();
- if(separador[4].equals(id)){
- borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
- }
- if (!buffer.equals(borrar)) {
- System.out.println(buffer);
- ficher2.write(buffer);
- bw.close();
- bw2.close();
- read.close();
- ficher2.close();
- ficher.close();
- if (fichero.delete()) {
- System.out.println("Eliminado");
- }else{System.out.println("No se puede");}
- boolean renombrar = fichero2.renameTo(fichero);
- fichero2.renameTo(fichero);
- if (renombrar) {
- System.out.println("Eliminado55");
- }else{System.out.println("No se puede55");}
- }
- }
- }
- catch (Exception e) { }
- }
- public static void ordena(){
- String contactos[][] = new String[5][5];
- try
- {
- FileReader read = new FileReader("Agenda.txt");
- BufferedReader bR = new BufferedReader(read);
- String buffer;
- System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
- while ((buffer = bR.readLine())!=null)
- {
- String separador[]=buffer.split(":");
- for (int i = 0; i <5; i++) {
- for (int j = 0; j <5; j++) {
- contactos[i][j]=separador[j];
- }
- }
- System.out.println();
- read.close();
- }
- }
- catch (Exception e) { }
- }
- public static void modifica(Scanner reader,Contacto cont[],String nume){
- String id;
- reader.nextLine();
- System.out.println("Introduce ID a buscar:");
- id =reader.nextLine();
- try
- {
- File fichero = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda.txt");
- FileWriter ficher = new FileWriter(fichero,true);
- File fichero2 = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda2.txt");
- FileWriter ficher2 = new FileWriter(fichero2);
- boolean renombrar = fichero2.renameTo(fichero);
- String camp;
- String nombre,apellido,id_n,telefono,correo;
- BufferedWriter bw = new BufferedWriter(ficher);
- BufferedWriter bw2 = new BufferedWriter(ficher2);
- FileReader read = new FileReader(fichero);
- BufferedReader bR = new BufferedReader(read);
- String texto="";
- String buffer;
- String borrar2="";
- String borrar="";
- String x="";
- System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
- while ((buffer = bR.readLine())!=null )
- {
- String separador[]=buffer.split(":");
- bR.readLine();
- if(separador[4].equals(id)){
- borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
- System.out.println(borrar);
- }
- System.out.println("Que campo quieres modificar?");
- camp=reader.nextLine();
- switch(camp){
- case "1":
- System.out.println("Introduce nombre: ");
- nombre=reader.nextLine();
- separador[0]=nombre;
- borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
- break;
- case "2":
- System.out.println("Introduce apellido: ");
- apellido=reader.nextLine();
- separador[1]=apellido;
- borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
- break;
- case "3":
- System.out.println("Introduce telefono: ");
- telefono=reader.nextLine();
- separador[2]=telefono;
- borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
- break;
- case "4":
- System.out.println("Introduce correo: ");
- correo=reader.nextLine();
- separador[3]=correo;
- borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
- break;
- case "5":
- System.out.println("Introduce ID: ");
- id_n=reader.nextLine();
- separador[4]=id_n;
- borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
- break;
- }
- System.out.println(borrar);
- if (!buffer.equals(borrar)) {
- x=borrar;
- //System.out.println(x);
- ficher2.write(buffer);
- //ficher2.write(borrar2);
- bR.close();
- bw2.close();
- ficher2.close();
- bw.close();
- read.close();
- ficher.close();
- }
- }
- if (fichero.delete()) {
- System.out.println("Eliminado");
- }else{System.out.println("No se puede");}
- fichero2.renameTo(fichero);
- if (renombrar) {
- System.out.println("Eliminado");
- }else{System.out.println("No se puede");}
- System.out.println(x+"esta es la linea");
- } catch (Exception e) { }
- }
- public static void llistat(Contacto cont[])
- {
- try
- {
- FileReader read = new FileReader("Agenda.txt");
- BufferedReader bR = new BufferedReader(read);
- String texto;
- System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
- while ((texto = bR.readLine())!=null)
- {
- System.out.println(texto);
- read.close();
- }
- }
- catch (Exception e) { }
- }
- public static void cerca(Scanner reader,Contacto cont[])
- {
- String id;
- reader.nextLine();
- System.out.println("Introduce ID a buscar:");
- id =reader.nextLine();
- try
- {
- FileReader read = new FileReader("Agenda.txt");
- BufferedReader bR = new BufferedReader(read);
- String texto="";
- String buffer;
- System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
- while ((buffer = bR.readLine())!=null)
- {
- String separador[]=buffer.split(":");
- bR.readLine();
- if(separador[4].equals(id)){
- System.out.println(separador[0]+" "+separador[1]+" "+separador[2]+" "+separador[3]+" "+separador[4]);
- }
- }
- }
- catch (Exception e) { }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement