-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlace.java
196 lines (167 loc) · 5.89 KB
/
Place.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.util.*;
import java.io.*;
public abstract class Place{
private double length;
private Place parent;
protected ArrayList<Node>links=new ArrayList<Node>();
private ArrayList<Place>children=new ArrayList<Place>();//useful for display
private ArrayList<Occupation>occupations=new ArrayList<Occupation>();
protected ArrayList<Car>cars=new ArrayList<Car>();
/**
* Creates a place from a parent place and a length.
* @param parent the parent node from which the place "begins"
* @param length the distance taken to traverse the newly created place
*/
public ArrayList<Node> getNodes(){
return this.links;
}
public ArrayList<Node> getAllNodes(){
ArrayList<Node> out=new ArrayList<Node>();
for (Node n: this.links){
if (n.origin()==this){
out.add(n);
}
}
for(Place p: children){
ArrayList<Node> toAdd=p.getAllNodes();
for (Node n: toAdd){
out.add(n);
}
}
return out;
}
public Place(Place parent,double length) throws AdoptionException{
if(parent.makeParent(this)){
Node n=new Node(parent,this, parent.x(), parent.y());
parent.addLink(n);
links.add(n);
this.length=length;
}
else{
throw (new AdoptionException("The parent failed to adopt"));
}
}
/**
* To be used only once in the creation of the initial Times Square
* @param length the distance taken to traverse the Times Square
*/
protected Place(double length){
//System.out.println(this.length);
this.length=length;
}
/**
* Creates a place that links a parent and target place. Nodes are created to link the new place to both parent and target. The place created will most likely be a road though an unconventional use to create an intersection linking two roads that coincide is possible. The target place will not however, be listed as a child since it is sufficient for every place to have only one parent.
* @param parent the parent node from which the place "begins"
* @param target the other place to which the new place will be connected to
* @param length the distance taken to traverse the newly created place
*/
public Place(Place parent,Place target,double length)throws AdoptionException{
Node n2=new Node(this,target,target.x(),target.y());
if(parent.makeParent(this)&&target.addLink(n2)){
Node n=new Node(parent,this, parent.x(),parent.y());
parent.addLink(n);
links.add(n);
this.length=length;
links.add(n2);
}
else{
if (!parent.makeParent(this)){
parent.disown(this);
throw (new AdoptionException("Parent failed to adopt."));
}
else{
throw(new AdoptionException("Target failed to take on a new node."));
}
}
}
public void disown(Place p){
this.children.remove(p);
}
/**
* Attempts to make this place a parent to another place
* @param p the "child" to be adopted
* @return whether or not the child can be adopted or not
*/
public boolean makeParent(Place p){
if (this instanceof Intersection){
this.children.add(p);
return true;
}
else if (this instanceof Road){
if(this.links.size()==1){
this.children.add(p);
return true;
}
else{
return false;
}
}
else{return false;}
}
/**
* Adds a node to this place thereby linking it to the other place the node
* @param n the node to be added
*/
public boolean addLink(Node n){
if (this instanceof Intersection){
this.links.add(n);
return true;
}
else if (this instanceof Road){
if(this.links.size()==1){
this.links.add(n);
}
else{
return false;
}
}
return false;
}
/**
* Returns the length of the node
* @return the length of the node
*/
public double length(){
//System.out.println(this.length);
return this.length;
}
public void removeCar(Car c){
this.cars.remove(c);
}
abstract boolean addCar(Car c);
/**
* The display method for a place
*/
abstract void display();
/**
* Calls the display method for this place and the the {@link #displayAll()} method for its children
*/
public void displayAll(){
this.display();
for (Place p :children){
p.displayAll();
}
}
/**
* Returns the X coordinate of the open part of a place. For an intersection this is always the intersection's location, and for roads this is the open end.
* @return the coordinate
*/
abstract double x();
/**
* Returns the Y coordinate of the open part of a place. For an intersection this is always the intersection's location, and for roads this is the open end.
* @return the coordinate
*/
abstract double y();
public void inputNodeValue(double nodeValue,ArrayList<Node>path, double speed){
for (Node n: this.links){
n.pushNodeValue((nodeValue+this.length/speed),this,path,speed);
}
}
public void inputNodeValue(double nodeValue, Node sourceNode, ArrayList<Node>path,double speed){
for (Node n: this.links){
if (n!=sourceNode){
n.pushNodeValue((nodeValue+this.length/speed),this,path,speed);
}
}
}
}