Hi i'm a beginner in Java. I have school assigment where they want to save employee array to the file and can load it back with replacing the current array in GUI when it hit button save or load. Also, if i want to save or load the array with the specific file name from JTextField what code should i include.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.JOptionPane;
public class Etracker
{
public static void main(String[] args)
{
Etracker etk = new Etracker();
Etracker file = new Etracker();
etk.getInput();
file.saveBytes();
file.loadBytes();
file.saveData();
file.loadData();
}
void saveBytes(){
File f = new File("Employee.txt");
try {
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
fos.close(); oos.close();
}
catch (FileNotFoundException e)
{
//joption needed
JOptionPane.showMessageDialog(null, "Sorry File not found", "Error",JOptionPane.ERROR_MESSAGE);
}
catch (IOException e)
{
//joption needed
JOptionPane.showMessageDialog(null, "unspecified I/O exception!", "Error",JOptionPane.ERROR_MESSAGE);
}
}
void loadBytes(){
File f = new File("Employee.txt");
try {
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ios = new ObjectInputStream(fis);
fis.close(); ios.close();
}
catch (FileNotFoundException e)
{
//joption needed
JOptionPane.showMessageDialog(null, "Sorry File not found", "Error",JOptionPane.ERROR_MESSAGE);
}
catch (IOException e)
{
//joption needed
JOptionPane.showMessageDialog(null, "unspecified I/O exception!", "Error",JOptionPane.ERROR_MESSAGE);
}
}
void saveData() {
File f = new File("Employee.txt");
try {
FileOutputStream fos = new FileOutputStream(f);
DataOutputStream dos = new DataOutputStream(fos);
System.out.println("data successfully saved");
dos.close(); fos.close();
}
catch (FileNotFoundException e)
{
//joption needed
JOptionPane.showMessageDialog(null, "Sorry File not found", "Error",JOptionPane.ERROR_MESSAGE);
}
catch (IOException e)
{
//joption needed
JOptionPane.showMessageDialog(null, "unspecified I/O exception!", "Error",JOptionPane.ERROR_MESSAGE);
}
}
void loadData() {
File f = new File("Employee.dat");
try {
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
System.out.println("data successfully loaded");
dis.close(); fis.close();
}
catch (FileNotFoundException e)
{
//joption needed
JOptionPane.showMessageDialog(null, "Sorry File not found", "Error",JOptionPane.ERROR_MESSAGE);
}
catch (IOException e)
{
//joption needed
JOptionPane.showMessageDialog(null, "unspecified I/O exception!", "Error",JOptionPane.ERROR_MESSAGE);
}
}
private EFrame gui;
private Employee[] e;
private Integer current;
public Etracker()
{
e = new Employee[0];
gui = new EFrame();
}
public void create(String lastName, String firstName)
{
Employee id = new Employee(e.length + 1, firstName, lastName);
expand(id);
}
public void expand(Employee id)
{
Employee[] temEmplAr = new Employee[e.length + 1];
for(int i = 0; i < e.length; i++)
{
temEmplAr[i] = e[i];
}
temEmplAr[e.length] = id;
e = temEmplAr;
}
public void getInput()
{
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("Enter Command (create, list , get lastn, get firstn, exit)");
String nextLine = sc.nextLine();
if(nextLine.startsWith("create"))
{
System.out.println("please enter lastname and firstname");
String nextLine_c = sc.nextLine();
String[] Ar = nextLine_c.split(" ");
create(Ar[0], Ar[1]);
}
else if(nextLine.startsWith("list"))
{
for(int i = 0; i < e.length; i++)
{
System.out.println("id: " + e[i].getId() + " " + e[i].getlastName() + ", " + e[i].getfirstName());
}
}
else if(nextLine.startsWith("get lastn"))
{
String[] Ar2 = nextLine.split(" ");
for(int i = 0; i < e.length; i++)
{
if(e[i].getlastName().equals(Ar2[2]))
{
System.out.println("id: " + e[i].getId() + " " + e[i].getlastName() + ", " + e[i].getfirstName());
}
}
}
else if(nextLine.startsWith("get firstn"))
{
String[] Ar3 = nextLine.split(" ");
for(int i = 0; i < e.length; i++)
{
if(e[i].getfirstName().equals(Ar3[2]))
{
System.out.println("id: " + e[i].getId() + " " + e[i].getlastName() + ", " + e[i].getfirstName());
}
}
}
else if(nextLine.startsWith("exit"))
{
System.out.println("Thank you for using our Employee Management Application");
System.exit(0);
}
}
}
class EFrame extends JFrame implements ActionListener {
JLabel jlID, jlFirstname, jlLastname, jlFilename;
JTextArea jta;
JPanel jpBase , jpTop, jpm, jpmm,jpmb , jpBottom, jpBBT;
JTextField jtfID, jtf2 , jtf3, jtf4, jtf5;
JButton bcreate, blist, bfirst, blast, bclear, bexit, bback, bforward, bshow, bupdate, bsave, bload;
JOptionPane jo;
JScrollBar jBar;
JScrollPane jsp;
EFrame() {
setTitle("GUI");
setSize(1000, 1000);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setMinimumSize(new Dimension(100, 100));
this.setMaximumSize(new Dimension(1920, 1080));
jsp = new JScrollPane(jta);
jpBase = new JPanel();
jpTop = new JPanel();
jpm = new JPanel();
jpmm = new JPanel();
jpmb = new JPanel();
jpBottom = new JPanel();
jpBBT = new JPanel();
jlID = new JLabel("ID");
jlFirstname = new JLabel("Firstname");
jlLastname = new JLabel("Lastname");
jta = new JTextArea("");
jta.setPreferredSize(new Dimension(700, 360));
jlFilename = new JLabel("filename");
jtfID = new JTextField(10);
jtf2 = new JTextField(10);
jtf3 = new JTextField(10);
jtf5 = new JTextField(10);
add(jpBase);
jpBase.setLayout(new GridLayout(8,1));
jpBase.setSize(1000, 1000);
jpBase.add(jpTop);
jpBase.add(jpm);
jpBase.add(jpmm);
jpBase.add(jpmb);
jpBase.add(jpBottom);
jpBase.add(jpBBT);
jpTop.setLayout(new GridLayout (1, 0));
jpTop.add(jlID); jpTop.add(jtfID);
jpm.setLayout(new GridLayout (1, 0));
jpm.add(jlFirstname); jpm.add(jtf2);
jpmm.setLayout(new GridLayout (1, 0));
jpmm.add(jlLastname); jpmm.add(jtf3);
jpmb.setLayout(new GridLayout (1, 0));
jpmb.add(jlFilename); jpmb.add(jtf5);
//add button
bcreate = new JButton("Create"); bcreate.addActionListener(this);
blist = new JButton("List"); blist.addActionListener(this);
bfirst = new JButton("Get first"); bfirst.addActionListener(this);
blast = new JButton("Get last"); blast.addActionListener(this);
bclear = new JButton("Clear"); bclear.addActionListener(this);
bexit = new JButton("Exit"); bexit.addActionListener(this);
bback = new JButton("<"); bback.addActionListener(this);
bforward = new JButton(">"); bforward.addActionListener(this);
bshow = new JButton("show"); bshow.addActionListener(this);
bupdate = new JButton("update"); bupdate.addActionListener(this);
bsave = new JButton("save"); bsave.addActionListener(this);
bload = new JButton("load"); bload.addActionListener(this);
jpBottom.setLayout(new GridLayout(1, 0));
jpBottom.add(bcreate);
jpBottom.add(blist);
jpBottom.add(bfirst);
jpBottom.add(blast);
jpBottom.add(bclear);
jpBottom.add(bexit);
jpBottom.add(bback);
jpBottom.add(bforward);
jpBottom.add(bshow);
jpBottom.add(bupdate);
jpBottom.add(bsave);
jpBottom.add(bload);
jpBBT.setLayout(new GridLayout (1, 1));
jpBBT.add(jta);
jta.add(jsp);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == bcreate) // create
{
String firstname;
String lastname;
String id;
firstname = jtf2.getText();
lastname = jtf3.getText();
create(firstname, lastname);
id = Integer.toString(e.length);
current = Integer.parseInt(id);
jta.setText("Employee #" + id + " " + firstname + " " + lastname + " was created.");
}
if(ae.getSource() == blist) //list
{
String firstname;
String lastname;
String id;
jta.setText("");
current = e[0].id;
for(int i = 0; i < e.length; i++)
{
jta.append("id: " + e[i].getId() + " " + e[i].getlastName() + ", " + e[i].getfirstName() + "\n");
}
}
if(ae.getSource() == bfirst) //getfir
{
String firstname;
String lastname;
Integer id;
jta.setText("");
current = -1;
for(int i = 0; i < e.length; i++)
{
Employee firstEmployee = e[i];
firstname = firstEmployee.getfirstName();
lastname = firstEmployee.getlastName();
id = firstEmployee.getId();
if(firstname.equals(jtf2.getText()))
{
if(current == -1)
{
current = id;
}
jta.append("id: " + id + " " + lastname + ", " + firstname + "\n");
}
}
}
if(ae.getSource() == blast)// getlast
{
String firstname;
String lastname;
Integer id;
jta.setText("");
for(int i = 0; i < e.length; i++)
{
Employee firstEmployee = e[i];
firstname = firstEmployee.getfirstName();
lastname = firstEmployee.getlastName();
id = firstEmployee.getId();
if(lastname.equals(jtf3.getText()))
{
if(current == -1)
{
current = id;
}
jta.append("id: " + id + " " + lastname + ", " + firstname + "\n");
}
}
}
if(ae.getSource() == bclear) //clear
{
jtfID.setText("");
jtf2.setText("");
jtf3.setText("");
}
if(ae.getSource() == bexit) //exit
{
System.exit(0);
}
if(ae.getSource() == bshow)
{
String firstname;
String lastname;
String id;
jta.setText("");
for(int i = 0; i < e.length; i++)
{
Employee firstEmployee = e[i];
firstname = firstEmployee.getfirstName();
lastname = firstEmployee.getlastName();
id = Integer.toString(firstEmployee.getId());
if(id.equals(jtfID.getText()))
{
if(current == -1)
{
current = Integer.parseInt(id);
}
jta.append("id: " + id + " " + lastname + ", " + firstname + "\n");
}
}
}
if(ae.getSource() == bback) // <
{
String firstname;
String lastname;
String id;
jta.setText("");
Employee firstEmployee = e[current - 1];
firstname = firstEmployee.getfirstName();
lastname = firstEmployee.getlastName();
id = Integer.toString(firstEmployee.getId());
jta.append("id: " + id + " " + lastname + ", " + firstname + current);
current = current - 1;
}
if(ae.getSource() == bforward)
{ // >
String firstname;
String lastname;
String id;
jta.setText("");
Employee firstEmployee = e[current + 1];
firstname = firstEmployee.getfirstName();
lastname = firstEmployee.getlastName();
id = Integer.toString(firstEmployee.getId());
jta.append("id: " + id + " " + lastname + ", " + firstname + current);
current = current + 1;
}
if(ae.getSource() == bupdate) //update
{
String firstname;
String lastname;
Integer id;
jta.setText("");
for(int i = 0; i < e.length; i++)
{
Employee firstEmployee = e[current + 0];
firstEmployee.updatefirstName(jtf2.getText());
firstEmployee.updatelastName(jtf3.getText());
}
}
if(ae.getSource()== bsave) // save
{
}
if(ae.getSource() == bload) // load
{
}
}
}
class Employee
{
private int id;
private String lastName;
private String firstName;
public Employee(int id, String lastName, String firstName) {
this.id = id;
this.lastName = lastName;
this.firstName = firstName;
}
public void updatelastName(String lastName) {
this.lastName = lastName;
}
public void updatefirstName(String firstName) {
this.firstName = firstName;
}
public String getlastName() {
return lastName;
}
public String getfirstName() {
return firstName;
}
public int getId() {
return id;
}
}
}