Question: Fix my method called store_interval in the class FoodSupplier that uses file manipulation to remove all of a stores orders between the startDate and endDate

Fix my method called store_interval in the class FoodSupplier that uses file manipulation to remove all of a stores orders between the startDate and endDate from a HashMap database called orders for a specific company.

The file that has to be manipulated is called supply.dat. Currently, the code removes everything from the file supply.dat.

The supply.dat file has contents like this:

05-28-2011 FiveGuys#2 burgers-2 buns-6 fries-8 cokesyrup-3 cups-1 napkins-1 straws-8 05-28-2011 FiveGuys#2 buns-3 fries-6 straws-6 05-29-2011 Hardees#61 burgers-3 buns-1 fries-1 cokesyrup-3 cups-1 napkins-5 straws-5 05-30-2011 BurgerKing#26 burgers-1 buns-3 fries-1 cokesyrup-2 cups-1 05-30-2011 BurgerKing#11 cokesyrup-1 cups-4 napkins-1 straws-3 05-30-2011 Wendys#23 burgers-8 buns-1 fries-4 cokesyrup-1 napkins-1 05-31-2011 BurgerKing#31 buns-5 cokesyrup-6 napkins-1

FoodSupplier:

public class FoodSupplier { Map> orders; public FoodSupplier() { orders = new TreeMap>(); }

public void addOrder(String order) { String date_str = order.split(" ")[0]; // Date order = order.substring(order.indexOf(" ") + 1, order.length()); // Everything else String[] date_split_strs = date_str.split("-"); int[] date_split_ints = new int[3]; int y = 0; for (String x : date_split_strs) { date_split_ints[y++] = Integer.parseInt(x); } Date order_date = new Date(date_split_ints[2] - 1900, date_split_ints[0] - 1, date_split_ints[1]); if (orders.get(order_date) == null) { orders.put(order_date, new LinkedList()); } orders.get(order_date).add(new Order(order)); }

public void store_interval(Date startDate, Date endDate, String company) {

try {

BufferedWriter bw = new BufferedWriter(new FileWriter("supply.dat"));

for (Date date : orders.keySet()) {

if (date.after(startDate) && date.before(endDate)) {

for (Order order : orders.get(date)) {

if (order.getCompanyName().equals(company)) {

bw.write(order.toString());

}

}

}

}

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

The Order class is given below:

import java.util.HashMap; import java.util.Map; public class Order { String company; Map order_items = new HashMap(); public Order(String order_info) { company = ""; for(String order_item:order_info.split(" ")) { if(company.equals("")) { company = order_item; } else { String[] order_details = order_item.split("-"); order_items.put(order_details[0], Integer.parseInt(order_details[1])); } } } public int getNumOrdered(String item) { if(order_items.get(item) == null) { return 0; } return order_items.get(item); } public String getCompanyName() { return company.split("#")[0]; } public int getStoreNumber() { return Integer.parseInt(company.split("#")[1]); } public String getStoreId() { return company; } public String toString() { String ret = company+" "; for(String item : order_items.keySet()) { ret += "{"+item + " : " + order_items.get(item) + "} | "; } return ret; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!