简体   繁体   中英

Why am I not able to add an order based on my DAO?

I am working on an assignment in a bootcamp.

I need to add an order and write it to a file. I am able to go through all of the classes and says it was successfully added but a file was not written. My code to write to a file is fine as I was told but in my add method that I have posted seems to have something off.

Here is my code:

public void writeToOrder () throws flooringMasteryPersistenceException {
    PrintWriter out = null;
    for (LocalDate entry : orders.keySet()) {
        try {
            out = new PrintWriter (new FileWriter (DateToFile (entry)));
        } catch (IOException ex) {
            System.out.println ("cannot write to file.");
        }

        List<Order> purchases = orders.get (entry);
        for (Order entryPoint : purchases) {
            out.println (
                entryPoint.getOrderNumber () + DELIMITER + 
                entryPoint.getDate () + DELIMITER + 
                entryPoint.getCustomerName () + DELIMITER + 
                entryPoint.getState () + DELIMITER + 
                entryPoint.getTaxRate () + DELIMITER + 
                entryPoint.getProductType () + DELIMITER + 
                entryPoint.getArea () + DELIMITER + 
                entryPoint.getCostPerSquareFoot () + DELIMITER + 
                entryPoint.getLaborCostPerSquareFoot () + DELIMITER + 
                entryPoint.getMaterialCost () + DELIMITER + 
                entryPoint.getLaborCost () + DELIMITER + 
                entryPoint.getTax () + DELIMITER + 
                entryPoint.getTotal ()
            );

            out.flush ();
        }

        out.close ();
    }
}

@Override
public void save () throws flooringMasteryPersistenceException {
    writeToOrder ();
}

@Override
public Order addOrder (Order order) throws flooringMasteryPersistenceException {   
    // List< Order> newDate = orders.get (LocalDate.now ());
    // if (!orders.containsKey (order.getDate ())) {
    //     orders.put(order.getDate(), Set < String > dates); 
    // } 
    // orders.get (order.getDate ());
    // newDate.add (order);
    // 
    // save ();
    //        
    // orders.put (order.getDate (), );
    // order.setOrderNumber (orderNumber);
    // return order;
}

See here,

out = new PrintWriter (new FileWriter (DateToFile (entry)));

You don't need new FileWriter(...) part, just specify the file name as below:

PrintWriter out = new PrintWriter("filename");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM