Ho usato Hashtable per creare una struttura dati che contenga degli oggetti di tipo employee.
Posto il codice che ho usato:
Il programma è composto da tre file:
Il programma principale "PropertiesTest1.java"
codice:
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PropertiesTest1 extends JFrame{
private Container c;
private JLabel status;
private JTextArea display;
private Employee1 emp;
private EmployeeList1 theEmployeeList1 = new EmployeeList1();
public PropertiesTest1() {
super("Hashtable Example");
final JLabel status = new JLabel();
final JTextArea display = new JTextArea(4, 20);
display.setEditable(false);
JPanel northPanel = new JPanel();
northPanel.setLayout(new BorderLayout());
JPanel northSubPanel = new JPanel();
northSubPanel.add(new JLabel("Nome"));
final JTextField fName = new JTextField(8);
northSubPanel.add(fName);
northSubPanel.add(new JLabel("Cognome"));
final JTextField lName = new JTextField(8);
northSubPanel.add(lName);
northSubPanel.add(new JLabel("Identificativo"));
final JTextField cod = new JTextField(8);
northSubPanel.add(cod);
northPanel.add(northSubPanel, BorderLayout.NORTH);
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(2, 5));
//BOTTONI
//PUT
JButton put = new JButton("Inserisci");
put.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (cod.getText().equals(""))
JOptionPane.showInternalMessageDialog(c, "Devi inserire il codice", "Errore", JOptionPane.ERROR_MESSAGE);
else {
if (theEmployeeList1.inserisci(cod.getText(), fName.getText(), lName.getText())==true)
JOptionPane.showInternalMessageDialog(c, "Inserito", "Ok", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showInternalMessageDialog(c, "Già inserito", "Errore", JOptionPane.ERROR_MESSAGE);
}
}
}
);
southPanel.add(put);
//PUT
JButton modify = new JButton("Modifica");
modify.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (cod.getText().equals(""))
JOptionPane.showInternalMessageDialog(c, "Devi inserire il codice", "Errore", JOptionPane.ERROR_MESSAGE);
else {
if (theEmployeeList1.modifica(cod.getText(), fName.getText(), lName.getText())==true)
JOptionPane.showInternalMessageDialog(c, "Modificato", "Ok", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showInternalMessageDialog(c, "Non trovato", "Errore", JOptionPane.ERROR_MESSAGE);
}
}
}
);
southPanel.add(modify);
//GET
JButton get = new JButton("Cerca");
get.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Object val = theEmployeeList1.cerca(cod.getText());
if (val != null)
JOptionPane.showInternalMessageDialog(c, "Trovato: " + val.toString(), "Ok", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showInternalMessageDialog(c, "Non trovato", "Errore", JOptionPane.WARNING_MESSAGE);
}
}
);
southPanel.add(get);
//LIST ELEMS
JButton listElems = new JButton("Stampa Lista");
listElems.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
display.setText(theEmployeeList1.listElems());
}
}
);
southPanel.add(listElems);
//SAVE
JButton save = new JButton("Save");
save.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
theEmployeeList1.salvaLista();
}
}
);
southPanel.add(save);
//LOAD
JButton load = new JButton("Load");
load.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
}
}
);
southPanel.add(load);
c = getContentPane();
c.add(northPanel, BorderLayout.NORTH);
c.add(new JScrollPane(display), BorderLayout.CENTER);
c.add(southPanel, BorderLayout.SOUTH);
setSize(580, 300);
show();
}
public void showStatus(String s)
{
status.setText(s);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
PropertiesTest1 app = new PropertiesTest1();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
La classe Lista "EmployeeList1.java"
codice:
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EmployeeList1 implements Serializable{
static final private Hashtable table = new Hashtable();
private Employee1 emp;
public EmployeeList1() {
}
public boolean inserisci(String cod, String fName, String lName)
{
emp = new Employee1(cod, fName, lName);
Object prova = table.get(cod);
Object val;
if (prova==null){
val = table.put(emp.getCod(), emp);
return true;
}
else
return false;
}
public boolean modifica(String cod, String fName, String lName)
{
emp = new Employee1(cod, fName, lName);
Object prova = table.get(cod);
Object val;
if (prova!=null){
val = table.put(emp.getCod(), emp);
return true;
}
else
return false;
}
public Object cerca(String cod)
{
return table.get(cod);
}
//LIST ELEMS
public String listElems()
{
StringBuffer buf = new StringBuffer();
for(Enumeration enum = table.elements(); enum.hasMoreElements();)
buf.append(enum.nextElement()).append("\n");
return buf.toString();
}
public void salvaLista()
{
}
public void caricaLista()
{
}
}
La classe Impiegato "Employee1.java"
codice:
import java.io.*;
class Employee1 implements Serializable{
private String cod, fName, lName;
/** Creates a new instance of Employee */
public Employee1(String cod, String fName, String lName) {
this.cod = cod;
this.fName = fName;
this.lName = lName;
}
public String getCod(){
this.cod = cod;
return cod;
}
public String toString(){
return cod + "\t" + fName + "\t" + lName;
}
}
due domande:
1: Fa troppo schifo il codice? (è solo una settimana che programma in Java...), se si, cosa mi consigliate per migliorarlo?
2: Vorrei memorizzare il contenuto della lista in un file per poi poterlo recuperare ad una seconda lettura. Come faccio?