*/
public class Coordinate {
/** 经度 */
private Double longitude;
/** 纬度 */
private Double latitude;
// 省略 getXxx,setXxx等方法
}
Taxi(出租车类)代码示例;
package com.liziba.dl;
import java.util.Objects;
/**
-
-
出租车类
-
@Author: Liziba
*/
public class Taxi {
/** 出租车唯一标志 */
private String id;
/** 当前坐标 */
private Coordinate location;
/** 目的地坐标 */
private Coordinate destination;
/** 所属车队 */
private final Fleet fleet;
/**
-
获取当前地址信息
-
@return
*/
public synchronized Coordinate getLocation() {
return location;
}
/**
-
更新当前地址信息
-
如果当前地址与目的地地址一致,则表名到达目的地需要通知车队,当前出租车空闲可用前往下一个目的地
-
@param location
*/
public synchronized void setLocation(Coordinate location) {
this.location = location;
if (location.equals(destination)) {
fleet.free(this);
}
}
public Coordinate getDestination() {
return destination;
}
/**
-
设置目的地
-
@param destination
*/
public synchronized void setDestination(Coordinate destination) {
this.destination = destination;
}
public Taxi(Fleet fleet) {
this.fleet = fleet;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Taxi taxi = (Taxi) o;
return Objects.equals(location, taxi.location) &&
Objects.equals(destination, taxi.destination);
}
@Override
public int hashCode() {
return Objects.hash(location, destination);
}
}
Fleet(出租车车队类)示例代码:
package com.liziba.dl;
import java.util.Set;
/**
-
-
车队类 -> 调度管理出租车
-
@Author: Liziba
*/
public class Fleet {
/** 车队中所有出租车 */
private final Set taxis;
/** 车队中目前空闲的出租车 */
private final Set availabl