I am now trying to modify my Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee.
This is what I have and receive 2 errors in the GUI and 5 errors in the inventory. The CD and Product files come back fine. There are four files total, each broke with ______________
Thanks for your help, it is really and truly appreciated.
Craig
CODE
public class CD extends Product {
public static void main(String[] args) {
}
// title of the CD
private String title;
// constructor
public CD(int item, String name, int units, double price, String title) {
super(item,name,units, price);
this.title = title;
}
public String getTitle() {
return title;
}
// total value with the 5% fee
public double value() {
return super.value()*1.05;
}
}
_______________________________________________________
CODE
import java.util.*;
public class Inventory {
// Stores multiple products...
public static void main(String[] args) {
}
private CD[] prods; // field
// Constructor, based on the size of the array we want
public Inventory(int size) {
prods = new CD[size];
}
// Add a product
public void add(int loc, CD p) {
prods[loc] = p;
}
// getter for a product
public CD get(int loc) {
return prods[loc];
}
// sort the array!
public void sort() {
Arrays.sort(prods);
}
public int size() {
return prods.length;
}
// Total the value of all the products
public double totalValue() {
double val = 0.0;
for (int i = 0; i < prods.length; i++) {
val += prods[i].value();
}
return val;
}
}
___________________________________________________________
CODE
import javax.swing.*;
import java.awt.event.*;
public class GUI1 extends JFrame implements ActionListener {
private JTextArea output;
private int current = 0;
private Inventory inv;
private JButton next;
public GUI1() {
super("Product GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // end the program when we are done
// number of products...
int number = Integer.parseInt(JOptionPane.showInputDialog("Please tell me the number of products to enter"));
// Set up the inventory
inv = new Inventory(number);
// enter the product info using a series of dialogs
for (int i = 0; i < number; i++) {
int item = Integer.parseInt(JOptionPane.showInputDialog("Enter the item's number"));
String name = JOptionPane.showInputDialog("Enter the item's name");
int units = Integer.parseInt(JOptionPane.showInputDialog("Enter the units in stock"));
double price = Double.parseDouble(JOptionPane.showInputDialog("Enter the price of each item"));
String title = JOptionPane.showInputDialog("Enter the title");
// make the object
CD p = new CD(item, name, units, price, title);
// put it in the inventory
inv.add(i,p);
}
// Sort the inv
inv.sort();
// now popup the "real" gui for the display of the products...
JPanel content = new JPanel();
setContentPane(content);
content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
output = new JTextArea(20,50);
output.setEditable(false);
content.add(output);
next = new JButton("Next");
next.setActionCommand("Next");
next.addActionListener(this);
content.add(next);
if (inv.size() > 0)
display(inv.get(0));
else {
next.setText("End");
next.setEnabled(false);
}
if (current == inv.size()-1) {
next.setText("End");
next.setEnabled(false);
}
}
// button pushes...
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("Next")) {
if (current < inv.size()-1) {
current++;
CD p = inv.get(current);
display(p);
}
if (current == inv.size()-1) {
next.setText("End");
next.setEnabled(false);
}
}
}
// show a record
public void display(CD p) {
String str = "";
str += "Item number: " + p.getItem() + "\n";
str += "Item name: " + p.getName() + "\n";
str += "Items in stock: " + p.getUnits() + "\n";
str += "Price: $" + p.getPrice() + "\n";
str += "Fee: $" + (p.value() - p.value()/1.05) + "\n";
str += "Value (including the fee): $" + p.value() + "\n";
// total value of it
str += "Total fee: $" + (inv.totalValue() - inv.totalValue()/1.05) + "\n";
str += "Total value (including the fee): $" + inv.totalValue() + "\n";
output.setText(str);
}
public static void main(String args[]) {
// setup and display the gui
GUI1 g = new GUI1();
g.pack();
g.setVisible(true);
}
}
__________________________________________________________
CODE
// Stores a Product
public class Product implements Comparable {
public static void main(String[] args) {
}
// fields
private int item;
private String name;
private int units;
private double price;
// constructor
public Product(int item, String name, int units, double price) {
this.item = item;
this.name = name;
this.units = units;
this.price = price;
}
// total value
public double value() {
return units*price;
}
// getters and setters
public int getItem() {
return item;
}
public void setItem(int item) {
this.item = item;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUnits() {
return units;
}
public void setUnits(int units) {
this.units = units;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// for the sorting
public int compareTo(Object p) {
final Product o = (Product)p; // make it a product
return (this.getName().compareToIgnoreCase(o.getName())); // compare the names
}
}
Edited to use the [ code] tags so we can see where we are going
This post has been edited by pbl: 24 Aug, 2008 - 09:41 PM