943,923 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 358
  • Java RSS
Aug 3rd, 2009
0

Inventory Program... again

Expand Post »
I am having an issue where with my program and reading all the posts on your site has been VERY useful in understanding this project thank you! Now for my issue i am having trouble getting the buttons to recognize the class provided for them. For instance (i use Netbeans) an i am getting an error for the Last, add, modify and so on. the only button that does not have an error is the previous button, I can not find what i screwed up! here is what i have been working with

Java Syntax (Toggle Plain Text)
  1. package javaapplication14;
  2.  
  3. import java.awt.*; // import the java awt package
  4. import java.awt.event.*; // Import the java event package
  5. import javax.swing.*; // Import the java swing package
  6.  
  7. public class VhsInventory {
  8.  
  9. // main method
  10. public static void main(String[] args) {
  11.  
  12. System.out.println("My Crappy VHS collection\n");
  13.  
  14. Movie vhs = null;
  15. Inventory inventory = new Inventory();
  16.  
  17. vhs = new Movie(1, "The Grinch", 2, 3f, "Comedy");
  18. System.out.println(vhs);
  19. inventory.addMovie(vhs);
  20.  
  21. vhs = new Movie(2, "Free Willy", 2, 1.50f, "Drama");
  22. System.out.println(vhs);
  23. inventory.addMovie(vhs);
  24.  
  25. vhs = new Movie(3, "Die Hard", 3, 5f, "Action");
  26. System.out.println(vhs);
  27. inventory.addMovie(vhs);
  28.  
  29. vhs = new Movie(4, "A Christmas Carol", 4, 4f, "Drama");
  30. inventory.addMovie(vhs);
  31. System.out.println(vhs);
  32.  
  33. vhs = new Movie(5, "The Mask", 3, 2.95f, "Comedy");
  34. System.out.println(vhs);
  35. inventory.addMovie(vhs);
  36.  
  37. inventory.printInventory();
  38. new InventoryGUI(inventory);
  39. } // end main
  40. } // end
  41.  
  42. class VHS {
  43.  
  44. private int itemNo;
  45. private String title;
  46. private int inStock;
  47. private double unitPrice;
  48.  
  49. VHS(int itemNo, String title, int inStock, double unitPrice) {
  50. this.itemNo = itemNo;
  51. this.title = title;
  52. this.inStock = inStock;
  53. this.unitPrice = unitPrice;
  54. }
  55.  
  56. public int getItemNo() {
  57. return itemNo;
  58. }
  59.  
  60. public String getTitle() {
  61. return title;
  62. }
  63.  
  64. public int getInStock() {
  65. return inStock;
  66. }
  67.  
  68. public double getUnitPrice() {
  69. return unitPrice;
  70. }
  71.  
  72. public double value() {
  73. return inStock * unitPrice;
  74. }
  75.  
  76. @Override
  77. public String toString() {
  78. return String.format("itemNo=%2d title=%-22s inStock=%3d price=$%7.2f value=$%8.2f",
  79. itemNo, title, inStock, unitPrice, value());
  80. }
  81. } // end
  82.  
  83. class Inventory {
  84. // Setup of array to hold 30 movies
  85.  
  86. private final int INVENTORY_SIZE = 30;
  87. private VHS[] items;
  88. private int numItems;
  89.  
  90. Inventory() {
  91. items = new Movie[INVENTORY_SIZE];
  92. numItems = 0;
  93. }
  94.  
  95. public int getNumItems() {
  96. return numItems;
  97. }
  98.  
  99. public VHS getVHS(int n) {
  100. return items[n];
  101. }
  102.  
  103. // Adds a Movie
  104. public void addMovie(VHS item) {
  105. items[numItems] = item;
  106. ++numItems;
  107. }
  108.  
  109. // Loop through
  110. public double value() {
  111. double sumOfInventory = 0.0;
  112.  
  113. for (int i = 0; i < numItems; i++) {
  114. sumOfInventory += items[i].value();
  115. }
  116.  
  117. return sumOfInventory;
  118. }
  119.  
  120. public void printInventory() {
  121. System.out.println("\n My Crappy VHS Collection\n");
  122.  
  123. // If no items were found
  124. if (numItems <= 0) {
  125. System.out.println("No Movies avalible.\n");
  126. } else {
  127. for (int i = 0; i < numItems; i++) {
  128. System.out.printf("%3d %s\n", i, items[i]);
  129. }
  130. System.out.printf("\n Inventory value is $%,.2f\n\n", value());
  131. }
  132. }
  133. } // end
  134.  
  135. class Movie extends VHS {
  136. // Holds movie Genre and restocking fee
  137.  
  138. public String genre;
  139.  
  140. // Constructor
  141. public Movie(int MovieID, String itemName, int quantityOnHand, double itemPrice, String genre) {
  142. super(MovieID, itemName, quantityOnHand, itemPrice);
  143. this.genre = genre;
  144. }
  145.  
  146. // To set the genre manually
  147. public void setgenre(String genre) {
  148. }
  149.  
  150. // Get the genre of this Movie
  151. public String getMoviegenre() {
  152. return genre;
  153. }
  154.  
  155. @Override
  156. public double value() {
  157. return super.value() + restockingFee();
  158. }
  159.  
  160. // 5% restocking fee
  161. public double restockingFee() {
  162. return super.value() * 0.05f;
  163. }
  164. } // end
  165.  
  166.  
  167. // GUI for the Inventory
  168. class Vhslogo extends JPanel {
  169.  
  170. @Override
  171. public void paintComponent(Graphics g) {
  172. super.paintComponent(g); // call paintComponent
  173.  
  174. // draws larger 3d rectangle
  175. g.setColor(Color.red);
  176. g.draw3DRect(45, 15, 180, 40, true);
  177.  
  178. // draws small black square
  179. g.setColor(Color.BLACK);
  180. g.draw3DRect(5, 15, 40, 40, true);
  181. g.fill3DRect(5, 15, 40, 40, false);
  182.  
  183. // draws company name
  184. g.setColor(Color.BLACK);
  185. g.setFont(new Font("Arial", Font.BOLD, 25));
  186. g.drawString("Crappy VHS Inventory", 10, 45);
  187.  
  188. } // end paintComponent
  189. } // end class
  190.  
  191. class InventoryGUI extends JFrame {
  192.  
  193. private Inventory theInventory;
  194. private int index = 1;
  195.  
  196. // GUI elements to display currently selected VHS information
  197. private final JLabel itemNumberLabel = new JLabel("Number:");
  198. private JTextField itemNumberText;
  199. private final JLabel prodnameLabel = new JLabel("Title:");
  200. private JTextField prodnameText;
  201. private final JLabel prodpriceLabel = new JLabel("Price: $");
  202. private JTextField prodpriceText;
  203. private final JLabel numinstockLabel = new JLabel(" Avalibable:");
  204. private JTextField numinstockText;
  205. private final JLabel valueLabel = new JLabel(" Total Price:");
  206. private JTextField valueText;
  207. private final JLabel restockingFeeLabel = new JLabel(" Restocking Fee:");
  208. private JTextField restockingFeeText;
  209. private final JLabel totalValueLabel = new JLabel(" Total Value:");
  210. private JTextField totalValueText;
  211. private JPanel centerPanel;
  212. private JPanel buttonPanel;
  213.  
  214. // constructor for the GUI
  215. InventoryGUI(Inventory inventory) {
  216. super ("My Crappy VHS Collection");
  217. Vhslogo jpanel = new Vhslogo();
  218. final Dimension dim = new Dimension(140, 60);
  219. final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
  220. JPanel jp;
  221.  
  222. theInventory = inventory;
  223.  
  224. // setup the GUI
  225.  
  226. // product information
  227. // setup a panel to collect all the components.
  228. centerPanel = new JPanel();
  229. centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
  230.  
  231. buttonPanel = new JPanel();
  232. JButton nextButton = new JButton("Next");
  233. nextButton.addActionListener(new NextButtonHandler());
  234. buttonPanel.add(nextButton);
  235. centerPanel.add(buttonPanel);
  236.  
  237. JButton previousButton = new JButton("Back");
  238. previousButton.addActionListener(new PreviousButtonHandler());
  239. buttonPanel.add(previousButton);
  240.  
  241.  
  242. JButton lastButton = new JButton("Last");
  243. lastButton.addActionListener(new LastButtonHandler());
  244. buttonPanel.add(lastButton);
  245.  
  246. JButton addButton = new JButton("Add");
  247. addButton.addActionListener(new AddButtonHandler());
  248. buttonPanel.add(addButton);
  249.  
  250. JButton deleteButton = new JButton("Delete");
  251. deleteButton.addActionListener(new DeleteButtonHandler());
  252. buttonPanel.add(deleteButton);
  253.  
  254. JButton modifyButton = new JButton("Modify");
  255. modifyButton.addActionListener(new ModifyButtonHandler());
  256. buttonPanel.add(modifyButton);
  257.  
  258. JButton saveButton = new JButton("Save");
  259. saveButton.addActionListener(new SaveButtonHandler());
  260. buttonPanel.add(saveButton);
  261.  
  262. JButton findButton = new JButton("Find");
  263. findButton.addActionListener(new findButtonHandler());
  264. buttonPanel.add(findButton);
  265.  
  266.  
  267. centerPanel.add(buttonPanel);
  268. jp = new JPanel(flo);
  269. itemNumberLabel.setPreferredSize(dim);
  270. jp.add(itemNumberLabel);
  271. itemNumberText = new JTextField(3);
  272. itemNumberText.setEditable(false);
  273. jp.add(itemNumberText);
  274. centerPanel.add(jp);
  275.  
  276. jp = new JPanel(flo);
  277. prodnameLabel.setPreferredSize(dim);
  278. jp.add(prodnameLabel);
  279. prodnameText = new JTextField(12);
  280. prodnameText.setEditable(false);
  281. jp.add(prodnameText);
  282. centerPanel.add(jp);
  283.  
  284. jp = new JPanel(flo);
  285. prodpriceLabel.setPreferredSize(dim);
  286. jp.add(prodpriceLabel);
  287. prodpriceText = new JTextField(12);
  288. prodpriceText.setEditable(false);
  289. jp.add(prodpriceText);
  290. centerPanel.add(jp);
  291.  
  292. jp = new JPanel(flo);
  293. numinstockLabel.setPreferredSize(dim);
  294. jp.add(numinstockLabel);
  295. numinstockText = new JTextField(5);
  296. numinstockText.setEditable(false);
  297. jp.add(numinstockText);
  298. centerPanel.add(jp);
  299.  
  300. jp = new JPanel(flo);
  301. restockingFeeLabel.setPreferredSize(dim);
  302. jp.add(restockingFeeLabel);
  303. restockingFeeText = new JTextField(5);
  304. restockingFeeText.setEditable(false);
  305. jp.add(restockingFeeText);
  306. centerPanel.add(jp);
  307.  
  308. jp = new JPanel(flo);
  309. valueLabel.setPreferredSize(dim);
  310. jp.add(valueLabel);
  311. valueText = new JTextField(17);
  312. valueText.setEditable(false);
  313. jp.add(valueText);
  314. centerPanel.add(jp);
  315.  
  316.  
  317. jp = new JPanel(flo);
  318. totalValueLabel.setPreferredSize(dim);
  319. jp.add(totalValueLabel);
  320. totalValueText = new JTextField(17);
  321. totalValueText.setEditable(false);
  322. jp.add(totalValueText);
  323. centerPanel.add(jp);
  324.  
  325. // add the panel to the GUI display
  326. setContentPane(centerPanel);
  327.  
  328. repaintGUI();
  329.  
  330. setDefaultCloseOperation(EXIT_ON_CLOSE);
  331. setSize(560, 530);
  332. setResizable(false);
  333. setLocationRelativeTo(null);
  334. setVisible(true);
  335. }
  336.  
  337. public void repaintGUI() {
  338. Movie temp = (Movie) theInventory.getVHS(index);
  339.  
  340. if (temp != null) {
  341. itemNumberText.setText("" + temp.getItemNo());
  342. prodnameText.setText(temp.getTitle());
  343. prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice()));
  344. restockingFeeText.setText(String.format("$%.2f", temp.restockingFee()));
  345. numinstockText.setText("" + temp.getInStock());
  346. valueText.setText(String.format("$%.2f", temp.value()));
  347. }
  348. totalValueText.setText(String.format("$%.2f", theInventory.value()));
  349. }
  350.  
  351. class FirstButtonHandler implements ActionListener {
  352.  
  353. public void actionPerformed(ActionEvent e) {
  354. index = 1;
  355. repaintGUI();
  356. }
  357. }
  358.  
  359. class PreviousButtonHandler implements ActionListener {
  360.  
  361. public void actionPerformed(ActionEvent e) {
  362. int numItems = theInventory.getNumItems();
  363. if (numItems != 0) {
  364. index = (--index) % numItems;
  365. }
  366.  
  367. if (index < 0) {
  368. index = numItems - 1;
  369. }
  370.  
  371. if (numItems == 0) // catches for last entry not needed because we add a blank entry at zero but just in case
  372. {
  373. JOptionPane.showMessageDialog(null, "That's the last entry.", "Please try again", JOptionPane.ERROR_MESSAGE);
  374. }
  375.  
  376. repaintGUI();
  377. }
  378.  
  379.  
  380. class NextButtonHandler implements ActionListener {
  381.  
  382. public void actionPerformed(ActionEvent e) {
  383. int numItems = theInventory.getNumItems();
  384. index = (++index) % numItems;
  385. repaintGUI();
  386. }
  387. }
  388.  
  389. class LastButtonHandler implements ActionListener{
  390.  
  391. public void actionPerformed(ActionEvent e) {
  392. int numItems = theInventory.getNumItems();
  393. index = (numItems - 1) % numItems;
  394. repaintGUI();
  395. }
  396. }
  397.  
  398.  
  399. class AddButtonHandler implements ActionListener {
  400.  
  401. public void actionPerformed(ActionEvent e) {
  402. repaintGUI();
  403. }
  404. }
  405.  
  406. class SaveButtonHandler implements ActionListener {
  407.  
  408. public void actionPerformed(ActionEvent e) {
  409. repaintGUI();
  410. }
  411. }
  412.  
  413. class ModifyButtonHandler implements ActionListener {
  414.  
  415. public void actionPerformed(ActionEvent e) {
  416. repaintGUI();
  417. }
  418.  
  419. }
  420. class DeleteButtonHandler implements ActionListener {
  421.  
  422. public void actionPerformed(ActionEvent e) {
  423. repaintGUI();
  424. }
  425. }
  426.  
  427. class findButtonHandler implements ActionListener {
  428.  
  429. public void actionPerformed(ActionEvent e) {
  430. repaintGUI();
  431. }
  432. }
  433. }
  434. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gueito is offline Offline
1 posts
since Aug 2009
Aug 4th, 2009
-1

Re: Inventory Program... again

You missing closing bracket on PreviousButtonHandler class
    class PreviousButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int numItems = theInventory.getNumItems();
            if (numItems != 0) {
                index = (--index) % numItems;
            }

            if (index < 0) {
                index = numItems - 1;
            }

            if (numItems == 0) // catches for last entry not needed because we add a blank entry at zero but just in case
            {
                JOptionPane.showMessageDialog(null, "That's the last entry.", "Please try again", JOptionPane.ERROR_MESSAGE);
            }

            repaintGUI();
        }
    }
and after adding this one you will need to delete one at the end of the code (end of VhsInventory class). Look like while you been adding new handlers you placed them directly in PreviousButtonHandler class.

PS: You should also start putting classes in they own files. Code is growing and difficult to maintain
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Aug 4th, 2009
0

Re: Inventory Program... again

I think you should look at all of your brackets, most of your classes are contained within the PreviousButton class or whatever it is called, so get counting

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Inventory program Pt3
Next Thread in Java Forum Timeline: Accessing the another class from Main (the whole class)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC