Advertisement
Guest User

Untitled

a guest
May 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.45 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package agenda;
  7.  
  8. import java.util.Scanner;
  9. import java.io.*;
  10. import sun.security.action.OpenFileInputStreamAction;
  11.  
  12.  
  13. public class Agenda {
  14.  
  15. /**
  16. * @param args the command line arguments
  17. */
  18. public static void main(String[] args) {
  19. Scanner reader = new Scanner(System.in);
  20.  
  21. boolean valido=false;
  22. int op=0,p=0;
  23.  
  24. Contacto cont[] = new Contacto[2];
  25.  
  26.  
  27.  
  28. String nume="abcdefghijklmnopqrstuvwxyz";
  29.  
  30.  
  31. //Crear fichero
  32. try {
  33. File fichero = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda.txt");
  34. if (fichero.exists()) {
  35.  
  36. }
  37. FileWriter ficher = new FileWriter(fichero);
  38. BufferedWriter bw = new BufferedWriter(ficher);
  39. //fichero.write("Nombre:Apellidos:Telefono:Correo:2\r\n");
  40. // bw.newLine();
  41.  
  42. ficher.close();
  43. }
  44. catch (Exception e) { }
  45.  
  46.  
  47.  
  48.  
  49. //Menu mientras opcion sea distinto de 7 = salir
  50. while (op!=7)
  51. {
  52.  
  53.  
  54.  
  55.  
  56. System.out.println("1-Donar Alta");
  57. System.out.println("2-Donar Baixa");
  58. System.out.println("3-Ordenar Llista A-Z");
  59. System.out.println("4-Modificar Registre");
  60. System.out.println("5-Llistat");
  61. System.out.println("6-Cerca Contacte");
  62. System.out.println("7-Sortir");
  63. op=reader.nextInt();
  64.  
  65. switch(op){
  66.  
  67. case 1:
  68. alta(reader,nume,cont);
  69. break;
  70.  
  71. case 2:
  72. baixa(reader, cont);
  73. break;
  74.  
  75.  
  76. case 3:
  77. ordena();
  78. break;
  79.  
  80.  
  81. case 4:
  82. modifica(reader, cont, nume);
  83. break;
  84.  
  85.  
  86. case 5:
  87. llistat(cont);
  88. break;
  89.  
  90.  
  91. case 6:
  92. cerca(reader, cont);
  93. break;
  94.  
  95.  
  96. }
  97.  
  98.  
  99. }
  100. }
  101.  
  102. //Funcion Alta
  103. public static boolean alta(Scanner reader,String nume,Contacto cont[])
  104. {
  105.  
  106.  
  107. String nombre,apellidos,correo,tel,id;
  108. String nueva="";
  109. int c = 0;
  110. char x=' ';
  111. boolean v_nombre=true,v_apellido=true,v_tel=true,v_correo=true;
  112. boolean encontrar =true,encontrar2=true,añadido=false;
  113.  
  114.  
  115. //Bucle mientras los datos no sean correctos
  116. while (añadido==false)
  117. {
  118.  
  119.  
  120. reader.nextLine();
  121.  
  122. System.out.println("Introduce nombre: ");
  123. nombre=reader.nextLine();
  124.  
  125. System.out.println("Introduce Apellidos: ");
  126. apellidos=reader.nextLine();
  127. System.out.println("Introduce Telefono: ");
  128. tel=reader.nextLine();
  129.  
  130. System.out.println("Introduce e-mail: ");
  131. correo=reader.nextLine();
  132.  
  133. System.out.println("Introduce ID: ");
  134. id=reader.nextLine();
  135.  
  136.  
  137. //Importando de Agenda.Comprobar.*
  138. v_nombre=agenda.Comprobar.comprobar_nom(nombre,nume,x,encontrar);
  139. v_apellido=agenda.Comprobar.comprobar_cognoms(apellidos,nume,x,encontrar2);
  140. v_tel=agenda.Comprobar.comprobar_tel(tel);
  141. v_correo=agenda.Comprobar.comprobar_correo(correo);
  142.  
  143. //Validar si los datos son correctos
  144. if (v_nombre==true)
  145. {
  146.  
  147. System.out.println("Nombre\t\t✔");
  148.  
  149. }else {System.out.println("Nombre\t\t✘ ");}
  150.  
  151.  
  152.  
  153.  
  154. if (v_apellido==true) {
  155. System.out.println("Apellidos\t✔");
  156. }else {System.out.println("Apellidos\t✘");}
  157.  
  158. if (v_tel==true) {
  159. System.out.println("Telefono\t✔");
  160. }else {System.out.println("Telefono\t✘");}
  161.  
  162.  
  163.  
  164. if (v_correo==true) {
  165. System.out.println("Correo\t\t✔");
  166. }else {System.out.println("Correo\t\t✘");}
  167.  
  168. //Si todo es correcto entonces añade los datos a la clase contacto
  169. if(v_nombre==true && v_apellido == true && v_correo==true && v_tel==true){
  170.  
  171. cont[0]=new Contacto(nombre, apellidos, tel, correo, id);
  172.  
  173.  
  174.  
  175.  
  176.  
  177. //Se añade contacto al fichero Agenda.txt ya existente
  178. try {
  179.  
  180. FileWriter fichero = new FileWriter("Agenda.txt",true);
  181. BufferedWriter bw = new BufferedWriter(fichero);
  182. bw.write(cont[c].getNombre()+":"+cont[c].getApellidos()+":"+cont[c].getTelefono()+":"+cont[c].getCorreo()+":"+cont[c].getId()+"\r\n");
  183. bw.newLine();
  184.  
  185.  
  186. bw.close();
  187. fichero.close();
  188.  
  189.  
  190.  
  191. }
  192. catch (Exception e) {
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199. //Si añadido es true saldra del while
  200. añadido=true;
  201. }else {añadido=false;}
  202. }
  203. return añadido;// devuelve boolean añadido
  204. }
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213. public static void baixa(Scanner reader,Contacto cont[]){
  214. String id;
  215. reader.nextLine();
  216. System.out.println("Introduce ID a buscar:");
  217. id =reader.nextLine();
  218.  
  219. try
  220. {
  221.  
  222. File fichero = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda.txt");
  223. FileWriter ficher = new FileWriter(fichero,true);
  224. File fichero2 = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda2.txt");
  225. FileWriter ficher2 = new FileWriter(fichero2);
  226. //boolean renombrar = fichero2.renameTo(fichero);
  227.  
  228. BufferedWriter bw = new BufferedWriter(ficher);
  229. BufferedWriter bw2 = new BufferedWriter(ficher2);
  230. FileReader read = new FileReader("Agenda.txt");
  231. BufferedReader bR = new BufferedReader(read);
  232. String texto="";
  233. String buffer;
  234. String borrar="";
  235. System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
  236. while ((buffer = bR.readLine())!=null)
  237. {
  238.  
  239. String separador[]=buffer.split(":");
  240. bR.readLine();
  241. if(separador[4].equals(id)){
  242.  
  243. borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
  244.  
  245. }
  246. if (!buffer.equals(borrar)) {
  247. System.out.println(buffer);
  248. ficher2.write(buffer);
  249.  
  250.  
  251.  
  252. bw.close();
  253. bw2.close();
  254. read.close();
  255. ficher2.close();
  256. ficher.close();
  257.  
  258.  
  259.  
  260.  
  261. if (fichero.delete()) {
  262. System.out.println("Eliminado");
  263. }else{System.out.println("No se puede");}
  264. boolean renombrar = fichero2.renameTo(fichero);
  265. fichero2.renameTo(fichero);
  266. if (renombrar) {
  267. System.out.println("Eliminado55");
  268. }else{System.out.println("No se puede55");}
  269. }
  270.  
  271.  
  272.  
  273.  
  274.  
  275. }
  276.  
  277. }
  278. catch (Exception e) { }
  279. }
  280. public static void ordena(){
  281. String contactos[][] = new String[5][5];
  282. try
  283. {
  284.  
  285. FileReader read = new FileReader("Agenda.txt");
  286. BufferedReader bR = new BufferedReader(read);
  287. String buffer;
  288. System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
  289.  
  290. while ((buffer = bR.readLine())!=null)
  291. {
  292. String separador[]=buffer.split(":");
  293. for (int i = 0; i <5; i++) {
  294. for (int j = 0; j <5; j++) {
  295. contactos[i][j]=separador[j];
  296. }
  297. }
  298. System.out.println();
  299. read.close();
  300. }
  301.  
  302.  
  303.  
  304. }
  305. catch (Exception e) { }
  306.  
  307.  
  308. }
  309. public static void modifica(Scanner reader,Contacto cont[],String nume){
  310.  
  311. String id;
  312. reader.nextLine();
  313. System.out.println("Introduce ID a buscar:");
  314. id =reader.nextLine();
  315.  
  316. try
  317. {
  318.  
  319. File fichero = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda.txt");
  320. FileWriter ficher = new FileWriter(fichero,true);
  321. File fichero2 = new File("C:\\Users\\carlo\\OneDrive\\Documentos\\NetBeansProjects\\Agenda\\Agenda2.txt");
  322. FileWriter ficher2 = new FileWriter(fichero2);
  323. boolean renombrar = fichero2.renameTo(fichero);
  324. String camp;
  325. String nombre,apellido,id_n,telefono,correo;
  326. BufferedWriter bw = new BufferedWriter(ficher);
  327. BufferedWriter bw2 = new BufferedWriter(ficher2);
  328. FileReader read = new FileReader(fichero);
  329. BufferedReader bR = new BufferedReader(read);
  330. String texto="";
  331. String buffer;
  332. String borrar2="";
  333. String borrar="";
  334.  
  335. String x="";
  336. System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
  337. while ((buffer = bR.readLine())!=null )
  338. {
  339.  
  340. String separador[]=buffer.split(":");
  341. bR.readLine();
  342.  
  343. if(separador[4].equals(id)){
  344.  
  345. borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
  346. System.out.println(borrar);
  347. }
  348.  
  349. System.out.println("Que campo quieres modificar?");
  350. camp=reader.nextLine();
  351. switch(camp){
  352.  
  353.  
  354. case "1":
  355. System.out.println("Introduce nombre: ");
  356. nombre=reader.nextLine();
  357. separador[0]=nombre;
  358. borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
  359. break;
  360.  
  361. case "2":
  362. System.out.println("Introduce apellido: ");
  363. apellido=reader.nextLine();
  364. separador[1]=apellido;
  365. borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
  366. break;
  367.  
  368. case "3":
  369. System.out.println("Introduce telefono: ");
  370. telefono=reader.nextLine();
  371. separador[2]=telefono;
  372. borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
  373. break;
  374.  
  375. case "4":
  376. System.out.println("Introduce correo: ");
  377. correo=reader.nextLine();
  378. separador[3]=correo;
  379. borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
  380. break;
  381.  
  382. case "5":
  383. System.out.println("Introduce ID: ");
  384. id_n=reader.nextLine();
  385. separador[4]=id_n;
  386. borrar=separador[0]+":"+separador[1]+":"+separador[2]+":"+separador[3]+":"+separador[4];
  387.  
  388. break;
  389.  
  390. }
  391.  
  392. System.out.println(borrar);
  393. if (!buffer.equals(borrar)) {
  394. x=borrar;
  395. //System.out.println(x);
  396. ficher2.write(buffer);
  397. //ficher2.write(borrar2);
  398.  
  399. bR.close();
  400. bw2.close();
  401. ficher2.close();
  402. bw.close();
  403. read.close();
  404. ficher.close();
  405.  
  406.  
  407.  
  408.  
  409.  
  410. }
  411. }
  412. if (fichero.delete()) {
  413. System.out.println("Eliminado");
  414. }else{System.out.println("No se puede");}
  415. fichero2.renameTo(fichero);
  416.  
  417. if (renombrar) {
  418. System.out.println("Eliminado");
  419. }else{System.out.println("No se puede");}
  420.  
  421. System.out.println(x+"esta es la linea");
  422.  
  423.  
  424. } catch (Exception e) { }
  425. }
  426.  
  427. public static void llistat(Contacto cont[])
  428. {
  429.  
  430. try
  431. {
  432.  
  433. FileReader read = new FileReader("Agenda.txt");
  434. BufferedReader bR = new BufferedReader(read);
  435. String texto;
  436. System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
  437.  
  438. while ((texto = bR.readLine())!=null)
  439. {
  440. System.out.println(texto);
  441. read.close();
  442. }
  443.  
  444.  
  445.  
  446. }
  447. catch (Exception e) { }
  448. }
  449.  
  450.  
  451. public static void cerca(Scanner reader,Contacto cont[])
  452. {
  453. String id;
  454. reader.nextLine();
  455. System.out.println("Introduce ID a buscar:");
  456. id =reader.nextLine();
  457.  
  458. try
  459. {
  460.  
  461. FileReader read = new FileReader("Agenda.txt");
  462. BufferedReader bR = new BufferedReader(read);
  463. String texto="";
  464. String buffer;
  465.  
  466. System.out.println("Nombre:Apellidos:Telefono:Correo:ID");
  467. while ((buffer = bR.readLine())!=null)
  468. {
  469.  
  470. String separador[]=buffer.split(":");
  471. bR.readLine();
  472. if(separador[4].equals(id)){
  473.  
  474. System.out.println(separador[0]+" "+separador[1]+" "+separador[2]+" "+separador[3]+" "+separador[4]);
  475. }
  476. }
  477.  
  478. }
  479. catch (Exception e) { }
  480. }
  481.  
  482.  
  483.  
  484. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement