how do i get my program to add,delete and save? i could use help asap thanks i will work on my code if i get to work ill let you all know here is what i have so far - // created by Nicholas Baatz on July 24,2007
-
import java.awt.BorderLayout;
-
import java.awt.Font;
-
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
-
{
-
static int dispProd = 0; // variable for actionEvents
-
// main method begins execution of java application
-
-
static JTextArea textArea;
-
-
public static void main(final String args[])
-
{
-
-
int i; // varialbe for looping
-
double total = 0; // variable for total inventory
-
-
// Instantiate a product object
-
final ProductAdd[] nwProduct = new ProductAdd[5];
-
// Instantiate objects for the array
-
for (i = 0; i < 5; i++)
-
{
-
nwProduct[0] = new ProductAdd("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 JButton addBtn = new JButton("Add");
-
final JButton deleteBtn = new JButton("Delete");
-
final JButton modifyBtn = new JButton("Modify");
-
-
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(0, 4)); // set layout
-
// add buttons to buttonPanel
-
buttonJPanel.add(firstBtn);
-
buttonJPanel.add(prevBtn);
-
buttonJPanel.add(nextBtn);
-
buttonJPanel.add(lastBtn);
-
-
buttonJPanel.add(addBtn);
-
buttonJPanel.add(deleteBtn);
-
buttonJPanel.add(modifyBtn);
-
-
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)
-
{
-
dispProd = 0;
-
textArea.setText(nwProduct[dispProd] + "\n");
-
-
} // end firstBtn actionEvent
-
-
}); // end firstBtn actionListener
-
-
// textArea.setText(nwProduct[4]+"n");
-
-
prevBtn.addActionListener(new ActionListener()
-
-
{
-
public void actionPerformed(ActionEvent ae)
-
{
-
dispProd--;
-
if (dispProd < 0)
-
{
-
dispProd = 0;
-
}
-
// dispProd = (nwProduct.length+dispProd-1) % nwProduct.length;
-
textArea.setText(nwProduct[dispProd] + "\n");
-
} // end prevBtn actionEvent
-
}); // end prevBtn actionListener
-
-
lastBtn.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent ae)
-
{
-
dispProd = nwProduct.length - 1;
-
textArea.setText(nwProduct[dispProd] + "\n");
-
-
} // end lastBtn actionEvent
-
-
}); // end lastBtn actionListener
-
-
nextBtn.addActionListener(new ActionListener()
-
-
{
-
public void actionPerformed(ActionEvent ae)
-
{
-
dispProd++;
-
if (dispProd >= nwProduct.length)
-
{
-
dispProd = nwProduct.length - 1;
-
}
-
textArea.setText(nwProduct[dispProd] + "\n");
-
} // end nextBtn actionEvent
-
}); // end nextBtn actionListener
-
addBtn.addActionListener(new extraBtnAxnL());
-
deleteBtn.addActionListener(new extraBtnAxnL());
-
modifyBtn.addActionListener(new extraBtnAxnL());
-
-
} // end main
-
-
static class extraBtnAxnL implements ActionListener
-
{
-
-
public void actionPerformed(ActionEvent arg0)
-
{
-
mybtnAction(arg0, null);
-
}
-
}
-
-
private static void mybtnAction(ActionEvent arg0, String stock)
-
{
-
String axnCmd = arg0.getActionCommand();
-
Font oldFont = textArea.getFont();
-
textArea.setFont(new Font(oldFont.getName(), Font.BOLD, 16));
-
if (axnCmd.equalsIgnoreCase("add"))
-
{
-
textArea.setText(new String( ));
-
}
-
else if (axnCmd.equalsIgnoreCase("delete"))
-
{
-
textArea.setText(new String());
-
}
-
else if (axnCmd.equalsIgnoreCase("modify"))
-
{
-
textArea.setText(new String());
-
}
-
}
-
-
-
};
-
-
// 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
3 1275
First of all, everything, which should be done after any Products were added (both the initial Products as any added by the user) shoud be moved into a seperate function. (Example: Calculating overall value.)
Then you have to write a listener for the add button, which can change the Collection of Products, which you allready have. I can see two possibilities right now: - You read out the size of the current Array (nwProduct.size) and then create a new Array, which is one bigger. Then copy all Products from the current Array to the new Array and add the one, which should be created.
- You use a Vector instead of an Array. Then you won't need all that copying, just say nwProduct.add(...) (nwProduct now being a Vector instead of an Array). When acessing it, you might have to cast the element (which you access with nwProduct.elementAt(i)) to a Product, but that's the least Problem, I would think.
im not quite fowining you on this im new to java but i think you mean make a class for it the set atvie listners?
im not quite fowining you on this im new to java but i think you mean make a class for it the set atvie listners?
No, I don't. Here's what I mean, shown with an example of how to add a product: -
// created by Nicholas Baatz on July 24,2007
-
import java.util.Vector;
-
-
import java.awt.BorderLayout;
-
import java.awt.Font;
-
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
-
{
-
static int dispProd = 0; // variable for actionEvents
-
// main method begins execution of java application
-
-
static JTextArea textArea;
-
-
public static int totalCost(Vector<ProductAdd> nwProduct)
-
{
-
int total = 0;
-
for (int i = 0; i < 5; i++)
-
total += nwProduct.size(); // calculate total inventory cost
-
return total;
-
-
}
-
-
public static void main(final String args[])
-
{
-
-
int i; // varialbe for looping
-
double total = 0; // variable for total inventory
-
-
// Instantiate a product object
-
final Vector<ProductAdd> nwProduct = new Vector<ProductAdd>();
-
// Instantiate objects for the array
-
nwProduct.add(new ProductAdd("Paper", 101, 10, 1.00, "Box"));
-
nwProduct.add(new ProductAdd("Pen", 102, 10, 0.75, "Pack"));
-
nwProduct.add(new ProductAdd("Pencil", 103, 10, 0.50, "Pack"));
-
nwProduct.add(new ProductAdd("Staples", 104, 10, 1.00, "Box"));
-
nwProduct.add(new ProductAdd("Clip Board", 105, 10, 3.00,
-
"Two Pack"));
-
-
total += totalCost(nwProduct);
-
-
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 JButton addBtn = new JButton("Add");
-
final JButton deleteBtn = new JButton("Delete");
-
final JButton modifyBtn = new JButton("Modify");
-
-
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(0, 4)); // set layout
-
// add buttons to buttonPanel
-
buttonJPanel.add(firstBtn);
-
buttonJPanel.add(prevBtn);
-
buttonJPanel.add(nextBtn);
-
buttonJPanel.add(lastBtn);
-
-
buttonJPanel.add(addBtn);
-
buttonJPanel.add(deleteBtn);
-
buttonJPanel.add(modifyBtn);
-
-
textArea = new JTextArea(nwProduct.elementAt(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)
-
{
-
dispProd = 0;
-
textArea.setText(nwProduct.elementAt(dispProd) + "\n");
-
-
} // end firstBtn actionEvent
-
-
}); // end firstBtn actionListener
-
-
// textArea.setText(nwProduct.elementAt(4)+"n");
-
-
prevBtn.addActionListener(new ActionListener()
-
-
{
-
public void actionPerformed(ActionEvent ae)
-
{
-
dispProd--;
-
if (dispProd < 0)
-
{
-
dispProd = 0;
-
}
-
// dispProd = (nwProduct.size()+dispProd-1) % nwProduct.size();
-
textArea.setText(nwProduct.elementAt(dispProd) + "\n");
-
} // end prevBtn actionEvent
-
}); // end prevBtn actionListener
-
-
lastBtn.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent ae)
-
{
-
dispProd = nwProduct.size() - 1;
-
textArea.setText(nwProduct.elementAt(dispProd) + "\n");
-
-
} // end lastBtn actionEvent
-
-
}); // end lastBtn actionListener
-
-
nextBtn.addActionListener(new ActionListener()
-
-
{
-
public void actionPerformed(ActionEvent ae)
-
{
-
dispProd++;
-
if (dispProd >= nwProduct.size())
-
{
-
dispProd = nwProduct.size() - 1;
-
}
-
textArea.setText(nwProduct.elementAt(dispProd) + "\n");
-
} // end nextBtn actionEvent
-
}); // end nextBtn actionListener
-
-
addBtn.addActionListener(new ActionListener()
-
-
{
-
public void actionPerformed(ActionEvent ae)
-
{
-
// Read in Information about the new Product
-
String prodName = "Folder";
-
int itmNumber = 42;
-
int units = 20;
-
double price = 0.89;
-
double value = 1.88;
-
String addFeat = "Two Pack";
-
// create new ProductAdd and add it to the Vector
-
nwProduct.add(new ProductAdd(prodName, itmNumber, units, price, addFeat));
-
total = totalCost(nwProduct);
-
}
-
});
-
deleteBtn.addActionListener(new extraBtnAxnL());
-
modifyBtn.addActionListener(new extraBtnAxnL());
-
-
} // end main
-
-
static class extraBtnAxnL implements ActionListener
-
{
-
-
public void actionPerformed(ActionEvent arg0)
-
{
-
mybtnAction(arg0, null);
-
}
-
}
-
-
private static void mybtnAction(ActionEvent arg0, String stock)
-
{
-
String axnCmd = arg0.getActionCommand();
-
Font oldFont = textArea.getFont();
-
textArea.setFont(new Font(oldFont.getName(), Font.BOLD, 16));
-
if (axnCmd.equalsIgnoreCase("add"))
-
{
-
textArea.setText(new String( ));
-
}
-
else if (axnCmd.equalsIgnoreCase("delete"))
-
{
-
textArea.setText(new String());
-
}
-
else if (axnCmd.equalsIgnoreCase("modify"))
-
{
-
textArea.setText(new String());
-
}
-
}
-
-
-
};
-
-
// 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
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: royaltiger |
last post by:
I am trying to copy the inventory database in Building Access Applications
by John L Viescas but when i try to run the database i get an error in the
orders form when i click on the allocate...
|
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...
|
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...
|
by: nexcompac |
last post by:
Ok, I posted a similar post but now need to jump back into it. Here is what I have been able to clean up. I am using textpad and jbuilder.
Still getting used to the whole java world and I am...
|
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...
|
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...
|
by: pinkf24 |
last post by:
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...
|
by: jcato77 |
last post by:
I need help with a class project I'm working on, Below is my assignment and the code I have currently created.
Assignment:
Modify the Inventory Program by creating a subclass of the product class...
|
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...
|
by: blitz1989 |
last post by:
Hello all, I'm new to this forum and Java and having a alot of problems understanding this language. I am working on an invetory program part 4. The assignment requirements are listed but the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |