use std::cmp::Ordering;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering as AtomicOrdering;
#[derive(Clone, Copy, Debug)]
pub struct Customer {
id: usize,
pub arrival_time: f64,
pub service_time: f64,
service_start: Option<f64>,
service_end: Option<f64>,
}
impl std::fmt::Display for Customer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Customer({}, {:.4}, {:.4})",
self.id, self.arrival_time, self.service_time
)
}
}
impl Customer {
pub fn new(arrival_time: f64, service_time: f64) -> Self {
static COUNTER: AtomicUsize = AtomicUsize::new(1);
let id = COUNTER.fetch_add(1, AtomicOrdering::Relaxed);
Self {
id,
arrival_time,
service_time,
service_start: None,
service_end: None,
}
}
pub fn waiting_time(&self) -> Option<f64> {
match self.service_start {
Some(service_start) => Some(service_start - self.arrival_time),
_ => None,
}
}
pub fn cycle_time(&self) -> Option<f64> {
match self.service_end {
Some(service_end) => Some(service_end - self.arrival_time),
_ => None,
}
}
pub fn set_service_start(&mut self, service_start: f64) {
self.service_start = Some(service_start);
}
pub fn set_service_end(&mut self, service_end: f64) {
self.service_end = Some(service_end);
}
}
impl Ord for Customer {
fn cmp(&self, other: &Self) -> Ordering {
self.arrival_time.partial_cmp(&other.arrival_time).unwrap()
}
}
impl PartialOrd for Customer {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.arrival_time.partial_cmp(&other.arrival_time)
}
}
impl Eq for Customer {}
impl PartialEq for Customer {
fn eq(&self, other: &Self) -> bool {
self.arrival_time == other.arrival_time
}
}