I'm still new to Java, and have made a timer that does a popup once an hour to remind the computer user to get up and move around, and essentially runs in the background until the popup is supposed to show. However, the while loop I decided to use in main takes a lot of resources. I've tried a Thread.sleep (different times used) method but can't get it to always line up with when the popup is supposed to go off. So, I decided not to use that method. Are there any other ideas for resource management for this app? Here is the source code for all of the classes.
MAIN CLASS AND METHOD
package houralarm;
public class HourAlarm
{
public static void main(String[] args)
{
// TimerLoop Class Object
TimerLoop timerLoop = new TimerLoop();
// bool to run the while loop
Bboolean run = true;
while(run)
{
if(timerLoop.quitInt == 0)
{
timerLoop.Loop();
}
}
}
}
GET TIME CLASS
package houralarm;
import java.util.Date;
import java.text.*;
public class GetTime
{
public String ReturnTime()
{
// string to hold the date format
String minutes;
// Date object
Date dateObj = new Date();
// SimpleDateFormat object with the formatting only. holding the minutes
SimpleDateFormat dateFormat = new. SimpleDateFormat("mm");
// Sets the date to the times variable
minutes = dateFormat.format(dateObj);
// Returns the minutes
return minutes;
}
}
TIMERLOOP CLASS package houralarm;
import javax.swing.JOptionPane;
public class TimerLoop
{
// GetTime Class Object
GetTime getTime = new GetTime();
WhenAlarm whenAlarm = new WhenAlarm();
// Holds the GetTime Classes return value from ReturnTime()
String t = getTime.ReturnTime();
// Used for the while loop
boolean quit = false;
// Used to tell Main() when to quit and start the loop again
int quitInt = 0;
String answer = whenAlarm.Answer();
public void Loop()
{
while (!quit)
{
// Runs continously to update the time
t = getTime.ReturnTime();
if (answer.equals("00") && t.equals("00"))
whenAlarm.OnHour(answer);
else if (answer.equals("15") && t.equals("15"))
whenAlarm.FifteenMinutes(answer);
else if (answer.equals("30") && t.equals("30"))
whenAlarm.ThirtyMinutes(answer);
else if(answer.equals("45") && t.equals("45"))
whenAlarm.FortyFiveMinutes(answer);
else
{
t = getTime.ReturnTime();
break;
}
}
}
}
WHEN ALARM CLASS
package houralarm;
import javax.swing.*;
public class WhenAlarm
{
GetTime getTime = new GetTime();
String t = getTime.ReturnTime();
boolean check = true;
public String Answer ()
{
String answer = JOptionPane.showInputDialog(null, "When would you like for this alarm to go off?\n"
+ "Type 15 for every hour on the quarter hour.\n" + "Type 30 for every hour on the half hour.\n" +
"Type 45 for every hour on the 3/4 hour.\n" + "Type 00 for every hour on the hour.", "When do you want this alarm to happen?" , JOptionPane.QUESTION_MESSAGE);
String returnAnswer = answer;
return returnAnswer;
}
public void FifteenMinutes(String time)
{
while(check){
if (time.equals("15"))
{
JOptionPane.showMessageDialog(null, "It's Time To Get Up And Walk Around!", "Hour Timer", JOptionPane.WARNING_MESSAGE);
t = getTime.ReturnTime();
break;
}
else
break;
}
}
public void ThirtyMinutes (String time)
{
while(check){
if (time.equals("30"))
{
JOptionPane.showMessageDialog(null, "It's Time To Get Up And Walk Around!", "Hour Timer", JOptionPane.WARNING_MESSAGE);
t = getTime.ReturnTime();
break;
}
else
break;
}
}
public void FortyFiveMinutes(String time)
{
while(check){
if (time.equals("45"))
{
JOptionPane.showMessageDialog(null, "It's Time To Get Up And Walk Around!", "Hour Timer", JOptionPane.WARNING_MESSAGE);
t = getTime.ReturnTime();
break;
}
else
break;
}
}
public void OnHour(String time)
{
while(check){
if (time.equals("00"))
{
JOptionPane.showMessageDialog(null, "It's Time To Get Up And Walk Around!", "Hour Timer", JOptionPane.WARNING_MESSAGE);
t = getTime.ReturnTime();
break;
}
else
break;
}
}
}