[JAVA] Find the section that satisfies the condition (more than the specified number of times) from the List (check the free time of the conference room)

Table of Contents ⇒ Java Algorithm Library-Artery-Sample

Q01_14.java



package jp.avaj.lib.algo;

import java.util.ArrayList;
import java.util.List;

import jp.avaj.lib.def.ArUseStatus;
import jp.avaj.lib.test.L;

/**
Conditions from List(More than specified number of times)Find a continuous and satisfying section(Check the free time of the conference room)
・ Conditions are specified by ArValidator.
・ You can also find it from behind the List..

-Examples of such processing include the following..
・ Ask for a time when the conference room is free for two hours or more.
・ For a period in which sales of 100 or more are 3 consecutive days or more.
・ Find the period during which the test result was 80 points or more and continued 3 times or more..
・ Find the period during which Nampa succeeded three or more times in a row(Lol).

・ In this sample, a conference room reservation is taken as an example..
・ One meeting room can be reserved on an hourly basis between 9:00 and 17:00.Note, can be used until 18:00.

 */
public class Q01_14 {
  public static void main(String[] args) {
    List<MeetingRoom> list = new ArrayList<MeetingRoom>();
    //Create a frame from 0:00 to 23:00
    for (int i=0; i<23; i++) {
      list.add(new MeetingRoom(i,((i>=9)&&(i<=17))?ArUseStatus.AVAILABLE:ArUseStatus.ILLEGAL));
    }
    //It is a little filled with reservations up to the day before.
    list.get(12).setStatus(ArUseStatus.IN_USE);
    list.get(13).setStatus(ArUseStatus.IN_USE);
    list.get(14).setStatus(ArUseStatus.IN_USE);
    //Check the situation
    L.p(list.toString());

    //Define an ArValidator to check for free
    ArValidator<MeetingRoom> validator = new ArValidator<MeetingRoom>() {
      @Override
      public boolean check(MeetingRoom value) {
        return value.getStatus() == ArUseStatus.AVAILABLE;
      }
    };

    List<Integer> availableList;
    //I'm a visitor at 10 o'clock so I want to make a reservation for 2 hours ⇒ Are you free?
    availableList = ArList.sameValueSequence(list, validator,2,10);
    //Check the result ⇒ It should be two hours free from 10:00, 15:00 and 16:00..
    L.p(availableList.toString());
    //Book for 2 hours from 10 o'clock
    list.get(10).setStatus(ArUseStatus.IN_USE);
    list.get(11).setStatus(ArUseStatus.IN_USE);
    L.p(list.toString());

    //Scheduled to leave early at 16:00 ⇒ I want to have a meeting for 1 hour before that ⇒ Book for 1 hour at the end of 16:00 ⇒ Are you free?
    availableList = ArList.sameValueSequenceReverse(list, validator,1,16);
    L.p(availableList.toString()); //⇒ 15:00 and 9:00 should be free...
    //Book for 1 hour from 15:00
    list.get(15).setStatus(ArUseStatus.IN_USE);

    //Check the situation
    L.p(list.toString());
  }
  //
  /**conference room(one hour)class*/
  static class MeetingRoom {
    /**constructor*/
    public MeetingRoom(int time,ArUseStatus status) {
      this.time = time;
      this.status = status;
    }
    /** (start)Times of Day*/
    private int time;
    /**status*/
    private ArUseStatus status;
    public int getTime() {
      return time;
    }
    public void setTime(int time) {
      this.time = time;
    }
    public ArUseStatus getStatus() {
      return status;
    }
    public void setStatus(ArUseStatus status) {
      this.status = status;
    }
    @Override
    public String toString() {
      String str = String.format("%02d:",time);
      return (status == ArUseStatus.ILLEGAL) ? "" : str+status.toString();
    }
  }
}


The result is as follows.

result.txt



[, , , , , , , , , 09:Can be used, 10:Can be used, 11:Can be used, 12:In use, 13:In use, 14:In use, 15:Can be used, 16:Can be used, 17:Can be used, , , , , ]
[10, 15, 16]
[, , , , , , , , , 09:Can be used, 10:In use, 11:In use, 12:In use, 13:In use, 14:In use, 15:Can be used, 16:Can be used, 17:Can be used, , , , , ]
[15, 9]
[, , , , , , , , , 09:Can be used, 10:In use, 11:In use, 12:In use, 13:In use, 14:In use, 15:In use, 16:Can be used, 17:Can be used, , , , , ]


Recommended Posts

Find the section that satisfies the condition (more than the specified number of times) from the List (check the free time of the conference room)
A program that counts the number of words in a List
Aggregate the number of people every 10 years from List <Person>
[Java] Delete the specified number of characters from the end of StringBuilder