1use std::cmp::Ordering;
2use std::sync::atomic::AtomicUsize;
3use std::sync::atomic::Ordering as AtomicOrdering;
4
5#[derive(Clone, Copy, Debug)]
6pub struct Customer {
7 id: usize,
8 pub arrival_time: f64,
9 pub service_time: f64,
10 service_start: Option<f64>,
11 service_end: Option<f64>,
12}
13
14impl std::fmt::Display for Customer {
15 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 write!(
17 f,
18 "Customer({}, {:.4}, {:.4})",
19 self.id, self.arrival_time, self.service_time
20 )
21 }
22}
23
24impl Customer {
25 pub fn new(arrival_time: f64, service_time: f64) -> Self {
26 static COUNTER: AtomicUsize = AtomicUsize::new(1);
27 let id = COUNTER.fetch_add(1, AtomicOrdering::Relaxed);
28
29 Self {
30 id,
31 arrival_time,
32 service_time,
33 service_start: None,
34 service_end: None,
35 }
36 }
37
38 pub fn waiting_time(&self) -> Option<f64> {
39 match self.service_start {
40 Some(service_start) => Some(service_start - self.arrival_time),
41 _ => None,
42 }
43 }
44
45 pub fn cycle_time(&self) -> Option<f64> {
46 match self.service_end {
47 Some(service_end) => Some(service_end - self.arrival_time),
48 _ => None,
49 }
50 }
51
52 pub fn set_service_start(&mut self, service_start: f64) {
53 self.service_start = Some(service_start);
54 }
55
56 pub fn set_service_end(&mut self, service_end: f64) {
57 self.service_end = Some(service_end);
58 }
59}
60
61impl Ord for Customer {
62 fn cmp(&self, other: &Self) -> Ordering {
63 self.arrival_time.partial_cmp(&other.arrival_time).unwrap()
64 }
65}
66
67impl PartialOrd for Customer {
68 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
69 self.arrival_time.partial_cmp(&other.arrival_time)
70 }
71}
72
73impl Eq for Customer {}
74
75impl PartialEq for Customer {
76 fn eq(&self, other: &Self) -> bool {
77 self.arrival_time == other.arrival_time
78 }
79}