added lab15 ex2

This commit is contained in:
Louis Heredero 2024-11-11 13:26:18 +01:00
parent b964385405
commit fa5b8bf520
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
6 changed files with 194 additions and 0 deletions

View File

@ -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 ";
}
}

View File

@ -0,0 +1,8 @@
package lab15_observer.ex2;
public interface ClockTimer {
int getHour();
int getMinute();
int getSecond();
void tick();
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,53 @@
package lab15_observer.ex2;
import java.util.*;
public class MyTimer implements ClockTimer {
private final List<TimerObserver> 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);
}
}

View File

@ -0,0 +1,5 @@
package lab15_observer.ex2;
public interface TimerObserver{
void update(int hours, int minutes, int seconds);
}