Help
Hey guys, i'm just trying to get this to work here.
I need the nextItem() and itemBrowser() to talk to each other, and exchange the value of the selected item, and same with prevItem() and itemBrowser(). Basically when i click the button for next item, i need it to pass the selected item from browser, to next or prev, (based on the button) edit the selected item number and pass that number back to the browser. After that, it's all peachy. I was pullnig my hair out on this for 6 hours last night. Any input? i've included all 3 class files. TY!
[565 byte] By [
Zerofurya] at [2007-11-27 10:25:55]

//--
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
//--
public class FowlerInventoryGUI2
{
private List<FowlerItemGUI2> myItems;
//-
public FowlerInventoryGUI2()
{
myItems = new ArrayList<FowlerItemGUI2>();
myItems.add(new FowlerItemGUI2("", 0, 0, "", 0, 0)); //dummy place holder for 0 index
myItems.add(new FowlerItemGUI2("Fire Gem", 50.00, 6, "A pre-bottled fire attack spell!", 2.50, 300.00));
myItems.add(new FowlerItemGUI2("Wind Gem", 50.00, 10, "A pre-bottled wind attack spell!", 2.50, 500.00));
myItems.add(new FowlerItemGUI2("Water Gem", 50.00, 4, "A pre-bottled water attack spell!", 2.50, 200.00));
myItems.add(new FowlerItemGUI2("Thunder Gem", 50.00, 3, "A pre-bottled lightning attack spell!", 2.50, 150.00));
myItems.add(new FowlerItemGUI2("Earth Gem", 50.00, 7, "A pre-bottled earth attack spell!", 2.50, 350.00));
myItems.add(new FowlerItemGUI2("Ice Gem", 50.00, 2, "A pre-bottled ice attack spell!", 2.50, 100.00));
myItems.add(new FowlerItemGUI2("Roc Feather", 25.00, 100, "A regeant used in 'Feather Fall' spells.", 1.25, 2500.00));
myItems.add(new FowlerItemGUI2("Weak Heal Potion", 40.00, 100, "Heals the user for 100 Life.", 2.00, 400.00));
myItems.add(new FowlerItemGUI2("Weak Mana Potion", 40.00, 100, "Restores 100 mana to the user.", 2.00, 400.00));
myItems.add(new FowlerItemGUI2("Heal Potion", 75.00, 42, "Heals the user for 500 Life.", 3.75, 3150.00));
myItems.add(new FowlerItemGUI2("Mana Potion", 75.00, 30, "Restores 350 mana to the user.", 3.75, 2250.00));
myItems.add(new FowlerItemGUI2("Great Heal Potion", 150, 30, "Heals the user for 1500 Life.", 7.50, 4500.00));
myItems.add(new FowlerItemGUI2("Great Mana Potion", 150, 25, "Restores 650 mana to the user.", 7.50, 3750.00));
} //stocks the inventory with objects (not classes!)
//-
public void showItem()
{
String stringItemChosen =
JOptionPane.showInputDialog("There are 13 items in stock.\nPlease input the item number you wish to view:"); //accept user input for item to view
int itemChosen = Integer.parseInt( stringItemChosen); //convert to item number
if (itemChosen <= 0)
{
do
{
stringItemChosen =
JOptionPane.showInputDialog("That is not a valid option. Re-enter:");
itemChosen = Integer.parseInt( stringItemChosen);
} while (itemChosen <= 0);
} else;
if (itemChosen > 13)
{
do
{
stringItemChosen =
JOptionPane.showInputDialog("That is not a valid option. Re-enter:");
itemChosen = Integer.parseInt( stringItemChosen);
} while (itemChosen > 13);
} else;
JOptionPane.showMessageDialog( null, myItems.get(itemChosen).toString(), "", JOptionPane.PLAIN_MESSAGE);
} //end showItem() module
//--
public int itemBrowser()
{
JOptionPane.showMessageDialog( null, myItems.get(selectedItem).toString(), "", JOptionPane.PLAIN_MESSAGE); FowlerButton itemChoice = new FowlerButton();
itemChoice.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
itemChoice.setSize( 275, 110 );
itemChoice.setVisible( true );
return selectedItem;
} //displays the item selected in gui
//-
public int nextItem()
{
selectedItem = selectedItem + 1;
return selectedItem;
}
//
public int prevItem()
{
selectedItem = selectedItem - 1;
return selectedItem;
}
//
public static void main(String[] args)
{
FowlerInventoryGUI2 myInventory = new FowlerInventory();
int choice = 0;
do
{
String stringChoice =
JOptionPane.showInputDialog("Welcome to the Hocus Pocus Magic Shop Inventory Program! Please choose an option from the menu\n [1] View one item only\n[2] Browse Stock\n[3] Exit\nTotal stock value on hand: $18550.00");
choice = Integer.parseInt( stringChoice );
//--
switch (choice)
{
case 1:
myInventory.showItem();
choice = 0; //causes loop to continue
break;
case 2:
myInventory.itemBrowser();
choice = 0;
break;
case 3:
JOptionPane.showMessageDialog( null, "Goodbye!", "", JOptionPane.PLAIN_MESSAGE );
break; //exits the program
default:
choice = 0; //causes loop to continue
break;
} // end switch
} while ( choice == 0); //end while
//
} // end main
//
}// end program
public class FowlerItemGUI2 implements Comparable <FowlerItemGUI2>
{
//--
private String name;
private double price;
private double quantity;
private String description;
private double restockFee;
private double stockValue;
//-
public FowlerItemGUI2(String name, double price, double quantity, String description, double restockFee, double stockValue)
{
this.name = name;
this.price = price;
this.quantity = quantity;
this.description = description;
this.restockFee = restockFee;
this.stockValue = stockValue;
}
//
public String getName()
{
return this.name;
}
//
public double getPrice()
{
return this.price;
}
//
public double getQuantity()
{
return this.quantity;
}
//
public String getDescription()
{
return this.description;
}
//
public double getRestockFee()
{
return this.restockFee;
}
//
public double getStockValue()
{
return this.stockValue;
}
//
public void setName(String name)
{
this.name = name;
}
//
public void setPrice(double price)
{
this.price = price;
}
//
public void setQuantity (double quantity)
{
this.quantity = quantity;
}
//
public void setDescription (String description)
{
this.description = description;
}
//
public void setRestockFee (double restockFee)
{
this.restockFee = restockFee;
}
//
public void setStockValue (double stockValue)
{
this.stockValue = stockValue;
}
//-
public int compareTo(FowlerItemGUI2 another)
{
return (this.name.compareTo(another.name));
}
//-
@Override
public String toString()
{
return "Item Name: " + name + "\n" + "Price: $" + String.valueOf(price) + "\n" + "Quantity: " + String.valueOf(quantity) + "\n" + "Description: "+ description + "\n"+ "Restock Fee 5%: $" + String.valueOf(restockFee) + "\n" + "Stock Value: $" + String.valueOf(stockValue) + "\n";
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class FowlerButton extends JFrame
{
private JButton previousButton;
private JButton nextButton;
FowlerInventoryGUI2 inventory = new FowlerInventoryGUI2();
public FowlerButton()
{
super( "" );
setLayout( new FlowLayout() );
previousButton = new JButton( "Previous Item" );
add( previousButton );
PreviousButtonHandler previousHandler = new PreviousButtonHandler();
previousButton.addActionListener( previousHandler );
nextButton = new JButton( "Next Item" );
add(nextButton);
NextButtonHandler nextHandler = new NextButtonHandler();
nextButton.addActionListener( nextHandler);
} // end button constructor
private class PreviousButtonHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
inventory.prevItem();
inventory.itemBrowser();
} // end actionPerformed
} // end PreviousButtonHandler
private class NextButtonHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
inventory.nextItem();
inventory.itemBrowser();
} // end actionPerformed
} // end NextButtonHandler
} // end Button Class