473,372 Members | 1,062 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Java Inventory Program Problem!!!Please HELP!

I cannot figure out how to add the following:
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.

Any help would be greatly appriciated I have been working on this for 8 hours!
Here is my code:
import java.util.Arrays;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JLabel;

class logo extends JPanel
{
public logo()
{
super(new GridLayout(1,1));
JLabel label1;
ImageIcon icon = new ImageIcon("C:/cdlogo.jpg");
label1 = new JLabel(icon);
label1.setVerticalTextPosition(JLabel.BOTTOM);
label1.setHorizontalTextPosition(JLabel.CENTER);
add(label1);//add label
}
}//end class logo

public class Inventory5 {


public static void main(String[] args) {

Album cd = null;
Inventory inventory = new Inventory();

cd = new Album(1, "Songs About Jane", 3, 14.99f, 2003);
System.out.println(cd);
inventory.addAlbum(cd);

cd = new Album(2, "30 Seconds to Mars", 5, 10.99f, 2005);
System.out.println(cd);
inventory.addAlbum(cd);

cd = new Album(3, "Mars Volta", 2, 13.95f, 1995);
System.out.println(cd);
inventory.addAlbum(cd);

cd = new Album(4, "The Mamas and The Papas", 9, 18.99f, 1970);
inventory.addAlbum(cd);
System.out.println(cd);

cd = new Album(5, "Idol Greatest Hits", 7, 15.98f, 2006);
System.out.println(cd);
inventory.addAlbum(cd);

inventory.printInventory();
new InventoryGUI(inventory);
} // end main

} // end Inventory5


class CD {
private int itemNo;
private String title;
private int inStock;
private float unitPrice;

CD(int itemNo, String title, int inStock, float unitPrice) {
this.itemNo = itemNo;
this.title = title;
this.inStock = inStock;
this.unitPrice = unitPrice;
}

public int getItemNo() { return itemNo; }
public String getTitle() { return title; }
public int getInStock() { return inStock; }
public float getUnitPrice() { return unitPrice; }

public float value() {
return inStock * unitPrice;
}

public String toString() {
return String.format("itemNo=%2d title=%-22s inStock=%3d price=$%7.2f value=$%8.2f",
itemNo, title, inStock, unitPrice, value());
}

} // end CD


class Inventory {
// Array of CDs (set it to hold 25 items)
private final int INVENTORY_SIZE = 25;
private CD[] items;
private int numItems;

Inventory() {
items = new Album[INVENTORY_SIZE];
numItems = 0;
}

public int getNumItems() {
return numItems;
}

public CD getCD(int n) {
return items[n];
}


public void addAlbum(CD item) {
items[numItems] = item;
++numItems;
}

public double value() {
double sumOfInventory = 0.0;

for (int i = 0; i < numItems; i++)
sumOfInventory += items[i].value();

return sumOfInventory;
}

public void printInventory() {
System.out.println("\nAmanda’s DVD Inventory\n");

if (numItems <= 0) {
System.out.println("Inventory is empty at the moment.\n");
} else {
for (int i = 0; i < numItems; i++)
System.out.printf("%3d %s\n", i, items[i]);
System.out.printf("\nTotal value in inventory is $%,.2f\n\n", value());
}
}

} // end Inventory



class Album extends CD {

private int albumYear;

public Album(int AlbumID, String itemName, int quantityOnHand, float itemPrice, int year) {
super(AlbumID, itemName, quantityOnHand, itemPrice);

this.albumYear = albumYear;
}


public void setYear(int year) {
albumYear = year;
}

public int getAlbumYear() {
return albumYear;
}

public float value() {
return super.value() + restockingFee();
}

public float restockingFee() {
return super.value() * 0.05f;
}

} // end Album


// GUI for inventory
class InventoryGUI extends JFrame
{
// access CD Collection inventory
private Inventory theInventory;

private int index = 0;


private final JLabel itemNumberLabel = new JLabel(" Item Number:");
private JTextField itemNumberText;

private final JLabel prodnameLabel = new JLabel(" Product Name:");
private JTextField prodnameText;

private final JLabel prodpriceLabel = new JLabel(" Price:");
private JTextField prodpriceText;

private final JLabel numinstockLabel = new JLabel(" Number in Stock:");
private JTextField numinstockText;

private final JLabel valueLabel = new JLabel(" Value:");
private JTextField valueText;

private final JLabel restockingFeeLabel = new JLabel(" Restocking Fee:");
private JTextField restockingFeeText;

private final JLabel totalValueLabel = new JLabel(" Inventory Total Value:");
private JTextField totalValueText;

private JPanel centerPanel;
private JPanel buttonPanel;


InventoryGUI(Inventory inventory) {
super("Amanda’s Movie Inventory");
final Dimension dim = new Dimension(140, 20);
final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
JPanel jp;

theInventory = inventory;



centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));

buttonPanel = new JPanel();

JButton firstButton = new JButton("First");
firstButton.addActionListener(new FirstButtonHandler());
buttonPanel.add(firstButton);

JButton previousButton = new JButton("Previous");
previousButton.addActionListener(new PreviousButtonHandler());
buttonPanel.add(previousButton);

JButton nextButton = new JButton("Next");
nextButton.addActionListener(new NextButtonHandler());
buttonPanel.add(nextButton);

JButton lastButton = new JButton("Last");
lastButton.addActionListener(new LastButtonHandler());
buttonPanel.add(lastButton);

JButton addButton = new JButton("Add");
addButton.addActionListener(new AddButtonHandler());
buttonPanel.add(addButton);

JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new DeleteButtonHandler());
buttonPanel.add(deleteButton);

JButton modifyButton = new JButton("Modify");
modifyButton.addActionListener(new ModifyButtonHandler());
buttonPanel.add(modifyButton);

JButton saveButton = new JButton("Save");
saveButton.addActionListener(new SaveButtonHandler());
buttonPanel.add(saveButton);

JButton searchButton = new JButton("Search");
searchButton.addActionListener(new SearchButtonHandler());
buttonPanel.add(searchButton);


centerPanel.add(buttonPanel);

jp = new JPanel(flo);
itemNumberLabel.setPreferredSize(dim);
jp.add(itemNumberLabel);
itemNumberText = new JTextField(3);
itemNumberText.setEditable(false);
jp.add(itemNumberText);
centerPanel.add(jp);

jp = new JPanel(flo);
prodnameLabel.setPreferredSize(dim);
jp.add(prodnameLabel);
prodnameText = new JTextField(17);
prodnameText.setEditable(false);
jp.add(prodnameText);
centerPanel.add(jp);

jp = new JPanel(flo);
prodpriceLabel.setPreferredSize(dim);
jp.add(prodpriceLabel);
prodpriceText = new JTextField(17);
prodpriceText.setEditable(false);
jp.add(prodpriceText);
centerPanel.add(jp);

jp = new JPanel(flo);
numinstockLabel.setPreferredSize(dim);
jp.add(numinstockLabel);
numinstockText = new JTextField(5);
numinstockText.setEditable(false);
jp.add(numinstockText);
centerPanel.add(jp);

jp = new JPanel(flo);
restockingFeeLabel.setPreferredSize(dim);
jp.add(restockingFeeLabel);
restockingFeeText = new JTextField(17);
restockingFeeText.setEditable(false);
jp.add(restockingFeeText);
centerPanel.add(jp);

jp = new JPanel(flo);
valueLabel.setPreferredSize(dim);
jp.add(valueLabel);
valueText = new JTextField(17);
valueText.setEditable(false);
jp.add(valueText);
centerPanel.add(jp);

jp = new JPanel(flo);
totalValueLabel.setPreferredSize(dim);
jp.add(totalValueLabel);
totalValueText = new JTextField(17);
totalValueText.setEditable(false);
jp.add(totalValueText);
centerPanel.add(jp);

// adds panel to display
setContentPane(centerPanel);

repaintGUI();

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(420, 480);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}

public void repaintGUI() {
Album temp = (Album) theInventory.getCD(index);

if (temp != null) {
itemNumberText.setText("" + temp.getItemNo());
prodnameText.setText(temp.getTitle());
prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice()));
restockingFeeText.setText(String.format("$%.2f", temp.restockingFee()));
numinstockText.setText("" + temp.getInStock());
valueText.setText(String.format("$%.2f", temp.value()));
}
totalValueText.setText(String.format("$%.2f", theInventory.value()));
}

class FirstButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
index = 0;
repaintGUI();
}
}

class PreviousButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
int numItems = theInventory.getNumItems();
index--;
if (index < 0)
index = numItems - 1;
repaintGUI();
}
}

class NextButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
int numItems = theInventory.getNumItems();
index = (++index) % numItems;
repaintGUI();
}
}
class LastButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
int numItems = theInventory.getNumItems();
index = (numItems -1) % numItems;
repaintGUI();

}
}

class AddButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
int numItems = theInventory.getNumItems();
index--;
if (index > 5)
index = numItems + 6;
repaintGUI();
}
}


class SaveButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}

class ModifyButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}

class DeleteButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}

class SearchButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}

} // End InventoryGUI class

I am sorry this is my first time posting a question so if i did it wrong please let me know. I just need some examples to help me start!
Feb 4 '08 #1
2 2493
Nevermind I finally got it!!
Feb 4 '08 #2
dlb53
3
I dont get it - can you explain?
Mar 6 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

109
by: zaidalin79 | last post by:
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
1
by: lucky29105 | last post by:
I am at my wits end here- I have an assignment due today (05/11) and I still cannot compile. I am getting 14 errors! I don't want someone to do my work, but I need help, I am stuck. Here is the...
9
by: xxplod | last post by:
I am suppose to modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including...
11
by: hamiltongreg | last post by:
I am new to Java and am having problems getting my program to compile correctly. My assignment is as follows; Choose a product that lends itself to an inventory (for example, products at your...
3
by: cblank | last post by:
I need some help if someone could help me. I know everyone is asking for help in java. But for some reason I'm the same as everyone else when it comes to programming in java. I have an inventory...
5
by: cblank | last post by:
I'm having some trouble with my inventory program. Its due tom and my teacher is not wanting to help. He keeps giving me a soluction that is not related to my code. I have everything working except...
3
by: 100grand | last post by:
Modify the 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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.