Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bin/main/Main.class
Binary file not shown.
Binary file modified bin/main/Producto.class
Binary file not shown.
9 changes: 9 additions & 0 deletions resources/prueba.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
59 changes: 32 additions & 27 deletions src/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Producto> 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");
}



}

}
13 changes: 11 additions & 2 deletions src/main/Producto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
}
}