吃惊!!!一个小小的控制台竟然能计时???

吃惊!!!一个小小的控制台竟然能计时???

TimeKeeper-一个简易的Java计时器控制台程序(开源 作者—吴昀杰)。
为了保证计时器的简易性与易用性,TimeKeeper舍弃了传统桌面应用程序的界面从而使用控制台运行。也许它的界面没有其他应用程序那么赏心悦目,但是其简易性远胜于那些拥有GUI界面的软件。该计时器适用于对计算机操作不是不是很熟悉以及不想在电脑内安装单独应用程序的用户(众所周知,控制台程序是不需要安装的)。读者还可以将此算法应用到其他程序开放中并加以更改。
——开发者 吴昀杰

源代码

  • 主类
/**
 *计时器程序
 *主类输入需要计算的时间并负责搭建舞台
 * 主类方法用来获得当前 时:分:秒
 * 根据“控制”类中的数据计算停止时间并输出命令
 * 结束程序
 */
package com.TimeKeeper;

/**
 * 导入相应的包
 */
import java.util.Calendar;

import java.util.Scanner;

/**
 * 主方法,调试程序
 *
 */
public class TimeKeeper_Main{

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		TimeKeeper_Main tk = new TimeKeeper_Main();
		
		int KeepTimeSec;
		int KeepTimeMin;
		int KeepTimeHou;
		int whileA = 0;
		
		/**
		 * do...while循环
		 * 实现重复程序
		 */
		do {
			
			/**
			 * 输入时间
			 */
			do {
				whileA = 0;
				
				System.out.print("请输入秒:");
				Scanner inputKeepTimeSec = new Scanner(System.in);
				KeepTimeSec = inputKeepTimeSec.nextInt();
				
				try {
					TheException te = new TheException();
					te.overSec(KeepTimeSec);
				}catch(Exception e) {
					System.out.println(e.getMessage());
					whileA = 1;
				}
				
			}while(whileA == 1);
			
			do {
				whileA = 0;
				
				System.out.print("请输入分:");
				Scanner inputKeepTimeMin = new Scanner(System.in);
				KeepTimeMin = inputKeepTimeMin.nextInt();
				
				try {
					TheException te = new TheException();
					te.overMin(KeepTimeMin);
				}catch(Exception e) {
					System.out.println(e.getMessage());
					whileA = 1;
				}
				
			}while(whileA == 1);
			
			do {
				whileA = 0;
				
				System.out.print("请输入时:");
				Scanner inputKeepTimeHou = new Scanner(System.in);
				KeepTimeHou = inputKeepTimeHou.nextInt();
				
				try {
					TheException te = new TheException();
					te.overHou(KeepTimeHou);
				}catch(Exception e) {
					System.out.println(e.getMessage());
					whileA = 1;
				}
				
			}while(whileA == 1);
			
			/**
			 * 实例化计时器
			 */
			TimeKeepCtrl tkc = new TimeKeepCtrl(tk.getStartHou(), tk.getStartMin(), tk.getStartSec(), KeepTimeSec, KeepTimeMin, KeepTimeHou);
			
			/**
			 * 控制开始于结束
			 */
			tk.JudgeEnd(tkc.getEndHou(), tkc.getEndMin(), tkc.getEndSec(), tkc);
			
		}while(tk.endCommand() == 1);
		
	}

	/**
	 * 主类方法
	 * 获取当前时间为开始时间
	 * @return
	 */
	public int getStartHou() {
		
		Calendar nowTime = Calendar.getInstance();
		return nowTime.get(Calendar.HOUR);
	}
	
	public int getStartMin() {
		
		Calendar nowTime = Calendar.getInstance();
		return nowTime.get(Calendar.MINUTE);
	}
	
	public int getStartSec() {
		
		Calendar nowTime = Calendar.getInstance();
		return nowTime.get(Calendar.SECOND);
	}
	
	/**
	 * 判断何时结束
	 * @param hour
	 * @param minute
	 * @param second
	 */
	@SuppressWarnings("resource")
	public void JudgeEnd(int hour, int minute, int second, TimeKeepCtrl tkc) {
		
		tkc.getStartTime();
		System.out.println("请等待,时间一到计时器将自动停止 ......");
		
		for (;;) {
			
			Calendar nowTime = Calendar.getInstance();
			
			if ((nowTime.get(Calendar.HOUR) == hour & minute == nowTime.get(Calendar.MINUTE) & second == nowTime.get(Calendar.SECOND)) || (hour == 0)) {
				
				System.out.print("现在时间为");
				tkc.getEndTime();
				System.out.println(",计时结束,谢谢使用!\n");
				break;
			}
			
		}
		
	}
	
	/**
	 * 计时结束后的命令
	 * 用户选择是“重新开始”还是“结束”
	 * @return
	 */
	@SuppressWarnings("resource")
	public int endCommand() {
		
		System.out.println("您希望继续执行怎样的操作???\n   1.从新开始\t2.结束");
		
		Scanner inputCtrlNum = new Scanner(System.in);
		int endCtrlNum = inputCtrlNum.nextInt();
		
		if (endCtrlNum == 2) {
			
			System.exit(0);
			return 0;
			
		}else {
			
			return endCtrlNum;
		}
		
	}
	
}

  • 控制类
/**
 * 用来控制计算结束时间并返回给主类
 */

package com.TimeKeeper;

/**
 * 实现计时器功能
 *
 */
public class TimeKeepCtrl {
	/**
	 * 定义开始时间
	 */
	private int startHou;
	private int startMin;
	private int startSec;
	
	/**
	 * 定义结束时间
	 */
	private int endHou;
	private int endMin;
	private int endSec;
	
	/**
	 * 定义持续时间
	 */
	private int KeepTimeSec;
	private int KeepTimeMin;
	private int KeepTimeHou;
	
	/**
	 * 构造方法,传值
	 * @param startMin
	 * @param startSec
	 * @param KeepTime
	 */
	public TimeKeepCtrl(int startHou, int startMin, int startSec, int KeepTimeSec, int KeepTimeMin, int KeepTimeHou) {
		this.startHou = startHou;
		this.startMin = startMin;
		this.startSec = startSec;
		this.KeepTimeSec = KeepTimeSec;
		this.KeepTimeMin = KeepTimeMin;
		this.KeepTimeHou = KeepTimeHou;
	}
	
	/**
	 * 输出时间
	 */
	public void getStartTime() {
		
		System.out.println("\n开始时间为:" + this.startHou + ":" + this.startMin + "." + this.startSec);
	}
	
	public void getEndTime() {
		
		System.out.print(this.endHou + ":" + this.endMin + "." + this.endSec);
	}
	
	
	/**
	 * 计算结束时间
	 */
		public int getEndSec() {
		
			this.endSec = startSec + KeepTimeSec;
			
			if (this.endSec < 60) {
				
				return this.endSec;
			}else {
				
				this.endSec = this.endSec % 60;
				
				return this.endSec;
			}
		}
		
		public int getEndMin() {
		
			if ((startSec + KeepTimeSec) >= 60) {
				
				this.endMin = startMin + KeepTimeMin + (startSec + KeepTimeSec) / 60;
			}else {
				
				this.endMin = startMin + KeepTimeMin;
				
				if (this.endMin < 60) {
					
					return this.endMin;
				}else {
					
					this.endMin = endMin % 60;
					
					return this.endMin;
				}
			}
			return 0;
		}
		
		public int getEndHou() {
		
			this.endHou = startHou + KeepTimeHou;
				
			return this.endHou;
		}
		
}

  • 异常类
package com.TimeKeeper;

@SuppressWarnings("serial")
public class TheException extends Exception {

	public void overSec(int KeepTimeSec) throws Exception {
		
		if (KeepTimeSec > 60) {
			throw new Exception("秒钟不得大于60s,请在下方重新输入");
		}
	}
	
	public void overMin(int KeepTimeMin) throws Exception {
		
		if (KeepTimeMin > 60) {
			throw new Exception("分钟不得大于60min,请在下方重新输入");
		}
	}
	
	public void overHou(int KeepTimeHou) throws Exception {
		
		if (KeepTimeHou > 24) {
			throw new Exception("时钟不得大于24h,请在下方重新输入");
		}
	}
}

一下是转换为.exe后的控制台运行界面:吃惊!!!一个小小的控制台竟然能计时???