Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.RandomAccessFile;
- public class DaoAula {
- final static int tamModelo = 20;
- final static int sizeNumOrd = Integer.SIZE/8;
- final static int sizeCapacidad = Integer.SIZE/8;
- final static int sizeModelo = tamModelo * Character.SIZE/8;
- final static int sizeHDD = Integer.SIZE/8;
- final static int sizeRAM = Integer.SIZE/8;
- final static int sizeOperativo = 1; //El tipo boolean siempre será 1.
- final static int SIZE = sizeNumOrd + sizeCapacidad + sizeModelo + sizeHDD + sizeRAM + sizeOperativo;
- public static void crearAula (String nombre, int capacidad) throws Exception{
- // Comprobamos si la carpeta 'Aulas' existe y si no la creamos.
- File carpeta = new File("aulas");
- if (!carpeta.exists())carpeta.mkdir();
- File f = new File (carpeta, nombre + ".aul");
- if (f.exists())throw new Exception ("El aula ya existe.");
- try (RandomAccessFile raf = new RandomAccessFile(f, "rw")){
- raf.writeInt(capacidad);
- for(int i=1; i<=capacidad+1; i++){
- int posicion = SIZE * (i-1);
- if (posicion == 0){
- raf.seek(sizeCapacidad);
- } else {
- raf.seek(SIZE * (i-1));
- }
- raf.writeInt(0);
- }
- }
- }
- public static void añadirOrdenador (String nombreAula, int numOrd, String modelo, int hdd, int ram, boolean operativo) throws Exception {
- File f = new File ("aulas", nombreAula + ".aul");
- if (!f.exists())throw new Exception ("El aula no existe.");
- try (RandomAccessFile raf = new RandomAccessFile(f, "rw")){
- raf.seek(0);
- int capacidad = raf.readInt();
- if (numOrd > capacidad) throw new Exception ("No caben tantos ordenadores.");
- if (numOrd == 1) {
- raf.seek(sizeCapacidad);
- } else {
- raf.seek(SIZE * (numOrd-1));
- if(raf.readInt() != 0) throw new Exception ("El ordenador ya existe.");
- raf.seek(SIZE * (numOrd-1));
- }
- raf.writeInt(numOrd);
- raf.writeUTF(modelo);
- raf.writeInt(hdd);
- raf.writeInt(ram);
- raf.writeBoolean(operativo);
- }
- }
- public static void mostrarDatosAula (String nombreAula) throws Exception{
- File f = new File("aulas", nombreAula + ".aul");
- try(RandomAccessFile raf = new RandomAccessFile(f, "rw")){
- raf.seek(0);
- int capacidad = raf.readInt();
- System.out.println("Capacidad: "+capacidad);
- for(int i=1; i<=capacidad; i++) {
- System.out.println("Ordenador: "+raf.readInt());
- System.out.println("Modelo: "+raf.readUTF());
- System.out.println("Hdd: "+raf.readInt());
- System.out.println("Ram: "+raf.readInt());
- System.out.println("Func: "+raf.readBoolean());
- raf.seek(SIZE*i);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement