Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving and appending a file using a GUI JAVA
    primarykey
    data
    text
    <p>Ok I have been working on this program (see code below) and I can save the data typed into the GUI to a text file. When I click the next button to clear the GUI text fields to input another employee, the file is not saving the data from the previous and current employee. My goal is to input an employee, then another employee, etc., then print out the a list of all the employees saved with their inputs. What is wrong with the program??? Why it is not appending? </p> <p>Ok I have classes for person, employee, gui and primary will paste the code in that order of classes. </p> <pre><code>import java.io.Serializable; public class person implements Serializable{ /** * */ private static final long serialVersionUID = 1L; public String Social, FirstName, LastName, MiddleName, Address; public person(){ } public person(String soc){ this(soc,null); } public person(String soc, String fn){ this(soc,fn,null); } public person(String soc,String fn, String ln){ this(soc,fn,ln,null); } public person(String soc,String fn,String ln,String mn){ this(soc,fn,ln,mn,null); } public person(String soc,String fn,String ln,String mn,String add){ setData(soc,fn, ln, mn, add); } public void setData(String soc,String fn,String ln,String mn,String add){ setSocial (soc); setFirstName (fn); setLastName (ln); setMiddleName (mn); setAddress (add); } public person(person obj) { Social = obj.Social; FirstName = obj.FirstName; MiddleName = obj.MiddleName; LastName = obj.LastName; Address = obj.Address; } public void makeCopy(person obj) { Social = obj.Social; FirstName = obj.FirstName; MiddleName = obj.MiddleName; LastName = obj.LastName; Address = obj.Address; } public person getCopy() { person obj = new person(this.Social); obj.FirstName = FirstName; obj.MiddleName = MiddleName; obj.LastName = LastName; obj.Address = Address; return obj; } public void input(){ System.out.println("Please enter first name"); // FirstName = entry.nextLine(); } public void input2(){ System.out.println("Please enter middle name"); // MiddleName = entry.nextLine(); } public void input3(){ System.out.println("Please enter last name"); //LastName = entry.nextLine(); } public void input4(){ System.out.println("Please enter social secuirty number"); // Social = entry.nextLine(); } public void input5(){ System.out.println("Please enter your address"); // Address = entry.nextLine(); } public void setSocial(String soc){ Social = soc; } public void setFirstName(String fn){ FirstName = fn; } public void setLastName(String ln){ LastName = ln; } public void setMiddleName(String mn){ MiddleName = mn; } public void setAddress(String add){ this.Address = add; } public String getSocial(){ return Social; } public String getFirstName(){ return FirstName; } public String getLastName(){ return LastName; } public String getMiddleName(){ return MiddleName; } public String getAddress(){ return Address; } public void personData(){ String data = getFirstName()+" "+ getMiddleName()+" "+ getLastName()+ " " +getSocial()+ " " +getAddress(); System.out.println(data); } } import java.io.Serializable; import java.util.Formatter; public class employee extends person implements Serializable { private static final long serialVersionUID = 1L; private String EmployeeId; double Rate; double Hours; double Gross; private String Department; public employee() { super(); EmployeeId = "0"; Rate = 0.0; Hours = 0.0; Gross = 0.0; Department = ""; setSocial(""); setLastName(""); setMiddleName(""); setFirstName(""); setAddress(""); } public employee(String id) { EmployeeId = id; Rate = 0.0; Hours = 0.0; Gross = 0.0; Department = ""; setSocial(""); setLastName(""); setMiddleName(""); setFirstName(""); setAddress(""); } public employee(String id, String last) { EmployeeId = id; Rate = 0.0; Hours = 0.0; Gross = 0.0; setSocial(""); setLastName(last); setMiddleName(""); setFirstName(""); setAddress(""); } public employee(String id, String last, String Department) { EmployeeId = id; Rate = 0.0; Hours = 0.0; Gross = 0.0; this.Department = Department; setSocial(""); setLastName(last); setMiddleName(""); setFirstName(""); setAddress(""); } public employee(String id, String last, double rate, double hours) { EmployeeId = id; this.Rate = rate; this.Hours = hours; Gross = rate * hours; Department = ""; setSocial(""); setLastName(last); setMiddleName(""); setFirstName(""); setAddress(""); } public employee(String id, String last, String Department, double rate, double hours) { EmployeeId = id; this.Rate = rate; this.Hours = hours; Gross = rate * hours; this.Department = Department; setSocial(""); setLastName(last); setMiddleName(""); setFirstName(""); setAddress(""); } public employee(employee obj) { EmployeeId = obj.EmployeeId; Rate = obj.Rate; Hours = obj.Hours; Gross = obj.Gross; Department = obj.Department; setSocial(obj.getSocial()); setLastName(obj.getLastName()); setMiddleName(obj.getMiddleName()); setFirstName(obj.getFirstName()); setAddress(obj.getAddress()); } public void makeCopy(employee obj) { EmployeeId = obj.EmployeeId; Rate = obj.Rate; Hours = obj.Hours; Gross = obj.Gross; Department = obj.Department; setSocial(obj.getSocial()); setLastName(obj.getLastName()); setMiddleName(obj.getMiddleName()); setFirstName(obj.getFirstName()); setAddress(obj.getAddress()); } @Override public employee getCopy() { employee obj = new employee(); obj.EmployeeId = EmployeeId; obj.Rate = Rate; obj.Hours = Hours; obj.Gross = Gross; obj.Department = Department; obj.setSocial(getSocial()); obj.setLastName(getLastName()); obj.setMiddleName(getMiddleName()); obj.setFirstName(getFirstName()); obj.setAddress(getAddress()); return obj; } /** * * @return */ public void setEmployeeID(String id) { EmployeeId = id; } public String getID() { return EmployeeId; } public double getRate() { return Rate; } public double getHours() { return Hours; } public double getGross() { return Gross; } public String getDepartment() { return Department; } public void setRate(double rate) { this.Rate = rate; this.Gross = rate * Hours; } public void setHours(double hours) { this.Hours = hours; this.Gross = Rate * hours; } public void setGross(double gross) { this.Gross = Rate * Hours; } public void setDepartment(String Department) { this.Department = Department; } public double calculateGross() { Gross = Rate * Hours; return Gross; } public boolean equals(employee test) { return EmployeeId.equals(test.EmployeeId); } @Override public String toString() { return ("employee ID: " + getID() + "\nSocial: " + getSocial() + "\nName: " + getLastName() + ", " + getFirstName() + " " + getMiddleName() + "\nHome Address: " + getAddress() + "\nDepartment: " + getDepartment() + "\nRate = " + String.format("%,.2f, Rate = %,.2f, Gross = %,.2f\n", getRate(), getHours(),getGross())); } public int IsBefore(employee obj, String sortByWhat) { if (sortByWhat.equalsIgnoreCase("firstname")) { int t = this.getFirstName().compareTo(obj.getFirstName()); return ((t &gt; 0) ? 1 : ((t&lt;0) ? -1 : 0)); } else if (sortByWhat.equalsIgnoreCase("lastname")) { int t = this.getLastName().compareTo(obj.getLastName()); return ((t &gt; 0) ? 1 : ((t&lt;0) ? -1 : 0)); } else if (sortByWhat.equalsIgnoreCase("lastnamefirstname")) { int t = getLastName().compareTo(obj.getLastName()); if (t == 0) { t = getFirstName().compareTo(obj.getFirstName()); return ((t &gt; 0) ? 1 : ((t&lt;0) ? -1 : 0)); } return ((t &gt; 0) ? 1 : ((t&lt;0) ? -1 : 0)); } else if (sortByWhat.equalsIgnoreCase("rate")) { double t = getRate() - obj.getRate(); return ((t &gt; 0) ? 1 : ((t&lt;0) ? -1 : 0)); } else if (sortByWhat.equalsIgnoreCase("gross")) { double t = getGross() - obj.getGross(); return ((t &gt; 0) ? 1 : ((t&lt;0) ? -1 : 0)); } else return 0; } } import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.GridLayout; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Formatter; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JOptionPane; public class gui extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JLabel idLabel; private JLabel socLabel; private JLabel fNameLabel; private JLabel mNameLabel; private JLabel lNameLabel; private JLabel AddLabel; private JLabel deptLabel; private JLabel rateLabel; private JLabel hoursLabel; private JTextField idField; private JTextField socField; private JTextField fNameField; private JTextField mNameField; private JTextField lNameField; private JTextField AddField; private JTextField deptField; private JTextField rateField; private JTextField hoursField; private JButton btnGross; private JButton btnExit; private JButton btnDelete; private JButton btnDisplay; private JButton btnSave; private JButton btnNext; private double storegross; private thehandler handler; private gui theApp; double Hours, Rate; private Formatter g; public gui(){ super ("this is the title"); setLayout(new GridLayout(0,2)); fNameLabel= new JLabel("Please enter your first name"); add( fNameLabel ); fNameField = new JTextField(10); add (fNameField); mNameLabel = new JLabel("Please enter your middle name"); add( mNameLabel ); mNameField = new JTextField(10); add (mNameField); lNameLabel = new JLabel ("Please enter your last name"); add( lNameLabel ); lNameField = new JTextField (10); add (lNameField); socLabel = new JLabel("Please enter Social security number"); add(socLabel); socField = new JTextField(10); add (socField); AddLabel = new JLabel("Please enter your address") ; add(AddLabel); AddField = new JTextField(10); add (AddField); deptLabel = new JLabel("Please enter your department") ; add( deptLabel ); deptField= new JTextField(10); add (deptField); idLabel = new JLabel("Please enter your ID number"); add (idLabel); idField = new JTextField(10); add (idField); hoursLabel = new JLabel("Please enter your hours") ; add( hoursLabel ); hoursField= new JTextField(10); add (hoursField); rateLabel = new JLabel("Please enter your rate") ; add(rateLabel ); rateField = new JTextField(10); add (rateField); btnGross = new JButton("Gross"); add(btnGross); btnDisplay = new JButton("Display Employee list"); add(btnDisplay); btnSave = new JButton("Save Employee"); add(btnSave); btnNext = new JButton("Next"); add(btnNext); btnExit = new JButton("Exit"); add(btnExit); btnDelete = new JButton("Delete employee"); add(btnDelete); thehandler handler = new thehandler(); btnGross.addActionListener(handler); btnDisplay.addActionListener(handler); btnSave.addActionListener(handler); btnNext.addActionListener(handler); btnDelete.addActionListener(handler); btnExit.addActionListener(handler); theApp = this; } public gui(employee emp){ super ("this is the title"); setLayout(new GridLayout(0,2)); fNameLabel= new JLabel("Please enter your first name"); add( fNameLabel ); fNameField = new JTextField(10); fNameField.setText(emp.getFirstName()); add (fNameField); mNameLabel = new JLabel("Please enter your middle name"); add( mNameLabel ); mNameField = new JTextField(emp.getMiddleName()); add (mNameField); lNameLabel = new JLabel ("Please enter your last name"); add( lNameLabel ); lNameField = new JTextField (emp.getLastName()); add (lNameField); socLabel = new JLabel("Please enter Social security number"); add(socLabel); socField = new JTextField(emp.getSocial()); add (socField); AddLabel = new JLabel("Please enter your address") ; add(AddLabel); AddField = new JTextField(emp.getAddress()); add (AddField); deptLabel = new JLabel("Please enter your department") ; add( deptLabel ); deptField= new JTextField(emp.getAddress()); add (deptField); idLabel = new JLabel("Please enter your ID number"); add (idLabel); idField = new JTextField(10); add (idField); hoursLabel = new JLabel("Please enter your hours") ; add( hoursLabel ); hoursField= new JTextField(String.format("%7.2f", emp.getHours())); add (hoursField); rateLabel = new JLabel("Please enter your rate") ; add(rateLabel ); rateField= new JTextField(String.format("%7.2f", emp.getRate())); add (rateField); btnGross = new JButton("Gross"); add(btnGross); btnDisplay = new JButton("Display Employee list"); add(btnDisplay); btnSave = new JButton("Save Employee"); add(btnSave); btnNext = new JButton("Next"); add(btnNext); btnExit = new JButton("Exit"); add(btnExit); btnDelete = new JButton("Delete employee"); add(btnDelete); thehandler handler = new thehandler(); btnGross.addActionListener(handler); btnDisplay.addActionListener(handler); btnSave.addActionListener(handler); btnNext.addActionListener(handler); btnDelete.addActionListener(handler); btnExit.addActionListener(handler); theApp = this; } private class thehandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if (e.getSource() == btnSave) { employee x = new employee(); x.setSocial(socField.getText()); x.setFirstName(fNameField.getText()); x.setMiddleName(mNameField.getText()); x.setLastName(lNameField.getText()); x.setAddress(AddField.getText()); x.setDepartment(deptField.getText()); x.setEmployeeID(idField.getText()); try { Hours = Double.parseDouble(hoursField.getText()); Rate = Double.parseDouble(rateField.getText()); x.setHours(Hours); x.setRate(Rate); FileOutputStream fos = new FileOutputStream("C:/CSCI/save.txt",true); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(x); oos.close(); } catch(NumberFormatException ex) { JOptionPane.showMessageDialog(null, ex.getMessage().toString()); return; } catch(IOException ex1) { JOptionPane.showMessageDialog(null, ex1.getMessage().toString()); return; } catch(Exception ex2) { JOptionPane.showMessageDialog(null, ex2.getMessage().toString()); return; } JOptionPane.showMessageDialog(null, "Saved"); } else if (e.getSource() == btnGross) { Hours = Double.parseDouble(hoursField.getText()); Rate = Double.parseDouble(rateField.getText()); storegross = (Hours * Rate); JOptionPane.showMessageDialog(null, "The gross for this employee is " + storegross); } else if (e.getSource()==btnNext) { try { idField.setText(""); socField.setText(""); fNameField.setText(""); mNameField.setText(""); lNameField.setText(""); AddField.setText(""); deptField.setText(""); rateField.setText(String.format("%7.2f", 0.0)); hoursField.setText(String.format("%7.2f", 0.0)); theApp.repaint(); } catch (Exception ex3) { JOptionPane.showMessageDialog(null, ex3.getMessage().toString()); return; } } else if (e.getSource()==btnDisplay) { try { FileInputStream fis = new FileInputStream("C:/CSCI/save.txt"); ObjectInputStream ois = new ObjectInputStream(fis); System.out.print(ois.readObject()); ois.close(); } catch(ClassNotFoundException ex) { } catch (IOException ex) { } } else if (e.getSource() ==btnExit) { System.exit(0); } } } } import java.io.*; import javax.swing.JFrame; public class primary { /** * @param args */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub gui go = new gui(); go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); go.setSize(465,390); go.setVisible(true); go.setLocationRelativeTo(null); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload