package lab10out; import java.io.*; import java.util.*; public class RlstStream implements Serializable { private String fn; private int mode; private FileInputStream fis; private FileOutputStream fos; private ObjectInputStream ois; private ObjectOutputStream oos; private int no_records = 0; public RlstStream(String input,int mode) { //Set the filename fn = input; //Set the mode this.mode = mode; //Open file appropriately if(mode == 0) { try { fis = new FileInputStream(fn); ois = new ObjectInputStream(fis); } catch(IOException io) { io.printStackTrace(); } } else if(mode == 1) { try { fos = new FileOutputStream(fn); oos = new ObjectOutputStream(fos); } catch(IOException io) { io.printStackTrace(); } } } public RlstRecord readRecord(int index) { try { ois.close(); fis = new FileInputStream(fn); ois = new ObjectInputStream(fis); } catch(IOException io) { io.printStackTrace(); } int count = 0; RlstRecord record = new RlstRecord(); while (count != (index + 1)) { try { record = (RlstRecord) ois.readObject(); } catch(ClassNotFoundException cnf) { cnf.printStackTrace(); } catch(IOException io) { io.printStackTrace(); } count++; } return record; } public void write(RlstRecord rr) { try { oos.writeObject(rr); } catch(IOException io) { io.printStackTrace(); } } public String getName(int index) { RlstRecord record = new RlstRecord(); record = readRecord(index); String return_val = new String(record.getName()); return return_val; } public String[] allNames() { try { ois.close(); fis = new FileInputStream(fn); ois = new ObjectInputStream(fis); } catch(IOException io) { io.printStackTrace(); } int count = 0; String[] name = new String[100]; RlstRecord record = new RlstRecord(); while(true) { try { record = (RlstRecord)ois.readObject(); } catch(ClassNotFoundException cnf) { cnf.printStackTrace(); } catch(IOException io) { //io.printStackTrace(); break; } name[count] = new String(record.getName()); count++; } String[] array = new String[count]; for(int i = 0; i < count; i++) { array[i] = name[i]; } return array; } public String getAddress(int index) { RlstRecord record = new RlstRecord(); record = readRecord(index); String addr = new String(record.getAddress()); return addr; } public String[] allAddresses() { try { ois.close(); fis = new FileInputStream(fn); ois = new ObjectInputStream(fis); } catch(IOException io) { io.printStackTrace(); } int count = 0; String[] addr = new String[100]; RlstRecord record = new RlstRecord(); while(true) { try { record = (RlstRecord)ois.readObject(); } catch(ClassNotFoundException cnf) { cnf.printStackTrace(); } catch(IOException io) { //io.printStackTrace(); break; } addr[count] = new String(record.getAddress()); count++; } String[] array = new String[count]; for(int i = 0; i < count; i++) { array[i] = addr[i]; } return array; } public int getArea(int index) { RlstRecord record = new RlstRecord(); record = readRecord(index); int area = record.getArea(); return area; } public int[] allAreas() { try { ois.close(); fis = new FileInputStream(fn); ois = new ObjectInputStream(fis); } catch(IOException io) { io.printStackTrace(); } int count = 0; int[] area = new int[100]; RlstRecord record = new RlstRecord(); while(true) { try { record = (RlstRecord)ois.readObject(); } catch(ClassNotFoundException cnf) { cnf.printStackTrace(); } catch(IOException io) { //io.printStackTrace(); break; } area[count] = record.getArea(); count++; } int[] array = new int[count]; for(int i = 0; i < count; i++) { array[i] = area[i]; } return array; } public double getPrice(int index) { RlstRecord record = new RlstRecord(); record = readRecord(index); return record.getPrice(); } public double[] allPrice() { try { ois.close(); fis = new FileInputStream(fn); ois = new ObjectInputStream(fis); } catch(IOException io) { io.printStackTrace(); } int count = 0; double[] price = new double[100]; RlstRecord record = new RlstRecord(); while(true) { try { record = (RlstRecord)ois.readObject(); } catch(ClassNotFoundException cnf) { cnf.printStackTrace(); } catch(IOException io) { //io.printStackTrace(); break; } price[count] = record.getPrice(); count++; } double[] array = new double[count]; for(int i = 0; i < count; i++) { array[i] = price[i]; } return array; } public int getId(int index) { RlstRecord record = new RlstRecord(); record = readRecord(index); int id = record.getId(); return id; } public int[] allId() { try { ois.close(); fis = new FileInputStream(fn); ois = new ObjectInputStream(fis); } catch(IOException io) { io.printStackTrace(); } int count = 0; int[] id = new int[100]; RlstRecord record = new RlstRecord(); while(true) { try { record = (RlstRecord)ois.readObject(); } catch(ClassNotFoundException cnf) { cnf.printStackTrace(); } catch(IOException io) { //io.printStackTrace(); break; } id[count] = record.getId(); count++; } int[] array = new int[count]; for(int i = 0; i < count; i++) { array[i] = id[i]; } return array; } public Realtor getRealtor(int index) { RlstRecord record = new RlstRecord(); record = readRecord(index); return record.getRealtor(); } public void close() { if(mode == 0) { try { fis.close(); ois.close(); } catch(IOException io) { io.printStackTrace(); } } else if(mode == 1) { try { fos.close(); oos.close(); } catch(IOException io) { io.printStackTrace(); } } } public RlstStream() { } public static void main(String[] args) { RlstStream rlststream = new RlstStream(); } }