From fa5b8bf52059b37dbbc5feccb76c0a2e1931cd2c Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 11 Nov 2024 13:26:18 +0100 Subject: [PATCH] added lab15 ex2 --- src/lab15_observer/ex2/AnalogTimer.java | 103 ++++++++++++++++++++++ src/lab15_observer/ex2/ClockTimer.java | 8 ++ src/lab15_observer/ex2/DigitalTimer.java | 15 ++++ src/lab15_observer/ex2/Main.java | 10 +++ src/lab15_observer/ex2/MyTimer.java | 53 +++++++++++ src/lab15_observer/ex2/TimerObserver.java | 5 ++ 6 files changed, 194 insertions(+) create mode 100644 src/lab15_observer/ex2/AnalogTimer.java create mode 100644 src/lab15_observer/ex2/ClockTimer.java create mode 100644 src/lab15_observer/ex2/DigitalTimer.java create mode 100644 src/lab15_observer/ex2/Main.java create mode 100644 src/lab15_observer/ex2/MyTimer.java create mode 100644 src/lab15_observer/ex2/TimerObserver.java diff --git a/src/lab15_observer/ex2/AnalogTimer.java b/src/lab15_observer/ex2/AnalogTimer.java new file mode 100644 index 0000000..1049ef5 --- /dev/null +++ b/src/lab15_observer/ex2/AnalogTimer.java @@ -0,0 +1,103 @@ +package lab15_observer.ex2; + +public class AnalogTimer implements TimerObserver { + private final MyTimer timer; + private static final String[][] digits = { + { + "┌─┐", + "│ │", + "└─┘" + }, + { + " ╷", + " │", + " ╵" + }, + { + "╶─┐", + "┌─┘", + "└─╴" + }, + { + "╶─┐", + " ─┤", + "╶─┘" + }, + { + "╷ ╷", + "└─┤", + " ╵" + }, + { + "┌─╴", + "└─┐", + "╶─┘" + }, + { + "┌─╴", + "├─┐", + "└─┘" + }, + { + "╶─┐", + " │", + " ╵" + }, + { + "┌─┐", + "├─┤", + "└─┘" + }, + { + "┌─┐", + "└─┤", + "╶─┘" + }, + }; + + public AnalogTimer(MyTimer timer) { + this.timer = timer; + timer.registerObserver(this); + } + + public void update(int hours, int minutes, int seconds) { + String[] lines = {"", "", ""}; + + addDigit(lines, hours / 10); + addSpace(lines); + addDigit(lines, hours % 10); + + addSep(lines); + + addDigit(lines, minutes / 10); + addSpace(lines); + addDigit(lines, minutes % 10); + + addSep(lines); + + addDigit(lines, seconds / 10); + addSpace(lines); + addDigit(lines, seconds % 10); + + System.out.println(lines[0]); + System.out.println(lines[1]); + System.out.println(lines[2]); + } + + private void addDigit(String[] lines, int digit) { + String[] parts = digits[digit]; + for (int i = 0; i < 3; i++) { + lines[i] += parts[i]; + } + } + private void addSpace(String[] lines) { + lines[0] += " "; + lines[1] += " "; + lines[2] += " "; + } + private void addSep(String[] lines) { + lines[0] += " o "; + lines[1] += " "; + lines[2] += " o "; + } +} diff --git a/src/lab15_observer/ex2/ClockTimer.java b/src/lab15_observer/ex2/ClockTimer.java new file mode 100644 index 0000000..e7bd7a9 --- /dev/null +++ b/src/lab15_observer/ex2/ClockTimer.java @@ -0,0 +1,8 @@ +package lab15_observer.ex2; + +public interface ClockTimer { + int getHour(); + int getMinute(); + int getSecond(); + void tick(); +} diff --git a/src/lab15_observer/ex2/DigitalTimer.java b/src/lab15_observer/ex2/DigitalTimer.java new file mode 100644 index 0000000..4d3e6b3 --- /dev/null +++ b/src/lab15_observer/ex2/DigitalTimer.java @@ -0,0 +1,15 @@ +package lab15_observer.ex2; + +public class DigitalTimer implements TimerObserver { + private final MyTimer timer; + + public DigitalTimer(MyTimer timer) { + this.timer = timer; + timer.registerObserver(this); + } + + @Override + public void update(int hours, int minutes, int seconds) { + System.out.printf("Time: %02d:%02d:%02d%n", hours, minutes, seconds); + } +} diff --git a/src/lab15_observer/ex2/Main.java b/src/lab15_observer/ex2/Main.java new file mode 100644 index 0000000..b2a5644 --- /dev/null +++ b/src/lab15_observer/ex2/Main.java @@ -0,0 +1,10 @@ +package lab15_observer.ex2; + +public class Main { + public static void main(String[] args) { + MyTimer clockTimer = new MyTimer(); + + new AnalogTimer(clockTimer); + new DigitalTimer(clockTimer); + } +} diff --git a/src/lab15_observer/ex2/MyTimer.java b/src/lab15_observer/ex2/MyTimer.java new file mode 100644 index 0000000..d6f76e0 --- /dev/null +++ b/src/lab15_observer/ex2/MyTimer.java @@ -0,0 +1,53 @@ +package lab15_observer.ex2; + +import java.util.*; + +public class MyTimer implements ClockTimer { + private final List observers = new ArrayList<>(); + private Calendar cal; + private Timer timer; + + public MyTimer() { + cal = Calendar.getInstance(); + timer = new Timer(); + timer.schedule(new TimerAction(), 0, 1000); + } + + class TimerAction extends TimerTask { + @Override + public void run() { + cal = Calendar.getInstance(); + tick(); + } + } + + @Override + public int getHour() { + return cal.get(Calendar.HOUR_OF_DAY); + } + + @Override + public int getMinute() { + return cal.get(Calendar.MINUTE); + } + + @Override + public int getSecond() { + return cal.get(Calendar.SECOND); + } + + @Override + public void tick() { + for (TimerObserver o : observers) { + o.update(getHour(), getMinute(), getSecond()); + } + } + + public void registerObserver(TimerObserver o) { + observers.add(o); + } + + public void removeObserver(TimerObserver o) { + observers.remove(o); + } +} diff --git a/src/lab15_observer/ex2/TimerObserver.java b/src/lab15_observer/ex2/TimerObserver.java new file mode 100644 index 0000000..d1dacf9 --- /dev/null +++ b/src/lab15_observer/ex2/TimerObserver.java @@ -0,0 +1,5 @@ +package lab15_observer.ex2; + +public interface TimerObserver{ + void update(int hours, int minutes, int seconds); +}