package lab3;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MilitaryAppointment {
	/*properties*/
	final int _start,
	          _end;

	/*constructor*/
	/**
	 * constructor
	 * @param start as int military form
	 * @param end as int military form
	 */
	private MilitaryAppointment(int start, int end){
		if (start < end){
			_start = start;
			_end = end;
		}
		else{
			_start = end;
			_end = start;
		}
	}

	/*static factory*/
	private static SimpleDateFormat _military = new SimpleDateFormat("HHmm");
	/**
	 * build object from two date
	 * @param start as Date
	 * @param end as Date
	 * @return new MilitaryAppointment
	 */
	public static MilitaryAppointment fromDate(final Date start, final Date end){
		return new MilitaryAppointment(Integer.parseInt(_military.format(start)),
									   Integer.parseInt(_military.format(end)));
	}

	/**
	 * build object from two military time
	 * @param start as int military time
	 * @param end as int military time
	 * @return new MilitaryAppointment
	 */
	public static MilitaryAppointment fromMilitaryIntTime(int start, int end){
		return new MilitaryAppointment(start, end);
	}

	/*getter*/
	/**
	 * get start time
	 * @return start of the appointment
	 */
	public int getStart(){
		return _start;
	}

	/**
	 * get end time
	 * @return end of the appointment
	 */
	public int getEnd(){
		return _end;
	}

	/**
	 * test if two appointment conflict
	 * @param m as military appointment
	 * @return true if conflicting
	 */
	public boolean conflictWith(final MilitaryAppointment m){
		return (m.getStart() >= _start && m.getStart() <= _end) ||
			   (m.getEnd() >= _start && m.getEnd() <= _end) ||
			   (m.getEnd() >= _end && m.getStart() <= _start) ||
			   (m.getStart() >= _start && m.getEnd() <= _end);
	}

}
