IT_Programming/Java

시간이 증가하는 Timer

JJun ™ 2007. 2. 6. 10:06

class MyTimerTest
{
      public static void main(String[] args)
      {
            MyTimer mt = new MyTimer();
            // 지정된 시간(시,분,초)까지 1초씩 증가한다.
            // 30초후에 멈추도록 했다.
            mt.start(0, 0, 30);      
      }
}

class MyTimer

{
      MyTime time = new MyTime();
      String getTime()

      {
            return time.toString();
      }

      void start(int hour, int min, int sec)

      {
            while(true)

            {
                  try

                  {
                        // 1초동안 수행을 멈춘다. 단위는 천분의 1초
                        // 숫자가 작을 수록 빨라진다.
                        Thread.sleep(1000);      
                   } catch(Exception e) {}

                  System.out.println(getTime());
                  incSecond();       // 시간을 1초씩 증가시킨다.
                  // 지정된 시간이 되면 멈춘다.
                  if (time.getHour()==hour && time.getMinute()==min && time.getSecond()==sec)

                  {
                        System.out.println(getTime());
                        System.exit(0);
                  }
            }
      }

      void incHour()

      {
            time.setHour(time.getHour()+1);
      }

      void incMinute()

      {
            int min = time.getMinute();

            if (min==59)

            {
                  time.setMinute(0);
                  incHour();
            }

            else

            {
                  time.setMinute(++min);
            }
      }

      void incSecond()

      {
            int sec = time.getSecond();

            if (sec==59)

            {
                  time.setSecond(0);
                  incMinute();
            }

            else

            {
                  time.setSecond(++sec);
            }
      }

      void reset()

      {
            // time = new MyTime();
            time.setHour(0);
            time.setMinute(0);
            time.setSecond(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:00:00
00:00:01
00:00:02
00:00:03
00:00:04
00:00:05
00:00:06
00:00:07
00:00:08
00:00:09
00:00:10
00:00:11
00:00:12
00:00:13
00:00:14
00:00:15
00:00:16
00:00:17
00:00:18
00:00:19
00:00:20
00:00:21
00:00:22
00:00:23
00:00:24
00:00:25
00:00:26
00:00:27
00:00:28
00:00:29
00:00:30
Output completed (30 sec consumed) - Normal Termination
*/