From 5fd9581670cacba30d491aaf0cf3240449713934 Mon Sep 17 00:00:00 2001 From: Jonas Hinterdorfer Date: Thu, 20 Mar 2025 11:59:40 +0100 Subject: [PATCH] implemted digital watch --- .../controller/FastWatchController.java | 19 ++++- .../at/ionas999/solution/model/WatchTime.java | 72 ++++++++++++++++--- 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/main/java/at/ionas999/solution/controller/FastWatchController.java b/src/main/java/at/ionas999/solution/controller/FastWatchController.java index 7f7f97c..068ae3a 100755 --- a/src/main/java/at/ionas999/solution/controller/FastWatchController.java +++ b/src/main/java/at/ionas999/solution/controller/FastWatchController.java @@ -1,12 +1,13 @@ package at.ionas999.solution.controller; import at.ionas999.solution.model.DayOfWeek; +import at.ionas999.solution.model.WatchTime; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.shape.Line; -import java.time.LocalTime; +import java.util.Arrays; public class FastWatchController { @FXML @@ -38,11 +39,19 @@ public class FastWatchController { @FXML private void initialize() { - //TODO: Fill ComboBox from enum. + WatchTime wt = WatchTime.getInstance(); + wt.setTime(4,1,DayOfWeek.FRIDAY); + cmbDayOfWeek.getItems().addAll(DayOfWeek.values()); //TODO: Bind labels of digital watch (one-way). Use StringBinding for the minutes to format them (leading 0s). + lblHours.textProperty().bind(wt.hoursProperty().asString()); + lblMinutes.textProperty().bind(wt.minutesProperty().asString()); + lblDayOfWeek.textProperty().bind(wt.dayOfWeekShortProperty()); + //TODO: Bind slider (bidirectionally!) and slider label (one-way). + lblTickDelay.textProperty().bind(wt.tickDelayProperty().asString()); + sldTickDelay.valueProperty().bindBidirectional(wt.tickDelayProperty()); //TODO: Add listener to redraw analog hands. @@ -66,7 +75,11 @@ public class FastWatchController { } public void btnAdjustOnAction(ActionEvent actionEvent) { + int[] time = Arrays.stream(this.txtTime.getText().split(":")) + .mapToInt(Integer::parseInt) + .toArray(); + DayOfWeek dayOfWeek = this.cmbDayOfWeek.getValue(); + WatchTime.getInstance().setTime(time[0],time[1], dayOfWeek); } - //TODO: Listen to the action event of the button. } diff --git a/src/main/java/at/ionas999/solution/model/WatchTime.java b/src/main/java/at/ionas999/solution/model/WatchTime.java index 1dccaf1..665b349 100755 --- a/src/main/java/at/ionas999/solution/model/WatchTime.java +++ b/src/main/java/at/ionas999/solution/model/WatchTime.java @@ -10,18 +10,46 @@ import java.util.TimerTask; public class WatchTime { //TODO: Singleton (lazy initialization) goes here. - private Timer timer; + public static WatchTime getInstance() { - private SimpleIntegerProperty hours; - private SimpleIntegerProperty minutes; - private SimpleStringProperty dayOfWeekShort; - private SimpleIntegerProperty tickDelay; + if(instance == null) { + instance = new WatchTime(); + } + + return WatchTime.instance; + } + + private static WatchTime instance = null; + private final Timer timer; + private final SimpleIntegerProperty hours; + private final SimpleIntegerProperty minutes; + private final SimpleStringProperty dayOfWeekShort; + private final SimpleIntegerProperty tickDelay; private DayOfWeek dayOfWeek; - //TODO: Getters go here. + public SimpleIntegerProperty hoursProperty() { + return hours; + } + + + public SimpleIntegerProperty minutesProperty() { + return minutes; + } + + + public SimpleStringProperty dayOfWeekShortProperty() { + return dayOfWeekShort; + } + + public SimpleIntegerProperty tickDelayProperty() { + return tickDelay; + } private WatchTime() { - //TODO: Initialize properties etc. + this.tickDelay = new SimpleIntegerProperty(500); + this.minutes = new SimpleIntegerProperty(); + this.hours = new SimpleIntegerProperty(); + this.dayOfWeekShort = new SimpleStringProperty(); this.timer = new Timer(true); this.tickTock(); @@ -32,7 +60,20 @@ public class WatchTime { @Override public void run() { Platform.runLater(() -> { - //TODO: Logic for each tick goes here... update hours, minutes, day of week accordingly. + WatchTime wt = WatchTime.getInstance(); + wt.minutes.set(wt.minutes.get() == 59 ? 0 : wt.minutes.get() + 1); + + + if(wt.minutesProperty().get() == 0) + { + wt.hours.set(wt.hours.get() == 23 ? 0 : wt.hours.get() + 1); + } + + + if(wt.minutesProperty().get() == 0 && wt.hoursProperty().get() == 0) + { + wt.setNextDay(); + } //DO NOT remove/change the following lines! tickTock(); @@ -42,6 +83,19 @@ public class WatchTime { } public void setTime(int hours, int minutes, DayOfWeek dayOfWeek) { - //TODO: Allow setting the time and day from outside. + this.hours.set(hours); + this.minutes.set(minutes); + this.dayOfWeek = dayOfWeek; + setDayOfWeekProp(); } + + private void setDayOfWeekProp() { + this.dayOfWeekShort.set(this.dayOfWeek.toString().substring(0,3)); + } + + private void setNextDay() { + int nextDayOrdinal = (this.dayOfWeek.ordinal() + 1) % DayOfWeek.values().length; + this.dayOfWeek = DayOfWeek.values()[nextDayOrdinal]; + setDayOfWeekProp(); + } }