-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdge.java
161 lines (149 loc) · 3.52 KB
/
Edge.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
/**
* Edge class is necessary to keep track of all the cars on the node and adjust the time necessary to cover the edge randomly
*
* @author Lekso Borashvili
* @version (a version number or a date)
*/
import java.util.*;
public class Edge
{
private int node1;
private int node2;
private String type;
private int curWeight;
private int freeWeight;
private int normalWeight;
private int busyWeight;
/**
* tree that contains all the cars on this edge
*/
TreeMap<Integer,Integer> cars = new TreeMap<Integer,Integer>();
/**
* builds an object with two end points and the type provided. Randomly sets the weights of different states of the edge.
* @param n1 one end point
* @param n2 second end point
* @param t type of the edge
*/
public Edge(int n1,int n2,String t)
{
node1 = n1;
node2 = n2;
type = t;
Random rand = new Random();
normalWeight = 2 + rand.nextInt(3);
freeWeight = normalWeight - rand.nextInt(2);
busyWeight = normalWeight + rand.nextInt(2);
if(t=="highway")
{
normalWeight *= 2;
freeWeight *= 2;
busyWeight *= 2;
}
randomBusyness();
}
/**
* randomizes the bussiness of the edge. Changes the time necessary to cover randomly
*/
public void randomBusyness()
{
Random rand = new Random();
int k = rand.nextInt(4);
if(k==0) curWeight = freeWeight;
else
if(k==1 || k==2) curWeight = normalWeight;
else
if(k==3) curWeight = busyWeight;
}
/**
* sets first node
* @param n first node
*/
public void setNode1(int n)
{
node1=n;
}
/**
* returns first node
* @return first node
*/
public int getNode1()
{
return node1;
}
/**
* sets second node
* @param n second node
*/
public void setNode2(int n)
{
node2=n;
}
/**
* returns second node
* @return second node
*/
public int getNode2()
{
return node2;
}
/**
* returns the current weight of the node
* @return current weight of the node
*/
public int getCurWeight()
{
return curWeight;
}
/**
* returns weight when the road is free
* @return free weight of the node
*/
public int getFreeWeight()
{
return freeWeight;
}
/**
* returns the weight when road is in normal state
* @return the weight when road is in normal state
*/
public int getNormalWeight()
{
return normalWeight;
}
/**
* returns weight of the road in a busy state
* @return weight of the road in a busy state
*/
public int getBusyWeight()
{
return busyWeight;
}
/**
* returns type of the road
* @return type of the road
*/
public String getType()
{
return type;
}
/**
* removes the car from the edge
* @param ID the id of the car removed
* @return number of cars on the road after car is removed
*/
public int removeCar(int ID)
{
cars.remove(ID);
return cars.size();
}
/**
* adds car to the edge
* @param ID id of the car
* @return number of cars on the road after car is added
*/
public int addCar(int ID)
{
cars.put(ID,ID);
return cars.size();
}
}