/*
   Chapter 10:   CoAlarm
   Programmer:   Brad Shedd
   Date:         May 6, 2004
   Program Name: CoAlarm.java
*/


import java.lang.Math;
import java.text.DecimalFormat;

public class CoAlarm extends SecurityAlarm
{
  
final double MAX_CO_THRESHOLD = 200.0;
  
final double CO_FAIL_THRESHOLD = 40.0;

  
double COThreshold = 0.0;
  
double COSensor = 0.0;
   String location;

  
public COAlarm(String loc)
   {
      location =
new String(loc);
      COThreshold = MAX_CO_THRESHOLD;
   }

  
public COAlarm(String loc, double threshold)
   {
      location =
new String(loc);
     
if(threshold < MAX_CO_THRESHOLD)
         COThreshold = MAX_CO_THRESHOLD;
     
else
         COThreshold = threshold;
   }

  
public int sensorSample()
   {
     
int status = NO_ALARM;
      DecimalFormat twoDigits =
new DecimalFormat("##,##0.0");

      COSensor = Math.random()*1000; 
// simulate getting a temperature reading

      if(COSensor > tempThreshold)
      {
          alarm();
          status = ALARM_ACTIVATED;
      }
     
else
         if(COSensor < CO_FAIL_THRESHOLD)
         {
            alarmFailure(
"\nThe "+location
                         +
" fire alarm senses a temperature of "
                         +twoDigits.format(COSensor)
                         +
"\nwhich is below the failure threshold of "+CO_FAIL_THRESHOLD);
            status = ALARM_FAILURE;
          }

     
return status;
   }

  
public void alarm()
   {
      DecimalFormat twoDigits =
new DecimalFormat("##,##0.0");

      System.out.println(
"*** The "+location+" fire alarm sensor has detected a potential fire.");
      System.out.println(
"*** The CO level of "+twoDigits.format(COSensor)+" has exceeded the threshold of "+COThreshold+"\n");
   }
}

Homepage