Advertisement
Guest User

Sensor

a guest
Jun 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package sensor;
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. class SensorEx{
  6.     int identificator;
  7.     int valoare;
  8.     public String nume(){
  9.         return getClass().getSimpleName();
  10.     }
  11.    
  12.     int getCurrentSensorRead(){
  13.         return valoare;
  14.     }
  15.    
  16.     Date getTime(){
  17.         return new Date();
  18.     }
  19.    
  20.     SensorEx(int i, int v){
  21.         identificator = i;
  22.         valoare = v;
  23.     }
  24. }
  25.  
  26. class LocationSensor extends SensorEx {
  27.     LocationSensor(int i, int v){ super(i, v); }
  28. }
  29.  
  30. class HeartBeatSensor extends SensorEx {
  31.     HeartBeatSensor(int i, int v){ super(i, v); }
  32. }
  33.  
  34. class TemperatureSensor extends SensorEx {
  35.     TemperatureSensor(int i, int v){ super(i, v); }
  36. }
  37.  
  38. class Pacient {
  39.     String nume;
  40.     String prenume;
  41.     String cnp;
  42.    
  43.     Random rand = new Random();
  44.    
  45.     LocationSensor sensorLoc;
  46.     HeartBeatSensor sensorHb;
  47.     TemperatureSensor sensorTmp;
  48.    
  49.     Pacient(String n, String pn, String cnnp, int loc, int hb, int tmp){
  50.         nume = n;
  51.         prenume = pn;
  52.         cnp = cnnp;
  53.        
  54.         sensorLoc = new LocationSensor(rand.nextInt(100) + 100, loc);
  55.         sensorHb = new HeartBeatSensor(rand.nextInt(100) + 100, hb);
  56.         sensorTmp = new TemperatureSensor(rand.nextInt(100) + 100, tmp);
  57.     }
  58.    
  59.     public String toString(){
  60.         return nume + prenume + cnp + " " + sensorLoc.valoare + " " + sensorTmp.identificator;
  61.     }
  62. }
  63.  
  64. class CiusException extends Exception{
  65. }
  66.  
  67. public class Sensor {
  68.     public static void main(String[] args) {
  69.         Pacient p = new Pacient("Victor", "Razvan", "1234", 111, 222, 333);
  70.        
  71.         // seriializare
  72.         String filename = "salvare.ser";
  73.         try {
  74.             FileOutputStream file = new FileOutputStream(filename);
  75.             ObjectOutputStream out = new ObjectOutputStream(file);
  76.            
  77.             out.writeObject(p);
  78.            
  79.             out.close();
  80.             file.close();
  81.         } catch (IOException ex){
  82.        
  83.         }
  84.        
  85.         Pacient z = null;
  86.         // deserializare
  87.         try {
  88.             FileInputStream file = new FileInputStream(filename);
  89.             ObjectInputStream in = new ObjectInputStream(file);
  90.            
  91.             z = (Pacient)in.readObject();
  92.             if(z.sensorTmp.valoare>440) throw new CiusException();
  93.             System.out.println("ceva");
  94.             in.close();
  95.             file.close();
  96.         } catch (IOException ex){
  97.        
  98.         } catch (ClassNotFoundException ex){
  99.        
  100.         } catch (CiusException ex) {
  101.             System.out.println("pfai");
  102.         }
  103.  
  104.        
  105.         System.out.println(p);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement