-
Notifications
You must be signed in to change notification settings - Fork 14
/
digitalWatch3524.java
58 lines (58 loc) · 2.32 KB
/
digitalWatch3524.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Scanner;
public class digitalWatch3524 {
public static void main(String[] args) {
System.out.println("2021503524 - Mugundh J B");
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
Scanner scanner = new Scanner(System.in);
// Prompt the user for input
System.out.print("Input day (Mon, Tue, Wed, Thu, Fri, Sat, Sun): ");
String day = scanner.nextLine();
System.out.print("Input hour (24-hour): ");
int hour = scanner.nextInt();
System.out.print("Input minute: ");
int minute = scanner.nextInt();
System.out.print("Input second: ");
int second = scanner.nextInt();
// Validating day input
if (!isValidDay(day)) {
System.out.println("Invalid day input!");
} else if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
System.out.println("Invalid time input!");
} else {
// Advancing time by one second
second++;
if (second == 60) {
second = 0;
minute++;
if (minute == 60) {
minute = 0;
hour++;
if (hour == 24) {
hour = 0;
day = getNextDay(day);
}
}
}
// Displaying the new time
System.out.println("New time: " + day + " " + hour + " " + minute + " " + second);
}
}
// Checking if the given day is valid
public static boolean isValidDay(String day) {
return day.equals("Mon") || day.equals("Tue") || day.equals("Wed") ||
day.equals("Thu") || day.equals("Fri") || day.equals("Sat") || day.equals("Sun");
}
// Getting the next day
public static String getNextDay(String day) {
if (day.equals("Sun")) return "Mon";
if (day.equals("Mon")) return "Tue";
if (day.equals("Tue")) return "Wed";
if (day.equals("Wed")) return "Thu";
if (day.equals("Thu")) return "Fri";
if (day.equals("Fri")) return "Sat";
if (day.equals("Sat")) return "Sun";
return "";
}
}