CODE/JAVA1

Time

maskan 2021. 1. 28. 13:44
public class Time {

	private int hour;
	private int minute;
	private int second;

	String getTime() {
		return hour + ":" + minute + ":" + second;
	}

	void clock() {
		while (true) {
			System.out.println(getTime());
			stop(1000);
			setSecond(second + 1);
		}
	}

	void stop(int interval) {
		try {
			Thread.sleep(interval);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {

		if (hour < 0) {
			this.hour = 0;
		} else if (23 < hour) {
			this.hour = 0;
		} else {
			this.hour = hour;
		}
	}

	public int getMinute() {
		return minute;
	}

	public void setMinute(int minute) {
		if (minute < 0) {
			this.minute = 0;
		} else if (59 < minute) {
			this.minute = 0;
			setHour(hour + 1);
		} else {
			this.minute = minute;
		}
	}

	public int getSecond() {
		return second;
	}

	public void setSecond(int second) {

		if (second < 0) {
			this.second = 0;
		} else if (59 < second) {
			this.second = 0;
			setMinute(minute + 1);
		} else {
			this.second = second;
		}
	}

}

'CODE > JAVA1' 카테고리의 다른 글

Interface  (0) 2021.01.28
Starcraft  (0) 2021.01.28
Abstract  (0) 2021.01.28
ExtendsStore  (0) 2021.01.28
Extends  (0) 2021.01.28