Advertisement
Guest User

DAO

a guest
Oct 23rd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.RandomAccessFile;
  3.  
  4.  
  5. public class DaoAula {
  6.    
  7.     final static int tamModelo = 20;
  8.    
  9.     final static int sizeNumOrd = Integer.SIZE/8;
  10.     final static int sizeCapacidad = Integer.SIZE/8;
  11.     final static int sizeModelo = tamModelo * Character.SIZE/8;
  12.     final static int sizeHDD = Integer.SIZE/8;
  13.     final static int sizeRAM = Integer.SIZE/8;
  14.     final static int sizeOperativo = 1; //El tipo boolean siempre será 1.
  15.    
  16.     final static int SIZE = sizeNumOrd + sizeCapacidad + sizeModelo + sizeHDD + sizeRAM + sizeOperativo;
  17.    
  18.     public static void crearAula (String nombre, int capacidad) throws Exception{
  19.         // Comprobamos si la carpeta 'Aulas' existe y si no la creamos.
  20.        
  21.         File carpeta = new File("aulas");
  22.         if (!carpeta.exists())carpeta.mkdir();
  23.        
  24.         File f = new File (carpeta, nombre + ".aul");
  25.         if (f.exists())throw new Exception ("El aula ya existe.");
  26.            
  27.         try (RandomAccessFile raf = new RandomAccessFile(f, "rw")){
  28.             raf.writeInt(capacidad);
  29.             for(int i=1; i<=capacidad+1; i++){
  30.                 int posicion = SIZE * (i-1);
  31.                 if (posicion == 0){
  32.                     raf.seek(sizeCapacidad);
  33.                 } else {
  34.                     raf.seek(SIZE * (i-1));
  35.                 }
  36.                 raf.writeInt(0);
  37.             }
  38.         }
  39.     }
  40.    
  41.    
  42.     public static void añadirOrdenador (String nombreAula, int numOrd, String modelo, int hdd, int ram, boolean operativo) throws Exception {
  43.        
  44.         File f = new File ("aulas", nombreAula + ".aul");
  45.         if (!f.exists())throw new Exception ("El aula no existe.");
  46.         try (RandomAccessFile raf = new RandomAccessFile(f, "rw")){
  47.            
  48.             raf.seek(0);
  49.             int capacidad = raf.readInt();
  50.             if (numOrd > capacidad) throw new Exception ("No caben tantos ordenadores.");
  51.            
  52.             if (numOrd == 1) {
  53.                 raf.seek(sizeCapacidad);
  54.             } else {
  55.                 raf.seek(SIZE * (numOrd-1));
  56.                 if(raf.readInt() != 0) throw new Exception ("El ordenador ya existe.");
  57.                 raf.seek(SIZE * (numOrd-1));
  58.             }  
  59.             raf.writeInt(numOrd);
  60.             raf.writeUTF(modelo);
  61.             raf.writeInt(hdd);
  62.             raf.writeInt(ram);
  63.             raf.writeBoolean(operativo);   
  64.         }
  65.     }
  66.    
  67.      public static void mostrarDatosAula (String nombreAula) throws Exception{
  68.           File f = new File("aulas", nombreAula + ".aul");
  69.          
  70.           try(RandomAccessFile raf = new RandomAccessFile(f, "rw")){
  71.              
  72.               raf.seek(0);
  73.               int capacidad = raf.readInt();
  74.               System.out.println("Capacidad: "+capacidad);
  75.              
  76.               for(int i=1; i<=capacidad; i++) {
  77.                   System.out.println("Ordenador: "+raf.readInt());
  78.                   System.out.println("Modelo: "+raf.readUTF());
  79.                   System.out.println("Hdd: "+raf.readInt());
  80.                   System.out.println("Ram: "+raf.readInt());
  81.                   System.out.println("Func: "+raf.readBoolean());
  82.                   raf.seek(SIZE*i);
  83.               }
  84.              
  85.          }
  86.  
  87.      }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement