kpu8
asked on
Invemtory program with Java Swing GUI gives me one error I can't debug
All,
The code below gives me this one error I can't seem to figure out:
Inventory5.java:239: cannot resolve symbol
symbol : method format (java.lang.String,java.lan g.String,i nt,int,dou ble,java.
lang.String,double,double)
location: class java.lang.String
return String.format("Product: %s\nItem Number: %d\nIn Stock: %d
\nPrice: $%.2f\nType: %s\nTotal Value of Stock: $%.2f\nRestock Cost: $%.2f\n\n\n
",
^
1 error
Essentially I'm trying to adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the and the user clicks on the previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.
Add a company logo to the GUI using Java graphics classes.
The code below gives me this one error I can't seem to figure out:
Inventory5.java:239: cannot resolve symbol
symbol : method format (java.lang.String,java.lan
lang.String,double,double)
location: class java.lang.String
return String.format("Product: %s\nItem Number: %d\nIn Stock: %d
\nPrice: $%.2f\nType: %s\nTotal Value of Stock: $%.2f\nRestock Cost: $%.2f\n\n\n
",
^
1 error
Essentially I'm trying to adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the and the user clicks on the previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.
Add a company logo to the GUI using Java graphics classes.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Inventory5
{
static int dispProd = 0; // variable for actionEvents
public static void main(final String args[])
{
int i; // varialbe for looping
double total = 0; // variable for total inventory
// Instantiate a product object
final ProductAdd[] nwProduct = new ProductAdd[5];
// Instantiate objects for the array
for (i=0; i<5; i++)
{
nwProduct[0] = new ProductAdd("Quantum of Solace", 101, 10, 24.00, "CD");
nwProduct[1] = new ProductAdd("Slumdog Millionaire", 102, 10, 25.75, "DVD");
nwProduct[2] = new ProductAdd("Iron Man", 103, 10, 25.50, "DVD");
nwProduct[3] = new ProductAdd("The Dark Knight", 104, 10, 25.00, "Box");
nwProduct[4] = new ProductAdd("Gran Torino", 105, 10, 23.00, "CD");
}
for (i=0; i<5; i++)
total += nwProduct.length; // calculate total inventory cost
final JButton firstBtn = new JButton("First"); // first button
final JButton prevBtn = new JButton("Previous"); // previous button
final JButton nextBtn = new JButton("Next"); // next button
final JButton lastBtn = new JButton("Last"); // last button
final JLabel label; // logo
final JTextArea textArea; // text area for product list
final JPanel buttonJPanel; // panel to hold buttons
//JLabel constructor for logo
Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
label = new JLabel(logo); // create logo label
label.setToolTipText("Companies Design"); // create tooltip
buttonJPanel = new JPanel(); // set up panel
buttonJPanel.setLayout( new GridLayout(1, 4)); //set layout
// add buttons to buttonPanel
buttonJPanel.add(firstBtn);
buttonJPanel.add(prevBtn);
buttonJPanel.add(nextBtn);
buttonJPanel.add(lastBtn);
textArea = new JTextArea(nwProduct[3]+"\n"); // create textArea for product display
// add total inventory value to GUI
textArea.append("\nTotal value of Inventory "+new java.text.DecimalFormat("$0.00").format(total)+"\n\n");
textArea.setEditable(false); // make text uneditable in main display
JFrame invFrame = new JFrame(); // create JFrame container
invFrame.setLayout(new BorderLayout()); // set layout
invFrame.getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER); // add textArea to JFrame
invFrame.getContentPane().add(buttonJPanel, BorderLayout.SOUTH); // add buttons to JFrame
invFrame.getContentPane().add(label, BorderLayout.NORTH); // add logo to JFrame
invFrame.setTitle("Office Min Inventory"); // set JFrame title
invFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // termination command
invFrame.setSize(400, 400); // set size of JPanel
invFrame.setLocationRelativeTo(null); // set screem location
invFrame.setVisible(true); // display window
// assign actionListener and actionEvent for each button
firstBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd = 0;
textArea.setText(nwProduct[dispProd]+"\n");
} // end firstBtn actionEvent
}
); // end firstBtn actionListener
prevBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd--;
if (dispProd < 0)
{
dispProd = 0;
}
textArea.setText(nwProduct[dispProd]+"\n");
} // end prevBtn actionEvent
}); // end prevBtn actionListener
lastBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd = nwProduct.length-1;
textArea.setText(nwProduct[dispProd]+"\n");
} // end lastBtn actionEvent
}); // end lastBtn actionListener
nextBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd++;
if (dispProd >= nwProduct.length)
{
dispProd = nwProduct.length-1;
}
textArea.setText(nwProduct[dispProd]+"\n");
} // end nextBtn actionEvent
}); // end nextBtn actionListener
} // end main
} // end class Inventory5
class Product
{
protected String prodName; // name of product
protected int itmNumber; // item number
protected int units; // number of units
protected double price; // price of each unit
protected double value; // value of total units
public Product(String name, int number, int unit, double each) // Constructor for class Product
{
prodName = name;
itmNumber = number;
units = unit;
price = each;
} // end constructor
public void setProdName(String name) // method to set product name
{
prodName = name;
}
public String getProdName() // method to get product name
{
return prodName;
}
public void setItmNumber(int number) // method to set item number
{
itmNumber = number;
}
public int getItmNumber() // method to get item number
{
return itmNumber;
}
public void setUnits(int unit) // method to set number of units
{
units = unit;
}
public int getUnits() // method to get number of units
{
return units;
}
public void setPrice(double each) // method to set price
{
price = each;
}
public double getPrice() // method to get price
{
return price;
}
public double calcValue() // method to set value
{
return units * price;
}
} // end class Product
class ProductAdd extends Product
{
private String feature; // variable for added feature
public ProductAdd(String name, int number, int unit, double each, String addFeat)
{
// call to superclass Product constructor
super(name, number, unit, each);
feature = addFeat;
}// end constructor
public void setFeature(String addFeat) // method to set added feature
{
feature = addFeat;
}
public String getFeature() // method to get added feature
{
return feature;
}
public double calcValueRstk() // method to set value and add restock fee
{
return units * price * 0.05;
}
public String toString()
{
return String.format("Product: %s\nItem Number: %d\nIn Stock: %d\nPrice: $%.2f\nType: %s\nTotal Value of Stock: $%.2f\nRestock Cost: $%.2f\n\n\n",
getProdName(),
getItmNumber(),
getUnits(),
getPrice(),
getFeature(),
calcValue(),
calcValueRstk());
}
} // end class ProductAdd
you need to be using java 1.5+ to use that method
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.