// Fig. 6.5: TimeTest.java
// Demonstrating the Time3 class set and get methods
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import com.deitel.jhtp2.ch06.Time3;

public class TimeTest extends Applet implements ActionListener {
   private Time3 t;
   private Label hourLabel, minuteLabel, secondLabel;
   private TextField hourField, minuteField,
                     secondField, display;
   private Button tickButton;

   public void init()
   {
      t = new Time3();

      hourLabel = new Label( "Set Hour" );
      hourField = new TextField( 10 );
      hourField.addActionListener( this );
      add( hourLabel );
      add( hourField );

      minuteLabel = new Label( "Set minute" );
      minuteField = new TextField( 10 );
      minuteField.addActionListener( this );
      add( minuteLabel );
      add( minuteField );

      secondLabel = new Label( "Set Second" );
      secondField = new TextField( 10 );
      secondField.addActionListener( this );
      add( secondLabel );
      add( secondField );

      display = new TextField( 30 );
      display.setEditable( false );
      add( display );

      tickButton = new Button( "Add 1 to Second" );
      tickButton.addActionListener( this );
      add( tickButton );

      updateDisplay();      
   }

   public void actionPerformed( ActionEvent e )
   {
      if ( e.getSource() == tickButton )
         tick();
      else if ( e.getSource() == hourField ) {
         t.setHour(
            Integer.parseInt( e.getActionCommand() ) );
         hourField.setText( "" );
      }
      else if ( e.getSource() == minuteField ) {
         t.setMinute(
            Integer.parseInt( e.getActionCommand() ) );
         minuteField.setText( "" );
      }
      else if ( e.getSource() == secondField ) {
         t.setSecond(
            Integer.parseInt( e.getActionCommand() ) );
         secondField.setText( "" );
      }

      updateDisplay();
   }

   public void updateDisplay()
   {
      display.setText( "Hour: " + t.getHour() +
         "; Minute: " + t.getMinute() +
         "; Second: " + t.getSecond() );
      showStatus( "Standard time is: " + t.toString() +
         "; Military time is: " + t.toMilitaryString() );
   }

   public void tick()
   {
      t.setSecond( ( t.getSecond() + 1 ) % 60 );

      if ( t.getSecond() == 0 ) {
         t.setMinute( ( t.getMinute() + 1 ) % 60 );

         if ( t.getMinute() == 0 )
            t.setHour( ( t.getHour() + 1 ) % 24 );
      }
   }
}


