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 4 of 4

Thread: Could I have made this simpiler?

  1. #1
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    24
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Wink Could I have made this simpiler?

    Hello,
    My previous post told of how I was learning about threads, objects, swings and more. After learning studying I made my own simple program using basic swing and I was wondering if I could have made it simpler?What this program does is allows the user to enter the employee's first and last name along with age. Then allows them to select their gender. If needed the user can print all employees he/she has made and even remove employees from the list. I'm not posting all of it just the "core".
    Thanks to anyone that just looks and BIG thanks to those who reply.
    	public void addEmployee()
    	{
    		getName = Name.getText();
    		getLName = LName.getText();
    		getGender = (String)gender.getSelectedItem();
     
    		area.setText("You added " + getName + " " + getLName + " Age " + getAge + " Gender " + getGender);
     
    		arrfill++;
     
    		employList.add(new Employee(getName,getLName,getAge,getGender));
     
    		list.addItem(getName + " " + getLName);
    	}
     
    	public void removeEmployee()
    	{
    		if (list.getItemCount() == 0)
    		{	
    			area.setText("Nothing to Remove!");
    		}
    		else
    		{
    			int count = 0;
    			String compare1;
    			String FName;
    			String LName;
    			Employee getcompare;
    			String compare2;
    			while(true)
    			{
    			compare1 = (String)list.getSelectedItem();
    			getcompare = employList.get(count);
    			FName = getcompare.getName();
    			LName = getcompare.getLastName();
    			compare2 = FName + " " + LName;
    				if (compare2.equals(compare1))
    				{
    					employList.remove(count);
    					list.removeItemAt(count);
    					arrfill--;
    					break;
    				}
    				else
    				{
    					count++;
    				}
    			}
    		}
    	}
     
    	public void printAllemps()
    	{
    		String print;
    		Employee temp;
    		area.setText("");
    		for (int count = 0; count <= arrfill; count++)
    		{
    			temp = employList.get(count);
    			print = temp.getName()+ " " + temp.getLastName() + " " + temp.getAge() + " " + temp.getGender();
    			area.append(print + "\n"); //creates new line
    		}
    	}
     
    	public boolean isInt(JTextField f)
    	{
    		try
    		{
    			Integer.parseInt(f.getText());
    			return true;
    		}
    		catch(NumberFormatException e)
    		{
    			area.setText("Re enter age");
    			f.requestFocus();
    			return false;
    		}
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource() == add)
    		{
    			if (isInt(Age))
    			{
    				getAge = Integer.parseInt(Age.getText());
    				addEmployee();
    			}
    		}
    		else if (e.getSource() == remove)
    		{
    			removeEmployee();
    		}
    		else if (e.getSource() == all)
    		{
    			printAllemps();
    		}
    	}
    The parts I cut out were the variable initializing, creating the jframe, panels, etc... and the main method. This program uses a custom Object called Employee which holds First/Last name, gender and age.
    Any questions just ask.
    lil_misfit


  2. #2
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: Could I have made this simpiler?

    Quote Originally Posted by lil_misfitss View Post
    Any questions just ask.
    Just one, Why do you need to make it simpler? It is OK. Maybe you can follow google java style to make some changes but the logic remains same. No need to trade this for a simpler logic. It is good.

  3. The Following User Says Thank You to Abhilash For This Useful Post:

    lil_misfitss (June 21st, 2014)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Could I have made this simpiler?

    Simpler is relative. There are always different ways to write a program, and what one programmer thinks as simple another might think complex, and vice-versa. Perhaps a better question is - are there other ways to accomplish the same task (s)? To provide some alternatives:
    1) The isInt method: rather than catching the exception, you can use the String.matches method to match the text against a number pattern
    2) I presume employList is an instance of List - so rather than incrementing/decrementing a variable (arrfill) when you add/remove, just use the built in size method
    3) what is the variable list? The code adds/removes data to/from 'list' and employList - any way to consolidate the two?
    4) For the actionListener, one could use different ActionListener's for each item rather than the code consolidated into one. If you wish to later add other Components (for instance, JMenuItem, JButton, etc...) to react in the same way, having multiple listeners preclude you from doing an instance check, rather one can just add the appropriate listener to the appoproate component
    Again, only different ways to do things (and each point has many more ways). Presuming you did not know about them, you now have more tools to tackle this or a future project.

  5. The Following User Says Thank You to copeg For This Useful Post:

    lil_misfitss (June 21st, 2014)

  6. #4
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    24
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Could I have made this simpiler?

    Wow! Thank you for the great responses and I'll try to use some of the suggestions you gave me (copeg).
    To answer your question the variable list is an JComboBox of Strings representing the first and last names from Employee objects while employList is an ArrayList of Employee objects.
    lil_misfit
    Last edited by lil_misfitss; June 21st, 2014 at 02:14 PM.

Similar Threads

  1. Replies: 4
    Last Post: December 28th, 2013, 12:58 AM
  2. changes made to JSP or servlets
    By the light in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: May 30th, 2011, 08:19 AM
  3. Bigger triangle made up of multiple ones?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 30th, 2011, 11:18 AM