Question
Need help writing the actionPerformedEvent outlined in code below. SoylentQueue class is associated with SoylentDataItem class. For increased clarity, I am providing the code for
Need help writing the actionPerformedEvent outlined in code below. SoylentQueue class is associated with SoylentDataItem class. For increased clarity, I am providing the code for both classes. Standard library PriorityQueue cannot be used. Most of my issues with this problem has been with which data structure to use to implement priority queue. Also provdied is a UML diagram of how these classes relate with each other.
***in java...obviously
//you will notice that only the method header for shuffleListModel() is provided, however
//it should not interfere
//Standard library PriorityQueue cannot be used
public class SoylentQueue {
private DefaultListModel
private ArrayList
private int timeDelay;
private Timer t;
private ArrayList
public SoylentQueue(int timeDelay) {
dlm = new DefaultListModel();
nameList = new ArrayList();
readNames();
processedCattle = new ArrayList();
this.timeDelay = timeDelay;
}
public DefaultListModel
return dlm;
}
public void runSimulation() {
t = new Timer(timeDelay, new EventGenerator());
t.start();
}
public ArrayList
return processedCattle;
}
public void shuffleListModel() {
//TODO Sort the default list model so all the elements are in the proper priority
//Do NOT make a new DefaultListModel - this will break relationship between this model
//and the JList!!!
}
private void readNames() {
Scanner s;
try {
s = new Scanner(new File("cattleNameList.txt"));
while (s.hasNextLine()) {
nameList.add(s.nextLine().trim());}
} catch (FileNotFoundException e) {
System.out.println("Do not forget to include the nameList!");
System.exit(0);
}}
private class EventGenerator implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("This currently does nothing");
//First, generate a random priority, 1 to 10
//Then, generate a new SoylentDataItem with that priority
//1) Select a random name from the nameList
//2) Generate a random ID between 100 and 10000, not allowing duplicates
//It is up to you how to prevent duplicates
//3)Age should be generated between 18 and 38
SoylentDataItem newItem = null;
//Finally, add the new SoylentDataItem to the model and shuffle the list model
dlm.addElement(newItem);
shuffleListModel();
//Further, change it so that the timer has a new delay - between 3 to 8 seconds
//for a new cattle to arrive
}
}
//The unfinished toString() methods should not interfere
public class SoylentDataItem {
private int cattleID;
private int cattlePriority;
private String cattleName;
private int cattleAge;
public SoylentDataItem(int priority, String name, int id, int age){
this.cattlePriority = priority;
this.cattleName = name;
this.cattleID = id;
this.cattleAge = age;
}
public String detailsString() {
/*TODO Generate a detail string for the cattle, including all of their fields,
* displayable in the JTextArea on the main GUI.
* You may add newlines to this String as needed for output formatting
*/
return null;
}
public String outputString() {
/*TODO generate a detail string for the cattle, including all of their fields,
* to go on one line - this will be used as output to the processing file
*
*/
return null;
}
public String toString() {
return "Cattle:" + cattleID;
}
}
UML----------------------------------------->
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started