简体   繁体   中英

how to insert a date-time hh: mm into an array list sorted by times only if the time is 10 min before or after the insertion point

I have one question for my first development project in Java9. I need to insert an Object with some info and a Date type, inside an ArrayList only if the time is not present in the ArrayList and if it respects the condition that every time in the ArrayList must be stored every 10 minutes.

So, for example the idea is something like that:

correct : [21:10, 21:20, 21:50, 22:00] ,

incorrect : [21:05, 21:10, 21:20, 21:55, 22:00]

I try something like this:

 private static ArrayList<Order> orders = new ArrayList<Order>();
    //ordinazione = orderTime,name,surname,adderess,pizze;
    public Pizzeria() {
       Order ord = new Order(name, surname, address, num_pizze);
       isValid(ord);
    }
    private void isValid(Order ord) {
                boolean valid = false;
                long prew,current;
                long add = ord.getOrderTime().getTime();
                int min10 = 10*60*1000; //10 minutes
                if(orders.size() == 0) {
                    addOrder(ord);
                }else if(orders.size() == 1){
                    current = orders.get(0).getOrderTime().getTime() / 1000;
                    if(add > current) {
                        if(add - current >= min10) {
                            valid = true;
                            addOrder(order);
                        }
                    }else{
                        if(current - add >= min10) {
                            valid = true;
                            addOrder(ord);
                        }
                    }
                }else{
                    int i = 0;  
                      while (orders.size() > i) {
                          prec =  i > 1 ? orders.get(i-1).getOrderTime().getTime() / 1000 : 0;
                          current = orders.get(i).getOrderTime().getTime() / 1000;
                          if (current - add >= min10 && add - prec >= min10) {
                              valid = true;
                              addOrder(ord);
                              break;
                          }
                          i++;
                      }
                }
                
                if(!valid) {
                    System.out.println("this time is not allowed!");
                }
            }
        
            public void addOrder(Orderd ord) {
                orders.add(ord);
            }

any advice?

It may not be the most beautiful solution, but it's pretty simple: allocate an array with a slot for an order every 10 minutes of the day.

First we need the array of orders:

/** Accept an order every 10 minutes */
private static final int SLOT_SIZE_MINUTES = 10;

private Order[] orders
        = new Order[Math.toIntExact(Duration.ofDays(1).toMinutes()) / SLOT_SIZE_MINUTES];

Now the method to fit a new order into the orders could be like:

/**
 * If the order fits into a vacant slot that agrees with the time +/- 10 minutes,
 * adds it there, adjusts the order time and returns true;
 * otherwise returns false
 */
private boolean addOrderIfValid(Order newOrder) {
    LocalTime orderTime = newOrder.getOrderTime();
    int slotIndex = orderTime.get(ChronoField.MINUTE_OF_DAY) / SLOT_SIZE_MINUTES;
    if (orders[slotIndex] != null) { // slot is already taken
        slotIndex++;
        if (slotIndex == orders.length) { // end of array
            // start over from 00:00
            slotIndex = 0;
        }
    }
    if (orders[slotIndex] == null) { // vacant
        newOrder.setOrderTime(LocalTime.MIN.with(ChronoField.MINUTE_OF_DAY, slotIndex * SLOT_SIZE_MINUTES));
        orders[slotIndex] = newOrder;
        return true;
    } else { // did not find a vacant slot
        return false;
    }
} 

What my method does: It tries to fit the order into the right 10 minutes slot based on the order time already in the order, truncating the time to a 10 minutes slot. If the slot is already taken, it looks into the following slot, using cyclic overflow. If either of the two slots tried is vacant, it sets the order time to the appropriate time for the slot found. This is also what ensures that the time will be at a whole 10 minutes of the hour: 21:00, 21:10, etc., not 21:05.

Link

Oracle tutorial: Date Time explaining how to use java.time.

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