为什么我无法根据我的DAO添加订单?

时间:2017-06-17 16:00:26

标签: java hashmap

我正在训练营工作。

我需要添加订单并将其写入文件。我能够通过所有类,并说它已成功添加,但没有写入文件。写入文件的代码很好,因为我被告知,但在我发布的添加方法中,我发布的内容似乎有些不合适。

这是我的代码:

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;
}

1 个答案:

答案 0 :(得分:0)

见这里,

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

您不需要new FileWriter(...)部分,只需指定文件名如下:

PrintWriter out = new PrintWriter("filename");