IT_Programming/Java

시간이 감소하는 타이머

JJun ™ 2007. 2. 6. 09:58

class MyTimerTest2
{
      public static void main(String[] args)
      {
            // 타이머가 1분 후에 멈추도록 설정한다.
            MyTimer mt = new MyTimer(0, 1, 0);
            mt.start();
      }
}

class MyTimer

{
      MyTime time;
      MyTimer(int hour, int min, int sec)

      {
            time = new MyTime(hour, min, sec);
      }
      String getTime()

      {
            return time.toString();
      }

      void start()

      { 
          while(true)

          {
                  try

                  {
                        Thread.sleep(1000);             // 1초동안 수행을 멈춘다.
                   } catch(Exception e) {}

                  System.out.println(time);
                  decSecond(); 
          }
      }

      // 시간을 감소시키는 함수
      void decHour()

      {
            if(time.getHour() > 0)

            {
                  time.setHour(time.getHour()-1);
                  time.setMinute(59);
                  time.setSecond(59);
            }

            else

            {
                  stop();            
            }

      }

      // 분을 감소시키는 함수
      void decMinute()

      {
            if(time.getMinute() > 0)

            {
                  time.setMinute(time.getMinute()-1);
                  time.setSecond(59);
            }

            else

            {
                  // 분이 0이라서 감소시킬수 없다면 시간을 감소시킨다.
                  decHour();
            } 
      }

      // 초를 감소시키는 함수
      void decSecond()

      {
            if(time.getSecond() > 0)

            {
                  time.setSecond(time.getSecond()-1);
            }

            else

           {
                  // 초가 0이라서 감소시킬수 없다면 분을 감소시킨다.
                  decMinute();
            }
      }

      void reset()

      {
            // time = new MyTime();
            time.setHour(0);
            time.setMinute(0);
            time.setSecond(0);
      }

      void stop()

      {
            System.out.println("Time out");
            System.exit(0);
      }
}


class MyTime

{
      int hour=0;
      int minute=0;
      int second=0;

      MyTime()

      {
            this(0,0,0);
      }

      MyTime(int hour, int minute, int second)

      {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
      }

      int getHour() { return hour; }
      int getMinute() { return minute; }
      int getSecond() { return second; }

      void setHour(int hour)

      {
            if(hour >= 0)

            {
                  this.hour = hour;
            }      
      }
      void setMinute(int minute)

      {
            if(minute >= 0 || minute < 60)

            {
                  this.minute = minute;
            }      
      }
      void setSecond(int second)

      {
            if(second >= 0 || second < 60)

            {
                  this.second = second;
            }            
      }

      public String toString()

      {
            String tmp = "";
            tmp += (hour < 10)? "0"+hour : ""+ hour;
            tmp += ":";
            tmp += (minute < 10)? "0"+minute : ""+ minute;
            tmp += ":";
            tmp += (second < 10)? "0"+second : ""+ second;

            return tmp;
      }
}

 
/*
---------- java ----------
00:01:00
00:00:59
00:00:58
00:00:57
00:00:56
00:00:55
00:00:54
00:00:53
00:00:52
00:00:51
00:00:50
00:00:49
00:00:48
00:00:47
00:00:46
00:00:45
00:00:44
00:00:43
00:00:42
00:00:41
00:00:40
00:00:39
00:00:38
00:00:37
00:00:36
00:00:35
00:00:34
00:00:33
00:00:32
00:00:31
00:00:30
00:00:29
00:00:28
00:00:27
00:00:26
00:00:25
00:00:24
00:00:23
00:00:22
00:00:21
00:00:20
00:00:19
00:00:18
00:00:17
00:00:16
00:00:15
00:00:14
00:00:13
00:00:12
00:00:11
00:00:10
00:00:09
00:00:08
00:00:07
00:00:06
00:00:05
00:00:04
00:00:03
00:00:02
00:00:01
00:00:00 

Time out
Output completed (1 min 1 sec consumed) - Normal Termination
*/