-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassengerSource.java
More file actions
89 lines (82 loc) · 2.55 KB
/
Copy pathPassengerSource.java
File metadata and controls
89 lines (82 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package model;
import java.util.Random;
/**
* Periodically generate passengers.
* Keep track of the number of passengers for whom
* a vehicle cannot be found.
*
* @author David J. Barnes and Michael Kolling. Modified ZHENG yujie, SU yu
* @version 2017.03.25
*/
public class PassengerSource implements Actor
{
private static final double CREATION_PROBABILITY = 0.06;
private City city;
private TaxiCompany company;
private Random rand;
private static int missedPickups;
/**
* Constructor for objects of class PassengerSource.
* @param company The company to be used. Must not be null.
* @throws NullPointerException if company is null.
*/
public PassengerSource(City city, TaxiCompany company)
{
if(city == null) {
throw new NullPointerException("city");
}
if(company == null) {
throw new NullPointerException("company");
}
this.city = city;
this.company = company;
// Use a fixed random seed for repeatable effects.
// Example for test: rand = new Random(12345);
rand = new Random();
missedPickups = 0;
}
/**
* Randomly generate a new passenger.
* Keep a count of missed pickups.
*/
public void act()
{
if(rand.nextDouble() <= CREATION_PROBABILITY) {
Passenger passenger = createPassenger();
if(company.requestPickup(passenger)) {
city.addItem(passenger);
}
else {
missedPickups++;
}
}
}
/**
* @return The number of passengers for whom a pickup
* could not be found.
*/
public static int getMissedPickups()
{
return missedPickups;
}
/**
* Create a new passenger with distinct pickup and
* destination locations.
* @return The created passenger.
*/
private Passenger createPassenger()
{
int cityWidth = city.getWidth();
int cityHeight = city.getHeight();
Location pickupLocation =
new Location(rand.nextInt(cityWidth),
rand.nextInt(cityHeight));
Location destination;
do{
destination =
new Location(rand.nextInt(cityWidth),
rand.nextInt(cityHeight));
} while(pickupLocation.equals(destination));
return new Passenger(pickupLocation, destination);
}
}