473,782 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inventory program in Java

7 New Member
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\invento ry.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
Jan 26 '07 #1
67 23044
horace1
1,510 Recognized Expert Top Contributor
you need to post the code you have so far - contributers can then comment on your problems
Jan 27 '07 #2
hollywoood
7 New Member
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import java.awt.*;
  6.  
  7. class Product implements Comparable {
  8.  String itemName;     // class variable that stores the item name
  9.  double itemNumber;     // class variable that stores the item number
  10.  long stockQuantity; // class variable that stores the quantity in stock
  11.  double price;     // class variable that stores the item price
  12.  
  13.  public Product() {
  14.        itemName = "";
  15.      itemNumber = 0.0;
  16.      stockQuantity = 0L;
  17.      price = 0.0;
  18.  
  19.  }
  20.  public Product(String name, int number, long stockQuantity, double price) {
  21.      this.itemName = name;
  22.      this.itemNumber = number;
  23.      this.stockQuantity = stockQuantity;
  24.      this.price = price;
  25.     }
  26.  public void setItemName(String name) {
  27.   this.itemName = itemName;
  28.  }
  29.  public String getItemName() {
  30.   return itemName;
  31.  }
  32.  public void setItemNumber(double number) {
  33.   this.itemNumber = itemNumber;
  34.  }
  35.  public double getItemNumber() {
  36.   return itemNumber;
  37.  }
  38.  public void setStockQuantity(long quantity) {
  39.   stockQuantity = quantity;
  40.  }
  41.  public long getStockQuantity() {
  42.   return stockQuantity;
  43.  }
  44.  public void setItemPrice(double price) {
  45.   this.price = price;
  46.  }
  47.  public double getItemPrice() {
  48.   return price;
  49.  }
  50.  public double calculateInventoryValue() {
  51.   return getItemPrice() * getStockQuantity();
  52.  }
  53.  public int compareTo (Object o) {
  54.   Product p = (Product)o;
  55.   return itemName.compareTo(p.getItemName());
  56.  }
  57.  public String toString() {
  58.   return "Title :"+getItemName() + "\nStock Number"+itemNumber+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
  59.  }
  60.  
  61. }
  62.  
  63. class DVDTitle extends Product implements Comparable {
  64. private String rating;
  65. public DVDTitle() {
  66. super(); //Call the constructor in Product
  67. rating = ""; //Add the additonal attribute
  68. }
  69. public DVDTitle(String itemName, int itemNumber, long stockQuantity, double price, String rating) {
  70. super(itemName, itemNumber, stockQuantity, price); //Call the constructor in Product
  71. this.rating = rating; //Add the additonal attribute
  72.     }
  73. public void setRating(String rating) {
  74. this.rating = rating;
  75. }
  76. public String getRating() {
  77. return rating;
  78. }
  79. public double calculateInventoryValue() {
  80. return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05; //Had you forgotten to include the restocking fee?
  81. }
  82. //What happens when we want to change the restocking fee?
  83. public double calculateRestockFee() {
  84. return getItemPrice() * 0.05;
  85. }
  86. public int compareTo (Object o) {
  87. Product p = (Product)o;
  88. return getItemName().compareTo(p.getItemName());
  89. }
  90. public String toString() {
  91. return "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nRating :"+getRating()+"\nValue"+calculateInventoryValue();
  92. }
  93. }
  94.  
  95.  
  96.  

I will have to post the other part of the code to another post.
Jan 28 '07 #3
hollywoood
7 New Member
Here is the other part of that code

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Inventory1 extends JFrame implements ActionListener {
  3. //Utility class for displaying the picture
  4. //If we are going to use a class/method/variable inside that class only, we declare it private in that class
  5. private class MyPanel extends JPanel {
  6.     ImageIcon image = new ImageIcon("dvd.gif");
  7.     int width = image.getIconWidth();
  8.     int height = image.getIconHeight();
  9.     long angle = 30;
  10.     public MyPanel(){
  11.      super();
  12.     }
  13.     public void paintComponent(Graphics g){
  14.      super.paintComponent(g);
  15.      Graphics2D g2d = (Graphics2D)g;
  16.      //g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
  17.      g2d.drawImage(image.getImage(), 60, 60, this);
  18.      g2d.dispose();
  19.     }
  20. }//end class MyPanel
  21.  
  22. int currentIndex; //Currently displayed Item
  23. Product[] supplies = new Product[4];
  24. JLabel itemName ;
  25. JLabel itemNumber;
  26. JLabel rating;
  27. JLabel quantity;
  28. JLabel price;
  29. JLabel fee;
  30. JLabel totalValue;
  31. JTextField itemNameField = new JTextField(20);
  32. JTextField itemNumberField = new JTextField(20);
  33. JTextField ratingField = new JTextField(20);
  34. JTextField quantityField = new JTextField(20);
  35. JTextField priceField = new JTextField(20);
  36.  
  37. JPanel display;
  38. JPanel displayHolder;
  39. JPanel panel;
  40. public Inventory1() {
  41. makeTheDataItems();
  42. setSize(600, 500);
  43. setTitle("Jeremy's DVD Inventory Program");
  44. //make the panels
  45. display = new JPanel();
  46. JPanel other = new JPanel();
  47. JPanel picture = new MyPanel();
  48. JPanel buttons = new JPanel();
  49. JPanel centerPanel = new JPanel();
  50. displayHolder = new JPanel();
  51. display.setLayout(new GridLayout(5, 3));
  52. other.setLayout(new GridLayout(2, 1));
  53. //make the labels
  54. itemName = new     JLabel("Name       :");
  55. itemNumber = new     JLabel("Number       :");
  56. rating = new     JLabel("Rating      :");
  57. quantity = new JLabel("Quantity       :");
  58. price = new     JLabel("Price       :");
  59. fee = new        JLabel("Fee       :");
  60. totalValue = new JLabel("Total Value       :");
  61. //Use the utility method to make the buttons
  62. JButton first = makeButton("First");
  63. JButton next = makeButton("Next");
  64. JButton previous = makeButton("Previous");
  65. JButton last = makeButton("Last");
  66. JButton exit = makeButton("Exit");
  67. //Other buttons
  68. JButton add = makeButton("Add");
  69.  
  70. //Add the labels to the display panel
  71. display.add(itemName);
  72. display.add(itemNumber);
  73. display.add(rating);
  74. display.add(quantity);
  75. display.add(price);
  76. display.add(fee);
  77. //add the buttons to the buttonPanel
  78. buttons.add(first);
  79. buttons.add(previous);
  80. buttons.add(next);
  81. buttons.add(last);
  82. buttons.add(exit);
  83. //Add the picture panel and display to the centerPanel
  84. displayHolder.add(display);
  85. centerPanel.setLayout(new GridLayout(2, 1));
  86. centerPanel.add(picture);
  87. centerPanel.add(displayHolder);
  88. other.add(buttons);
  89. JPanel forAdd = new JPanel(); // add the other buttons to this panel
  90. forAdd.add(add);
  91. other.add(forAdd);
  92.  
  93. //Add the panels to the frame
  94. getContentPane().add(centerPanel, "Center");
  95. getContentPane().add(other, "South");
  96. this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  97. setVisible(true);
  98. }
  99. private void makeTheDataItems () {
  100.   Product p1 = new DVDTitle("XMEN-The Last Stand", 001, 200, 12.99, "PG-13");
  101.   Product p2 = new DVDTitle("Harry Potter-The Goblet of Fire", 002, 50, 9.95, "PG-13");
  102.   Product p3 = new DVDTitle("Jackass-Number Two", 003, 100, 19.95, "R");
  103.   Product p4 = new DVDTitle("Pirates of the Caribbean", 004, 75, 9.99, "PG-13");
  104.  
  105.   supplies[0] = p1;
  106.   supplies[1] = p2;
  107.   supplies[2] = p3;
  108.   supplies[3] = p4;
  109. }
  110. //Utility method for creating and dressing buttons
  111. private JButton makeButton(String label) {
  112. JButton button = new JButton(label);
  113. button.setActionCommand(label);
  114. button.addActionListener(this);
  115. return button;
  116. }
  117. private void addItem() {
  118. System.out.println("eeeeeeeeee");
  119. panel = new JPanel();
  120. JPanel add = new JPanel();
  121. add.setLayout(new GridLayout(2, 1));
  122. add.setLayout(new GridLayout(4, 4));
  123. JButton addIt = makeButton("Add Item");
  124. JLabel itemName = new JLabel("Name       :");
  125. //JLabel itemNumber = new JLabel("Number       :");
  126. JLabel rating = new JLabel("Rating       :");
  127. JLabel quantity = new JLabel("Quantity    :");
  128. JLabel price = new JLabel("Price     :");
  129. add.add(itemName); add.add(itemNameField);
  130. //add.add(itemNumber); add.add(itemNumberField);
  131. add.add(rating); add.add(ratingField);
  132. add.add(quantity); add.add(quantityField);
  133. add.add(price); add.add(priceField);
  134. panel.add(add);
  135. JPanel forAddIt = new JPanel();
  136. forAddIt.add(addIt);
  137. panel.add(forAddIt);
  138. displayHolder.remove(display);
  139. displayHolder.add(panel);
  140. //display = panel;
  141. this.setVisible(true);
  142. }
  143.  
  144. public static void main( String args[]) {
  145. Inventory1 object = new Inventory1(); //The main method should not have too much code
  146. } // end main method
  147. public void actionPerformed(ActionEvent event) {
  148. String command = event.getActionCommand(); //This retrieves the command that we set for the button
  149. //Always compare strings using the .equals method and not using ==
  150. if(command.equals("First")) {
  151. displayFirst();
  152. }
  153. else if(command.equals("Next")) {
  154. displayNext();
  155. }
  156. else if(command.equals("Previous")) {
  157. displayPrevious();
  158. }
  159. else if(command.equals("Last")) {
  160. displayLast();
  161. }
  162. else if(command.equals("Exit")) {
  163. this.dispose();
  164. System.exit(0);
  165. }
  166. else if(command.equals("Add")) {
  167. addItem();
  168. }
  169. else if(command.equals("Add Item")) {
  170. addItemToArray();
  171. }
  172.  
  173. }
  174. private void addItemToArray() {
  175. Product p = new DVDTitle(itemNameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
  176. Double.parseDouble(priceField.getText()), ratingField.getText());
  177. //Extend size of array by one first
  178. Product[] ps = new Product[supplies.length + 1];
  179. for(int i = 0; i < ps.length-1; i++) {
  180. ps[i] = supplies[i];
  181. }
  182. ps[supplies.length] = p;
  183. supplies = ps;
  184. displayHolder.remove(panel);
  185. displayHolder.add(display);
  186. displayLast();
  187. this.setVisible(false);
  188. this.setVisible(true);
  189. }
  190. //Utility method to ease the typing and reuse code
  191. //This method reduces the number of lines of our code
  192. private void displayItemAt(int index) {
  193. DVDTitle product = (DVDTitle)supplies[index];
  194. itemName.setText("Item Name: "+ product.getItemName());
  195. itemNumber.setText("Item Number: "+ product.getItemNumber());
  196. rating.setText("Rating: "+ product.getRating());
  197. quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
  198. price.setText("Item Price: "+ product.getItemPrice());
  199. totalValue.setText("Total: " + product.calculateInventoryValue());
  200. fee.setText("Fee :"+product.calculateRestockFee());
  201. this.setVisible(true);
  202. }
  203. public void displayFirst() {
  204. displayItemAt(0);
  205. currentIndex = 0;
  206. }
  207. public void displayNext() {
  208. if(currentIndex == supplies.length-1) {
  209. displayFirst();
  210. currentIndex = 0;
  211. }
  212. else {
  213. displayItemAt(currentIndex + 1);
  214. currentIndex++;
  215. }
  216. }
  217. public void displayPrevious() {
  218. if(currentIndex == 0) {
  219. displayLast();
  220. currentIndex = supplies.length-1;
  221. }
  222. else {
  223. displayItemAt(currentIndex - 1);
  224. currentIndex--;
  225. }
  226. }
  227. public void displayLast() {
  228. displayItemAt(supplies.length-1);
  229. currentIndex = supplies.length-1;
  230. }
  231. }//end class Inventory2.1
  232.  
  233.  
  234.  
Jan 28 '07 #4
hollywoood
7 New Member
I have the add button working......I just need help with the other.

Thanks
Jan 28 '07 #5
stevedub
40 New Member
When I try to compile the Inventory1 class I get 87 errors. Why is this? I am also working on this assignment.
Feb 4 '07 #6
r035198x
13,262 MVP
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?
Feb 5 '07 #7
stevedub
40 New Member
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.
Feb 5 '07 #8
r035198x
13,262 MVP
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.
Feb 5 '07 #9
stevedub
40 New Member
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.
Feb 5 '07 #10

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

Similar topics

109
25888
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 assignments... 4. CheckPoint: Inventory Program Part 1 • Resource: Java: How to Program • Due Date: Day 5 forum • Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD...
0
7115
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 the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment. Here is the assignment: Modify the Inventory program by adding a button to the GUI that allows the user to move...
4
2312
nexcompac
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 trying to compile a program using TextPad. Here is what I have so far. /* * Main.java * * Created on July 19, 2007, 5:54 PM *
9
7610
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 the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory. • Create a method to calculate...
3
7001
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 program that I have built so far with no problem. But I cannot figure out how to turn it into a GUI application. This is what I'm trying to get it to do. Modify the Inventory Program to use a GUI. The GUI should display the information one product...
5
3378
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 the Delete and Modify Button. The Search button works but it only searchs the new .dat file or what is set in the array. Not sure what is going on there. But if I can get my delete and modify button to work that will be good. Code.... ...
2
2543
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 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...
3
4486
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 of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee. Here is my Inventory program from 1 to 3: package...
16
6006
by: lilsugaman | last post by:
I have to assignment which includes the following: 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 the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire...
0
9639
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.