Advertisement
dinhkk

Java for beginner - simple alarm clock

Sep 16th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package Thread;
  2.  
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5.  
  6. /**
  7.  *
  8.  * @author LYCOCO
  9.  */
  10. public class Clock implements Runnable{
  11.     private int hour;
  12.     private int minute;
  13.     private int second;
  14.     private int alarmH;
  15.     private int alarmM;
  16.     private int alarmS;
  17.    
  18.     Clock(int hour,int minite,int second){
  19.         this.hour = hour;
  20.         this.minute = minite;
  21.         this.second = second;
  22.     }
  23.    
  24.     public void  run(){
  25.         while(true){
  26.             second ++;
  27.             if(second == 60){
  28.                 minute++;
  29.                 second = 0;
  30.             }
  31.             if(minute == 60){
  32.                 hour++;
  33.                 minute = 0;
  34.             }
  35.             if(hour == 24){
  36.                 hour = 0;
  37.             }
  38.             System.out.println(this);
  39.        
  40.             try{
  41.                 Thread.sleep(1000);
  42.                 this.runAlarm();
  43.             } catch(InterruptedException ex){
  44.                 ex.printStackTrace();
  45.             }
  46.         }
  47.          
  48.     }
  49.    
  50.     @Override
  51.     public String toString(){
  52.         return "Hour:" + this.hour + " Minute:" + this.minute + " Second:" + this.second;
  53.     }
  54.     public void setAlarm(int hour,int minite,int second){
  55.         this.alarmH = hour;
  56.         this.alarmM = minite;
  57.         this.alarmS = second;
  58.     }
  59.    
  60.    
  61.     public void runAlarm(){
  62.         if(this.hour == this.alarmH && this.minute == this.alarmM && this.second == this.alarmS){  
  63.             for(int i = 0;i<10;i++){
  64.                 System.out.println("GET UP !");
  65.             }
  66.         }
  67.     }
  68.    
  69.     public static void main(String args[]) {
  70.       Clock a = new Clock(23,59,57);
  71.       Thread t = new Thread(a);
  72.       t.start();
  73.       a.setAlarm(0, 0, 0);
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement