Okay so, I was given task to create basic alarm implementing interface.
Here's what I've got so far.
Here is my main method.
import java.util.Scanner;
import java.time.LocalTime;
public class Monday extends Weekday{
private String time;
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
Monday m = new Monday();
String alarmer;
System.out.print("Enter time for alarm in this format (HH:MM): " );
alarmer = reader.nextLine();
m.setAlarm(alarmer);
m.showAlarm();
}
@Override
public void setAlarm(String time) {
this.time = time;
LocalTime alarm = LocalTime.parse(time);
LocalTime now = LocalTime.now();
if (alarm.isBefore(now)) {
System.out.println("I'll wake you up later!");
}
else {
System.out.println("Alarm is set for tomorrow!");
}
}
@Override
public String showAlarm() {
return time;
}
}
and the next one is the interface
import java.util.Scanner; import java.time.LocalTime;
interface Alarm {
void setAlarm(String time);
String showAlarm();
}
The problem here is that even though the entered input is past the current time, I always got the "I'll wake you up later". Any ideas to avoid getting the same output?
Enter time for alarm in this format (HH:MM): 05:30
I'll wake you up later!
No matter what is the input it will only always execute and display the "I'll wake you up later!" When the time entered is past the current time it should display the line of code "Alarm is set for tomorrow"
Given the Question and Comments, you seem to be confused about the logic of a 24-hour clock without the context of dates.
Going past midnight means you will have moved into a new day. So any time-of-day earlier than now means tomorrow.
Here is some simple example code.
Simulate a current time-of-day of 5 PM.
// ----------| now = 17:00 |------------------------------
LocalTime now = LocalTime.of( 17 , 0 );
System.out.println( "now = " + now );
Set alarm at various time-of-day values: 22:00, 15:00, and 05:30.
// ----------| alarm 22:00 |------------------------------
LocalTime tenPM = LocalTime.parse( "22:00" );
System.out.println( "" );
System.out.println( "tenPM = " + tenPM );
if ( tenPM.isBefore( now ) )
{
System.out.println( "I'll wake you up tomorrow." );
} else
{
System.out.println( "Alarm is set for today." );
}
// ----------| alarm 15:00 |------------------------------
LocalTime threePM = LocalTime.parse( "15:00" );
System.out.println( "" );
System.out.println( "threePM = " + threePM );
if ( threePM.isBefore( now ) )
{
System.out.println( "I'll wake you up tomorrow." );
} else
{
System.out.println( "Alarm is set for today." );
}
// ----------| alarm 05:30 |------------------------------
LocalTime fiveThirtyInMorning = LocalTime.parse( "05:30" );
System.out.println( "" );
System.out.println( "fiveThirtyInMorning = " + fiveThirtyInMorning );
if ( threePM.isBefore( now ) )
{
System.out.println( "I'll wake you up tomorrow." );
} else
{
System.out.println( "Alarm is set for today." );
}
When run:
now = 17:00
tenPM = 22:00
Alarm is set for today.
threePM = 15:00
I'll wake you up tomorrow.
fiveThirtyInMorning = 05:30
I'll wake you up tomorrow.
Your alarm is not tracking a moment, a specific point on the timeline. That would involve the Instant
, OffsetDateTime
, or ZonedDateTime
classes.
Your alarm is tracking a time-of-day without the context of a date and without the context of a time zone or offset-from-UTC. So your alarm uses LocalTime
class.