i made this program but yet it doesnt work can some one give me some suggestions.
first class file:
import java.io.*;
import javax.swing.*;
public class Animal
{
private String CommonName, LatinName, GivenName, ID, Origin, Diet;
private int Age;
public void setAll(String ID, String Common, String Latin, String Given)
{
PrintWriter output = null;
try
{
FileWriter outputFile = new FileWriter("Animals.dat",true);
output = new PrintWriter(outputFile);
}
catch(IOException ex)
{
System.out.println("Cannot open the Animals file");
}
output.println(ID);
output.println(Common);
output.println(Latin);
output.println(Given);
output.close();
JOptionPane.showMessageDialog(null,"Record written for " + Common);
}
public String search(String Common, String Latin)
{
BufferedReader input = null;
String field = "zzz", result = "";
boolean found = false;
try
{
input = new BufferedReader(new FileReader("Animals.dat"));
while(field != null)
{
field = input.readLine();
if(field != null)
{
ID = field;
CommonName = input.readLine();
LatinName = input.readLine();
GivenName = input.readLine();
if((CommonName.equalsIgnoreCase(Common)) || (LatinName.equalsIgnoreCase(Latin)))
result = result + "\n" + ID + "\n" + CommonName + "\n" + LatinName + "\n" + GivenName + "\n";
}
}
}
catch(IOException ex)
{
System.out.println("File error!");
return "";
}
try
{
input.close();
}
catch(IOException ex)
{
return "";
}
return result;
}
}
second class file:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ZooInterface extends JFrame implements ActionListener
{
private JLabel lblTitle, lblPrompt, lblID, lblCommon, lblLatin, lblGiven, lblDiet;
private JTextField txtID, txtCommon, txtLatin, txtGiven, txtDiet;
private JTextArea data;
private JButton btnSearch, btnAdd, btnUpdate, btnVetenary, btnExit;
private JScrollPane scrollPane;
private JPanel North, East, South, West, Center, AddSearch, Buttons;
Animal myAnimal = new Animal();
public ZooInterface()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("WSC ZOO");
Container c = getContentPane();
c.setLayout(new BorderLayout());
setSize(600,600);
North = new JPanel();
North.setLayout(new FlowLayout());
lblTitle = new JLabel("WSC ZOO");
North.add(lblTitle);
lblTitle.setFont(new Font("Arial",Font.PLAIN,36));
c.add(North,BorderLayout.NORTH);
East = new JPanel();
c.add(East,BorderLayout.EAST);
West = new JPanel();
West.setLayout(new GridLayout(0,1,20,20));
lblID = new JLabel(" ID");
West.add(lblID);
lblID = new JLabel(" Common Name ");
West.add(lblCommon);
lblID = new JLabel(" Latin Name ");
West.add(lblLatin);
lblID = new JLabel(" Known as ");
West.add(lblGiven);
lblID = new JLabel(" Diet ");
West.add(lblDiet);
c.add(West,BorderLayout.WEST);
Center = new JPanel();
Center.setLayout(new GridLayout(0,1,20,20));
txtID = new JTextField(20);
Center.add(txtID);
txtCommon = new JTextField(20);
Center.add(txtCommon);
txtLatin = new JTextField(20);
Center.add(txtLatin);
txtGiven = new JTextField(20);
Center.add(txtGiven);
txtDiet = new JTextField(20);
Center.add(txtDiet);
c.add(Center,BorderLayout.CENTER);
South = new JPanel();
South.setLayout(new BorderLayout());
AddSearch = new JPanel();
AddSearch.setLayout(new FlowLayout());
btnSearch = new JButton("Search");
AddSearch.add(btnSearch,BorderLayout.NORTH);
btnSearch.addActionListener(this);
btnAdd = new JButton("Add");
AddSearch.add(btnAdd,BorderLayout.NORTH);
btnAdd.addActionListener(this);
South.add(AddSearch,BorderLayout.NORTH);
data = new JTextArea(10,45);
scrollPane = new JScrollPane(data);
South.add(scrollPane,BorderLayout.CENTER);
Buttons = new JPanel();
btnUpdate = new JButton("Update");
Buttons.add(btnUpdate);
btnUpdate.addActionListener(this);
btnVetenary = new JButton("Vetenary");
Buttons.add(btnVetenary);
btnVetenary.addActionListener(this);
btnExit = new JButton("Exit");
Buttons.add(btnExit);
btnExit.addActionListener(this);
South.add(Buttons,BorderLayout.SOUTH);
c.add(South,BorderLayout.SOUTH);
show();
}
public static void main(String[] args)
{
ZooInterface myInterface = new ZooInterface();
}
public void actionPerformed(ActionEvent arg0)
{
if(arg0.getSource() == btnExit)
System.exit(0);
else if(arg0.getSource() == btnAdd)
myAnimal.setAll(txtID.getText(),txtCommon.getText(), txtLatin.getText(), txtGiven.getText(),txtDiet.getText());
else if(arg0.getSource() == btnSearch)
{
data.setText(myAnimal.search(txtCommon.getText(),txtLatin.getText()));
clear();
}
else if(arg0.getSource() == btnVetenary)
{
if(txtID.getText().equals(""))
JOptionPane.showMessageDialog(null,"Please enter animals ID number");
else
{
hide();
//Vet myVet = new Vet(txtID.getText());
}
}
}
private void clear()
{
txtID.setText("");
txtCommon.setText("");
txtLatin.setText("");
txtGiven.setText("");
txtDiet.setText("");
}
}
:::
if anyone can help will be good
first class file:
import java.io.*;
import javax.swing.*;
public class Animal
{
private String CommonName, LatinName, GivenName, ID, Origin, Diet;
private int Age;
public void setAll(String ID, String Common, String Latin, String Given)
{
PrintWriter output = null;
try
{
FileWriter outputFile = new FileWriter("Animals.dat",true);
output = new PrintWriter(outputFile);
}
catch(IOException ex)
{
System.out.println("Cannot open the Animals file");
}
output.println(ID);
output.println(Common);
output.println(Latin);
output.println(Given);
output.close();
JOptionPane.showMessageDialog(null,"Record written for " + Common);
}
public String search(String Common, String Latin)
{
BufferedReader input = null;
String field = "zzz", result = "";
boolean found = false;
try
{
input = new BufferedReader(new FileReader("Animals.dat"));
while(field != null)
{
field = input.readLine();
if(field != null)
{
ID = field;
CommonName = input.readLine();
LatinName = input.readLine();
GivenName = input.readLine();
if((CommonName.equalsIgnoreCase(Common)) || (LatinName.equalsIgnoreCase(Latin)))
result = result + "\n" + ID + "\n" + CommonName + "\n" + LatinName + "\n" + GivenName + "\n";
}
}
}
catch(IOException ex)
{
System.out.println("File error!");
return "";
}
try
{
input.close();
}
catch(IOException ex)
{
return "";
}
return result;
}
}
second class file:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ZooInterface extends JFrame implements ActionListener
{
private JLabel lblTitle, lblPrompt, lblID, lblCommon, lblLatin, lblGiven, lblDiet;
private JTextField txtID, txtCommon, txtLatin, txtGiven, txtDiet;
private JTextArea data;
private JButton btnSearch, btnAdd, btnUpdate, btnVetenary, btnExit;
private JScrollPane scrollPane;
private JPanel North, East, South, West, Center, AddSearch, Buttons;
Animal myAnimal = new Animal();
public ZooInterface()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("WSC ZOO");
Container c = getContentPane();
c.setLayout(new BorderLayout());
setSize(600,600);
North = new JPanel();
North.setLayout(new FlowLayout());
lblTitle = new JLabel("WSC ZOO");
North.add(lblTitle);
lblTitle.setFont(new Font("Arial",Font.PLAIN,36));
c.add(North,BorderLayout.NORTH);
East = new JPanel();
c.add(East,BorderLayout.EAST);
West = new JPanel();
West.setLayout(new GridLayout(0,1,20,20));
lblID = new JLabel(" ID");
West.add(lblID);
lblID = new JLabel(" Common Name ");
West.add(lblCommon);
lblID = new JLabel(" Latin Name ");
West.add(lblLatin);
lblID = new JLabel(" Known as ");
West.add(lblGiven);
lblID = new JLabel(" Diet ");
West.add(lblDiet);
c.add(West,BorderLayout.WEST);
Center = new JPanel();
Center.setLayout(new GridLayout(0,1,20,20));
txtID = new JTextField(20);
Center.add(txtID);
txtCommon = new JTextField(20);
Center.add(txtCommon);
txtLatin = new JTextField(20);
Center.add(txtLatin);
txtGiven = new JTextField(20);
Center.add(txtGiven);
txtDiet = new JTextField(20);
Center.add(txtDiet);
c.add(Center,BorderLayout.CENTER);
South = new JPanel();
South.setLayout(new BorderLayout());
AddSearch = new JPanel();
AddSearch.setLayout(new FlowLayout());
btnSearch = new JButton("Search");
AddSearch.add(btnSearch,BorderLayout.NORTH);
btnSearch.addActionListener(this);
btnAdd = new JButton("Add");
AddSearch.add(btnAdd,BorderLayout.NORTH);
btnAdd.addActionListener(this);
South.add(AddSearch,BorderLayout.NORTH);
data = new JTextArea(10,45);
scrollPane = new JScrollPane(data);
South.add(scrollPane,BorderLayout.CENTER);
Buttons = new JPanel();
btnUpdate = new JButton("Update");
Buttons.add(btnUpdate);
btnUpdate.addActionListener(this);
btnVetenary = new JButton("Vetenary");
Buttons.add(btnVetenary);
btnVetenary.addActionListener(this);
btnExit = new JButton("Exit");
Buttons.add(btnExit);
btnExit.addActionListener(this);
South.add(Buttons,BorderLayout.SOUTH);
c.add(South,BorderLayout.SOUTH);
show();
}
public static void main(String[] args)
{
ZooInterface myInterface = new ZooInterface();
}
public void actionPerformed(ActionEvent arg0)
{
if(arg0.getSource() == btnExit)
System.exit(0);
else if(arg0.getSource() == btnAdd)
myAnimal.setAll(txtID.getText(),txtCommon.getText(), txtLatin.getText(), txtGiven.getText(),txtDiet.getText());
else if(arg0.getSource() == btnSearch)
{
data.setText(myAnimal.search(txtCommon.getText(),txtLatin.getText()));
clear();
}
else if(arg0.getSource() == btnVetenary)
{
if(txtID.getText().equals(""))
JOptionPane.showMessageDialog(null,"Please enter animals ID number");
else
{
hide();
//Vet myVet = new Vet(txtID.getText());
}
}
}
private void clear()
{
txtID.setText("");
txtCommon.setText("");
txtLatin.setText("");
txtGiven.setText("");
txtDiet.setText("");
}
}
:::
if anyone can help will be good