I am trying to display an array list with multiple types, item by item. So I suppose this is a two part question. First, how do I convert this array list to a string in order to print it out (I am not sure how important this is). And second how do I move from item to item using an ActionEvent.
My Array list has String, int, int, double. I have already figured out how to format the double to be a string, however how do I do this for the entire arraylist? The arraylist is already sorted in order to display it in alphabetical order.
Below is my array list and the sorting
inventory.add(new inventoryItem("Pencil", 1111, 50, .25));
inventory.add(new inventoryItem("Pen", 2222, 50, 1.00));
inventory.add(new inventoryItem("Marker", 3333, 5, 2.00));
inventory.add(new inventoryItem("Notebook", 4444, 10, 2.50));
inventory.add(new officeSupplyItem("Mechanical Pencil", 1112, 25, .50));
inventory.add(new officeSupplyItem("Lead Pencil", 1113, 25, .25));
inventory.add(new officeSupplyItem("Blue Pen", 2221, 25, 1.00));
inventory.add(new officeSupplyItem("Black Pen", 2223, 5, 1.00));
inventory.add(new officeSupplyItem("Red Pen", 2224, 20, 1.00));
inventory.add(new officeSupplyItem("Steno Notebook", 4441, 5, 2.50));
inventory.add(new officeSupplyItem("Legal Pad", 4442, 5, 2.50));
inventory = sortInventory(inventory);
for (int i = 0; i < inventory.size(); i++) {
inventory.get(i).output(outputText);
}
inventoryTotal(inventory, outputText);
sortInventory(inventory);
}
static ArrayList sortInventory(ArrayList<inventoryItem> unsorted) {
ArrayList<inventoryItem> sorted = new ArrayList<>(); //create new array list to sort
inventoryItem alpha = null;
int lowestIndex = -1;
while (unsorted.size() > 0) { //while my unsorted array is less than 0 do the following
for (int i = 0; i < unsorted.size(); i++) { //increment through
if (alpha == null) {
alpha = unsorted.get(i); //get the next line in the inventoryItem array
lowestIndex = i;
} else if (unsorted.get(i).itemName.compareToIgnoreCase(alpha.itemName) < 0) { //compare items to determine which has a higher value
alpha = unsorted.get(i);
lowestIndex = i;
}
}
sorted.add(alpha); //reset the index so it will loop until there are no more items in the unsorted array
unsorted.remove(lowestIndex);
alpha = null;
lowestIndex = -1;
}
return sorted; //return the sorted arraylist
}
My buttons are set up like this
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
outputText.append("\n this should move me forward in the list");
}
});
previousButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
outputText.append("\n this should move me backward in the list");
}
});
exitButton.addActionListener(new ActionListener() { //setup listener for the exit button
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0); //once the exit button is pressed, exit the program
}
});
thePanel.add(nextButton); //move forward in the arraylist
thePanel.add(previousButton); //move to previous item in the arraylist
thePanel.add(exitButton); //add exit button to the panel
Any help would be greatly appreciated.
toString()on each element