-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lane.java
executable file
·164 lines (124 loc) · 4.66 KB
/
Lane.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
// Programmer: Arthur Charlesworth (c) Copyright 2016
// Modified by GOATS team: Alex David Rex Tom Zihao
// *****************************************************************************
// *****************************************************************************
// **** Lane
// *****************************************************************************
// *****************************************************************************
// Latest Enhancement: Stage 1 Style
//Lane Class
//This class defines a "Lane" object, which will hold cars.
//Lanes can be either incoming or outgoing depending on their direction in
//relation to intersections.
//Each lane will belong to 2 intersections: as an incoming lane for one
//and an outgoing lane for another
import java.util.Queue;
import java.util.LinkedList; //The implementation of a queue
import java.util.HashMap;
public class Lane {
//**************Instance Variables**************
private Queue<Car> q; // Stores the cars in the lane
private HashMap<Car,Integer> timesInQueue; // Stores the length of time
// each car spends in the lane
private int capacity;
private int minTimeToTraverse;
public boolean inboundBoundary;
public boolean outboundBoundary;
//**************Constructor**************
public Lane(int capacity, int minTimeToTraverse)
{
q = new LinkedList<Car>();
timesInQueue = new HashMap<Car,Integer>();
this.capacity = capacity;
this.minTimeToTraverse = minTimeToTraverse;
} // end of constructor
//**************Instance Methods**************
//removes a Car from a Lane and returns it
public Car get()
{
if( this.peek() != null)
{
// if a car is able to exit, remove it from the queue
return q.poll();
} else {
// return null if no car is able to exit the queue yet.
return null;
}
} // end of get()
public Car peek()
{
Car c = q.peek();
if( c != null && timesInQueue.get(c) >= minTimeToTraverse)
{
return c;
} else {
// return null if no car is able to exit the queue yet.
return null;
}
} // end of peek()
//adds a provided Car c to the Lane
public boolean add(Car c)
{
if(this.isFull())
{
System.out.println("Illegal attempt to add to a full lane");
return true;
} // end of if(this.isFull())
if(this.isOutboundBoundary())
{
System.out.println(" car#" + c.getID() + " has left the grid");
// Future enhancement: update stats for the car leaving
} // end of if(this.isOutboundBoundary())
timesInQueue.put(c,0);
c.setHasChangedLanes(true);
return q.add(c);
} // end of add(Car c)
//adds car at boundary lane
public boolean initialAdd(Car c)
{
boolean done = this.add(c);
timesInQueue.put(c, minTimeToTraverse);
return done;
}
// We "update" the length of time each car spends in the lane
public void update()
{
for( Car c : q )
{
if (!c.getHasChangedLanes()){
// Increment c's timeInQueue
timesInQueue.put(c,timesInQueue.get(c)+1);
}
} // end of for( Car c : q )
} // end of update()
//checks if lane is empty
public boolean isEmpty()
{
return (q.peek() == null);
} // end of isEmpty()
//checks if lane is full
public boolean isFull()
{
if( this.isInboundBoundary() || this.isOutboundBoundary() )
{
return false;
} // end of if ( this.isInboundBoundary() || this.isOutboundBoundary() )
return q.size() >= capacity;
} // end of isFull()
//set method for boundary lanes entering grid
public void setInboundBoundary() {
this.inboundBoundary = true;
} // end of isInboundBoundary()
//set method for boundary lanes exiting grid
public void setOutboundBoundary() {
this.outboundBoundary = true;
} // end of isOutboundBoundary()
//checks if lane is boundary lane entering grid
public boolean isInboundBoundary() {
return this.inboundBoundary;
} // end of isInboundBoundary()
//checks if lane is boundary lane exiting grid
public boolean isOutboundBoundary() {
return this.outboundBoundary;
} // end of isOutboundBoundary()
}