-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrafficSignal.cpp
115 lines (92 loc) · 2.65 KB
/
TrafficSignal.cpp
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
//* Author: Dhiraj Amarnani, Hassan Naveed, Andre Koppert
//* Date: December 18 2017
#include "TrafficSignal.h"
#include <iostream>
using namespace std;
//*******************************************************************
//* TrafficSignal()
//*
//* The constructor of our TrafficSignal class that creates a new
//* instance of our trafficSignal class. It takes the red (r), yellow
//* (y) and green (g) ticks for the light duration and its initial
//* light color. It does not return anything.
//*
//* Pre-conditions: Int r,y,g for red, yellow green light durations.
//* Char initState for its initial state
//*
//*******************************************************************
TrafficSignal::TrafficSignal(int r, int y, int g, char initState){
//initialize red, yellow, green light durations
redTicks = r;
yellowTicks = y;
greenTicks = g;
switch(initState){
case 'R':
timeUntilNext = r;
light = R;
break;
case 'G':
timeUntilNext = g;
light = G;
break;
}
}
//*******************************************************************
//* ~TrafficSignal()
//*
//* The destructor of our TrafficSignal class. It does not take any
//* parameters or return anything.
//*
//*******************************************************************
TrafficSignal::~TrafficSignal(){
}
//*******************************************************************
//* updateTimeUntilNext()
//*
//* This method updates the time until the light changes every time
//* it is called. It does not take any parameters or return anything.
//*
//*******************************************************************
void TrafficSignal::updateTimeUntilNext(){
//decrement time until next
timeUntilNext = timeUntilNext - 1;
//change light and timeUntilNext light accordingly
if(timeUntilNext == 0){
if(light == R){
light = G;
timeUntilNext = greenTicks;
}
else if(light == G){
light = Y;
timeUntilNext = yellowTicks;
}
else{
light = R;
timeUntilNext = redTicks;
}
}
}
//*******************************************************************
//* printCurrent()
//*
//* This method prints the current state of this trafficSignal every
//* time it is called. It prints the time until the next change and
//* the current light. It does not take any parameters or return
//* anything.
//*
//*******************************************************************
void TrafficSignal::printCurrent(){
//print following to standard output
cout << "Time Until Next: " << timeUntilNext << endl;
switch(light){
case R:
cout << "Red" << endl;
break;
case Y:
cout << "Yellow" << endl;
break;
case G:
cout << "Green" << endl;
break;
}
}