PDA

Click to See Complete Forum and Search --> : need help writing a java program please? (using menus)


dante27
Apr 24th, 2005, 06:18 PM
hello,

I am working on a program for my Advaced java class and was wondering if anyone here could help I have most of it done but I am having problems creating a sort for the three menu items and a serach for the dept and salary fields the sort button works but i would like it to be a menu but not sure how to go about it. I am not sure how to go about doing it.
any help would be apprecitated


this is the program that i need to write:


Create an application that presents the following options to the user:

Add records - adds records to a list of records
Sort records - sorts the records on one of three fields(the program must be able to sort on all three sort keys)
Search records - retrieves a record using any one of the keys. On multiple occurrences display more than one record.
The methods must be in a list class.
Provide handlers that support the add, sort and search features as well as exception handling for bad input.
The sort methods should run in their own thread.
Use GUI components that make the application simple for the user.

Employee Record layout :

name String
dept int
salary double



thank you


********************************************************
here is the code that I have done so far:

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

class Employee
{
String name;
int dept;
double salary;
public Employee(String n,int d,double sal)
{
name=n;
dept=d;
salary=sal;
}
public String toString()
{
return name+" "+dept+" "+salary;
}
}



class EmployeeList
{
int next=0;
Employee list[]=new Employee[3];
public EmployeeList()
{

}

public void sortByName()
{
for(int pass =1;pass<next;pass++)
for(int element=0;element<next-1;element++)
{
try{
Thread.sleep(5000);
}catch(InterruptedException ex){}
if(list[element].name.compareTo(list[element+1].name)>0)
swap(element,element+1);
}

}

public void swap(int first, int second)
{
Employee hold;
hold=list[first];
list[first]=list[second];
list[second]=hold;
}





public String searchOnName(String key)
{
String ret="";
for(int i=0;i<next;i++)
if(list[i].name.equals(key))
return list[i].toString();
return ret;
}
//Overload methods so developers
//can use the methods easier
//the argument type must differ
public String search(double key)
{
String ret="";
for(int i=0;i<next;i++)
if(list[i].salary==key)
return list[i].toString();
return ret;
}
public String search(int key)
{
String ret="";
for(int i=0;i<next;i++)
if(list[i].dept==key)
return list[i].toString();
return ret;
}


}

public class MidtermAppWithSort2 extends JFrame

{
EmployeeList empList=new EmployeeList();
Container container;
JMenuBar bar;
JMenu searchMenu,sortMenu;
JMenuItem nameItem,deptItem,salaryItem;
JMenuItem nameItem2,deptItem2,salaryItem2;
JLabel nameLabel,deptLabel,salaryLabel;
JTextField nameText,deptText,salaryText;
JTextArea area;
JButton submit,display,sort;
JPanel northPanel,southPanel;

public MidtermAppWithSort2()
{
container=getContentPane();
//container.setLayout(new FlowLayout());

sortMenu=new JMenu("Sort");
searchMenu=new JMenu("Search");
nameItem=new JMenuItem("Name");
deptItem=new JMenuItem("Dept");
salaryItem=new JMenuItem("Salary");

nameItem2=new JMenuItem("Name");
deptItem2=new JMenuItem("Dept");
salaryItem2=new JMenuItem("Salary");

//add items to the menu
searchMenu.add(nameItem);
searchMenu.add(deptItem);
searchMenu.add(salaryItem);

sortMenu.add(nameItem2);
sortMenu.add(deptItem2);
sortMenu.add(salaryItem2);
//add the menu to menubar
bar=new JMenuBar();
bar.add(searchMenu);
bar.add(sortMenu);
//set the bar
setJMenuBar(bar);

sort=new JButton("SortOnName");

area=new JTextArea(30,20);
submit=new JButton("Submit");
display=new JButton("Display");
ButtonHandler bh=new ButtonHandler();
MenuHandler mh=new MenuHandler();
nameItem.addActionListener(mh);
submit.addActionListener(bh);
display.addActionListener(bh);

nameLabel=new JLabel("NAME");
nameText=new JTextField(10);
deptLabel=new JLabel("DEPT");
deptText=new JTextField(10);
salaryLabel=new JLabel("SALARY");
salaryText=new JTextField(10);

//Create a panel and place it in the north border
northPanel=new JPanel();
northPanel.add(nameLabel);
northPanel.add(nameText);
northPanel.add(deptLabel);
northPanel.add(deptText);
northPanel.add(salaryLabel);
northPanel.add(salaryText);

northPanel.add(sort);
sort.addActionListener(bh);

container.add(northPanel,BorderLayout.NORTH);
//container.add(label);
//container.add(text);
container.add(area,BorderLayout.CENTER);
//Create a panel and place it in the south border
southPanel=new JPanel();
southPanel.setLayout(new GridLayout(1,2));
southPanel.add(submit);
southPanel.add(display);
container.add(southPanel,BorderLayout.SOUTH);
//container.add(submit);
//container.add(display);
setSize(200,300);
show();

}

public static void main(String s[])
{

MidtermAppWithSort2 app=new MidtermAppWithSort2();

}
//inner class
private class ButtonHandler implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==submit)
{
String name=nameText.getText();
String dept=deptText.getText();
String salary=salaryText.getText();
if(empList.next<3)
{
empList.list[empList.next++]=new Employee(name,
Integer.parseInt(dept),Double.parseDouble(salary));
}else
area.append("No more room\n");
}
if(e.getSource()==display)
//area.append("Display clicked\n");
for(int i=0;i<empList.next;i++)
area.append(empList.list[i].toString()+"\n");


//sort by name
if(e.getSource()==sort){
empList.sortByName();
SortThread thread=new SortThread(empList);
thread.start();
}

}//end of actionPerformed

}//end of ButtonHandler class
//inner class
private class MenuHandler implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
if(ev.getSource()==nameItem)
{
String retValue=empList.searchOnName(
nameText.getText());
if(!retValue.equals(""))
area.append(retValue+"\n");
else
area.append("Not found\n");
}
}




}

}//end of GUIApp class


class SortThread extends Thread
{
EmployeeList list;
public SortThread(EmployeeList l)
{
list=l;
}
public void run()
{
list.sortByName();
}
}//end class



********************************************************

System_Error
Apr 25th, 2005, 05:38 PM
What do you mean a sort for the three menu's?

Just use a simple actionListener for them:



public void actionPerformed(ActionEvent ae)
{
if (ae.getSource().equals("MENU_ITEM_NAME"))
{
/**
code goes here
**/
}
}

dante27
Apr 25th, 2005, 06:01 PM
say i enter : this data for the fields

cindy 3 33
dan 2 22
adam 1 11

I would like it to be have the chioce to sort the data by name, dept or salary
just not sure how to go about writing the code for that sort

thanks for the reply

Andy_Hollywood
May 1st, 2005, 09:11 AM
ok, so your storing each of these employees in a list?

if its a list you can write a comparator, then use Arrays.sort(list, comparator) to have the items in the list resorted...

or, if you have a UI, use Jtable then implement a sortable Jtable which will sort the view rather than the underlying data structure.

System_Error
May 1st, 2005, 10:20 AM
StringTokenizer will help you split that data up.