I am currently trying to make a GUI alarm clock using a priority queue. When go to enter a second alarm it changes the first alarm entered to the same time as second one entered. Any advice as to why this is happening would be greatly appreciated.
I'm sorry if this explanation is badly phrased .
Code is below.
Alarm.java
public class Alarm implements Comparable<Alarm> {
private final Calendar currentTime = Calendar.getInstance();
private final int hour;
private final int minute;
private final int second;
private static boolean isAlarmSet = false;
static int alarmHour;
static int alarmMinute;
// you will have to edit filepath to get sound clip working in program
File yourFile = new File("my file path for sound clip would be hear");
AudioInputStream stream;
AudioFormat format;
DataLine.Info info;
private static Clip clip;
public Alarm(int alarmHour,int alarmMinute) {
this.alarmHour = alarmHour;
this.alarmMinute = alarmMinute;
//used to compare the current hour against the alarm hour
this.hour = currentTime.get(Calendar.HOUR_OF_DAY);
//used to compare the current minute against the alarm minute
this.minute = currentTime.get(Calendar.MINUTE);
this.second = currentTime.get(Calendar.SECOND);
//try catch statement for the audio clip to play when alarm is going off
try {
stream = AudioSystem.getAudioInputStream(yourFile);
format = stream.getFormat();
info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
} catch (UnsupportedAudioFileException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Alarm alarm = (Alarm) o;
return Integer.compare(alarm.alarmHour, alarmHour) == 0 &&
Objects.equals(alarmMinute, alarm.alarmMinute);
}
@Override
public int hashCode() {
return this.alarmHour;
}
@Override
public String toString() {
return "Alarm{" +
"Hour='" + alarmHour + '\'' +
", minute=" + alarmMinute +
'}';
}
@Override
public int compareTo(Alarm o) {
int cHour = o.getAlarmHour();
int cMinute = o.getAlarmMinute();
if(this.getAlarmHour() > cHour && this.getAlarmMinute() > cMinute){
return 1;
}
if(this.getAlarmHour() < cHour && this.getAlarmMinute() < cMinute){
return -1;
}else{
return 0;
}
}
public int getHour() {
return this.hour;
}
public int getMinute() {
return this.minute;
}
public int getSecond() {
return this.second;
}
public void setAlarmHour(int theHour){
this.alarmHour = theHour;
}
public void setAlarmMinute(int theMinute){
this.alarmMinute = theMinute;
}
public int getAlarmHour() {
return alarmHour;
}
public int getAlarmMinute() {
return alarmMinute;
}
public boolean getAlarm() {
return isAlarmSet;
}
public void setAlarm(boolean alarm) {
isAlarmSet = alarm;
}
public Clip getClip() {
return clip;
}
}
Controller
public class Control implements ActionListener {
PriorityQueue<Alarm> alarmPriorityQueue = new PriorityQueue<>();
View view;
Alarm alarm;
public Control(View view, Alarm alarm) {
this.view = view;
this.alarm = alarm;
this.view.getAlarmButton().addActionListener(this);
this.view.getStopAlarmButton().addActionListener(this);
this.view.getSetButton().addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Calendar currentTime = Calendar.getInstance();
//when the get alarm button is pressed the JPanel will change visibility to true
//this will also change the alarm check boolean to true to show an alarm is being set
if (e.getSource() == view.getAlarmButton()) {
view.getAlarmSettings();
alarm.setAlarm(true);
/**
* this action event will be in charge of stopping the alarm
* firstly the alarm sound clip will turn off
* then the alarm check boolean will change to false indicating that there is no more alarm
* finally the button will take the head of the priority queue and remove it
*/
} else if (e.getSource() == view.getStopAlarmButton()) {
alarm.getClip().stop();
alarm.setAlarm(false);
alarmPriorityQueue.poll();
System.out.println(alarmPriorityQueue.peek());
/**
* when the set button is clicked this will check to see if both text field contain a value
* if not it will prompt the user asking them to enter both an hour and a minute.
* if both an hour an a minute exist then the alarm will take the text from the fields and parse it so it can be assigned to the alarm hour and minute
* these values are then added to the priority queue
*/
}else if (e.getSource() == view.getSetButton()) {
if (view.getHourMark().getText().equals("") || view.getHourMark().getText() == null) {
view.errorHourMessage();
} else if (view.getMinuteMark().getText().equals("") || view.getMinuteMark().getText() == null) {
view.errorMinuteMessage();
} else {
alarm.setAlarmHour(Integer.parseInt(view.getHourMark().getText()));
alarm.setAlarmMinute(Integer.parseInt(view.getMinuteMark().getText()));
alarmPriorityQueue.add(new Alarm(Alarm.alarmHour,Alarm.alarmMinute));
System.out.println(alarmPriorityQueue);
}
}
}
}
Example of output first is the primary alarm in the priority queue then the first and second alarm in the priority queue as you can see the first one has changed to the same as the second alarm
[Alarm{Hour='17', minute=19}]
[Alarm{Hour='17', minute=18}, Alarm{Hour='17', minute=18}]
I apologise for the bad code, I'm still learning