3 Replies - 541 Views - Last Post: 15 January 2013 - 11:04 PM Rate Topic: -----

#1 raptor13fox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-January 13

Having Issue with inventory program

Posted 15 January 2013 - 09:43 PM

I have been trying to get this inventory program GUI to properly work. Several issues I am running into are:


Inability to pull my supplies into the GUI and be displayed from my main program.
Unable to get the buttons to work properly. (the quit button works)
My inventory should pop up when the program runs and the buttons should cycle correctly.
If I run my main program by itself, I get only a Gui box, nothing filled in it so something isnt being passed and I dont understand why.
I believe when I run my main program, The gui should run as well but im not sure how to get it to do that.

I honestly dont know how to compile my program ( im very noob with java and in dire need of a walkthrough)

I will post my code, but if there is someone who can offer me assistance and walk through this with me either on the phone, skype or something, it would be of great help. Feel free to tear into it and let me know in detail if you can what i need to change. Any help is appreciated.

Main Program***

// This program calculates inventory value
/**
  
 *
 * @author Keith
 */

import javax.swing.*;

import java.util.Arrays;



 
public class InventoryProgramPart3{
        
    
     // main method begins program execution
     public static void main(String args[] )
     {
         
 
          // display a welcome message to the InventoryProgramPart3 user
         
          System.out.println( "Welcome to Inventory Program Part 3!" );
 
          // office supplies
          
          
           
          //supplies[] supplies = new supplies[5];
          supplies [] suppliesArray;
                suppliesArray = new supplies[5];
                 
          supplies notepads = new supplies( 1712, "notepads", 60, 2.75 );
          
          supplies pencils = new supplies( 1679, "pencils", 75, 1.25 );
          
          supplies folders = new supplies( 2651, "folders", 30, 4.75 );
          
          supplies envelopes = new supplies( 7884, "envelopes", 15, 5.25 );
          
          supplies markers = new supplies( 2115, "markers", 45, 3.50 );
                             
          // display the inventories one at a time
          
          envelopes.showInventory();
          
          folders.showInventory();
          
          markers.showInventory();
          
          notepads.showInventory();
          
          pencils.showInventory();
          
 
          // sort supplies by name
       
          
          for ( int i = 0; i < args.length; i++ ) {
              
             System.out.println( args[i] + ", " );
         }
 
          double array[] = { 78.75, 142.50, 157.50, 165.00, 93.75 };
          
          double total = 0;
          
 
          // add each element's value to total
          
          for ( int counter = 0; counter < array.length; counter++) {
              
             total += array[ counter ];
             
         }
          System.out.printf( "\nTotal inventory value is: $%.2f\n", total );
 
          System.out.println( "\nThank you for using Inventory Program Part 5!\n" );
          
          JFrame suppliesGUI=new JFrame(); 
          
          suppliesGUI.setVisible(true);
          
          suppliesGUI.setSize(600, 400);
       
          
     }  //end method main
 
} // end class InventoryProgramPart3  
 
// Office Supplies


class supplies
{
     public int suppliesNumber;
     
     public String suppliesName = new String();
     
     public int suppliesUnits;
     
     public double suppliesPrice;
 
     // set supplies number
     public void setSuppliesNumber( int number )
     {
          this.suppliesNumber = number;
     } // end method set supplies number
 
     // return supplies number
     public int getSuppliesNumber()
     {
          return suppliesNumber;
     } // end method get supplies number
 
     // set supplies name
     public void setSuppliesName( String name )
     {
          this.suppliesName = name;
     } // end method set supplies name
 
     // return supplies name
     public String getSuppliesName()
     {
          return suppliesName;
     } // end method get supplies name
 
     // set supplies in stock
     public void setSuppliesUnits( int units )
     {
          this.suppliesUnits = units;
     } // end method set supplies units
 
     // return supplies units
     public int getSuppliesUnits()
     {
          return suppliesUnits;
     } // end method get supplies units
 
     // set supplies price
     public void setSuppliesPrice( double price )
     {
          this.suppliesPrice = price;
     } // end method set supplies price
 
     // return supplies price
     public double getSuppliesPrice()
     { 
          return suppliesPrice;
     } // end method get supplies price
 
     // calculate supplies inventory value
     public double getValue()
     {
          return suppliesUnits * suppliesPrice;
     } // end method supplies inventory value 
 
    // four-argument constructor
     supplies( int number, String name, int units, double price )
     { 
          suppliesNumber = number;
          suppliesName = name;
          suppliesUnits = units;
          suppliesPrice = price;
     } // end four-argument constructor
 
    // display inventory
     public void showInventory()
             
     {
         
          System.out.println(); // outputs blank line
 
          System.out.println( "Product Number:  "+suppliesNumber );
         
          System.out.println( "Product Name:  "+suppliesName );
          
          System.out.println( "Units in Stock:  "+suppliesUnits );
          
          System.out.printf( "Unit Price:  $%.2f", suppliesPrice );
                     
          // value() method and display the value
          System.out.printf( "\nInventory value of "+suppliesName+ " is = $%.2f\n",
               getValue() );     
   
    } // end display inventory
 
} // end class supplies

 



GUI***
//Checkpoint: Inventory Program Part 5
//Implement GUI interface


import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;



class suppliesGUI extends JFrame implements ActionListener //define class

{


	public static JButton enterButton;
        
	public static JButton quitButton;
        
	public static JButton showButton;
        
        public static JButton previousButton;
        
        public static JButton nextButton;
        
        public static JButton addButton;
        
        public static JButton deleteButton;
        
        public static JButton modifyButton;
        
        public static JButton saveButton;
        
        public static JButton searchButton;
        
	public static JTextField ProductNameField;
        
	public static JTextField ProductNumberField;
        
	public static JTextField ProductPriceField;
        
	public static JTextField ProductQuantityField;
        
	public static JTextArea ProductArea;
        
	public static ArrayList<supplies> suppliesArray = new ArrayList<supplies>();
        
        
      


	public suppliesGUI()
	{
            
		super ( "suppliesGUI" );

		supplies [] suppliesArray;
                
                suppliesArray = new supplies[6];
                

		Container con = getContentPane();

		BoxLayout layout = new BoxLayout(con, BoxLayout.Y_AXIS);
                
		con.setLayout(layout);

		JPanel topPanel= new JPanel();
                
		JPanel bottomPanel= new JPanel();
                
		con.add(topPanel, BorderLayout.NORTH);
                
		con.add(bottomPanel, BorderLayout.SOUTH);

		Box lBoxUpperL= new Box(BoxLayout.Y_AXIS);
                
		Box lBoxUpperR= new Box(BoxLayout.Y_AXIS);
                
		Box lBoxLowerL= new Box(BoxLayout.Y_AXIS);
                
		Box lBoxLowerR= new Box(BoxLayout.Y_AXIS);
                
			
		topPanel.add(lBoxUpperL);
                
		topPanel.add(lBoxUpperR);
                
		bottomPanel.add(lBoxLowerL);
                
		bottomPanel.add(lBoxLowerR);
                
		
		JLabel ProductNameLabel = new JLabel( "Product Name :" );
                
		ProductNameField = new JTextField("",50);
		
		JLabel ProductNumberLabel = new JLabel( " Product Number:" );
                
		ProductNumberField = new JTextField("",20);

		JLabel ProductPriceLabel = new JLabel( " Product Price:" );
                
		ProductPriceField = new JTextField("",6);
		
		JLabel ProductQuantityLabel = new JLabel( "# in stock:" );
                
		ProductQuantityField = new JTextField("",3);
                
		JLabel ProductLabel = new JLabel("Product List");
                
		ProductArea = new JTextArea ();
                
                
		lBoxUpperL.add(ProductNameLabel);
                
		lBoxUpperL.add(ProductNameField);
                
		lBoxUpperL.add(ProductNumberLabel);
                
		lBoxUpperL.add(ProductNumberField);
                
		lBoxUpperL.add(ProductPriceLabel);
                
		lBoxUpperL.add(ProductPriceField);
                
		lBoxUpperL.add(ProductQuantityLabel);
                
		lBoxUpperL.add(ProductQuantityField);
              
		lBoxUpperL.add(ProductLabel);
                
		lBoxUpperL.add(ProductArea);
                
                
                
                

		enterButton = new JButton("Enter");
                
                enterButton.addActionListener (this);
                
		showButton = new JButton("Show");
                
                showButton.addActionListener (this);
                
		quitButton = new JButton("Quit");
                
                quitButton.addActionListener (this);
                
                previousButton = new JButton ("Previous") ;
                
                previousButton.addActionListener(this);
                
                nextButton = new JButton ("Next") ;
                
                nextButton.addActionListener(this);
                
                addButton = new JButton("Add");
                
                addButton.addActionListener (this);
                
		deleteButton = new JButton("Delete");
                
                deleteButton.addActionListener (this);
                
		modifyButton = new JButton("Modify");
                
                modifyButton.addActionListener (this);
                
                searchButton = new JButton ("Search") ;
                
                searchButton.addActionListener(this);
                
                saveButton = new JButton ("Save") ;
                
                saveButton.addActionListener(this);
                
		lBoxUpperL.add(enterButton);
                
		lBoxUpperL.add(showButton);
                
		lBoxUpperR.add(quitButton);
                
                lBoxLowerR.add(nextButton);
                
                lBoxLowerL.add(previousButton);
                
                lBoxUpperL.add(addButton);
                
		lBoxUpperL.add(deleteButton);
                
		lBoxUpperR.add(modifyButton);
                
                lBoxLowerR.add(searchButton);
                
                lBoxLowerL.add(saveButton);
                
                
	
		//Pack the graphics output and set it to visible
		pack();        
		setVisible( true);
                
               
		//Create a Close Button Listener Class 
		class CloseButtonListener implements ActionListener
		{
                    @Override
			public void actionPerformed( ActionEvent e)
			{ 
				//This closes the current JFrame Window                              
                              	suppliesGUI.this.dispose();
                        }

	}
                
   

	quitButton.addActionListener( new CloseButtonListener());
                
	setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);

	addWindowListener( new WindowAdapter() 
	{
         
            
            @Override
		public void windowClosed(WindowEvent e)
		{
			//This outputs into the command line window so we can terminate the Java app
			System.out.println( "Thanks for playing!");
			System.exit(0);
		}
	}
	);

	}

		public static void main(String args []) //main method
		{
			/* Create an Instance of the class */
			suppliesGUI myFrame;
                        
                        myFrame = new suppliesGUI();
                        
			myFrame.setSize(600,400);
                        
 
                }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
                        
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Having Issue with inventory program

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Having Issue with inventory program

Posted 15 January 2013 - 10:13 PM

I think you're off to a good start on the OO side, but you're missing the mark in a lot of ways on the GUI. I honestly think you should drop the GUI on this project and focus on building on the basics. You need a strong grasp of OOP and the basics before delving into the Java GUI. You're really all over the place on that one, and it would be better to scrap it and start over than to fix what you have.

You aren't really using arrays and your class correctly. If you have a supplies[], you shouldn't have five separate supplies variables. Also, you shouldn't be declaring an array and filling it with constants to get the total. Use the getter methods on your supplies objects.

Also, remember to use proper naming conventions. Class names in Java are capitalized, and classes model singular objects. So naming your class Supply is more descriptive and standards adherent.

I have a tutorial Moving Away From Parallel Arrays that covers basic OOP in the context of an inventory program. It should help you get going in the right direction.

Quote

I honestly dont know how to compile my program ( im very noob with java and in dire need of a walkthrough)

Read the Tutoracles on configuring your environment to compile.

Hope this helps some!
Was This Post Helpful? 1
  • +
  • -

#3 raptor13fox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-January 13

Re: Having Issue with inventory program

Posted 15 January 2013 - 10:52 PM

This project is the last part of my final. My instructor is useless as I have been asking for help since day 1 with no care apparently from her. This program can be seen here:

Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee.

Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.

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.
________________________________-

I do not expect work to be done for me but i will basically bed for help on how to get this figured out. I am using netbeans and usually just edit my program for the next week without creating a new one. This may not be how it needs to be done as I understand it gets alittle messy.

I will check out your tutorials but in the meantime, If you could give me pointers one where to start( getting the inventory to show up in the GUI, then getting the buttons to function, that would be amazing. I am currently in phase 2 and 3 BNOC for the military so my best time is 6pm central US timezone to work on this.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Having Issue with inventory program

Posted 15 January 2013 - 11:04 PM

The first thing is that I can hardly read your GUI code. Please make sure to properly format your code. NetBeans has a tool to help you do this.

Next, don't use the static keyword. It's not good OO practice. Your main() method when all is said and done should just instantiate an instance of the GUI and leave it at that.

Quote

Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee.

The key with this is managing an index variable. The first button should set the index = 0, and the last button should set the index = array.length. The next and previous buttons get a little tricky. The next button is fairly straight-forward until you get to the end. The modulus operator (%) is helpful here. Setting index = (index + 1) % array.length; will increment index and reset it to 0 when it has reached the end of the array. With previous, you have to check when index = 0, and then set it to array.length-1.

Quote

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.

After reading this. An ArrayList might be a better choice to use than a static array, as it handles insertion and deletion. Modification should be fairly straight-forward.

Quote

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.

This involves a basic linear search, or using a for loop to traverse your ArrayList.

Hopefully this gets you thinking in the right direction. The key here is to take it piece by piece. Given the complexity of the program, I would advise some architecture and design. I have a tutorial on architecture as well as a tutorial on getting started with Swing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1