Java中的多功能stopWatch类

时间:2014-10-14 08:34:55

标签: java timer stopwatch

我正在用java编写我的第一个项目,我可以使用一些帮助和经验丰富的视图。我正在尝试编写stopWatch类,或者更像是厨房计时器的东西,它可以提供这个功能:


  - 设置硬启动和结束时间,
  - 从同一个地方暂停和开始时间
  - 设置时间是减少还是增加
  - 上下移动时间
  - addTime(示例:如果我们添加35秒,则假设在30:00结束时开始于00:00结束,stopWatch将在30:35结束);
  - 每秒显示时间

我在堆栈溢出时没有找到关于这种stopWatch / kitchen定时器的任何好线程。我的代码工作正常,但我对OOD和java一般都没有多少经验,所以我期待一些瑕疵。使用一些预先编写的课程是合理的吗?或日期时间操作?

我的代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

// class for handle timer operation
public class stopWatch implements ActionListener {

public Timer timer; // timer object
private int startTime; // start time in seconds
private int endTime; // end time in seconds
private int actualTime; // actual time in seconds
private long memoryUnixTime; // actual time in Unix format 
private boolean decrease; // is Timer decrease or incerease Time? 
private int presize; // how often timer check for change of time

// construct
public stopWatch(int startTime, int endTime, boolean decrease, int presize){
    this.startTime = startTime;
    this.endTime = endTime;
    this.decrease = decrease;
    this.presize = presize;
    this.actualTime = startTime;
}

// moving in Time UP
public void moveTimeUp (int seconds){
    if(inScope("up",seconds))
        actualTime += seconds;
}

// moving in Time DOWN
public void moveTimeDown (int seconds){
    if(inScope("down",seconds))
        actualTime -= seconds;
}


// is added time still inScope
private boolean inScope(String direction,int seconds){
    boolean inScopeP;
    switch(direction){
        case "up":
            inScopeP = ((this.actualTime + seconds) <= this.endTime);
        break;
        case "down":
            inScopeP = ((this.actualTime - seconds) >= this.startTime);
        break;
        default:
            inScopeP = false;
        break;
    }

    return inScopeP;
}

// addTime - this option will be used only if we use increasing timer
public void addTime(int seconds){
    if(this.decrease == false)
        this.endTime += seconds;
}

// run stopwatch
public void run(){
    System.out.println("start");
    this.timer = new Timer(this.presize, this);
    this.timer.setRepeats(true);
    this.timer.start();

}

//stop stopwatch
public void pause(){
    this.timer.stop();
    this.timer = null;

}

// end of counting
public void end(){
    System.out.println("END");
    this.pause();
}

// event listener, handle change time
@Override
public void actionPerformed(ActionEvent e) {

    // If the timer caused this event.
    if (e.getSource().equals(timer)) {
        long currentTime = System.currentTimeMillis();

        // If time had change  we decrease or increase time and print it out
        if(changeTimeP(currentTime)){

            // if time`s up we end the counting
            if(this.actualTime == this.endTime){
                this.end();

            // else we do the math increase 
            } else{

                this.memoryUnixTime = currentTime/1000;

                if(decrease == true)
                {
                    this.actualTime--;
                } else {
                    this.actualTime++;
                }

                // println into console for testing purposes
                int min = this.actualTime / 60;
                int sec = this.actualTime % 60;
                System.out.println(min+":"+sec);
            }
        }
    }
}

// did time changed? (in matter of one second)
private boolean changeTimeP(long currentTime){
    if(this.memoryUnixTime != (currentTime/1000L))
        return true;
            else return false;
}

}

彼得

0 个答案:

没有答案