import java.time.LocalTime;
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Clock> clockList = Clocks.init();
print(clockList);
addMinute(clockList ,1);
System.out.println();
print(clockList);
}
private static void print(List<Clock> clockList) {
for (Clock item : clockList) {
System.out.println(item);
}
}
private static List<Clock> addMinute(List<Clock> clocks, int minute) {
for (Clock item : clocks) {
item.addMinutes(minute);
}
return clocks;
}
}
class Clock {
private LocalTime time;
private int hour;
private int minute;
public Clock(LocalTime time) {
this.time = time;
}
public Clock addMinutes(int minute) {
time = time.plusMinutes(minute);
return new Clock(time);
}
@Override
public String toString() {
return "Godzina: " + time;
}
}
class Clocks {
private static final Random random = new Random();
private static List<Clock> clocksList;
private static Clock randomClock() {
int randomHour = random.nextInt(23);
int randomMinute = random.nextInt(59);
return new Clock(LocalTime.of(randomHour, randomMinute));
}
public static List<Clock> init() {
clocksList = new LinkedList<>();
int clocksSize = random.nextInt(5) + 2;
for (int i = 0; i < clocksSize; i++) {
clocksList.add(randomClock());
}
return clocksList;
}
}
I found such a solution on the internet but I don't understand what the linkedlist structure looks like because it is randomly generated
I have to do a similar task but I wanted to use my own fixed list, can you explain this to me?
actually I would like to understand it in these two ways
import java.util.*;
class Clock {
private int hour;
private int minute;
public Clock(int hour, int minute) {
this.hour = hour;
this.minute = minute;
}
public int addMinutes(int minute) {
minute = minute + 1;
return minute;
}
public int minuteChceck() {
while (minute == 60) {
hour = hour + 1;
} return hour;
}
@Override
public String toString() {
return "Godzina: " + hour +":"+ minute;
}
}
public class Program {
public static void main(String[] args) {
LinkedList<Clock> clocks = new LinkedList<>();
clocks.add(new Clock(10, 20));
clocks.add(new Clock(19, 59));
System.out.println(clocks);
}
}
I tried something like this, but as it happens with time 59 + 1 minute, it should not display 60 minutes: D