Search code examples
javafilewriter

Cannot get product of if statement to write to a new .txt file


I'm working on a sales tool for my Java class and I'm able to get everything working except for writing my results to a new .txt file. To give some backstory, I have a GUI that is writing data to a text file named out1.txt. I am reading data back in from this file and displaying anything with computesales >= 8,000 in a JtextArea as shown in the code below.

 private void evaluateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    saleOutput.setText("");        
    SaleReader reader = new SaleReader();

    try
    {
        List<Sales> sale = reader.readSale("out1.txt");   
        for (Sales s : sale)
        {
            if(s.computeSales()>= 8000)
            {
            saleOutput.append(s.toString());
            saleOutput.append("\n");

            }
        }
    }catch (IOException ex) {
        JOptionPane.showMessageDialog(this, "Unable to open file");
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }


}                                              

I have tried a variation of what I have below that writes to my out1.txt file, but it doesn't seem to work at all. It won't even create the file. My coworker seemed to think that it may be an issues with the filewriter not being flushed?

 public void writeSales(Sales s) throws IOException
{
    File testWriter = new File ("out1.txt");
    BufferedWriter out = new BufferedWriter(new FileWriter(testWriter, true));

    out.write("" + s.getSalesID());
    out.write(", ");
    out.write(s.getFirstName());
    out.write(", ");
    out.write(s.getLastName());
    out.write(", ");
    out.write("" + s.getOfficeSupplies());
    out.write(", ");
    out.write("" + s.getBooks());
    out.write(", ");
    out.write("" + s.getPaper());
    out.write(", ");
    out.write("" + s.getDistrictValue());        
    out.write(", ");
    out.write("" + s.getPreferredContact());   
    out.write(", ");
    out.write("" + s.computeSales());
    out.newLine();       
    out.close();
}

Here is what I'm writing in a separate class to write to the new stars.txt file, but is not working. Any suggestions?

public void writeStars(Sales f) throws IOException
{
    File testStars = new File ("stars.txt");
    BufferedWriter outStars = new BufferedWriter(new FileWriter(testStars, true));
    SaleReader reader = new SaleReader();

     try
    {
        List<Sales> sale = reader.readSale("out1.txt");   
        for (Sales s : sale)
        {
            if(s.computeSales()>= 8000)
            {
            outStars.write("" + s.getDistrictValue());
            outStars.newLine();
            outStars.close();

            }
        }
    }catch (IOException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I'm calling it from here.

public class Sales 
{
 private int salesID;
 private String firstName;
 private String lastName;
 private double officeSupplies;
 private double books;
 private double paper;
 private String districtValue;
 private String preferredContact;

public int getSalesID() {
    return salesID;
}

public void setSalesID(int salesID) {
    this.salesID = salesID;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public double getOfficeSupplies() {
    return officeSupplies;
}

public void setOfficeSupplies(double officeSupplies) {
    this.officeSupplies = officeSupplies;
}

public double getBooks() {
    return books;
}

public void setBooks(double books) {
    this.books = books;
}

public double getPaper() {
    return paper;
}

public void setPaper(double paper) {
    this.paper = paper;
}

public String getPreferredContact() {
    return preferredContact;
}

public void setPreferredContact(String preferredContact) {
    this.preferredContact = preferredContact;
}    

public String getDistrictValue() {
    return districtValue;
}

public void setDistrictValue(String districtValue) {
    this.districtValue = districtValue;
}

@Override
public String toString() {
    return "Sales{" + "salesID=" + salesID + ", firstName=" + firstName + ", lastName=" + lastName + ", officeSupplies=" + officeSupplies + ", books=" + books + ", paper=" + paper + ", districtValue=" + districtValue + ", preferredContact=" + preferredContact + '}';
}

public double computeSales()
{
    return officeSupplies + books + paper;
}   

}


Solution

  • If your class is called for example SalesWriter and you have this code in it

    public void writeStars() throws IOException
    {
        File testStars = new File ("stars.txt");
        BufferedWriter outStars = new BufferedWriter(new FileWriter(testStars, true));
        SaleReader reader = new SaleReader();
    
         try
        {
            List<Sales> sale = reader.readSale("out1.txt");   
            for (Sales s : sale)
            {
                if(s.computeSales()>= 8000)
                {
                outStars.write(s);
                outStars.newLine();
    
                }
            }
            outStars.close();
    
        }catch (IOException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    Then from your main code you can call it like

    new SalesWriter ().writeStars ();
    

    edit

    See how I have removed the parameter to this method, as your are reading the file in this method anyway.