need help with java program

This is a discussion on "need help with java program" within the Other Programming Languages section. This forum, and the thread "need help with java program are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Other Programming Languages

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Aug 19th, 2007, 20:45
New Member
Join Date: Aug 2007
Location: North Dakota
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
need help with java program

this is what i have to do but i can only get two buttons to work any help would be great

Modify the Inventory Program by 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 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.


here is my program so far
Code: Select all
// created by Nicholas Baatz on July 24,2007
  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 Inventory2
{
//main method begins execution of java application
public static void main(final String args[])
{

int i; // varialbe for looping
double total = 0; // variable for total inventory
final int dispProd = 0; // variable for actionEvents

// 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("Paper", 101, 10, 1.00, "Box");
nwProduct[1] = new ProductAdd("Pen", 102, 10, 0.75, "Pack");
nwProduct[2] = new ProductAdd("Pencil", 103, 10, 0.50, "Pack");
nwProduct[3] = new ProductAdd("Staples", 104, 10, 1.00, "Box");
nwProduct[4] = new ProductAdd("Clip Board", 105, 10, 3.00, "Two Pack");
}



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("Company Logo"); // 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.pack();
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)
{
textArea.setText(nwProduct[0]+"\n");
} // end firstBtn actionEvent

}); // end firstBtn actionListener
textArea.setText(nwProduct[4]+"n");

// prevBtn.addActionListener(new ActionListener()

// {
// public void actionPerformed(ActionEvent ae)
// {
// dispProd = (nwProduct.length+dispProd-1) % nwProduct.length;
// textArea.setText(nwProduct.display(dispProd)+"\n");
// } // end prevBtn actionEvent
// }); // end prevBtn actionListener





} // end main

} // end class Inventory2

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
Reply With Quote

  #2 (permalink)  
Old Aug 28th, 2007, 14:46
Up'n'Coming Member
Join Date: Aug 2006
Location: Peru
Age: 21
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Re: need help with java program

Please, don't post your homework on the forum.
Reply With Quote
Reply

Tags
newbee

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
IF I were to use a program... Nightmare Website Planning 15 Jun 23rd, 2007 07:18
ftp program aflacduck Starting Out 21 Jun 16th, 2007 22:59
PHP Program DregondRahl PHP Forum 10 May 26th, 2007 12:58
Java/GUI/Server Developer (Java, EJB/Hibernate, SWING) - Berkshire Web JobBot Job Opportunities 0 Jan 16th, 2007 09:30
Run .EXE program via PHP kal PHP Forum 3 Dec 11th, 2005 23:24


All times are GMT. The time now is 12:42.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43