I am trying to add the Delete button, save and search buttons. I have tried to call my teacher and he is absolutly no help and i have read and reread the text but still have no idea what is going on.........I have the added the add button but now i am stuck. Is there a simple way in completeing this??
Please help
• Due Date: Day 7 [Individual] forum
• Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit.An item added to the inventory should have an item number one more than the previous last item.
• Add a Save button to the GUI that saves the inventory to a C:\data\inventory.dat file.
• Use exception handling to create the directory and file if necessary.
• Add a search button to the GUI that allows the user to search for an item in the inventory by the product name. If the product is not found, the GUI should display an appropriate message. If the product is found, the GUI should display that product’s information in the
GUI. • Post as an attachment
67 21989
you need to post the code you have so far - contributers can then comment on your problems
I am totally lost in this class and i have no clue what is going on here. My teacher is no help and i cannot figure this out to save my life. I need to add a Delete button, a search button and be able to save it so that the added items will show up after i close it and reopen it.
Any help would be greatly appriciated -
-
import java.util.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.awt.*;
-
-
class Product implements Comparable {
-
String itemName; // class variable that stores the item name
-
double itemNumber; // class variable that stores the item number
-
long stockQuantity; // class variable that stores the quantity in stock
-
double price; // class variable that stores the item price
-
-
public Product() {
-
itemName = "";
-
itemNumber = 0.0;
-
stockQuantity = 0L;
-
price = 0.0;
-
-
}
-
public Product(String name, int number, long stockQuantity, double price) {
-
this.itemName = name;
-
this.itemNumber = number;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
public void setItemName(String name) {
-
this.itemName = itemName;
-
}
-
public String getItemName() {
-
return itemName;
-
}
-
public void setItemNumber(double number) {
-
this.itemNumber = itemNumber;
-
}
-
public double getItemNumber() {
-
return itemNumber;
-
}
-
public void setStockQuantity(long quantity) {
-
stockQuantity = quantity;
-
}
-
public long getStockQuantity() {
-
return stockQuantity;
-
}
-
public void setItemPrice(double price) {
-
this.price = price;
-
}
-
public double getItemPrice() {
-
return price;
-
}
-
public double calculateInventoryValue() {
-
return getItemPrice() * getStockQuantity();
-
}
-
public int compareTo (Object o) {
-
Product p = (Product)o;
-
return itemName.compareTo(p.getItemName());
-
}
-
public String toString() {
-
return "Title :"+getItemName() + "\nStock Number"+itemNumber+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
-
}
-
-
}
-
-
class DVDTitle extends Product implements Comparable {
-
private String rating;
-
public DVDTitle() {
-
super(); //Call the constructor in Product
-
rating = ""; //Add the additonal attribute
-
}
-
public DVDTitle(String itemName, int itemNumber, long stockQuantity, double price, String rating) {
-
super(itemName, itemNumber, stockQuantity, price); //Call the constructor in Product
-
this.rating = rating; //Add the additonal attribute
-
}
-
public void setRating(String rating) {
-
this.rating = rating;
-
}
-
public String getRating() {
-
return rating;
-
}
-
public double calculateInventoryValue() {
-
return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05; //Had you forgotten to include the restocking fee?
-
}
-
//What happens when we want to change the restocking fee?
-
public double calculateRestockFee() {
-
return getItemPrice() * 0.05;
-
}
-
public int compareTo (Object o) {
-
Product p = (Product)o;
-
return getItemName().compareTo(p.getItemName());
-
}
-
public String toString() {
-
return "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nRating :"+getRating()+"\nValue"+calculateInventoryValue();
-
}
-
}
-
-
-
I will have to post the other part of the code to another post.
Here is the other part of that code -
-
public class Inventory1 extends JFrame implements ActionListener {
-
//Utility class for displaying the picture
-
//If we are going to use a class/method/variable inside that class only, we declare it private in that class
-
private class MyPanel extends JPanel {
-
ImageIcon image = new ImageIcon("dvd.gif");
-
int width = image.getIconWidth();
-
int height = image.getIconHeight();
-
long angle = 30;
-
public MyPanel(){
-
super();
-
}
-
public void paintComponent(Graphics g){
-
super.paintComponent(g);
-
Graphics2D g2d = (Graphics2D)g;
-
//g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
-
g2d.drawImage(image.getImage(), 60, 60, this);
-
g2d.dispose();
-
}
-
}//end class MyPanel
-
-
int currentIndex; //Currently displayed Item
-
Product[] supplies = new Product[4];
-
JLabel itemName ;
-
JLabel itemNumber;
-
JLabel rating;
-
JLabel quantity;
-
JLabel price;
-
JLabel fee;
-
JLabel totalValue;
-
JTextField itemNameField = new JTextField(20);
-
JTextField itemNumberField = new JTextField(20);
-
JTextField ratingField = new JTextField(20);
-
JTextField quantityField = new JTextField(20);
-
JTextField priceField = new JTextField(20);
-
-
JPanel display;
-
JPanel displayHolder;
-
JPanel panel;
-
public Inventory1() {
-
makeTheDataItems();
-
setSize(600, 500);
-
setTitle("Jeremy's DVD Inventory Program");
-
//make the panels
-
display = new JPanel();
-
JPanel other = new JPanel();
-
JPanel picture = new MyPanel();
-
JPanel buttons = new JPanel();
-
JPanel centerPanel = new JPanel();
-
displayHolder = new JPanel();
-
display.setLayout(new GridLayout(5, 3));
-
other.setLayout(new GridLayout(2, 1));
-
//make the labels
-
itemName = new JLabel("Name :");
-
itemNumber = new JLabel("Number :");
-
rating = new JLabel("Rating :");
-
quantity = new JLabel("Quantity :");
-
price = new JLabel("Price :");
-
fee = new JLabel("Fee :");
-
totalValue = new JLabel("Total Value :");
-
//Use the utility method to make the buttons
-
JButton first = makeButton("First");
-
JButton next = makeButton("Next");
-
JButton previous = makeButton("Previous");
-
JButton last = makeButton("Last");
-
JButton exit = makeButton("Exit");
-
//Other buttons
-
JButton add = makeButton("Add");
-
-
//Add the labels to the display panel
-
display.add(itemName);
-
display.add(itemNumber);
-
display.add(rating);
-
display.add(quantity);
-
display.add(price);
-
display.add(fee);
-
//add the buttons to the buttonPanel
-
buttons.add(first);
-
buttons.add(previous);
-
buttons.add(next);
-
buttons.add(last);
-
buttons.add(exit);
-
//Add the picture panel and display to the centerPanel
-
displayHolder.add(display);
-
centerPanel.setLayout(new GridLayout(2, 1));
-
centerPanel.add(picture);
-
centerPanel.add(displayHolder);
-
other.add(buttons);
-
JPanel forAdd = new JPanel(); // add the other buttons to this panel
-
forAdd.add(add);
-
other.add(forAdd);
-
-
//Add the panels to the frame
-
getContentPane().add(centerPanel, "Center");
-
getContentPane().add(other, "South");
-
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
-
setVisible(true);
-
}
-
private void makeTheDataItems () {
-
Product p1 = new DVDTitle("XMEN-The Last Stand", 001, 200, 12.99, "PG-13");
-
Product p2 = new DVDTitle("Harry Potter-The Goblet of Fire", 002, 50, 9.95, "PG-13");
-
Product p3 = new DVDTitle("Jackass-Number Two", 003, 100, 19.95, "R");
-
Product p4 = new DVDTitle("Pirates of the Caribbean", 004, 75, 9.99, "PG-13");
-
-
supplies[0] = p1;
-
supplies[1] = p2;
-
supplies[2] = p3;
-
supplies[3] = p4;
-
}
-
//Utility method for creating and dressing buttons
-
private JButton makeButton(String label) {
-
JButton button = new JButton(label);
-
button.setActionCommand(label);
-
button.addActionListener(this);
-
return button;
-
}
-
private void addItem() {
-
System.out.println("eeeeeeeeee");
-
panel = new JPanel();
-
JPanel add = new JPanel();
-
add.setLayout(new GridLayout(2, 1));
-
add.setLayout(new GridLayout(4, 4));
-
JButton addIt = makeButton("Add Item");
-
JLabel itemName = new JLabel("Name :");
-
//JLabel itemNumber = new JLabel("Number :");
-
JLabel rating = new JLabel("Rating :");
-
JLabel quantity = new JLabel("Quantity :");
-
JLabel price = new JLabel("Price :");
-
add.add(itemName); add.add(itemNameField);
-
//add.add(itemNumber); add.add(itemNumberField);
-
add.add(rating); add.add(ratingField);
-
add.add(quantity); add.add(quantityField);
-
add.add(price); add.add(priceField);
-
panel.add(add);
-
JPanel forAddIt = new JPanel();
-
forAddIt.add(addIt);
-
panel.add(forAddIt);
-
displayHolder.remove(display);
-
displayHolder.add(panel);
-
//display = panel;
-
this.setVisible(true);
-
}
-
-
public static void main( String args[]) {
-
Inventory1 object = new Inventory1(); //The main method should not have too much code
-
} // end main method
-
public void actionPerformed(ActionEvent event) {
-
String command = event.getActionCommand(); //This retrieves the command that we set for the button
-
//Always compare strings using the .equals method and not using ==
-
if(command.equals("First")) {
-
displayFirst();
-
}
-
else if(command.equals("Next")) {
-
displayNext();
-
}
-
else if(command.equals("Previous")) {
-
displayPrevious();
-
}
-
else if(command.equals("Last")) {
-
displayLast();
-
}
-
else if(command.equals("Exit")) {
-
this.dispose();
-
System.exit(0);
-
}
-
else if(command.equals("Add")) {
-
addItem();
-
}
-
else if(command.equals("Add Item")) {
-
addItemToArray();
-
}
-
-
}
-
private void addItemToArray() {
-
Product p = new DVDTitle(itemNameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
-
Double.parseDouble(priceField.getText()), ratingField.getText());
-
//Extend size of array by one first
-
Product[] ps = new Product[supplies.length + 1];
-
for(int i = 0; i < ps.length-1; i++) {
-
ps[i] = supplies[i];
-
}
-
ps[supplies.length] = p;
-
supplies = ps;
-
displayHolder.remove(panel);
-
displayHolder.add(display);
-
displayLast();
-
this.setVisible(false);
-
this.setVisible(true);
-
}
-
//Utility method to ease the typing and reuse code
-
//This method reduces the number of lines of our code
-
private void displayItemAt(int index) {
-
DVDTitle product = (DVDTitle)supplies[index];
-
itemName.setText("Item Name: "+ product.getItemName());
-
itemNumber.setText("Item Number: "+ product.getItemNumber());
-
rating.setText("Rating: "+ product.getRating());
-
quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
-
price.setText("Item Price: "+ product.getItemPrice());
-
totalValue.setText("Total: " + product.calculateInventoryValue());
-
fee.setText("Fee :"+product.calculateRestockFee());
-
this.setVisible(true);
-
}
-
public void displayFirst() {
-
displayItemAt(0);
-
currentIndex = 0;
-
}
-
public void displayNext() {
-
if(currentIndex == supplies.length-1) {
-
displayFirst();
-
currentIndex = 0;
-
}
-
else {
-
displayItemAt(currentIndex + 1);
-
currentIndex++;
-
}
-
}
-
public void displayPrevious() {
-
if(currentIndex == 0) {
-
displayLast();
-
currentIndex = supplies.length-1;
-
}
-
else {
-
displayItemAt(currentIndex - 1);
-
currentIndex--;
-
}
-
}
-
public void displayLast() {
-
displayItemAt(supplies.length-1);
-
currentIndex = supplies.length-1;
-
}
-
}//end class Inventory2.1
-
-
-
I have the add button working......I just need help with the other.
Thanks
When I try to compile the Inventory1 class I get 87 errors. Why is this? I am also working on this assignment.
When I try to compile the Inventory1 class I get 87 errors. Why is this? I am also working on this assignment.
Did you import the required packages for GUI? Did you compile the Produvt class first?
I compiled the product and DVD class first, but I am not sure how to import the GUI components. This class is really getting difficult now, only two weeks left.
I compiled the product and DVD class first, but I am not sure how to import the GUI components. This class is really getting difficult now, only two weeks left.
To import classes in the awt package simply put
import java.awt.*; at the top of the java file.
If you are still getting errors post the code you are using.
Ok, I am still getting errors. The code I am trying is the above code, I am basically just trying to get an idea of how the program should look. I also tried to put the import at the top of the Inventory1 program, but I am getting the same errors.
Ok, I got it to compile, I just needed to add a couple more imports at the top of the inventory program.
Ok, I got it to compile, I just needed to add a couple more imports at the top of the inventory program.
I hope you understand all of it.
Ok, after running the program I noticed that this is a step ahead of what I need. What I need to do is have that code display just a list. here is a link to what it is suppose to look like, we can pick whichever way to display, the top or bottom version . http://i88.photobucket.com/albums/k1...oryexample.jpg
If you can help at all with this I will forever be in debt.
Here is my code from last week's assignment. We are to modify this to look like the link above: -
class Product implements Comparable
-
{
-
private String title; // class variable that stores the item name
-
private int item; // class variable that stores the item number
-
private double stockQuantity; // class variable that stores the quantity in stock
-
private double price; // class variable that stores the item price
-
-
public Product()
-
{
-
title = "";
-
item = 0;
-
stockQuantity = 0;
-
price = 0.0;
-
}
-
-
public Product(String title, int item, double stockQuantity, double price)
-
{
-
this.title = title;
-
this.item = item;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
-
public void setTitle(String title)
-
{
-
this.title = title;
-
}
-
-
public String getTitle()
-
{
-
return title;
-
}
-
-
public void setItem(int item)
-
{
-
this.item = item;
-
}
-
-
public int getItem()
-
{
-
return item;
-
}
-
-
public void setStockQuantity(double quantity)
-
{
-
stockQuantity = quantity;
-
}
-
-
public double getStockQuantity()
-
{
-
return stockQuantity;
-
}
-
-
public void setItemPrice(double price)
-
{
-
this.price = price;
-
}
-
-
public double getItemPrice()
-
{
-
return price;
-
}
-
-
public double calculateInventoryValue()
-
{
-
return (price * stockQuantity)*.05+(price*stockQuantity);
-
}
-
-
public int compareTo(Object o)
-
{
-
Product p = null;
-
try
-
{
-
p = (Product) o;
-
}
-
catch (ClassCastException cE)
-
{
-
cE.printStackTrace();
-
}
-
return title.compareTo(p.getTitle());
-
}
-
-
public String toString()
-
{
-
return "DVD Title: " + title + "\nItem #: " + item + "\nPrice: $" + price + "\nQuantity: "
-
+ stockQuantity + "\nValue: $" + calculateInventoryValue();
-
}
-
}
-
-
-
class DVD extends Product
-
{
-
-
String genre;
-
double restockingFee;
-
-
-
public DVD(String title, int item, double stockQuantity,double price ,double restockingFee, String genre)
-
{
-
super(title,item, stockQuantity, price);
-
this.genre = genre;
-
this.restockingFee = 0.05;
-
-
// TODO Auto-generated constructor stub
-
}
-
-
//returns the value of the inventory, plus the restocking fee
-
public double getInventoryValue()
-
{
-
// TODO Auto-generated method stub
-
return super.getItemPrice() + restockingFee;
-
}
-
-
public String toString()
-
{
-
//this is not efficient
-
//StringBuffer sb = new StringBuffer("Genre \t").append(genre).append("\n");
-
//sb.append(super.toString());
-
-
return super.toString() + "\nGenre \t" + genre + "\nRestockingFee \t" +restockingFee;
-
}
-
-
}
-
-
-
-
-
public class Inventory2
-
{
-
Product[] supplies;
-
-
public static void main(String[] args)
-
{
-
Inventory2 inventory = new Inventory2();
-
inventory.addProduct(new DVD("Beerfest", 1, 15, 22.95,.05,"Action"));
-
inventory.addProduct(new DVD("Illustionist", 2, 25, 25.95,.05,"Comedy"));
-
inventory.addProduct(new DVD("Employee of the Month", 3, 10,23.95,.05,"Comedy"));
-
-
System.out.println("Inventory of DVD Movies:\n\n");
-
-
-
System.out.println();
-
inventory.showInventory();
-
inventory.sortByName();
-
System.out.println();
-
inventory.showInventory();
-
-
double total = inventory.calculateTotalInventory();
-
System.out.println("Total Value is: $" + total);
-
-
-
}
-
-
public void sortByName()
-
{
-
for (int i = 1; i < supplies.length; i++)
-
{
-
int j;
-
Product val = supplies[i];
-
for (j = i - 1; j > -1; j--)
-
{
-
Product temp = supplies[j];
-
if (temp.compareTo(val) <= 0)
-
{
-
break;
-
}
-
supplies[j + 1] = temp;
-
}
-
supplies[j + 1] = val;
-
}
-
}
-
-
//creates a String representation of the array of products
-
public String toString()
-
{
-
String s = "";
-
for (Product p : supplies)
-
{
-
s = s + p.toString();
-
s = s + "\n\n";
-
}
-
return s;
-
}
-
-
//Using an array so adding an item requires us to increase the size of the array first
-
public void addProduct(Product p1)
-
{
-
if (supplies == null)
-
{
-
supplies = new Product[0];
-
}
-
Product[] p = supplies; //Copy all products into p first
-
Product[] temp = new Product[p.length + 1]; //create bigger array
-
for (int i = 0; i < p.length; i++)
-
{
-
temp[i] = p[i];
-
}
-
temp[(temp.length - 1)] = p1; //add the new product at the last position
-
supplies = temp;
-
}
-
-
//sorting the array using Bubble Sort
-
public double calculateTotalInventory()
-
{
-
double total = 0.0;
-
for (int i = 0; i < supplies.length; i++)
-
{
-
total = total + supplies[i].calculateInventoryValue();
-
}
-
return total;
-
}
-
-
public void showInventory()
-
{
-
System.out.println(toString()); //call our toString method
-
}
-
}
-
Also, with the OP's code, how do you get your image to show? I tried to make a logo, but I don't see anything. What I did was I made an image and named it "dvd.gif" and put it in the same folder as the code. Is this the correct way to do that?
Also, with the OP's code, how do you get your image to show? I tried to make a logo, but I don't see anything. What I did was I made an image and named it "dvd.gif" and put it in the same folder as the code. Is this the correct way to do that?
Alright Steve let's do this one step at a time. Were you able to display the products on a panel on the JFrame?
Ok, I was able to get the OP's code to load properly. I have the same assignment due this Sunday. I think I may be to late to turn in the assignment from last week, the one that I gave the link to. So I guess I will try to work on the assignment for this week. Here is the link for this assingment.
https://ecampus.wintu.edu/afm101/secure/view-attachment.jspa? ID=2016792&messageID=10060298&name=Week%208%20chec kpoint%202%20example.doc
Ok, I was able to get the OP's code to load properly. I have the same assignment due this Sunday. I think I may be to late to turn in the assignment from last week, the one that I gave the link to. So I guess I will try to work on the assignment for this week. Here is the link for this assingment.
https://ecampus.wintu.edu/afm101/secure/view-attachment.jspa? ID=2016792&messageID=10060298&name=Week%208%20chec kpoint%202%20example.doc
That link requires me to log in with details which I don't have. Just post the code for the stage you are at and tell us what the next step is.
Here is the code: -
import java.util.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.awt.*;
-
-
class Product implements Comparable {
-
String itemName; // class variable that stores the item name
-
double itemNumber; // class variable that stores the item number
-
long stockQuantity; // class variable that stores the quantity in stock
-
double price; // class variable that stores the item price
-
-
public Product() {
-
itemName = "";
-
itemNumber = 0.0;
-
stockQuantity = 0L;
-
price = 0.0;
-
-
}
-
public Product(String name, int number, long stockQuantity, double price) {
-
this.itemName = name;
-
this.itemNumber = number;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
public void setItemName(String name) {
-
this.itemName = itemName;
-
}
-
public String getItemName() {
-
return itemName;
-
}
-
public void setItemNumber(double number) {
-
this.itemNumber = itemNumber;
-
}
-
public double getItemNumber() {
-
return itemNumber;
-
}
-
public void setStockQuantity(long quantity) {
-
stockQuantity = quantity;
-
}
-
public long getStockQuantity() {
-
return stockQuantity;
-
}
-
public void setItemPrice(double price) {
-
this.price = price;
-
}
-
public double getItemPrice() {
-
return price;
-
}
-
public double calculateInventoryValue() {
-
return getItemPrice() * getStockQuantity();
-
}
-
public int compareTo (Object o) {
-
Product p = (Product)o;
-
return itemName.compareTo(p.getItemName());
-
}
-
public String toString() {
-
return "Title :"+getItemName() + "\nStock Number"+itemNumber+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
-
}
-
-
}
-
-
class DVD extends Product implements Comparable {
-
private String rating;
-
public DVD() {
-
super(); //Call the constructor in Product
-
rating = ""; //Add the additonal attribute
-
}
-
public DVD(String itemName, int itemNumber, long stockQuantity, double price, String rating) {
-
super(itemName, itemNumber, stockQuantity, price); //Call the constructor in Product
-
this.rating = rating; //Add the additonal attribute
-
}
-
public void setRating(String rating) {
-
this.rating = rating;
-
}
-
public String getRating() {
-
return rating;
-
}
-
public double calculateInventoryValue() {
-
return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05; //Had you forgotten to include the restocking fee?
-
}
-
//What happens when we want to change the restocking fee?
-
public double calculateRestockFee() {
-
return getItemPrice() * 0.05;
-
}
-
public int compareTo (Object o) {
-
Product p = (Product)o;
-
return getItemName().compareTo(p.getItemName());
-
}
-
public String toString() {
-
return "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nRating :"+getRating()+"\nValue"+calculateInventoryValue();
-
}
-
}
-
-
-
import java.util.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.awt.*;
-
-
public class Inventory1 extends JFrame implements ActionListener {
-
//Utility class for displaying the picture
-
//If we are going to use a class/method/variable inside that class only, we declare it private in that class
-
private class MyPanel extends JPanel {
-
ImageIcon image = new ImageIcon("dvd.gif");
-
int width = image.getIconWidth();
-
int height = image.getIconHeight();
-
long angle = 30;
-
public MyPanel(){
-
super();
-
}
-
public void paintComponent(Graphics g){
-
super.paintComponent(g);
-
Graphics2D g2d = (Graphics2D)g;
-
//g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
-
g2d.drawImage(image.getImage(), 60, 60, this);
-
g2d.dispose();
-
}
-
}//end class MyPanel
-
-
int currentIndex; //Currently displayed Item
-
Product[] supplies = new Product[4];
-
JLabel itemName ;
-
JLabel itemNumber;
-
JLabel rating;
-
JLabel quantity;
-
JLabel price;
-
JLabel fee;
-
JLabel totalValue;
-
JTextField itemNameField = new JTextField(20);
-
JTextField itemNumberField = new JTextField(20);
-
JTextField ratingField = new JTextField(20);
-
JTextField quantityField = new JTextField(20);
-
JTextField priceField = new JTextField(20);
-
-
JPanel display;
-
JPanel displayHolder;
-
JPanel panel;
-
public Inventory1() {
-
makeTheDataItems();
-
setSize(600, 500);
-
setTitle("DVD Inventory Program");
-
//make the panels
-
display = new JPanel();
-
JPanel other = new JPanel();
-
JPanel picture = new MyPanel();
-
JPanel buttons = new JPanel();
-
JPanel centerPanel = new JPanel();
-
displayHolder = new JPanel();
-
display.setLayout(new GridLayout(5, 3));
-
other.setLayout(new GridLayout(2, 1));
-
//make the labels
-
itemName = new JLabel("Name :");
-
itemNumber = new JLabel("Number :");
-
rating = new JLabel("Rating :");
-
quantity = new JLabel("Quantity :");
-
price = new JLabel("Price :");
-
fee = new JLabel("Fee :");
-
totalValue = new JLabel("Total Value :");
-
//Use the utility method to make the buttons
-
JButton first = makeButton("First");
-
JButton next = makeButton("Next");
-
JButton previous = makeButton("Previous");
-
JButton last = makeButton("Last");
-
JButton exit = makeButton("Exit");
-
//Other buttons
-
JButton add = makeButton("Add");
-
-
//Add the labels to the display panel
-
display.add(itemName);
-
display.add(itemNumber);
-
display.add(rating);
-
display.add(quantity);
-
display.add(price);
-
display.add(fee);
-
//add the buttons to the buttonPanel
-
buttons.add(first);
-
buttons.add(previous);
-
buttons.add(next);
-
buttons.add(last);
-
buttons.add(exit);
-
//Add the picture panel and display to the centerPanel
-
displayHolder.add(display);
-
centerPanel.setLayout(new GridLayout(2, 1));
-
centerPanel.add(picture);
-
centerPanel.add(displayHolder);
-
other.add(buttons);
-
JPanel forAdd = new JPanel(); // add the other buttons to this panel
-
forAdd.add(add);
-
other.add(forAdd);
-
-
//Add the panels to the frame
-
getContentPane().add(centerPanel, "Center");
-
getContentPane().add(other, "South");
-
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
-
setVisible(true);
-
}
-
private void makeTheDataItems () {
-
Product p1 = new DVDTitle("Beerfest", 001, 200, 12.99, "R");
-
Product p2 = new DVDTitle("The Illusionist", 002, 50, 9.95, "PG-13");
-
Product p3 = new DVDTitle("Employee of the Month ", 003, 100, 19.95, "R");
-
Product p4 = new DVDTitle("Pirates of the Caribbean 2 ", 004, 75, 9.99, "PG-13");
-
-
supplies[0] = p1;
-
supplies[1] = p2;
-
supplies[2] = p3;
-
supplies[3] = p4;
-
}
-
//Utility method for creating and dressing buttons
-
private JButton makeButton(String label) {
-
JButton button = new JButton(label);
-
button.setActionCommand(label);
-
button.addActionListener(this);
-
return button;
-
}
-
private void addItem() {
-
System.out.println("eeeeeeeeee");
-
panel = new JPanel();
-
JPanel add = new JPanel();
-
add.setLayout(new GridLayout(2, 1));
-
add.setLayout(new GridLayout(4, 4));
-
JButton addIt = makeButton("Add Item");
-
JLabel itemName = new JLabel("Name :");
-
//JLabel itemNumber = new JLabel("Number :");
-
JLabel rating = new JLabel("Rating :");
-
JLabel quantity = new JLabel("Quantity :");
-
JLabel price = new JLabel("Price :");
-
add.add(itemName); add.add(itemNameField);
-
//add.add(itemNumber); add.add(itemNumberField);
-
add.add(rating); add.add(ratingField);
-
add.add(quantity); add.add(quantityField);
-
add.add(price); add.add(priceField);
-
panel.add(add);
-
JPanel forAddIt = new JPanel();
-
forAddIt.add(addIt);
-
panel.add(forAddIt);
-
displayHolder.remove(display);
-
displayHolder.add(panel);
-
//display = panel;
-
this.setVisible(true);
-
}
-
-
public static void main( String args[]) {
-
Inventory1 object = new Inventory1(); //The main method should not have too much code
-
} // end main method
-
public void actionPerformed(ActionEvent event) {
-
String command = event.getActionCommand(); //This retrieves the command that we set for the button
-
//Always compare strings using the .equals method and not using ==
-
if(command.equals("First")) {
-
displayFirst();
-
}
-
else if(command.equals("Next")) {
-
displayNext();
-
}
-
else if(command.equals("Previous")) {
-
displayPrevious();
-
}
-
else if(command.equals("Last")) {
-
displayLast();
-
}
-
else if(command.equals("Exit")) {
-
this.dispose();
-
System.exit(0);
-
}
-
else if(command.equals("Add")) {
-
addItem();
-
}
-
else if(command.equals("Add Item")) {
-
addItemToArray();
-
}
-
-
}
-
private void addItemToArray() {
-
Product p = new DVDTitle(itemNameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
-
Double.parseDouble(priceField.getText()), ratingField.getText());
-
//Extend size of array by one first
-
Product[] ps = new Product[supplies.length + 1];
-
for(int i = 0; i < ps.length-1; i++) {
-
ps[i] = supplies[i];
-
}
-
ps[supplies.length] = p;
-
supplies = ps;
-
displayHolder.remove(panel);
-
displayHolder.add(display);
-
displayLast();
-
this.setVisible(false);
-
this.setVisible(true);
-
}
-
//Utility method to ease the typing and reuse code
-
//This method reduces the number of lines of our code
-
private void displayItemAt(int index) {
-
DVDTitle product = (DVDTitle)supplies[index];
-
itemName.setText("Item Name: "+ product.getItemName());
-
itemNumber.setText("Item Number: "+ product.getItemNumber());
-
rating.setText("Rating: "+ product.getRating());
-
quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
-
price.setText("Item Price: "+ product.getItemPrice());
-
totalValue.setText("Total: " + product.calculateInventoryValue());
-
fee.setText("Fee :"+product.calculateRestockFee());
-
this.setVisible(true);
-
}
-
public void displayFirst() {
-
displayItemAt(0);
-
currentIndex = 0;
-
}
-
public void displayNext() {
-
if(currentIndex == supplies.length-1) {
-
displayFirst();
-
currentIndex = 0;
-
}
-
else {
-
displayItemAt(currentIndex + 1);
-
currentIndex++;
-
}
-
}
-
public void displayPrevious() {
-
if(currentIndex == 0) {
-
displayLast();
-
currentIndex = supplies.length-1;
-
}
-
else {
-
displayItemAt(currentIndex - 1);
-
currentIndex--;
-
}
-
}
-
public void displayLast() {
-
displayItemAt(supplies.length-1);
-
currentIndex = supplies.length-1;
-
}
-
}//end class Inventory2.1
-
-
I need to display a company logo, this is where I am having trouble
-
-
-
import java.util.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.awt.*;
-
-
class Product implements Comparable {
-
String name;
-
double number;
-
long stockQuantity;
-
double price;
-
-
public Product() {
-
name = "";
-
number = 0.0;
-
stockQuantity = 0L;
-
price = 0.0;
-
-
}
-
public Product(String name, int number, long stockQuantity, double price) {
-
this.name = name;
-
this.number = number;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
public void setItemName(String name) {
-
this.name = name;
-
}
-
public String getItemName() {
-
return name;
-
}
-
public void setItemNumber(double number) {
-
this.number = number;
-
}
-
public double getItemNumber() {
-
return number;
-
}
-
public void setStockQuantity(long quantity) {
-
stockQuantity = quantity;
-
}
-
public long getStockQuantity() {
-
return stockQuantity;
-
}
-
public void setItemPrice(double price) {
-
this.price = price;
-
}
-
public double getItemPrice() {
-
return price;
-
}
-
public double calculateInventoryValue() {
-
return getItemPrice() * getStockQuantity();
-
}
-
public int compareTo (Object o) {
-
Product p = (Product)o;
-
return name.compareTo(p.getItemName());
-
}
-
public String toString() {
-
return "Name :"+getItemName() + "\nNumber"+number+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
-
}
-
-
}
-
-
-
-
class DVD extends Product implements Comparable {
-
private String title;
-
public DVD() {
-
super(); //Call the constructor in Product
-
title = ""; //Add the additonal attribute
-
}
-
public DVD(String name, int number, long stockQuantity, double price, String title) {
-
super(name, number, stockQuantity, price); //Call the constructor in Product
-
this.title = title; //Add the additonal attribute
-
}
-
public void setTitle(String title) {
-
this.title = title;
-
}
-
public String getTitle() {
-
return title;
-
}
-
public double calculateInventoryValue() {
-
return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05;
-
}
-
-
public double calculateRestockFee() {
-
return getItemPrice() * 0.05;
-
}
-
public int compareTo (Object o) {
-
Product p = (Product)o;
-
return getItemName().compareTo(p.getItemName());
-
}
-
public String toString() {
-
return "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nTitle :"+getTitle()+"\nValue"+calculateInventoryValue();
-
}
-
}
-
-
public class Inventory2 extends JFrame implements ActionListener {
-
-
//Utility class for displaying the picture
-
//If we are going to use a class/method/variable inside that class only, we declare it private in that class
-
private class MyPanel extends JPanel {
-
ImageIcon image = new ImageIcon("Sample.jpg");
-
int width = image.getIconWidth();
-
int height = image.getIconHeight();
-
long angle = 30;
-
-
public MyPanel(){
-
super();
-
}
-
public void paintComponent(Graphics g){
-
super.paintComponent(g);
-
Graphics2D g2d = (Graphics2D)g;
-
g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
-
g2d.drawImage(image.getImage(), 60, 60, this);
-
g2d.dispose();
-
}
-
}//end class MyPanel
-
-
-
int currentIndex; //Currently displayed Item
-
Product[] supplies = new Product[4];
-
JLabel name ;
-
JLabel number;
-
JLabel title;
-
JLabel quantity;
-
JLabel price;
-
JLabel fee;
-
JLabel totalValue;
-
-
JTextField nameField = new JTextField(20);
-
JTextField numberField = new JTextField(20);
-
JTextField titleField = new JTextField(20);
-
JTextField quantityField = new JTextField(20);
-
JTextField priceField = new JTextField(20);
-
-
-
JPanel display;
-
JPanel displayHolder;
-
JPanel panel;
-
public Inventory2() {
-
makeTheDataItems();
-
-
setSize(500, 500);
-
setTitle("Inventory Program");
-
//make the panels
-
display = new JPanel();
-
JPanel other = new JPanel();
-
JPanel picture = new MyPanel();
-
JPanel buttons = new JPanel();
-
JPanel centerPanel = new JPanel();
-
displayHolder = new JPanel();
-
display.setLayout(new GridLayout(7, 1));
-
//other.setLayout(new GridLayout(1, 1));
-
-
//make the labels
-
name = new JLabel("Name :");
-
number = new JLabel("Number :");
-
title = new JLabel("Title :");
-
quantity = new JLabel("Quantity :");
-
price = new JLabel("Price :");
-
fee = new JLabel("Fee :");
-
totalValue = new JLabel("Total Value :");
-
-
//Use the utility method to make the buttons
-
JButton first = makeButton("First");
-
JButton next = makeButton("Next");
-
JButton previous = makeButton("Previous");
-
JButton last = makeButton("Last");
-
JButton exit = makeButton("Exit");
-
-
//Other buttons
-
JButton add = makeButton("Add");
-
-
-
//Add the labels to the display panel
-
display.add(name);
-
display.add(number);
-
display.add(title);
-
display.add(quantity);
-
display.add(price);
-
display.add(fee);
-
-
//add the buttons to the buttonPanel
-
buttons.add(first);
-
buttons.add(previous);
-
buttons.add(next);
-
buttons.add(last);
-
buttons.add(exit);
-
-
//Add the picture panel and display to the centerPanel
-
displayHolder.add(display);
-
centerPanel.setLayout(new GridLayout(2, 1));
-
centerPanel.add(picture);
-
centerPanel.add(displayHolder);
-
other.add(buttons);
-
JPanel forAdd = new JPanel(); // add the other buttons to this panel
-
forAdd.add(add);
-
other.add(forAdd);
-
-
-
//Add the panels to the frame
-
-
getContentPane().add(centerPanel, "Center");
-
getContentPane().add(other, "South");
-
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
-
setVisible(true);
-
}
-
-
private void makeTheDataItems () {
-
Product p1 = new DVD("test", 001, 200, 100, "The one");
-
Product p2 = new DVD("test", 002, 500, 10000, "test");
-
Product p3 = new DVD("test", 003, 100, 3000, "test");
-
Product p4 = new DVD("test", 100, 3000, 9000, "test");
-
supplies[0] = p1;
-
supplies[1] = p2;
-
supplies[2] = p3;
-
supplies[3] = p4;
-
}
-
-
//Utility method for creating and dressing buttons
-
private JButton makeButton(String label) {
-
JButton button = new JButton(label);
-
button.setActionCommand(label);
-
button.addActionListener(this);
-
return button;
-
}
-
-
private void addItem() {
-
panel = new JPanel();
-
JPanel add = new JPanel();
-
add.setLayout(new GridLayout(2, 1));
-
add.setLayout(new GridLayout(4, 4));
-
JButton addIt = makeButton("Add Item");
-
-
JLabel name = new JLabel("Name :");
-
//JLabel number = new JLabel("Number :");
-
JLabel title = new JLabel("Title :");
-
JLabel quantity = new JLabel("Quantity :");
-
JLabel price = new JLabel("Price :");
-
-
add.add(name); add.add(nameField);
-
//add.add(number); add.add(numberField);
-
add.add(title); add.add(titleField);
-
add.add(quantity); add.add(quantityField);
-
add.add(price); add.add(priceField);
-
panel.add(add);
-
JPanel forAddIt = new JPanel();
-
forAddIt.add(addIt);
-
panel.add(forAddIt);
-
displayHolder.remove(display);
-
displayHolder.add(panel);
-
-
//display = panel;
-
this.setVisible(true);
-
}
-
-
-
public static void main( String args[]) {
-
Inventory2 object = new Inventory2(); //The main method should not have too much code
-
} // end main method
-
-
public void actionPerformed(ActionEvent event) {
-
String command = event.getActionCommand(); //This retrieves the command that we set for the button
-
//Always compare strings using the .equals method and not using ==
-
if(command.equals("First")) {
-
displayFirst();
-
}
-
else if(command.equals("Next")) {
-
displayNext();
-
}
-
else if(command.equals("Previous")) {
-
displayPrevious();
-
}
-
else if(command.equals("Last")) {
-
displayLast();
-
}
-
else if(command.equals("Exit")) {
-
this.dispose();
-
System.exit(0);
-
}
-
else if(command.equals("Add")) {
-
addItem();
-
}
-
else if(command.equals("Add Item")) {
-
addItemToArray();
-
}
-
-
-
}
-
-
private void addItemToArray() {
-
Product p = new DVD(nameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
-
Double.parseDouble(priceField.getText()), titleField.getText());
-
//Extend size of array by one first
-
Product[] ps = new Product[supplies.length + 1];
-
for(int i = 0; i < ps.length-1; i++) {
-
ps[i] = supplies[i];
-
}
-
ps[supplies.length] = p;
-
supplies = ps;
-
displayHolder.remove(panel);
-
displayHolder.add(display);
-
displayLast();
-
this.setVisible(false);
-
this.setVisible(true);
-
-
}
-
-
//Utility method to ease the typing and reuse code
-
//This method reduces the number of lines of our code
-
private void displayItemAt(int index) {
-
MobilePhone product = (MobilePhone)supplies[index];
-
name.setText("Item Name: "+ product.getItemName());
-
number.setText("Item Number: "+ product.getItemNumber());
-
title.setText("Title: "+ product.getBrand());
-
quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
-
price.setText("Item Price: "+ product.getItemPrice());
-
totalValue.setText("Total: " + product.calculateInventoryValue());
-
fee.setText("Fee :"+product.calculateRestockFee());
-
this.repaint();
-
this.setVisible(true);
-
}
-
-
public void displayFirst() {
-
displayItemAt(0);
-
currentIndex = 0;
-
}
-
-
public void displayNext() {
-
if(currentIndex == supplies.length-1) {
-
displayFirst();
-
currentIndex = 0;
-
}
-
else {
-
displayItemAt(currentIndex + 1);
-
currentIndex++;
-
-
}
-
}
-
-
public void displayPrevious() {
-
if(currentIndex == 0) {
-
displayLast();
-
currentIndex = supplies.length-1;
-
}
-
else {
-
displayItemAt(currentIndex - 1);
-
currentIndex--;
-
}
-
}
-
-
public void displayLast() {
-
displayItemAt(supplies.length-1);
-
currentIndex = supplies.length-1;
-
}
-
-
}//end class Inventory2
-
-
I've edited some code I already had which is not really fully functional yet (I hope you will be able to change that).
You should ask where you don't understand and point out where it differs from your requirements
Ok, I am getting some errors when trying to compile the edited code. I noticed their was some code about a mobile phone in there too. I will try to sit down and go through it to see if I can see what's wrong.
Thanks again so much for all of your help, you don't know how much I appreciate it.
Ok, I am getting some errors when trying to compile the edited code. I noticed their was some code about a mobile phone in there too. I will try to sit down and go through it to see if I can see what's wrong.
Thanks again so much for all of your help, you don't know how much I appreciate it.
It was initially used with DVD replaced by MobilePhone and was the last stage we reached on it. I simply removed the Mobile phone stuff and replaced it with DVD stuff. When I compiled it it was fine so if you get any compilation errors please post them and let's see if you are doing anything wrong.
Ah cool, I got it to work. I just had to change a couple things around and it looks great. Now, all I need to do is display a company logo some how. Thanks again, you rock!
I must have something set up wrong in the MyPanel part of the code, am I correct? I just need to figure out how to display the logo.
I must have something set up wrong in the MyPanel part of the code, am I correct? I just need to figure out how to display the logo.
Put the image in the same folder as the code is in and change the name Sample.jpg in the MyPanel class to the name of your image.
Ok, I changed the image name, but it is still not appearing. Do I need to edit the size in the code? I think I'm getting really close, just one thing is missing I think.
Ah, I've got it finally. Something must not of saved right, it is now displaying. Thanks so much, I'm sure I'll be back here with the next step.
Ok r035198x, it is time for my final assignment and I am way lost. I could really use your expertise if you have the time. Here is what the assignment is:
• Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item.
• Add a Save button to the GUI that saves the inventory to a C:\data\inventory.dat file.
• Use exception handling to create the directory and file if necessary.
• Add a search button to the GUI that allows the user to search for an item in the inventory by the product name. If the product is not found, the GUI should display an appropriate message. If the product is found, the GUI should display that product’s information in the GUI.
If you can help me at all I would greatly appreciate it. This class has proven to be too difficult for a beginner in Java, and it is definately not something that can be taught in 9 weeks.
Ok r035198x, it is time for my final assignment and I am way lost. I could really use your expertise if you have the time. Here is what the assignment is:
• Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item.
• Add a Save button to the GUI that saves the inventory to a C:\data\inventory.dat file.
• Use exception handling to create the directory and file if necessary.
• Add a search button to the GUI that allows the user to search for an item in the inventory by the product name. If the product is not found, the GUI should display an appropriate message. If the product is found, the GUI should display that product’s information in the GUI.
If you can help me at all I would greatly appreciate it. This class has proven to be too difficult for a beginner in Java, and it is definately not something that can be taught in 9 weeks.
You will do well to understand every part of this assignment as it covers most of the core elementary concepts in Java. Now please post the current code you have now. It is now important to be able to have the interface showing right.
-
-
-
import java.util.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.awt.*;
-
-
-
-
-
-
public class Inventory2 extends JFrame implements ActionListener {
-
-
//Utility class for displaying the picture
-
//If we are going to use a class/method/variable inside that class only, we declare it private in that class
-
private class MyPanel extends JPanel {
-
ImageIcon image = new ImageIcon("Sample.jpg");
-
int width = image.getIconWidth();
-
int height = image.getIconHeight();
-
long angle = 30;
-
-
public MyPanel(){
-
super();
-
}
-
public void paintComponent(Graphics g){
-
super.paintComponent(g);
-
Graphics2D g2d = (Graphics2D)g;
-
g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
-
g2d.drawImage(image.getImage(), 60, 60, this);
-
g2d.dispose();
-
}
-
}//end class MyPanel
-
-
-
int currentIndex; //Currently displayed Item
-
Product[] supplies = new Product[4];
-
JLabel name ;
-
JLabel number;
-
JLabel title;
-
JLabel quantity;
-
JLabel price;
-
JLabel fee;
-
JLabel totalValue;
-
-
JTextField nameField = new JTextField(20);
-
JTextField numberField = new JTextField(20);
-
JTextField titleField = new JTextField(20);
-
JTextField quantityField = new JTextField(20);
-
JTextField priceField = new JTextField(20);
-
-
-
JPanel display;
-
JPanel displayHolder;
-
JPanel panel;
-
boolean locked = false; //Notice how I've used this flag to keep the interface clean
-
public Inventory2() {
-
makeTheDataItems();
-
-
setSize(700, 500);
-
setTitle("Inventory Program");
-
//make the panels
-
display = new JPanel();
-
JPanel other = new JPanel();
-
other.setLayout(new GridLayout(2, 1));
-
JPanel picture = new MyPanel();
-
JPanel buttons = new JPanel();
-
JPanel centerPanel = new JPanel();
-
displayHolder = new JPanel();
-
display.setLayout(new GridLayout(7, 1));
-
//other.setLayout(new GridLayout(1, 1));
-
-
//make the labels
-
name = new JLabel("Name :");
-
number = new JLabel("Number :");
-
title = new JLabel("Title :");
-
quantity = new JLabel("Quantity :");
-
price = new JLabel("Price :");
-
fee = new JLabel("Fee :");
-
totalValue = new JLabel("Total Value :");
-
-
//Use the utility method to make the buttons
-
JButton first = makeButton("First");
-
JButton next = makeButton("Next");
-
JButton previous = makeButton("Previous");
-
JButton last = makeButton("Last");
-
JButton search = makeButton("Search");
-
-
-
//Other buttons
-
JButton add = makeButton("Add");
-
JButton modify = makeButton("Modify");
-
JButton delete = makeButton("Delete");
-
JButton save = makeButton("Save");
-
JButton exit = makeButton("Exit");
-
-
//Add the labels to the display panel
-
display.add(name);
-
display.add(number);
-
display.add(title);
-
display.add(quantity);
-
display.add(price);
-
display.add(fee);
-
-
//add the buttons to the buttonPanel
-
buttons.add(first);
-
buttons.add(previous);
-
buttons.add(next);
-
buttons.add(last);
-
buttons.add(search);
-
-
-
//Add the picture panel and display to the centerPanel
-
displayHolder.add(display);
-
centerPanel.setLayout(new GridLayout(2, 1));
-
centerPanel.add(picture);
-
centerPanel.add(displayHolder);
-
other.add(buttons);
-
JPanel forAdd = new JPanel(); // add the other buttons to this panel
-
forAdd.add(add);
-
forAdd.add(modify);
-
forAdd.add(delete);
-
forAdd.add(save);
-
forAdd.add(exit);
-
other.add(forAdd);
-
-
-
//Add the panels to the frame
-
-
getContentPane().add(centerPanel, "Center");
-
getContentPane().add(other, "South");
-
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
-
setVisible(true);
-
}
-
-
private void makeTheDataItems () {
-
Product p1 = new DVD("The one", 001, 200, 100, "The one");
-
Product p2 = new DVD("Once upon a time in China V", 002, 500, 10000, "Once upon a time in China V");
-
Product p3 = new DVD("Rat Race", 003, 100, 3000, "Rat Race");
-
Product p4 = new DVD("The Man in the Iron Mask", 004, 3000, 9000, "The Man in the Iron Mask");
-
supplies[0] = p1;
-
supplies[1] = p2;
-
supplies[2] = p3;
-
supplies[3] = p4;
-
}
-
-
//Utility method for creating and dressing buttons
-
private JButton makeButton(String label) {
-
JButton button = new JButton(label);
-
button.setPreferredSize(new Dimension(100, 25));
-
button.setActionCommand(label);
-
button.addActionListener(this);
-
return button;
-
}
-
-
private void addItem() {
-
panel = new JPanel();
-
JPanel add = new JPanel();
-
-
add.setLayout(new GridLayout(7, 2));
-
JButton addIt = makeButton("Add Item");
-
-
JLabel name = new JLabel("Name :");
-
JLabel title = new JLabel("Title :");
-
JLabel quantity = new JLabel("Quantity :");
-
JLabel price = new JLabel("Price :");
-
-
add.add(name); add.add(nameField);
-
-
add.add(title); add.add(titleField);
-
add.add(quantity); add.add(quantityField);
-
add.add(price); add.add(priceField);
-
panel.add(add);
-
JPanel forAddIt = new JPanel();
-
forAddIt.add(addIt);
-
panel.add(forAddIt);
-
-
displayHolder.remove(display);
-
displayHolder.add(panel);
-
-
//display = panel;
-
this.setVisible(true);
-
}
-
-
-
public static void main( String args[]) {
-
new Inventory2().displayFirst(); //The main method should not have too much code
-
} // end main method
-
-
public void actionPerformed(ActionEvent event) {
-
String command = event.getActionCommand(); //This retrieves the command that we set for the button
-
//Always compare strings using the .equals method and not using ==
-
if(command.equals("First")) {
-
if(!locked) {
-
displayFirst();
-
}
-
}
-
else if(command.equals("Next")) {
-
if(!locked) {
-
displayNext();
-
}
-
}
-
else if(command.equals("Previous")) {
-
if(!locked) {
-
displayPrevious();
-
}
-
}
-
else if(command.equals("Last")) {
-
if(!locked) {
-
displayLast();
-
}
-
}
-
else if(command.equals("Exit")) {
-
this.dispose();
-
System.exit(0);
-
}
-
else if(command.equals("Add")) {
-
if(!locked) {
-
addItem();
-
locked = true;
-
}
-
}
-
else if(command.equals("Add Item")) {
-
if(!locked) {
-
addItemToArray();
-
locked = true;
-
}
-
}
-
else if(command.equals("Modify")) {
-
if(!locked) {
-
modify();
-
locked = true;
-
}
-
}
-
else if(command.equals("Update")) {
-
if(!locked) {
-
modifyItemInArray();
-
locked = true;
-
}
-
}
-
else if(command.equals("Delete")) {
-
if(!locked) {
-
DVD dvd = (DVD)supplies[currentIndex];
-
int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete item "+dvd.getItemNumber());
-
if(confirm == JOptionPane.YES_OPTION) {
-
removeItemAt(currentIndex);
-
displayFirst();
-
}
-
}
-
-
}
-
-
}
-
private void modify() {
-
DVD dvd = (DVD)supplies[currentIndex];
-
panel = new JPanel();
-
JPanel add = new JPanel();
-
add.setLayout(new GridLayout(7, 2));
-
JButton update = makeButton("Update");
-
JLabel number = new JLabel("Number :");
-
JLabel name = new JLabel("Name :");
-
JLabel title = new JLabel("Title :");
-
JLabel quantity = new JLabel("Quantity :");
-
JLabel price = new JLabel("Price :");
-
add.add(number);
-
numberField.setText(""+dvd.getItemNumber()); numberField.setEditable(false); add.add(numberField);
-
add.add(name);
-
nameField.setText(dvd.getItemName()); add.add(nameField);
-
titleField.setText(dvd.getTitle()); titleField.setEditable(false);
-
add.add(title); add.add(titleField);
-
add.add(quantity);
-
quantityField.setText(""+dvd.getStockQuantity());
-
add.add(quantityField);
-
add.add(price);
-
add.add(priceField); priceField.setText(""+dvd.getItemPrice());
-
panel.add(add);
-
JPanel forAddIt = new JPanel();
-
forAddIt.add(update);
-
panel.add(forAddIt);
-
displayHolder.remove(display);
-
displayHolder.add(panel);
-
//display = panel;
-
this.setVisible(true);
-
-
}
-
-
private void addItemToArray() {
-
Product p = new DVD(nameField.getText(), supplies.length + 1, Long.parseLong(quantityField.getText()),
-
Double.parseDouble(priceField.getText()), titleField.getText());
-
//Extend size of array by one first
-
Product[] ps = new Product[supplies.length + 1];
-
for(int i = 0; i < ps.length-1; i++) {
-
ps[i] = supplies[i];
-
}
-
ps[supplies.length] = p;
-
supplies = ps;
-
displayHolder.remove(panel);
-
displayHolder.add(display);
-
displayLast();
-
this.setVisible(false);
-
this.setVisible(true);
-
-
}
-
-
//Utility method to ease the typing and reuse code
-
//This method reduces the number of lines of our code
-
private void displayItemAt(int index) {
-
-
DVD product = (DVD)supplies[index];
-
name.setText("Item Name: "+ product.getItemName());
-
number.setText("Item Number: "+ product.getItemNumber());
-
title.setText("Title: "+ product.getTitle());
-
quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
-
price.setText("Item Price: "+ product.getItemPrice());
-
totalValue.setText("Total: " + product.calculateInventoryValue());
-
fee.setText("Fee :"+product.calculateRestockFee());
-
locked = false;
-
this.repaint();
-
-
-
this.setVisible(true);
-
}
-
-
private void modifyItemInArray() {
-
Product p = new DVD(nameField.getText(), supplies.length + 1, Long.parseLong(quantityField.getText()),
-
Double.parseDouble(priceField.getText()), titleField.getText());
-
supplies[currentIndex] = p;
-
displayHolder.remove(panel);
-
displayHolder.add(display);
-
displayItemAt(currentIndex);
-
this.setVisible(false);
-
this.setVisible(true);
-
-
}
-
-
private void removeItemAt(int index) {
-
Product[] temp = new Product[supplies.length-1];
-
int counter = 0;
-
for(int i = 0; i < supplies.length;i++) {
-
if(i == index) { //skip the item to delete
-
}
-
else {
-
temp[counter++] = supplies[i];
-
}
-
}
-
supplies = temp;
-
}
-
-
public void displayFirst() {
-
displayItemAt(0);
-
currentIndex = 0;
-
}
-
-
public void displayNext() {
-
if(currentIndex == supplies.length-1) {
-
displayFirst();
-
currentIndex = 0;
-
}
-
else {
-
displayItemAt(currentIndex + 1);
-
currentIndex++;
-
-
}
-
}
-
-
public void displayPrevious() {
-
if(currentIndex == 0) {
-
displayLast();
-
currentIndex = supplies.length-1;
-
}
-
else {
-
displayItemAt(currentIndex - 1);
-
currentIndex--;
-
}
-
}
-
-
public void displayLast() {
-
displayItemAt(supplies.length-1);
-
currentIndex = supplies.length-1;
-
}
-
-
}//end class Inventory2
-
-
-
Only two buttons not yet working now. This should not take a day to complete now. I've deliberately stopped there because I'm sure you have lots of questions to ask at this current stage. You must understand everyline of code there first before we finish it off
-
-
-
class Product implements Comparable {
-
String name;
-
double number;
-
long stockQuantity;
-
double price;
-
-
public Product() {
-
name = "";
-
number = 0.0;
-
stockQuantity = 0L;
-
price = 0.0;
-
-
}
-
public Product(String name, int number, long stockQuantity, double price) {
-
this.name = name;
-
this.number = number;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
public void setItemName(String name) {
-
this.name = name;
-
}
-
public String getItemName() {
-
return name;
-
}
-
public void setItemNumber(double number) {
-
this.number = number;
-
}
-
public double getItemNumber() {
-
return number;
-
}
-
public void setStockQuantity(long quantity) {
-
stockQuantity = quantity;
-
}
-
public long getStockQuantity() {
-
return stockQuantity;
-
}
-
public void setItemPrice(double price) {
-
this.price = price;
-
}
-
public double getItemPrice() {
-
return price;
-
}
-
public double calculateInventoryValue() {
-
return getItemPrice() * getStockQuantity();
-
}
-
public int compareTo (Object o) {
-
Product p = (Product)o;
-
return name.compareTo(p.getItemName());
-
}
-
public String toString() {
-
return "Name :"+getItemName() + "\nNumber"+number+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
-
}
-
-
}
-
-
-
-
class DVD extends Product implements Comparable {
-
private String title;
-
public DVD() {
-
super(); //Call the constructor in Product
-
title = ""; //Add the additonal attribute
-
}
-
public DVD(String name, int number, long stockQuantity, double price, String title) {
-
super(name, number, stockQuantity, price); //Call the constructor in Product
-
this.title = title; //Add the additonal attribute
-
}
-
public void setTitle(String title) {
-
this.title = title;
-
}
-
public String getTitle() {
-
return title;
-
}
-
public double calculateInventoryValue() {
-
return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05;
-
}
-
-
public double calculateRestockFee() {
-
return getItemPrice() * 0.05;
-
}
-
public int compareTo (Object o) {
-
Product p = (Product)o;
-
return getItemName().compareTo(p.getItemName());
-
}
-
public String toString() {
-
return "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nTitle :"+getTitle()+"\nValue"+calculateInventoryValue();
-
}
-
}
-
-
The Product and DVD classes remain unchanged
I think I am slowly starting to understand the code, and what it is actually doing. I think my biggest problem is trying to figure out the order in which to present the code. This may take some time to digest, but it looks great, thank you so much.
I think I am slowly starting to understand the code, and what it is actually doing. I think my biggest problem is trying to figure out the order in which to present the code. This may take some time to digest, but it looks great, thank you so much.
Stevedub,
Looks like I am in the same class you are but only in Week 5. I am completely lost and the teacher is NO help!
Can you offer any advice for the Wk 5 Checkpoint? It is the Inventory Program Part 1.
ITQUEST
Stevedub,
Looks like I am in the same class you are but only in Week 5. I am completely lost and the teacher is NO help!
Can you offer any advice for the Wk 5 Checkpoint? It is the Inventory Program Part 1.
ITQUEST
Me again...
I am also wondering how to set up a class. I get how to establish the Java file but the class thing has me confused. What is the difference? I guess I have just been getting by until now, but I can't seem to get it right!
ITQUEST
Hehe, I feel your pain bro, wait till you get further into the class. Here is the code I have from week 5: -
// Product class
-
-
-
public class Product {
-
-
private String title;
-
private double item;
-
private double stock;
-
private double price;
-
-
// Constructor to initialize the product information.
-
public Product(String title, double item, double stock,
-
double price) {
-
setTitle(title);
-
setItem(item);
-
setStock(stock);
-
setPrice(price);
-
-
}
-
-
// Method to get the title
-
public String getTitle() {
-
return title;
-
}
-
-
// Method to get the item number
-
public double getItem() {
-
return item;
-
}
-
-
//Method to get the stock
-
public double getStock() {
-
return stock;
-
}
-
-
//Method to get the price
-
public double getPrice() {
-
return price;
-
-
-
-
}
-
-
// Method to set the title name.
-
public void setTitle(String title) {
-
this.title = title;
-
}
-
-
// Method to set the item number.
-
public void setItem(double item) {
-
this.item = item;
-
}
-
-
// Method to set the stock available.
-
public void setStock(double stock) {
-
this.stock = stock;
-
}
-
-
//Method to set the price.
-
public void setPrice(double price) {
-
this.price = price;
-
-
}
-
// Method to compute the value of inventory.
-
public double getInvValue() {
-
return this.stock *this.price;
-
}
-
-
}
-
-
-
// This is a new class Inventory
-
// Inventory program
-
// Main application
-
-
-
-
public class Inventory
-
{
-
public static void main (String args [])
-
{
-
-
System.out.println ("Inventory of DVD Movies:\n");
-
-
String title = "Beerfest";
-
double item = 1;
-
double stock = 15;
-
double price = 22.50;
-
double value;
-
-
Product product = new Product (title, item, stock, price);
-
-
System.out.printf( "%s%18s\n", "DVD Title:", product.getTitle() );
-
-
System.out.printf( "%s%16s\n", "Item #:", product.getItem() );
-
-
System.out.printf( "%s%8s\n", "Number in Stock:" , product.getStock() );
-
-
System.out.printf( "%s%18s\n", "Price:", product.getPrice() );
-
-
-
-
System.out.printf( "%s%9s\n", "Inventory Value:", product.getInvValue() );
-
-
}
-
}
-
Let me know if you have any questions about the code.
Let me know if you have any questions about the code.
Do you post the first half as your .java and the second as your .class to get it to run? I understand how you have it written. I can read the material and understand it when I see it, but putting the two together is the challenge for me.
I have to say that all of the classes so far have been fine, but this one is bad. Do you mind me asking questions along the way or will you leave this forum after you complete the course?
ITQUEST
Do you post the first half as your .java and the second as your .class to get it to run? I understand how you have it written. I can read the material and understand it when I see it, but putting the two together is the challenge for me.
I have to say that all of the classes so far have been fine, but this one is bad. Do you mind me asking questions along the way or will you leave this forum after you complete the course?
ITQUEST
I run it and get the following error:
NoClassDefFoundError: Product
Any suggestions?
I run it and get the following error:
NoClassDefFoundError: Product
Any suggestions?
.java classes are compiled to into .class files. If you have a .java file called Product.java, when you compile it a .class is created for every class in the file Product.java if there are no errors. In this case you are getting this error because there is no class file called Product.class so you must compile the Product class first.
Yes, you have to put each part of the program in a seperate file and compile them using the name of the class. Make sure to save the program with the .java extension. Also make sure both files are in the same location.
As for comming back after this assignment, I will gladly try to help out once I get this final assignment figured out. ro35198x has been such a great help for me, and I would be glad to pass on the knowledge he has shared to me.
Should he start a new thread r035198x so we don't get the two projects confused?
Hey r035198x, for some reason the add feature doesn't seem to be working for me any more, I'm not sure what got changed exactly.
Hey r035198x, for some reason the add feature doesn't seem to be working for me any more, I'm not sure what got changed exactly.
Ok I see where the problem is. Just change the addItem part to -
-
else if(command.equals("Add Item")) {
-
addItemToArray();
-
}
-
That part does not need to check the lock
Is this the part I am suppose to change? I don't think I did it right. -
//Always compare strings using the .equals method and not using ==
-
if(command.equals("First")) {
-
if(!locked) {
-
displayFirst();
-
}
-
}
-
else if(command.equals("Next")) {
-
if(!locked) {
-
displayNext();
-
}
-
}
-
else if(command.equals("Previous")) {
-
if(!locked) {
-
displayPrevious();
-
}
-
}
-
else if(command.equals("Last")) {
-
if(!locked) {
-
displayLast();
-
}
-
}
-
else if(command.equals("Exit")) {
-
this.dispose();
-
System.exit(0);
-
}
-
-
}
-
}
-
else if(command.equals("Add Item")) {
-
addItemToArray();
-
}
-
}
-
}
-
else if(command.equals("Modify")) {
-
if(!locked) {
-
modify();
-
locked = true;
-
}
-
}
-
else if(command.equals("Update")) {
-
if(!locked) {
-
modifyItemInArray();
-
locked = true;
-
}
-
}
-
Is this the part I am suppose to change? I don't think I did it right. -
//Always compare strings using the .equals method and not using ==
-
if(command.equals("First")) {
-
if(!locked) {
-
displayFirst();
-
}
-
}
-
else if(command.equals("Next")) {
-
if(!locked) {
-
displayNext();
-
}
-
}
-
else if(command.equals("Previous")) {
-
if(!locked) {
-
displayPrevious();
-
}
-
}
-
else if(command.equals("Last")) {
-
if(!locked) {
-
displayLast();
-
}
-
}
-
else if(command.equals("Exit")) {
-
this.dispose();
-
System.exit(0);
-
}
-
-
}
-
}
-
else if(command.equals("Add Item")) {
-
addItemToArray();
-
}
-
}
-
}
-
else if(command.equals("Modify")) {
-
if(!locked) {
-
modify();
-
locked = true;
-
}
-
}
-
else if(command.equals("Update")) {
-
if(!locked) {
-
modifyItemInArray();
-
locked = true;
-
}
-
}
-
Yes that's the part. Why are you unsure about it?
Well, I get this error when compiled: -
C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:217: 'class' or 'interface' expected
-
else if(command.equals("Add Item")) {
-
^
-
C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:220: 'class' or 'interface' expected
-
}
-
^
-
C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:377: 'class' or 'interface' expected
-
^
-
3 errors
-
-
Tool completed with exit code 1
-
What did I miss?
Well, I get this error when compiled: -
C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:217: 'class' or 'interface' expected
-
else if(command.equals("Add Item")) {
-
^
-
C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:220: 'class' or 'interface' expected
-
}
-
^
-
C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:377: 'class' or 'interface' expected
-
^
-
3 errors
-
-
Tool completed with exit code 1
-
What did I miss?
Could be something to do with your braces. Here is the complete actionPerformed method -
-
public void actionPerformed(ActionEvent event) {
-
String command = event.getActionCommand(); //This retrieves the command that we set for the button
-
//Always compare strings using the .equals method and not using ==
-
if(command.equals("First")) {
-
if(!locked) {
-
displayFirst();
-
}
-
}
-
else if(command.equals("Next")) {
-
if(!locked) {
-
displayNext();
-
}
-
}
-
else if(command.equals("Previous")) {
-
if(!locked) {
-
displayPrevious();
-
}
-
}
-
else if(command.equals("Last")) {
-
if(!locked) {
-
displayLast();
-
}
-
}
-
else if(command.equals("Exit")) {
-
this.dispose();
-
System.exit(0);
-
}
-
else if(command.equals("Add")) {
-
if(!locked) {
-
addItem();
-
locked = true;
-
}
-
}
-
else if(command.equals("Add Item")) {
-
addItemToArray();
-
}
-
else if(command.equals("Modify")) {
-
if(!locked) {
-
modify();
-
locked = true;
-
}
-
}
-
else if(command.equals("Update")) {
-
if(!locked) {
-
modifyItemInArray();
-
locked = true;
-
}
-
}
-
else if(command.equals("Delete")) {
-
if(!locked) {
-
DVD dvd = (DVD)supplies[currentIndex];
-
int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete item "+dvd.getItemNumber());
-
if(confirm == JOptionPane.YES_OPTION) {
-
removeItemAt(currentIndex);
-
displayFirst();
-
}
-
}
-
}
-
}
-
Ok, it works now. I always screw stuff up with the copy and paste, hehe. I think all I need now is to have the modify button to work. Oh, I also have to code in to show the items inventory value + restock fee, and total value, I should be able to figure that one out.
Ok, it works now. I always screw stuff up with the copy and paste, hehe. I think all I need now is to have the modify button to work. Oh, I also have to code in to show the items inventory value + restock fee, and total value, I should be able to figure that one out.
The modify and delete buttons are already working! Only the save and search are not yet done.
Opps, thats what I meant, modify and delete are good to go. I'm telling ya, its been quite the month hehe.
Post your reply Sign in to post your reply or Sign up for a free account.
|