Last active
April 22, 2018 16:35
-
-
Save JuanjoSalvador/48ef55aabb500818d9e2c49ffc428dbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.Scanner; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class Agenda { | |
public static void AfegirContacte(File agenda, String contacte, String telefon){ | |
try { | |
// abre un nuevo flujo de escritura en el archivo indicado | |
PrintWriter pw = new PrintWriter(agenda); | |
// escribe los datos del contacto | |
pw.println("Contacte: " + contacte); | |
pw.println("Telefon: " + telefon); | |
// cierra el flujo | |
pw.close(); | |
} catch(IOException io) { | |
io.printStackTrace(); | |
} | |
} | |
public static void MostrarAgenda(File agenda) { | |
try { | |
// abre el archivo nuevo y lo guarda en un Buffer de Java | |
FileReader fr = new FileReader(agenda); | |
BufferedReader br = new BufferedReader(fr); | |
String s; | |
// lee todo el buffer línea a línea y va imprimiéndolas en pantalla | |
while((s = br.readLine()) != null) { | |
System.out.println(s); | |
} | |
// espacio en blanco para rellenar y que quede bonito | |
System.out.println(""); | |
fr.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
File agenda = new File("C:\\Users\\Feyza CM\\Desktop\\agenda.txt"); //arxiu | |
String contacte; | |
String telefon; | |
int opcio; | |
boolean sortir = false; | |
do { | |
System.out.println("1 - Afegir contacte"); | |
System.out.println("2 - Mostrar llistat"); | |
System.out.println("3 - Sortir"); | |
System.out.println("Escull una opcio: "); | |
opcio = sc.nextInt(); | |
switch (opcio) { | |
case 1: | |
System.out.println("Has escollit : Afegir contacte "); | |
System.out.print("Nom del contacte: "); | |
contacte = sc.next(); | |
System.out.print("Telefon del contacte: "); | |
telefon = sc.next(); | |
AfegirContacte(agenda, contacte, telefon); | |
break; | |
case 2: | |
System.out.println("Has escollit : Mostrar llistat"); | |
MostrarAgenda(agenda); | |
break; | |
case 3: | |
sortir = true; | |
break; | |
default: | |
System.out.println("S'ha introduit una opcio no valida,escull l'opcio de sortir"); | |
} | |
} while (!sortir); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment