My inventory program was working until I got to the part where I have to incorporate GUI functionality. I have my code and incorporated some GUI code at type, but unable to get them to work together. My code compiles, runs, but acts as if I never added the GUI code. What I am trying to accomplish is: the GUI should display the information one product at a time, including the item number, name of product, number of units in stock, the price of each unit, and the value of the inventory of that product. It should also display the value of the entire inventory, the restocking fee...and have buttons that should allow the user to move to the first item, the previous item and the next item, and last item. If the last item is displayed, and the user clicks on the next button, then the first item should display. I think I have the logic in there, but cant get it to display in GUI format. Can you offer some advice? I have attached my code. Thanks!!
Catgirl 0 Newbie Poster
/**
* CheckPoint Inventory Program Part 5
*/
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.net.URL;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class ToolBarDemo extends JPanel
implements ActionListener {
protected JTextArea textArea;
protected String newline = "\n";
static final private String PREVIOUS = "previous";
static final private String UP = "up";
static final private String NEXT = "next";
public ToolBarDemo() {
super(new BorderLayout());
//Create the toolbar.
JToolBar toolBar = new JToolBar("Still draggable");
addButtons(toolBar);
//Create the text area used for output. Request
//enough space for 5 rows and 30 columns.
textArea = new JTextArea(5, 30);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
//Lay out the main panel.
setPreferredSize(new Dimension(450, 130));
add(toolBar, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
protected void addButtons(JToolBar toolBar) {
JButton button = null;
//first button
button = makeNavigationButton("Back24", PREVIOUS,
"Back to previous something-or-other",
"Previous");
toolBar.add(button);
//second button
button = makeNavigationButton("Up24", UP,
"Up to something-or-other",
"Up");
toolBar.add(button);
//third button
button = makeNavigationButton("Forward24", NEXT,
"Forward to something-or-other",
"Next");
toolBar.add(button);
}
protected JButton makeNavigationButton(String imageName,
String actionCommand,
String toolTipText,
String altText) {
//Look for the image.
String imgLocation = "images/"
+ imageName
+ ".gif";
URL imageURL = ToolBarDemo.class.getResource(imgLocation);
//Create and initialize the button.
JButton button = new JButton();
button.setActionCommand(actionCommand);
button.setToolTipText(toolTipText);
button.addActionListener(this);
if (imageURL != null) { //image found
button.setIcon(new ImageIcon(imageURL, altText));
} else { //no image found
button.setText(altText);
System.err.println("Resource not found: "
+ imgLocation);
}
return button;
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String description = null;
// Handle each button.
if (PREVIOUS.equals(cmd)) { //first button clicked
description = "taken you to the previous <something>.";
} else if (UP.equals(cmd)) { // second button clicked
description = "taken you up one level to <something>.";
} else if (NEXT.equals(cmd)) { // third button clicked
description = "taken you to the next <something>.";
}
displayResult("If this were a real app, it would have "
+ description);
}
protected void displayResult(String actionDescription) {
textArea.append(actionDescription + newline);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ToolBarDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new ToolBarDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
class Product {
// main methods begins execution of java application
public static void main( String args[])
{
}
private String name = "";
private int number = 0;
private double price = 0;
private int quantity = 0;
// Constructor
public Product(int number, String name, int quantity, double price) {
this.number = number;
this.name = name;
this.quantity = quantity;
this.price = price;
}
// Public accessor methods
public String getName() { return name; }
public int getNumber() { return number; }
public double getPrice() { return price; }
public int getQuantity() { return quantity; }
// Get value of product
public double getValue() {
return (double) quantity * price;
}
}
class DVD extends Product {
public DVD(int number, String DVDname, int DVDquantity, double DVDprice) {
super(number, DVDname, DVDquantity, DVDprice);
}
// Overrides the Product's getValue()
// Call inventory and determine value with 5% restock fee
public double getValue() {
double inventoryvalue = super.getValue();
return (inventoryvalue * 1.05);
}
}
public class Inventory
{
// setup inventory to hold 10 products
public static final int MAXIMUM_ITEMS = 10;
private static Product product[] = new DVD[MAXIMUM_ITEMS];
public static void main(String args[]) {
buildInventory();
getInventory();
}
// Build the inventory, storing 10 different products.
public static void buildInventory() {
product[0] = new DVD(0, "Rush Hour",6,12.99);
product[1] = new DVD(1, "Nightmare On Elm Street",8,9.99);
product[2] = new DVD(2, "Amityville Horror",3,15.99);
product[3] = new DVD(3, "The Jackal",7,12.99);
product[4] = new DVD(4, "The Princees Bride",4,19.99);
product[5] = new DVD(5, "Back To School",1,9.99);
product[6] = new DVD(6, "Titanic",3,21.99);
product[7] = new DVD(7, "Total Recall",2,11.99);
product[8] = new DVD(8, "Bad Santa",4,14.99);
product[9] = new DVD(9, "The Bone Collector",1,19.99);
}
// to sort array (I cant get it to work, so I commented out)
// public static void sortEm(String[] array, int len)
// java.util.Arrays.sort(array);
// Loop through products and ptint info for each
public static void getInventory() {
for(int i = 0; i < product.length; i++) {
System.out.println("Number: " + product[i].getNumber());
System.out.println("Name: " + product[i].getName());
System.out.println("Quantity: " + product[i].getQuantity());
System.out.println("Price: " + product[i].getPrice());
System.out.println("Item Value: " + product[i].getQuantity() * product[i].getPrice());
System.out.println("Restock Fee: " + (product[i].getQuantity() * product[i].getPrice()) * 0.05);
System.out.println();
}
}
}
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.