public class Alumno {
// El public es un modificador de acceso
// Quiere decir que cualquiera puede acceder al valor
public String nombre;
// Tenemos tres sabores de protección
// public, protected y private
private ArrayList<Integer> notas;
// Los parámetros que queremos poner al crear la clase
public Alumno(String nombre) {
// Las cosas que queremos que se ejecuten
// cuando creamos la clase
// this.nombre es el nombre del objeto
// nombre es el parámetro que le pasamos
this.nombre = nombre;
notas = new ArrayList<Integer>();
}
public Alumno() {
// Llamando a la sobrecarga del constructor
this("anonimo");
}
public int ponNota(int nota) {
// Me aseguro de que mis notas están entre 0 10
if (nota > 0 && nota <= 10) {
notas.add(nota);
}
return notas.size();
}
public double media() {
double total = 0;
// Desde dentro de la clase si que puedo acceder
for (int nota : notas) {
total += nota;
}
return total / this.notas.size();
}
}
public static void main(String[] args) {
ArrayList<Alumno> alumnos=importar("archivo.txt");
System.out.println(estadoCarpeta("c:\\pepe"));
ArrayList<String> nombres = new ArrayList<>();
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.println("Introduce nombre " + i);
nombres.add(sc.next());
}
Path path = Paths.get("nombres.txt");
try {
Files.write(path, nombres, StandardOpenOption.CREATE);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static int estadoCarpeta(String ruta) {
Path path = Paths.get(ruta);
if (!Files.isDirectory(path)) {
return -1;
}
try {
List<Path> ficheros = Files.list(path).collect(Collectors.toList());
if (ficheros.size() == 0) {
return 0;
}
// TODO: Crear un método aparte
for (Path fichero : ficheros) {
if (Files.isDirectory(fichero)) {
return 2;
}
}
return 1;
} catch (IOException e) {
System.out.println(e.getMessage());
return -1;
}
}
public static ArrayList<Alumno> importar(String ruta) {
List<String> contenido;
// Leer fichero
Path path = Paths.get(ruta);
try {
contenido = Files.readAllLines(path);
} catch (Exception ex) {
System.out.println(ex.getMessage());
return null;
}
// Crear arraylist
/*
* Nombre;nota Ana;5;6;7 Pep;3;3 Eva;5;5
*/
ArrayList<Alumno> alumnos= new ArrayList<Alumno>();
for(int i=1;i<contenido.size();i++) {
String[] elementos=contenido.get(i).split(";");
Alumno temp=new Alumno(elementos[0]);
for(int j=1;j<elementos.length;j++) {
temp.ponNota(Integer.parseInt(elementos[j]));
}
alumnos.add(temp);
}
return alumnos;
}
public class Jugador {
private String nombre;
private ArrayList<Carta> mano;
public Jugador(String nombre) {
super();
this.nombre = nombre;
mano = new ArrayList<>();
}
protected String getNombre() {
return nombre;
}
protected void setNombre(String nombre) {
this.nombre = nombre;
}
protected ArrayList<Carta> getMano() {
return mano;
}
protected void setMano(ArrayList<Carta> mano) {
this.mano = mano;
}
protected void setMano(Carta carta) {
this.mano.add(carta);
}
@Override
public String toString() {
return "Jugador [nombre=" + nombre + ", mano=" + mano + "]";
}
public void logFichero(String ruta) {
ArrayList<String> resultado = new ArrayList<>();
// Un arraylist con los datos que quiero escribir
resultado.add("Nombre jugador: " + this.getNombre());
resultado.add("Cartas:");
for (Carta carta : mano) {
resultado.add(carta.toString());
}
// Escribir los datos
try {
Path path = Paths.get(ruta);
if (!Files.exists(path)) {
Files.createFile(path);
}
if (Files.isWritable(path)) {
Files.write(path, resultado, StandardOpenOption.TRUNCATE_EXISTING);
}else {
// TODO: Lanzar excepción
System.out.println("No se puede escribir");
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
Jugador ana=new Jugador("Ana Pi");
ana.setMano(new Espanyola("oros",12));
ana.setMano(new Espanyola("espadas",6));
ana.setMano(new Espanyola("copas",8));
ana.setMano(new Espanyola("oros",10));
ana.logFichero("cartas_ana.txt");
}