Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to create a menu for the user which add new parts,search existing parts and display the table to view all in Java?

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to create a menu for the user which add new parts,search existing parts and display the table to view all in Java?

    In this assignment we are to create a two dimensional array to do the following
    1. Create a menu for the user to add new parts (without duplicating or leaving any fields blank)
    2. Search for exiting parts using Plain Wrap as the reference point
    3. Populate the table for the user to view all
    Below is my code, and it won't add the new part numbers or allow me to search or display the table with all the parts.

    I'm sure i'm forgetting a couple of easy things here, but i've been working on this for the last few hours. Help me keep my sanity!!!

    Thanks in advance!!


    import javax.swing.*;
     
    import java.awt.event.*;
     
    public class Ch10Assignment extends JFrame implements ActionListener {
     
         String[][] partNumberString
         = {  {"PR214","PR223","PR224","PR246","PR247","PR248","PR324", "PR326","PR444"},
              {"MR43T","R43","R43N","R46N","R46TS","R46TX","S46","SR46E","47L"},
              {"RBL8","RJ6","RN4","RN8","RBL17Y","RBL12-6","J11","XEJ8","H12"},
              {"14K22","14K24","14K30","14K32","14K33","14K35","14K38","14K40","14K44"}  };
         String [] brandString = {"Plain Wrap", "Brand A", "Brand C", "Brand X"};
         int counterInteger = 0;
         int plainWrapInteger [] = new int [3];
         String plainWrapString, brandAString, brandCString, brandXString;
         JPanel mainPanel = new JPanel();
         JTextField plainWrapTextField = new JTextField(10);
         JTextField brandATextField = new JTextField(10);
         JTextField brandCTextField = new JTextField(10);
         JTextField brandXTextField = new JTextField(10);
         JButton addPartsButton = new JButton("Add Parts");
         JTextField searchTextField = new JTextField(10);
         JButton searchButton = new JButton("Search");
         JButton listAllButton = new JButton("List All");
         JTextArea outputTextArea = new JTextArea(15,10);
         JScrollPane outputScrollPane = new JScrollPane(outputTextArea);    
         JComboBox plainWrapJComboBox = new JComboBox(partNumberString[0]);
         JComboBox brandAJComboBox = new JComboBox(partNumberString[1]);
         JComboBox brandCJComboBox = new JComboBox(partNumberString[2]);
         JComboBox brandXJComboBox = new JComboBox(partNumberString[3]);
     
         String nameString;
     
        public static void main(String[] args) {
     
            Ch10Assignment partNumber = new Ch10Assignment();
            partNumber.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
     
        public Ch10Assignment()
        {
            mainPanel.add(new JLabel("Plain Wrap "));
            mainPanel.add(plainWrapTextField);
            mainPanel.add(new JLabel("Brand A "));
            mainPanel.add(brandATextField);
            mainPanel.add(new JLabel("Brand C "));
            mainPanel.add(brandCTextField);
            mainPanel.add(new JLabel("Brand X "));
            mainPanel.add(brandXTextField);
            mainPanel.add(addPartsButton);
            mainPanel.add(searchTextField);
            mainPanel.add(searchButton);
            mainPanel.add(listAllButton);
            mainPanel.add(outputScrollPane);
            mainPanel.add(new JLabel("Plain Wrap "));
            mainPanel.add(plainWrapJComboBox);
            mainPanel.add(new JLabel("Brand A "));
            mainPanel.add(brandAJComboBox);
            mainPanel.add(new JLabel("Brand C "));
            mainPanel.add(brandCJComboBox);
            mainPanel.add(new JLabel("Brand X "));
            mainPanel.add(brandXJComboBox);
     
            add(mainPanel);
            setSize(173,758);
            setVisible(true);
            addPartsButton.addActionListener(this);
            searchButton.addActionListener(this);
            listAllButton.addActionListener(this);
     
        }
        public void actionPerformed(ActionEvent evt)
        {
            Object sourceObject = evt.getSource();
            if (sourceObject == addPartsButton)
            {
                getInput();
                addParts();
             }
            else if(sourceObject == searchButton)
            {
                search();
            }
            else if(sourceObject == listAllButton)
            {
                listAll();
            }
     
        }
     
        public void getInput()
        {
             //validate entry
     
            try
            {
                if((!(plainWrapTextField.getText()).equals("")))
                {
                     nameString = plainWrapTextField.getText();
     
                    if((!(brandATextField.getText()).equals("")))
                    {
                             nameString = brandCTextField.getText();
                            if((!(brandCTextField.getText()).equals("")))
                            {
                                    nameString = brandCTextField.getText(); 
                                    if((!(brandXTextField.getText()).equals("")))
                                    {
                                            nameString = brandXTextField.getText(); 
     
                                    try 
                                    {
     
                                        addParts();
                                    }
     
                                    catch (NullPointerException e)
                                    {
     
                                    }
                                    }
                                    else
                                    {    JOptionPane.showMessageDialog(null, "Enter Brand X");
                                    brandXTextField.selectAll();
                                    brandXTextField.requestFocus();
                            }}
                            else
                            {    JOptionPane.showMessageDialog(null, "Enter Brand C");
                            brandCTextField.selectAll();
                            brandCTextField.requestFocus();
                    }}
                    else
                    {
                            JOptionPane.showMessageDialog(null, "Enter Brand A");
                            brandATextField.selectAll();
                            brandATextField.requestFocus();
                    }
                }
     
                else
                {
                    JOptionPane.showMessageDialog(null, "Enter Plain Wrap");
                    plainWrapTextField.selectAll();
                    plainWrapTextField.requestFocus();
                }
            }
            catch(NullPointerException e)
            {
                JOptionPane.showMessageDialog(null, "Quantity is invalid");
                brandATextField.selectAll();
                brandATextField.requestFocus();
     
            }
        }
     
     
        public void addParts()
        {
            boolean done = false;
            int arrayCount = plainWrapJComboBox.getItemCount() - 1;
            for (int i = 0; i <= arrayCount; i++)
            if(plainWrapString.equals(partNumberString[0][i]))
            {
            done = true;            
            break;
            }
            if(done)
            {
                JOptionPane.showMessageDialog(null, "Duplicate part number!");
            }
            else if(plainWrapString.length() != 0 && brandAString.length() != 0 
            && brandCString.length() != 0 && brandXString.length() != 0)
            {
            plainWrapJComboBox.addItem(plainWrapString);
            brandAJComboBox.addItem(brandAString);
            brandCJComboBox.addItem(brandCString);
            brandXJComboBox.addItem(brandXString);
            }
        }
        public void search()
        {
            plainWrapString = plainWrapTextField.getText();
            boolean done = false;
            int arrayCount = plainWrapJComboBox.getItemCount() - 1;
            for (int i = 0; i <= arrayCount; i++)
            {
            if(!plainWrapString.equals(partNumberString[0][i]))
            {
            done = true;    
            break;
            }
            if(done)
            {
                JOptionPane.showMessageDialog(null, "Part does not exist.");
            }
            else
            {
                 outputTextArea.setText('\n' + "Brand A:" + partNumberString[1][i] + 
                 '\n' + "Brand C:" + partNumberString[2][i] +
                 '\n' + "Brand X:" + partNumberString[3][i]);
            }
            }
        }
     
        public void listAll()
        {
            plainWrapString = plainWrapTextField.getText();
            boolean done = false;
            int arrayCount = plainWrapJComboBox.getItemCount() - 1;
            for (int i = 0; i <= arrayCount; i++)
            {
                outputTextArea.append('\n' + "List of All Parts" + '\n');
     
                for (int countInteger = 0; countInteger < plainWrapString.length() ; countInteger++)
                {
                    outputTextArea.append("Plain Wrap Part # " + '\t'+ "Brand A" + 
                            "Brand C" + '\t'+ "Brand X" + '\n' + partNumberString[0] + '\t'+
                            partNumberString[1] + '\t'+ partNumberString[2] + '\t'+ partNumberString[3] + '\t'
                            + '\n');
                }
            }
        }
    }


  2. #2
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Project help needed

    I suppose I can do it but whats this "Plain Wrap" ? If you have the assignment document somewhere so i can take a look that would be great

  3. #3
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Project help needed

    I wrote this a couple of hours ago:



    let me know waht you think. The seach and listall doesn't do anything because you can see all the items on the left.

  4. #4
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Project help needed

    Code:

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.BorderFactory;
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
     
    public class Ch10Assignment extends JFrame implements ActionListener, ListSelectionListener {
    	JList pWrapList;
    	DefaultListModel listElements = new DefaultListModel();
    	JLabel pWrapLbl,pInfo, pWrapInputLbl, brandAInputLbl, brandBInputLbl, brandCInputLbl;
    	JTextArea brands;
    	JTextField pWrapInputTxt, brandAInputTxt, brandBInputTxt, brandCInputTxt;
    	JScrollPane scrollPane;
    	String[][] partNumberString = { 
    			{"PR214",	"MR43T", 	"RBL8",	"14K22"},			
    			{"PR223",	"R43",		"RJ6 ",	"14K24"},
    			{"PR224",	"R43N ",	"RN4 ",	"14K30"},
    			{"PR246",	"R46N",		"RN8 ",	"14K32"}};	//etc..
    	String[][] tmp;
    	JButton addBtn, searchBtn, confirmBtn, cancelBtn, listAllBtn;
    	boolean show = false;
     
    	int counterInteger = 0;
    	int plainWrapInteger [] = new int [3];
    	String plainWrapString, brandAString, brandCString, brandXString;
     
    	public void showGui(){
    		getContentPane().setLayout(null);
    		pWrapLbl = new JLabel("Choose plain wrap: ");
    		pWrapLbl.setBounds(10,0,150,20);
     
    		pInfo  = new JLabel("Parts:");
    		pInfo.setBounds(170,0,100,20);
     
    		pWrapList = new JList(listElements);
    		pWrapList.addListSelectionListener(this);
    		scrollPane = new JScrollPane(pWrapList);
    		scrollPane.setBounds(10,20,150,150);
     
    		brands = new JTextArea("Brand A	Brand B	Brand C\n" +
    		"none	none	none");
    		brands.setEditable(false);
    		brands.setBorder(BorderFactory.createLineBorder(Color.black, 1));
    		brands.setBounds(170,20,280,40);
     
    		addBtn = new JButton("Add new");
    		addBtn.addActionListener(this);
    		addBtn.setBounds(460,20,100,20);
     
    		searchBtn = new JButton("Search");
    		searchBtn.setEnabled(false);
    		searchBtn.addActionListener(this);
    		searchBtn.setBounds(460,40,100,20);
     
    		listAllBtn= new JButton("List all");
    		listAllBtn.setEnabled(false);
    		listAllBtn.addActionListener(this);
    		listAllBtn.setBounds(460,60,100,20);
     
    		pWrapInputLbl = new JLabel("Wrap:");		
    		pWrapInputLbl.setBounds(170,70,100,20);
     
    		brandAInputLbl = new JLabel("Brand A:");	
    		brandAInputLbl.setBounds(170,90,100,20);	
     
    		brandBInputLbl = new JLabel("Brand B:");	
    		brandBInputLbl.setBounds(170,110,100,20);
     
    		brandCInputLbl = new JLabel("Brand C:");
    		brandCInputLbl.setBounds(170,130,100,20);
     
    		pWrapInputTxt = new JTextField();
    		pWrapInputTxt.setBounds(250,70,200,20);
     
    		brandAInputTxt = new JTextField();
    		brandAInputTxt.setBounds(250,90,200,20);
     
    		brandBInputTxt = new JTextField();
    		brandBInputTxt.setBounds(250,110,200,20);
     
    		brandCInputTxt = new JTextField();
    		brandCInputTxt.setBounds(250,130,200,20);
     
    		confirmBtn = new JButton("Ok");
    		confirmBtn.addActionListener(this);
    		confirmBtn.setBounds(250,150,100,20);
     
    		cancelBtn = new JButton("Cancel");
    		cancelBtn.addActionListener(this);
    		cancelBtn.setBounds(350,150,100,20);
     
    		add(pWrapLbl);	add(scrollPane);
    		add(brands);	add(addBtn);
    		add(cancelBtn);	add(pInfo);
    		// not needed, dropped.
    		add(searchBtn);		
    		add(listAllBtn);
     
    		add(pWrapInputLbl);		add(brandAInputLbl);
    		add(brandBInputLbl);	add(brandCInputLbl);
    		add(pWrapInputTxt);		add(brandAInputTxt);
    		add(brandBInputTxt);	add(brandCInputTxt);
    		add(confirmBtn);	
     
    		showForm(show);
     
    		setSize(600,200);
    		setTitle("By: HappyFace - Engineeringserver.com");
    		setResizable(false);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    	}
     
    	public static void main(String[] args) {
    		Ch10Assignment partNumber = new Ch10Assignment();
    		partNumber.setInitialState();
    		partNumber.showGui();
    	}
     
    	public void addNew(){
    		boolean duplicate = false;
    		tmp = new String[partNumberString.length+1][4];
     
    		for( int i = 0; i < partNumberString.length; i++ ){
    			tmp[i][0] = partNumberString[i][0];
    			for(int j = 1; j <partNumberString[i].length; j++){
    				tmp[i][j] = partNumberString[i][j];
    			}	
    		}
     
    		//add the new part(s)	
    		for(int i = 0; i<listElements.getSize(); i++ ){
    			if(listElements.get(i).equals(pWrapInputTxt.getText().toUpperCase())){
    				duplicate= true;
    				JOptionPane.showMessageDialog(null,"Duplicate found, try again!","Warning",0);			
    			}		
    		}		
     
     
    		if(	!pWrapInputTxt.getText().equals("")			&& 
    				!brandAInputTxt.getText().equals("")	&& 
    				!brandBInputTxt.getText().equals("")	&&
    				!brandCInputTxt.getText().equals("") 	&& 
    				!duplicate && pWrapInputTxt.getText().substring(0, 2).equals("PR") )
    		{		
    			tmp[partNumberString.length][0] = pWrapInputTxt.getText();
    			tmp[partNumberString.length][1] = brandAInputTxt.getText();	
    			tmp[partNumberString.length][2] = brandBInputTxt.getText();	
    			tmp[partNumberString.length][3] = brandCInputTxt.getText();
     
    			cleanForm();
    			listElements.addElement(tmp[partNumberString.length][0]);
    			showForm(false);
     
    			//copy the tmp array back to the original array
    			partNumberString = new String[tmp.length][4];
    			for( int i = 0; i < tmp.length; i++ ){
    				partNumberString[i][0] = tmp[i][0];
    				for(int j = 1; j <tmp[i].length; j++){
    					partNumberString[i][j] = tmp[i][j];
    				}	
    			}			
    		}
    		else{
    			JOptionPane.showMessageDialog(null,"Item not added, try again!","Warning",0);
    		}	
    	}
     
    	public void setInitialState(){
    		tmp = new String[partNumberString.length][4];
    		for( int i = 0; i < partNumberString.length; i++ ){
    			tmp[i][0] = partNumberString[i][0];	
    			listElements.addElement("" + tmp[i][0]);	
    		}			
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == addBtn){
    			showForm(true);
    		}
     
    		if(e.getSource() == confirmBtn){
    			addNew();
    		}
     
    		if(e.getSource() == cancelBtn){
    			showForm(false);
    			cleanForm();
    		}
    	}
     
    	public void showForm(boolean show){
    		pWrapInputLbl.setVisible(show);			brandAInputLbl.setVisible(show);
    		brandBInputLbl.setVisible(show);		brandCInputLbl.setVisible(show);
    		pWrapInputTxt.setVisible(show);			brandAInputTxt.setVisible(show);
    		brandBInputTxt.setVisible(show);		brandCInputTxt.setVisible(show);
    		confirmBtn.setVisible(show);			cancelBtn.setVisible(show);
    	}
     
    	public void cleanForm(){
    		pWrapInputTxt.setText("");	brandAInputTxt.setText("");		
    		brandBInputTxt.setText("");	
    		brandCInputTxt.setText("");
    	}
     
    	public void valueChanged(ListSelectionEvent f){
    		int selection = pWrapList.getSelectedIndex();
    		brands.setText("Brand A	Brand B	Brand C\n" +
    				partNumberString[selection][1] + "	" + 
    				partNumberString[selection][2] + "	" + 
    				partNumberString[selection][3]);
    	}
    }

  5. #5
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Project help needed

    Thank you Engineeringserver!!!!

Similar Threads

  1. Program to take name as input and write it backwards
    By snake101 in forum Java Theory & Questions
    Replies: 7
    Last Post: June 10th, 2009, 04:35 AM
  2. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM
  3. Adventure ride project
    By Ciwan in forum Java Theory & Questions
    Replies: 12
    Last Post: February 15th, 2009, 12:08 PM
  4. Database connection without using netbease
    By haygaurav in forum Java IDEs
    Replies: 9
    Last Post: December 9th, 2008, 07:14 AM
  5. Java/J2EE with Linus/Unix
    By markthomas20 in forum Paid Java Projects
    Replies: 2
    Last Post: November 5th, 2008, 09:19 AM