diff --git a/bin/main/Main.class b/bin/main/Main.class index 0d1c8a7..71aa605 100644 Binary files a/bin/main/Main.class and b/bin/main/Main.class differ diff --git a/bin/main/Producto.class b/bin/main/Producto.class index 9dd06f5..8409030 100644 Binary files a/bin/main/Producto.class and b/bin/main/Producto.class differ diff --git a/resources/prueba.txt b/resources/prueba.txt index 366a4fe..3d423af 100644 --- a/resources/prueba.txt +++ b/resources/prueba.txt @@ -18,3 +18,12 @@ Mi iteración: 4: e [Nombre]: Ratón; [Precio]: 12.0; [Descripcion]: Gamer [Nombre]: Ordenador; [Precio]: 200.0; [Descripcion]: Workstation + +[Nombre]: qs; [Precio]: 32.0; [Descripcion]: qwe +[Availability]:true + +[Nombre]: ewq; [Precio]: 12.0; [Descripcion]: wqe +[Availability]:true + +[Nombre]: eqw; [Precio]: 12.0; [Descripcion]: qwe +[Availability]:true diff --git a/src/main/Main.java b/src/main/Main.java index 989b17b..2d0a28c 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -5,69 +5,74 @@ import java.util.Scanner; public class Main { - + /** * Leer un fichero y mostrarlo por consola - * */ + */ public static void leerFichero(File f) throws IOException { FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String miLinea = br.readLine(); - while(miLinea != null) { + while (miLinea != null) { System.out.println(miLinea); miLinea = br.readLine(); } fr.close(); - br.close(); + br.close(); } + /** * Escribir un fichero en un array 5 objetos de la clase productos. - * */ + */ public static void escribirFichero(File f) throws IOException { - //Se puede escribir en el fichero. - FileWriter fw = new FileWriter(f,true); + // Se puede escribir en el fichero. + FileWriter fw = new FileWriter(f, true); PrintWriter pw = new PrintWriter(fw); - Scanner sc = new Scanner(System.in); + ArrayList productos = new ArrayList<>(); - for(int i=0; i<5; i++) { - System.out.println("Mi iteración: "+i); + for (int i = 0; i < 3; i++) { + Scanner sc = new Scanner(System.in); + System.out.println("Mi iteración: " + i); System.out.print("Introduzca Nombre del producto: "); String nombreProducto = sc.next(); System.out.print("Introduzca Precio del producto: "); float precio = sc.nextFloat(); System.out.print("Introduzca Descripción del producto: "); String desc = sc.next(); - //Añado elementos al array - productos.add(new Producto(precio,nombreProducto,desc)); - //Escribo en el fichero la información de cada producto + // Añado elementos al array + productos.add(new Producto(precio, nombreProducto, desc, true)); + // Escribo en el fichero la información de cada producto pw.append(productos.get(i).toString()); } pw.close(); fw.close(); - + } + public static void main(String[] args) throws IOException { // TODO Auto-generated method stub - File f = new File(System.getProperty("user.dir")+File.separator+"resources/prueba.txt"); + File f = new File(System.getProperty("user.dir") + File.separator + "resources/prueba.txt"); File newFolder = new File("resources"); - //Comprobmos si existe el directorio y si no lo creamos - if(!newFolder.exists()){ + // Comprobmos si existe el directorio y si no lo creamos + if (!newFolder.exists()) { newFolder.mkdir(); } - //Ahora comprobamos si existe el archivo - if(!f.exists()) { + // Ahora comprobamos si existe el archivo + if (!f.exists()) { f.createNewFile(); } - - if(f.canRead()) { + + if (f.canRead()) { leerFichero(f); - }else if(f.canWrite()) { - escribirFichero(f); - }else { - System.out.println("NO SE PUEDE ESCRIBIR EN EL ARCHIVO"); + if (f.canWrite()) { + escribirFichero(f); + } else { + System.out.println("NO SE PUEDE ESCRIBIR EN EL ARCHIVO"); + } + } else { + System.out.println("NO SE PUEDE LEER EN EL ARCHIVO"); } - - + } } diff --git a/src/main/Producto.java b/src/main/Producto.java index 0de6226..11a5b52 100644 --- a/src/main/Producto.java +++ b/src/main/Producto.java @@ -4,13 +4,15 @@ public class Producto { private float precio; private String nombre; private String desc; + private boolean available; - public Producto(float precio, String nombre, String desc) { + public Producto(float precio, String nombre, String desc, boolean available) { super(); this.precio = precio; this.nombre = nombre; this.desc = desc; + this.available = available; } public float getPrecio() { return precio; @@ -30,7 +32,14 @@ public String getDesc() { public void setDesc(String desc) { this.desc = desc; } + + public boolean isAvailable() { + return available; + } + public void setAvailable(boolean available) { + this.available = available; + } public String toString() { - return "\n[Nombre]: "+this.nombre+"; "+"[Precio]: "+this.precio+"; "+"[Descripcion]: "+this.desc+"\n"; + return "\n[Nombre]: "+this.nombre+"; "+"[Precio]: "+this.precio+"; "+"[Descripcion]: "+this.desc+"\n" + "[Availability]:" + this.available+"\n"; } }