By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

Inventory program in Java

P: 7
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
Jan 26 '07 #1
Share this Question
Share on Google+
67 Replies


Expert 100+
P: 1,510
you need to post the code you have so far - contributers can then comment on your problems
Jan 27 '07 #2

P: 7
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

P: 7
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

P: 7
I have the add button working......I just need help with the other.

Thanks
Jan 28 '07 #5

P: 40
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

10K+
P: 13,264
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

P: 40
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

10K+
P: 13,264
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

P: 40
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

P: 40
Ok, I got it to compile, I just needed to add a couple more imports at the top of the inventory program.
Feb 5 '07 #11

10K+
P: 13,264
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.
Feb 5 '07 #12

P: 40
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.
Feb 5 '07 #13

P: 40
Here is my code from last week's assignment. We are to modify this to look like the link above:

Expand|Select|Wrap|Line Numbers
  1. class Product implements Comparable
  2. {
  3.    private String title;   // class variable that stores the item name
  4.    private int item;     // class variable that stores the item number
  5.    private double stockQuantity;   // class variable that stores the quantity in stock
  6.    private double price;     // class variable that stores the item price
  7.  
  8.    public Product()
  9.    {
  10.       title = "";
  11.       item = 0;
  12.       stockQuantity = 0;
  13.       price = 0.0;
  14.    }
  15.  
  16.    public Product(String title, int item, double stockQuantity, double price)
  17.    {
  18.       this.title = title;
  19.       this.item = item;
  20.       this.stockQuantity = stockQuantity;
  21.       this.price = price;
  22.    }
  23.  
  24.    public void setTitle(String title)
  25.    {
  26.       this.title = title;
  27.    }
  28.  
  29.    public String getTitle()
  30.    {
  31.       return title;
  32.    }
  33.  
  34.    public void setItem(int item)
  35.    {
  36.       this.item = item;
  37.    }
  38.  
  39.    public int getItem()
  40.    {
  41.       return item;
  42.    }
  43.  
  44.    public void setStockQuantity(double quantity)
  45.    {
  46.       stockQuantity = quantity;
  47.    }
  48.  
  49.    public double getStockQuantity()
  50.    {
  51.       return stockQuantity;
  52.    }
  53.  
  54.    public void setItemPrice(double price)
  55.    {
  56.       this.price = price;
  57.    }
  58.  
  59.    public double getItemPrice()
  60.    {
  61.       return price;
  62.    }
  63.  
  64.    public double calculateInventoryValue()
  65.    {
  66.       return (price * stockQuantity)*.05+(price*stockQuantity);
  67.    }
  68.  
  69.    public int compareTo(Object o)
  70.    {
  71.       Product p = null;
  72.       try
  73.       {
  74.          p = (Product) o;
  75.       }
  76.       catch (ClassCastException cE)
  77.       {
  78.          cE.printStackTrace();
  79.       }
  80.       return title.compareTo(p.getTitle());
  81.    }
  82.  
  83.    public String toString()
  84.    {
  85.       return "DVD Title: " + title + "\nItem #: " + item + "\nPrice: $" + price + "\nQuantity: "
  86.       + stockQuantity + "\nValue: $" + calculateInventoryValue();
  87.    }
  88. }
  89.  
  90.  
  91. class DVD extends Product
  92. {
  93.  
  94.    String genre;
  95.    double restockingFee;
  96.  
  97.  
  98.    public DVD(String title, int item, double stockQuantity,double price ,double restockingFee, String genre)
  99.    {
  100.       super(title,item,  stockQuantity, price);
  101.       this.genre = genre;
  102.       this.restockingFee = 0.05;
  103.  
  104.       // TODO Auto-generated constructor stub
  105.    }
  106.  
  107.    //returns the value of the inventory, plus the restocking fee
  108.    public double getInventoryValue()
  109.    {
  110.       // TODO Auto-generated method stub
  111.       return super.getItemPrice() + restockingFee;
  112.    }
  113.  
  114.    public String toString()
  115.    {
  116.       //this is not efficient
  117.       //StringBuffer sb = new StringBuffer("Genre      \t").append(genre).append("\n");
  118.       //sb.append(super.toString());
  119.  
  120.       return super.toString() + "\nGenre      \t" + genre + "\nRestockingFee  \t" +restockingFee;
  121.    }
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128. public class Inventory2
  129. {
  130.    Product[] supplies;
  131.  
  132.    public static void main(String[] args)
  133.    {
  134.       Inventory2 inventory = new Inventory2();
  135.       inventory.addProduct(new DVD("Beerfest", 1, 15, 22.95,.05,"Action"));
  136.       inventory.addProduct(new DVD("Illustionist", 2, 25, 25.95,.05,"Comedy"));
  137.       inventory.addProduct(new DVD("Employee of the Month", 3, 10,23.95,.05,"Comedy"));
  138.  
  139.       System.out.println("Inventory of DVD Movies:\n\n");
  140.  
  141.  
  142.       System.out.println();
  143.       inventory.showInventory();
  144.       inventory.sortByName();
  145.       System.out.println();
  146.       inventory.showInventory();
  147.  
  148.       double total = inventory.calculateTotalInventory();
  149.       System.out.println("Total Value is: $" + total);
  150.  
  151.  
  152.    }
  153.  
  154.    public void sortByName()
  155.    {
  156.       for (int i = 1; i < supplies.length; i++)
  157.       {
  158.          int j;
  159.          Product val = supplies[i];
  160.          for (j = i - 1; j > -1; j--)
  161.          {
  162.             Product temp = supplies[j];
  163.             if (temp.compareTo(val) <= 0)
  164.             {
  165.                break;
  166.             }
  167.             supplies[j + 1] = temp;
  168.          }
  169.          supplies[j + 1] = val;
  170.       }
  171.    }
  172.  
  173.    //creates a String representation of the array of products
  174.    public String toString()
  175.    {
  176.       String s = "";
  177.       for (Product p : supplies)
  178.       {
  179.          s = s + p.toString();
  180.          s = s + "\n\n";
  181.       }
  182.       return s;
  183.    }
  184.  
  185.    //Using an array so adding an item requires us to increase the size of the array first
  186.    public void addProduct(Product p1)
  187.    {
  188.       if (supplies == null)
  189.       {
  190.          supplies = new Product[0];
  191.       }
  192.       Product[] p = supplies; //Copy all products into p first
  193.       Product[] temp = new Product[p.length + 1]; //create bigger array
  194.       for (int i = 0; i < p.length; i++)
  195.       {
  196.          temp[i] = p[i];
  197.       }
  198.       temp[(temp.length - 1)] = p1; //add the new product at the last position
  199.       supplies = temp;
  200.    }
  201.  
  202.    //sorting the array using Bubble Sort
  203.    public double calculateTotalInventory()
  204.    {
  205.       double total = 0.0;
  206.       for (int i = 0; i < supplies.length; i++)
  207.       {
  208.          total = total + supplies[i].calculateInventoryValue();
  209.       }
  210.       return total;
  211.    }
  212.  
  213.    public void showInventory()
  214.    {
  215.       System.out.println(toString()); //call our toString method
  216.    }
  217. }
  218.  
Feb 5 '07 #14

P: 40
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?
Feb 6 '07 #15

10K+
P: 13,264
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?
Feb 6 '07 #16

P: 40
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
Feb 6 '07 #17

10K+
P: 13,264
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.
Feb 6 '07 #18

P: 40
Here is the code:

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

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

10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import java.util.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. import java.awt.*;
  7.  
  8. class Product implements Comparable {
  9.  String name;
  10.  double number;
  11.  long stockQuantity;
  12.  double price;
  13.  
  14.  public Product() {
  15.   name = "";
  16.      number = 0.0;
  17.      stockQuantity = 0L;
  18.      price = 0.0;
  19.  
  20.  }
  21.  public Product(String name, int number, long stockQuantity, double price) {
  22.   this.name = name;
  23.      this.number = number;
  24.      this.stockQuantity = stockQuantity;
  25.      this.price = price;
  26.     }
  27.     public void setItemName(String name) {
  28.   this.name = name;
  29.  }
  30.  public String getItemName() {
  31.   return name;
  32.  }
  33.  public void setItemNumber(double number) {
  34.   this.number = number;
  35.  }
  36.  public double getItemNumber() {
  37.   return number;
  38.  }
  39.  public void setStockQuantity(long quantity) {
  40.   stockQuantity = quantity;
  41.  }
  42.  public long getStockQuantity() {
  43.   return stockQuantity;
  44.  }
  45.  public void setItemPrice(double price) {
  46.   this.price = price;
  47.  }
  48.  public double getItemPrice() {
  49.   return price;
  50.  }
  51.  public double calculateInventoryValue() {
  52.   return getItemPrice() * getStockQuantity();
  53.  }
  54.  public int compareTo (Object o) {
  55.   Product p = (Product)o;
  56.   return name.compareTo(p.getItemName());
  57.  }
  58.  public String toString() {
  59.   return "Name :"+getItemName() + "\nNumber"+number+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
  60.  }
  61.  
  62.  
  63.  
  64.  
  65. class DVD extends Product implements Comparable {
  66.  private String title;
  67.  public DVD() {
  68.   super(); //Call the constructor in Product
  69.   title = ""; //Add the additonal attribute
  70.  }
  71.  public DVD(String name, int number, long stockQuantity, double price, String title) {
  72.   super(name, number, stockQuantity, price); //Call the constructor in Product
  73.   this.title = title; //Add the additonal attribute
  74.     }
  75.     public void setTitle(String title) {
  76.   this.title = title;
  77.  }
  78.  public String getTitle() {
  79.   return title;
  80.  }
  81.  public double calculateInventoryValue() {
  82.   return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05;
  83.  }
  84.  
  85.  public double calculateRestockFee() {
  86.   return getItemPrice() * 0.05;
  87.  }
  88.  public int compareTo (Object o) {
  89.   Product p = (Product)o;
  90.   return getItemName().compareTo(p.getItemName());
  91.  }
  92.  public String toString() {
  93.   return "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nTitle :"+getTitle()+"\nValue"+calculateInventoryValue();
  94.  }
  95. }
  96.  
  97. public class Inventory2 extends JFrame implements ActionListener {
  98.  
  99.  //Utility class for displaying the picture
  100.  //If we are going to use a class/method/variable inside that class only, we declare it private in that class
  101.  private class MyPanel extends JPanel {
  102.     ImageIcon image = new ImageIcon("Sample.jpg");
  103.     int  width  = image.getIconWidth();
  104.     int  height  = image.getIconHeight();
  105.     long angle = 30;
  106.  
  107.     public MyPanel(){
  108.      super();
  109.     }
  110.     public void paintComponent(Graphics g){
  111.        super.paintComponent(g);
  112.        Graphics2D g2d = (Graphics2D)g;
  113.        g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
  114.        g2d.drawImage(image.getImage(), 60, 60, this);
  115.        g2d.dispose();
  116.     }
  117.  }//end class MyPanel
  118.  
  119.  
  120.  int currentIndex; //Currently displayed Item
  121.  Product[] supplies = new Product[4];
  122.  JLabel name ;
  123.  JLabel number;
  124.  JLabel title;
  125.  JLabel quantity;
  126.  JLabel price;
  127.  JLabel fee;
  128.  JLabel totalValue;
  129.  
  130.  JTextField nameField = new JTextField(20);
  131.  JTextField numberField = new JTextField(20);
  132.  JTextField titleField = new JTextField(20);
  133.  JTextField quantityField = new JTextField(20);
  134.  JTextField priceField = new JTextField(20);
  135.  
  136.  
  137.  JPanel display;
  138.  JPanel displayHolder;
  139.  JPanel panel;
  140.  public Inventory2() {
  141.   makeTheDataItems();
  142.  
  143.   setSize(500, 500);
  144.   setTitle("Inventory Program");
  145.   //make the panels
  146.   display = new JPanel();
  147.   JPanel other = new JPanel();
  148.   JPanel picture = new MyPanel();
  149.   JPanel buttons = new JPanel();
  150.   JPanel centerPanel = new JPanel();
  151.   displayHolder = new JPanel();
  152.   display.setLayout(new GridLayout(7, 1));
  153.   //other.setLayout(new GridLayout(1, 1));
  154.  
  155.   //make the labels
  156.   name = new       JLabel("Name        :");
  157.   number = new     JLabel("Number      :");
  158.   title = new      JLabel("Title       :");
  159.   quantity = new   JLabel("Quantity    :");
  160.   price = new      JLabel("Price       :");
  161.   fee = new        JLabel("Fee         :");
  162.   totalValue = new JLabel("Total Value :");
  163.  
  164.   //Use the utility method to make the buttons
  165.   JButton first = makeButton("First");
  166.   JButton next = makeButton("Next");
  167.   JButton previous = makeButton("Previous");
  168.   JButton last = makeButton("Last");
  169.   JButton exit = makeButton("Exit");
  170.  
  171.   //Other buttons
  172.   JButton add = makeButton("Add");
  173.  
  174.  
  175.   //Add the labels to the display panel
  176.   display.add(name);
  177.   display.add(number);
  178.   display.add(title);
  179.   display.add(quantity);
  180.   display.add(price);
  181.   display.add(fee);
  182.  
  183.   //add the buttons to the buttonPanel
  184.   buttons.add(first);
  185.   buttons.add(previous);
  186.   buttons.add(next);
  187.   buttons.add(last);
  188.   buttons.add(exit);
  189.  
  190.   //Add the picture panel and display to the centerPanel
  191.   displayHolder.add(display);
  192.   centerPanel.setLayout(new GridLayout(2, 1));
  193.   centerPanel.add(picture);
  194.   centerPanel.add(displayHolder);
  195.   other.add(buttons);
  196.   JPanel forAdd = new JPanel(); // add the other buttons to this panel
  197.   forAdd.add(add);
  198.   other.add(forAdd);
  199.  
  200.  
  201.   //Add the panels to the frame
  202.  
  203.   getContentPane().add(centerPanel, "Center");
  204.   getContentPane().add(other, "South");
  205.   this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  206.   setVisible(true);
  207.  }
  208.  
  209.  private void makeTheDataItems () {
  210.   Product p1 = new DVD("test", 001, 200, 100, "The one");
  211.   Product p2 = new DVD("test", 002, 500, 10000, "test");
  212.   Product p3 = new DVD("test", 003, 100, 3000, "test");
  213.   Product p4 = new DVD("test", 100, 3000, 9000, "test");
  214.   supplies[0] = p1;
  215.   supplies[1] = p2;
  216.   supplies[2] = p3;
  217.   supplies[3] = p4;
  218.  }
  219.  
  220.  //Utility method for creating and dressing buttons
  221.  private JButton makeButton(String label) {
  222.   JButton button = new JButton(label);
  223.   button.setActionCommand(label);
  224.   button.addActionListener(this);
  225.   return button;
  226.  }
  227.  
  228.  private void addItem() {
  229.   panel = new JPanel();
  230.   JPanel add = new JPanel();
  231.   add.setLayout(new GridLayout(2, 1));
  232.   add.setLayout(new GridLayout(4, 4));
  233.   JButton addIt = makeButton("Add Item");
  234.  
  235.   JLabel name = new JLabel("Name        :");
  236.   //JLabel number = new JLabel("Number      :");
  237.   JLabel title = new JLabel("Title       :");
  238.   JLabel quantity = new JLabel("Quantity    :");
  239.   JLabel price = new JLabel("Price       :");
  240.  
  241.   add.add(name); add.add(nameField);
  242.   //add.add(number); add.add(numberField);
  243.   add.add(title); add.add(titleField);
  244.   add.add(quantity); add.add(quantityField);
  245.   add.add(price); add.add(priceField);
  246.   panel.add(add);
  247.   JPanel forAddIt = new JPanel();
  248.   forAddIt.add(addIt);
  249.   panel.add(forAddIt);
  250.   displayHolder.remove(display);
  251.   displayHolder.add(panel);
  252.  
  253.   //display = panel;
  254.   this.setVisible(true);
  255.  }
  256.  
  257.  
  258.  public static void main( String args[]) {
  259.   Inventory2 object = new Inventory2(); //The main method should not have too much code
  260.  } // end main method
  261.  
  262.  public void actionPerformed(ActionEvent event) {
  263.   String command = event.getActionCommand(); //This retrieves the command that we set for the button
  264.   //Always compare strings using the .equals method and not using ==
  265.   if(command.equals("First")) {
  266.    displayFirst();
  267.   }
  268.   else if(command.equals("Next")) {
  269.    displayNext();
  270.   }
  271.   else if(command.equals("Previous")) {
  272.    displayPrevious();
  273.   }
  274.   else if(command.equals("Last")) {
  275.    displayLast();
  276.   }
  277.   else if(command.equals("Exit")) {
  278.    this.dispose();
  279.    System.exit(0);
  280.   }
  281.   else if(command.equals("Add")) {
  282.    addItem();
  283.   }
  284.   else if(command.equals("Add Item")) {
  285.    addItemToArray();
  286.   }
  287.  
  288.  
  289.  }
  290.  
  291.  private void addItemToArray() {
  292.   Product p = new DVD(nameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
  293.   Double.parseDouble(priceField.getText()), titleField.getText());
  294.   //Extend size of array by one first
  295.   Product[] ps = new Product[supplies.length + 1];
  296.   for(int i = 0; i < ps.length-1; i++) {
  297.    ps[i] = supplies[i];
  298.   }
  299.   ps[supplies.length] = p;
  300.   supplies = ps;
  301.   displayHolder.remove(panel);
  302.   displayHolder.add(display);
  303.   displayLast();
  304.   this.setVisible(false);
  305.   this.setVisible(true);
  306.  
  307.  }
  308.  
  309.  //Utility method to ease the typing and reuse code
  310.  //This method reduces the number of lines of our code
  311.  private void displayItemAt(int index) {
  312.    MobilePhone product = (MobilePhone)supplies[index];
  313.    name.setText("Item Name: "+ product.getItemName());
  314.    number.setText("Item Number: "+ product.getItemNumber());
  315.    title.setText("Title: "+ product.getBrand());
  316.    quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
  317.    price.setText("Item Price: "+ product.getItemPrice());
  318.    totalValue.setText("Total: " + product.calculateInventoryValue());
  319.    fee.setText("Fee :"+product.calculateRestockFee());
  320.    this.repaint();
  321.    this.setVisible(true);
  322.  }
  323.  
  324.  public void displayFirst() {
  325.   displayItemAt(0);
  326.   currentIndex = 0;
  327.  }
  328.  
  329.  public void displayNext() {
  330.   if(currentIndex == supplies.length-1) {
  331.    displayFirst();
  332.    currentIndex = 0;
  333.   }
  334.   else {
  335.    displayItemAt(currentIndex + 1);
  336.    currentIndex++;
  337.  
  338.   }
  339.  }
  340.  
  341.  public void displayPrevious() {
  342.   if(currentIndex == 0) {
  343.    displayLast();
  344.    currentIndex = supplies.length-1;
  345.   }
  346.   else {
  347.    displayItemAt(currentIndex - 1);
  348.    currentIndex--;
  349.   }
  350.  }
  351.  
  352.  public void displayLast() {
  353.   displayItemAt(supplies.length-1);
  354.   currentIndex = supplies.length-1;
  355.  }
  356.  
  357. }//end class Inventory2
  358.  
  359.  
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
Feb 6 '07 #21

P: 40
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.
Feb 6 '07 #22

10K+
P: 13,264
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.
Feb 7 '07 #23

P: 40
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!
Feb 7 '07 #24

P: 40
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.
Feb 8 '07 #25

10K+
P: 13,264
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.
Feb 8 '07 #26

P: 40
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.
Feb 8 '07 #27

P: 40
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.
Feb 8 '07 #28

P: 40
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.
Feb 12 '07 #29

10K+
P: 13,264
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.
Feb 12 '07 #30

10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import java.util.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. import java.awt.*;
  7.  
  8.  
  9.  
  10.  
  11.  
  12. public class Inventory2 extends JFrame implements ActionListener {
  13.  
  14. //Utility class for displaying the picture
  15. //If we are going to use a class/method/variable inside that class only, we declare it private in that class
  16. private class MyPanel extends JPanel {
  17. ImageIcon image = new ImageIcon("Sample.jpg");
  18. int width = image.getIconWidth();
  19. int height = image.getIconHeight();
  20. long angle = 30;
  21.  
  22. public MyPanel(){
  23. super();
  24. }
  25. public void paintComponent(Graphics g){
  26.     super.paintComponent(g);
  27.     Graphics2D g2d = (Graphics2D)g;
  28.     g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
  29.     g2d.drawImage(image.getImage(), 60, 60, this);
  30.     g2d.dispose();
  31. }
  32. }//end class MyPanel
  33.  
  34.  
  35. int currentIndex; //Currently displayed Item
  36. Product[] supplies = new Product[4];
  37. JLabel name ;
  38. JLabel number;
  39. JLabel title;
  40. JLabel quantity;
  41. JLabel price;
  42. JLabel fee;
  43. JLabel totalValue;
  44.  
  45. JTextField nameField = new JTextField(20);
  46. JTextField numberField = new JTextField(20);
  47. JTextField titleField = new JTextField(20);
  48. JTextField quantityField = new JTextField(20);
  49. JTextField priceField = new JTextField(20);
  50.  
  51.  
  52. JPanel display;
  53. JPanel displayHolder;
  54. JPanel panel;
  55. boolean locked = false; //Notice how I've used this flag to keep the interface clean
  56. public Inventory2() {
  57. makeTheDataItems();
  58.  
  59. setSize(700, 500);
  60. setTitle("Inventory Program");
  61. //make the panels
  62. display = new JPanel();
  63. JPanel other = new JPanel();
  64. other.setLayout(new GridLayout(2, 1));
  65. JPanel picture = new MyPanel();
  66. JPanel buttons = new JPanel();
  67. JPanel centerPanel = new JPanel();
  68. displayHolder = new JPanel();
  69. display.setLayout(new GridLayout(7, 1));
  70. //other.setLayout(new GridLayout(1, 1));
  71.  
  72. //make the labels
  73. name = new    JLabel("Name :");
  74. number = new JLabel("Number :");
  75. title = new JLabel("Title    :");
  76. quantity = new JLabel("Quantity :");
  77. price = new JLabel("Price    :");
  78. fee = new JLabel("Fee :");
  79. totalValue = new JLabel("Total Value :");
  80.  
  81. //Use the utility method to make the buttons
  82. JButton first = makeButton("First");
  83. JButton next = makeButton("Next");
  84. JButton previous = makeButton("Previous");
  85. JButton last = makeButton("Last");
  86. JButton search = makeButton("Search");
  87.  
  88.  
  89. //Other buttons
  90. JButton add = makeButton("Add");
  91. JButton modify = makeButton("Modify");
  92. JButton delete = makeButton("Delete");
  93. JButton save = makeButton("Save");
  94. JButton exit = makeButton("Exit");
  95.  
  96. //Add the labels to the display panel
  97. display.add(name);
  98. display.add(number);
  99. display.add(title);
  100. display.add(quantity);
  101. display.add(price);
  102. display.add(fee);
  103.  
  104. //add the buttons to the buttonPanel
  105. buttons.add(first);
  106. buttons.add(previous);
  107. buttons.add(next);
  108. buttons.add(last);
  109. buttons.add(search);
  110.  
  111.  
  112. //Add the picture panel and display to the centerPanel
  113. displayHolder.add(display);
  114. centerPanel.setLayout(new GridLayout(2, 1));
  115. centerPanel.add(picture);
  116. centerPanel.add(displayHolder);
  117. other.add(buttons);
  118. JPanel forAdd = new JPanel(); // add the other buttons to this panel
  119. forAdd.add(add);
  120. forAdd.add(modify);
  121. forAdd.add(delete);
  122. forAdd.add(save);
  123. forAdd.add(exit);
  124. other.add(forAdd);
  125.  
  126.  
  127. //Add the panels to the frame
  128.  
  129. getContentPane().add(centerPanel, "Center");
  130. getContentPane().add(other, "South");
  131. this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  132. setVisible(true);
  133. }
  134.  
  135. private void makeTheDataItems () {
  136. Product p1 = new DVD("The one", 001, 200, 100, "The one");
  137. Product p2 = new DVD("Once upon a time in China V", 002, 500, 10000, "Once upon a time in China V");
  138. Product p3 = new DVD("Rat Race", 003, 100, 3000, "Rat Race");
  139. Product p4 = new DVD("The Man in the Iron Mask", 004, 3000, 9000, "The Man in the Iron Mask");
  140. supplies[0] = p1;
  141. supplies[1] = p2;
  142. supplies[2] = p3;
  143. supplies[3] = p4;
  144. }
  145.  
  146. //Utility method for creating and dressing buttons
  147. private JButton makeButton(String label) {
  148. JButton button = new JButton(label);
  149. button.setPreferredSize(new Dimension(100, 25));
  150. button.setActionCommand(label);
  151. button.addActionListener(this);
  152. return button;
  153. }
  154.  
  155. private void addItem() {
  156. panel = new JPanel();
  157. JPanel add = new JPanel();
  158.  
  159. add.setLayout(new GridLayout(7, 2));
  160. JButton addIt = makeButton("Add Item");
  161.  
  162. JLabel name = new JLabel("Name :");
  163. JLabel title = new JLabel("Title    :");
  164. JLabel quantity = new JLabel("Quantity :");
  165. JLabel price = new JLabel("Price    :");
  166.  
  167. add.add(name); add.add(nameField);
  168.  
  169. add.add(title); add.add(titleField);
  170. add.add(quantity); add.add(quantityField);
  171. add.add(price); add.add(priceField);
  172. panel.add(add);
  173. JPanel forAddIt = new JPanel();
  174. forAddIt.add(addIt);
  175. panel.add(forAddIt);
  176.  
  177. displayHolder.remove(display);
  178. displayHolder.add(panel);
  179.  
  180. //display = panel;
  181. this.setVisible(true);
  182. }
  183.  
  184.  
  185. public static void main( String args[]) {
  186. new Inventory2().displayFirst(); //The main method should not have too much code
  187. } // end main method
  188.  
  189. public void actionPerformed(ActionEvent event) {
  190. String command = event.getActionCommand(); //This retrieves the command that we set for the button
  191. //Always compare strings using the .equals method and not using ==
  192. if(command.equals("First")) {
  193. if(!locked) {
  194.     displayFirst();
  195. }
  196. }
  197. else if(command.equals("Next")) {
  198. if(!locked) {
  199.     displayNext();
  200. }
  201. }
  202. else if(command.equals("Previous")) {
  203. if(!locked) {
  204.     displayPrevious();
  205. }
  206. }
  207. else if(command.equals("Last")) {
  208. if(!locked) {
  209.     displayLast();
  210. }
  211. }
  212. else if(command.equals("Exit")) {
  213. this.dispose();
  214. System.exit(0);
  215. }
  216. else if(command.equals("Add")) {
  217. if(!locked) {
  218.     addItem();
  219.     locked = true;
  220. }
  221. }
  222. else if(command.equals("Add Item")) {
  223. if(!locked) {
  224.     addItemToArray();
  225.     locked = true;
  226. }
  227. }
  228. else if(command.equals("Modify")) {
  229. if(!locked) {
  230.     modify();
  231.     locked = true;
  232. }
  233. }
  234. else if(command.equals("Update")) {
  235.      if(!locked) {
  236.     modifyItemInArray();
  237.     locked = true;
  238. }
  239. }
  240. else if(command.equals("Delete")) {
  241. if(!locked) {
  242.     DVD dvd = (DVD)supplies[currentIndex];
  243.      int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete item "+dvd.getItemNumber());
  244.          if(confirm == JOptionPane.YES_OPTION) {
  245.      removeItemAt(currentIndex);
  246.      displayFirst();
  247.      }
  248. }
  249.  
  250. }
  251.  
  252. }
  253. private void modify() {
  254. DVD dvd = (DVD)supplies[currentIndex];
  255. panel = new JPanel();
  256. JPanel add = new JPanel();
  257. add.setLayout(new GridLayout(7, 2));
  258. JButton update = makeButton("Update");
  259. JLabel number = new JLabel("Number :");
  260. JLabel name = new JLabel("Name :");
  261. JLabel title = new JLabel("Title    :");
  262. JLabel quantity = new JLabel("Quantity :");
  263. JLabel price = new JLabel("Price    :");
  264. add.add(number);
  265. numberField.setText(""+dvd.getItemNumber()); numberField.setEditable(false); add.add(numberField);
  266. add.add(name);
  267. nameField.setText(dvd.getItemName()); add.add(nameField);
  268. titleField.setText(dvd.getTitle()); titleField.setEditable(false);
  269. add.add(title); add.add(titleField);
  270. add.add(quantity);
  271. quantityField.setText(""+dvd.getStockQuantity());
  272. add.add(quantityField);
  273. add.add(price);
  274. add.add(priceField); priceField.setText(""+dvd.getItemPrice());
  275. panel.add(add);
  276. JPanel forAddIt = new JPanel();
  277. forAddIt.add(update);
  278. panel.add(forAddIt);
  279. displayHolder.remove(display);
  280. displayHolder.add(panel);
  281. //display = panel;
  282.      this.setVisible(true);
  283.  
  284. }
  285.  
  286. private void addItemToArray() {
  287. Product p = new DVD(nameField.getText(), supplies.length + 1, Long.parseLong(quantityField.getText()),
  288. Double.parseDouble(priceField.getText()), titleField.getText());
  289. //Extend size of array by one first
  290. Product[] ps = new Product[supplies.length + 1];
  291. for(int i = 0; i < ps.length-1; i++) {
  292. ps[i] = supplies[i];
  293. }
  294. ps[supplies.length] = p;
  295. supplies = ps;
  296. displayHolder.remove(panel);
  297. displayHolder.add(display);
  298. displayLast();
  299. this.setVisible(false);
  300. this.setVisible(true);
  301.  
  302. }
  303.  
  304. //Utility method to ease the typing and reuse code
  305. //This method reduces the number of lines of our code
  306. private void displayItemAt(int index) {
  307.  
  308. DVD product = (DVD)supplies[index];
  309. name.setText("Item Name: "+ product.getItemName());
  310. number.setText("Item Number: "+ product.getItemNumber());
  311. title.setText("Title: "+ product.getTitle());
  312. quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
  313. price.setText("Item Price: "+ product.getItemPrice());
  314. totalValue.setText("Total: " + product.calculateInventoryValue());
  315. fee.setText("Fee :"+product.calculateRestockFee());
  316. locked = false;
  317. this.repaint();
  318.  
  319.  
  320. this.setVisible(true);
  321. }
  322.  
  323. private void modifyItemInArray() {
  324. Product p = new DVD(nameField.getText(), supplies.length + 1, Long.parseLong(quantityField.getText()),
  325. Double.parseDouble(priceField.getText()), titleField.getText());
  326. supplies[currentIndex] = p;
  327. displayHolder.remove(panel);
  328. displayHolder.add(display);
  329.     displayItemAt(currentIndex);
  330. this.setVisible(false);
  331. this.setVisible(true);
  332.  
  333. }
  334.  
  335. private void removeItemAt(int index) {
  336. Product[] temp = new Product[supplies.length-1];
  337. int counter = 0;
  338. for(int i = 0; i < supplies.length;i++) {
  339. if(i == index) { //skip the item to delete
  340. }
  341. else {
  342.     temp[counter++] = supplies[i];
  343. }
  344. }
  345. supplies = temp;
  346. }
  347.  
  348. public void displayFirst() {
  349. displayItemAt(0);
  350. currentIndex = 0;
  351. }
  352.  
  353. public void displayNext() {
  354. if(currentIndex == supplies.length-1) {
  355. displayFirst();
  356. currentIndex = 0;
  357. }
  358. else {
  359. displayItemAt(currentIndex + 1);
  360. currentIndex++;
  361.  
  362. }
  363. }
  364.  
  365. public void displayPrevious() {
  366. if(currentIndex == 0) {
  367. displayLast();
  368. currentIndex = supplies.length-1;
  369. }
  370. else {
  371. displayItemAt(currentIndex - 1);
  372. currentIndex--;
  373. }
  374. }
  375.  
  376. public void displayLast() {
  377. displayItemAt(supplies.length-1);
  378. currentIndex = supplies.length-1;
  379. }
  380.  
  381. }//end class Inventory2
  382.  
  383.  
  384.  
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
Feb 12 '07 #31

10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. class Product implements Comparable {
  4.  String name;
  5.  double number;
  6.  long stockQuantity;
  7.  double price;
  8.  
  9.  public Product() {
  10.   name = "";
  11.   number = 0.0;
  12.   stockQuantity = 0L;
  13.   price = 0.0;
  14.  
  15.  }
  16.  public Product(String name, int number, long stockQuantity, double price) {
  17.   this.name = name;
  18.   this.number = number;
  19.   this.stockQuantity = stockQuantity;
  20.   this.price = price;
  21.  }
  22.  public void setItemName(String name) {
  23.   this.name = name;
  24.  }
  25.  public String getItemName() {
  26.   return name;
  27.  }
  28.  public void setItemNumber(double number) {
  29.   this.number = number;
  30.  }
  31.  public double getItemNumber() {
  32.   return number;
  33.  }
  34.  public void setStockQuantity(long quantity) {
  35.   stockQuantity = quantity;
  36.  }
  37.  public long getStockQuantity() {
  38.   return stockQuantity;
  39.  }
  40.  public void setItemPrice(double price) {
  41.   this.price = price;
  42.  }
  43.  public double getItemPrice() {
  44.   return price;
  45.  }
  46.  public double calculateInventoryValue() {
  47.   return getItemPrice() * getStockQuantity();
  48.  }
  49.  public int compareTo (Object o) {
  50.   Product p = (Product)o;
  51.   return name.compareTo(p.getItemName());
  52.  }
  53.  public String toString() {
  54.   return "Name :"+getItemName() + "\nNumber"+number+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
  55.  }
  56.  
  57.  
  58.  
  59.  
  60. class DVD extends Product implements Comparable {
  61.  private String title;
  62.  public DVD() {
  63.   super(); //Call the constructor in Product
  64.   title = ""; //Add the additonal attribute
  65.  }
  66.  public DVD(String name, int number, long stockQuantity, double price, String title) {
  67.   super(name, number, stockQuantity, price); //Call the constructor in Product
  68.   this.title = title; //Add the additonal attribute
  69.  }
  70.  public void setTitle(String title) {
  71.   this.title = title;
  72.  }
  73.  public String getTitle() {
  74.   return title;
  75.  }
  76.  public double calculateInventoryValue() {
  77.   return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05;
  78.  }
  79.  
  80.  public double calculateRestockFee() {
  81.   return getItemPrice() * 0.05;
  82.  }
  83.  public int compareTo (Object o) {
  84.   Product p = (Product)o;
  85.   return getItemName().compareTo(p.getItemName());
  86.  }
  87.  public String toString() {
  88.   return "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nTitle :"+getTitle()+"\nValue"+calculateInventoryValue();
  89.  }
  90. }
  91.  
  92.  
The Product and DVD classes remain unchanged
Feb 12 '07 #32

P: 40
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.
Feb 12 '07 #33

P: 11
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
Feb 13 '07 #34

P: 11
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
Feb 13 '07 #35

P: 40
Hehe, I feel your pain bro, wait till you get further into the class. Here is the code I have from week 5:
Expand|Select|Wrap|Line Numbers
  1. // Product class
  2.  
  3.  
  4. public class Product {
  5.  
  6.     private String title;
  7.     private double item;
  8.     private double stock;
  9.     private double price;
  10.  
  11.     // Constructor to initialize the product information.
  12.     public Product(String title, double item, double stock,
  13.             double price) {
  14.         setTitle(title);
  15.         setItem(item);
  16.         setStock(stock);
  17.         setPrice(price);
  18.  
  19.     }
  20.  
  21.     // Method to get the title
  22.     public String getTitle() {
  23.         return title;
  24.     }
  25.  
  26.     // Method to get the item number
  27.     public double getItem() {
  28.         return item;
  29.     }
  30.  
  31.     //Method to get the stock
  32.     public double getStock() {
  33.         return stock;
  34.     }
  35.  
  36.     //Method to get the price
  37.     public double getPrice() {
  38.         return price;
  39.  
  40.  
  41.  
  42.     }
  43.  
  44.     // Method to set the title name.
  45.     public void setTitle(String title) {
  46.         this.title = title;
  47.     }
  48.  
  49.     // Method to set the item number.
  50.     public void setItem(double item) {
  51.         this.item = item;
  52.     }
  53.  
  54.     // Method to set the stock available.
  55.     public void setStock(double stock) {
  56.         this.stock = stock;
  57.     }
  58.  
  59.     //Method to set the price.
  60.     public void setPrice(double price) {
  61.         this.price = price;
  62.  
  63.     }
  64.     // Method to compute the value of inventory.
  65.     public double getInvValue() {
  66.         return this.stock *this.price;
  67.     }
  68.  
  69. }
  70.  
  71.  
  72. // This is a new class Inventory
  73. // Inventory program
  74. // Main application
  75.  
  76.  
  77.  
  78. public class Inventory
  79. {
  80.     public static void main (String args [])
  81.     {
  82.  
  83.     System.out.println ("Inventory of DVD Movies:\n");
  84.  
  85.         String title = "Beerfest";
  86.         double item = 1;
  87.         double stock = 15;
  88.         double price = 22.50;
  89.         double value;
  90.  
  91.     Product product = new Product (title, item, stock, price);
  92.  
  93.     System.out.printf( "%s%18s\n", "DVD Title:", product.getTitle() );
  94.  
  95.     System.out.printf( "%s%16s\n", "Item #:", product.getItem() );
  96.  
  97.     System.out.printf( "%s%8s\n", "Number in Stock:" , product.getStock() );
  98.  
  99.     System.out.printf( "%s%18s\n", "Price:", product.getPrice() );
  100.  
  101.  
  102.  
  103.     System.out.printf( "%s%9s\n", "Inventory Value:", product.getInvValue() );
  104.  
  105.     }
  106. }
  107.  
Feb 13 '07 #36

P: 40
Let me know if you have any questions about the code.
Feb 13 '07 #37

P: 11
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
Feb 13 '07 #38

P: 11
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?
Feb 13 '07 #39

10K+
P: 13,264
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.
Feb 13 '07 #40

P: 40
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?
Feb 13 '07 #41

P: 40
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.
Feb 13 '07 #42

10K+
P: 13,264
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. else if(command.equals("Add Item")) {
  3.    addItemToArray();
  4.   }
  5.  
That part does not need to check the lock
Feb 13 '07 #43

P: 40
Is this the part I am suppose to change? I don't think I did it right.

Expand|Select|Wrap|Line Numbers
  1. //Always compare strings using the .equals method and not using ==
  2. if(command.equals("First")) {
  3. if(!locked) {
  4.     displayFirst();
  5. }
  6. }
  7. else if(command.equals("Next")) {
  8. if(!locked) {
  9.     displayNext();
  10. }
  11. }
  12. else if(command.equals("Previous")) {
  13. if(!locked) {
  14.     displayPrevious();
  15. }
  16. }
  17. else if(command.equals("Last")) {
  18. if(!locked) {
  19.     displayLast();
  20. }
  21. }
  22. else if(command.equals("Exit")) {
  23. this.dispose();
  24. System.exit(0);
  25.  }
  26.  
  27. }
  28. }
  29. else if(command.equals("Add Item")) {
  30.    addItemToArray();
  31.   }
  32. }
  33. }
  34. else if(command.equals("Modify")) {
  35. if(!locked) {
  36.     modify();
  37.     locked = true;
  38. }
  39. }
  40. else if(command.equals("Update")) {
  41.      if(!locked) {
  42.     modifyItemInArray();
  43.     locked = true;
  44. }
  45. }
  46.  
Feb 13 '07 #44

10K+
P: 13,264
Is this the part I am suppose to change? I don't think I did it right.

Expand|Select|Wrap|Line Numbers
  1. //Always compare strings using the .equals method and not using ==
  2. if(command.equals("First")) {
  3. if(!locked) {
  4.     displayFirst();
  5. }
  6. }
  7. else if(command.equals("Next")) {
  8. if(!locked) {
  9.     displayNext();
  10. }
  11. }
  12. else if(command.equals("Previous")) {
  13. if(!locked) {
  14.     displayPrevious();
  15. }
  16. }
  17. else if(command.equals("Last")) {
  18. if(!locked) {
  19.     displayLast();
  20. }
  21. }
  22. else if(command.equals("Exit")) {
  23. this.dispose();
  24. System.exit(0);
  25. }
  26.  
  27. }
  28. }
  29. else if(command.equals("Add Item")) {
  30. addItemToArray();
  31. }
  32. }
  33. }
  34. else if(command.equals("Modify")) {
  35. if(!locked) {
  36.     modify();
  37.     locked = true;
  38. }
  39. }
  40. else if(command.equals("Update")) {
  41.      if(!locked) {
  42.     modifyItemInArray();
  43.     locked = true;
  44. }
  45. }
  46.  
Yes that's the part. Why are you unsure about it?
Feb 13 '07 #45

P: 40
Well, I get this error when compiled:
Expand|Select|Wrap|Line Numbers
  1. C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:217: 'class' or 'interface' expected
  2. else if(command.equals("Add Item")) {
  3. ^
  4. C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:220: 'class' or 'interface' expected
  5. }
  6. ^
  7. C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:377: 'class' or 'interface' expected
  8. ^
  9. 3 errors
  10.  
  11. Tool completed with exit code 1
  12.  
What did I miss?
Feb 13 '07 #46

10K+
P: 13,264
Well, I get this error when compiled:
Expand|Select|Wrap|Line Numbers
  1. C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:217: 'class' or 'interface' expected
  2. else if(command.equals("Add Item")) {
  3. ^
  4. C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:220: 'class' or 'interface' expected
  5. }
  6. ^
  7. C:\Documents and Settings\stevew\My Documents\Java test\Inventory2.java:377: 'class' or 'interface' expected
  8. ^
  9. 3 errors
  10.  
  11. Tool completed with exit code 1
  12.  
What did I miss?
Could be something to do with your braces. Here is the complete actionPerformed method

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void actionPerformed(ActionEvent event) {
  3.   String command = event.getActionCommand(); //This retrieves the command that we set for the button
  4.   //Always compare strings using the .equals method and not using ==
  5.   if(command.equals("First")) {
  6.    if(!locked) {
  7.     displayFirst();
  8.    }
  9.   }
  10.   else if(command.equals("Next")) {
  11.    if(!locked) {
  12.     displayNext();
  13.    }
  14.   }
  15.   else if(command.equals("Previous")) {
  16.    if(!locked) {
  17.     displayPrevious();
  18.    }
  19.   }
  20.   else if(command.equals("Last")) {
  21.    if(!locked) {
  22.     displayLast();
  23.    }
  24.   }
  25.   else if(command.equals("Exit")) {
  26.    this.dispose();
  27.    System.exit(0);
  28.   }
  29.   else if(command.equals("Add")) {
  30.    if(!locked) {
  31.     addItem();
  32.     locked = true;
  33.    }
  34.   }
  35.   else if(command.equals("Add Item")) {
  36.    addItemToArray();
  37.   }
  38.   else if(command.equals("Modify")) {
  39.    if(!locked) {
  40.     modify();
  41.     locked = true;
  42.    }
  43.   }
  44.   else if(command.equals("Update")) {
  45.      if(!locked) {
  46.     modifyItemInArray();
  47.     locked = true;
  48.    }
  49.   }
  50.   else if(command.equals("Delete")) {
  51.    if(!locked) {
  52.     DVD dvd = (DVD)supplies[currentIndex];
  53.        int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete item "+dvd.getItemNumber());
  54.           if(confirm == JOptionPane.YES_OPTION) {
  55.      removeItemAt(currentIndex);
  56.      displayFirst();
  57.       }
  58.    }
  59.   } 
  60.  }
  61.  
Feb 13 '07 #47

P: 40
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.
Feb 13 '07 #48

10K+
P: 13,264
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.
Feb 13 '07 #49

P: 40
Opps, thats what I meant, modify and delete are good to go. I'm telling ya, its been quite the month hehe.
Feb 13 '07 #50

67 Replies

Post your reply

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