Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sensor;
- import java.util.*;
- import java.io.*;
- class SensorEx{
- int identificator;
- int valoare;
- public String nume(){
- return getClass().getSimpleName();
- }
- int getCurrentSensorRead(){
- return valoare;
- }
- Date getTime(){
- return new Date();
- }
- SensorEx(int i, int v){
- identificator = i;
- valoare = v;
- }
- }
- class LocationSensor extends SensorEx {
- LocationSensor(int i, int v){ super(i, v); }
- }
- class HeartBeatSensor extends SensorEx {
- HeartBeatSensor(int i, int v){ super(i, v); }
- }
- class TemperatureSensor extends SensorEx {
- TemperatureSensor(int i, int v){ super(i, v); }
- }
- class Pacient {
- String nume;
- String prenume;
- String cnp;
- Random rand = new Random();
- LocationSensor sensorLoc;
- HeartBeatSensor sensorHb;
- TemperatureSensor sensorTmp;
- Pacient(String n, String pn, String cnnp, int loc, int hb, int tmp){
- nume = n;
- prenume = pn;
- cnp = cnnp;
- sensorLoc = new LocationSensor(rand.nextInt(100) + 100, loc);
- sensorHb = new HeartBeatSensor(rand.nextInt(100) + 100, hb);
- sensorTmp = new TemperatureSensor(rand.nextInt(100) + 100, tmp);
- }
- public String toString(){
- return nume + prenume + cnp + " " + sensorLoc.valoare + " " + sensorTmp.identificator;
- }
- }
- class CiusException extends Exception{
- }
- public class Sensor {
- public static void main(String[] args) {
- Pacient p = new Pacient("Victor", "Razvan", "1234", 111, 222, 333);
- // seriializare
- String filename = "salvare.ser";
- try {
- FileOutputStream file = new FileOutputStream(filename);
- ObjectOutputStream out = new ObjectOutputStream(file);
- out.writeObject(p);
- out.close();
- file.close();
- } catch (IOException ex){
- }
- Pacient z = null;
- // deserializare
- try {
- FileInputStream file = new FileInputStream(filename);
- ObjectInputStream in = new ObjectInputStream(file);
- z = (Pacient)in.readObject();
- if(z.sensorTmp.valoare>440) throw new CiusException();
- System.out.println("ceva");
- in.close();
- file.close();
- } catch (IOException ex){
- } catch (ClassNotFoundException ex){
- } catch (CiusException ex) {
- System.out.println("pfai");
- }
- System.out.println(p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement