Signal.java
public class Signal {
int firstTime, secondTime, thirdTime;
String color, color2, color3;
private Time[] times;
public Signal(int firstTime, String color, int secondTime, String color2, int thirdTime, String color3) {
this.color= color;
this.firstTime= firstTime;
this.secondTime= secondTime;
this.color2= color2;
this.thirdTime= thirdTime;
this.color3= color3;
}
public String getColor(int time) {
int adjustedTime= time%(firstTime+ secondTime + thirdTime);
if(adjustedTime < firstTime)
return color;
else if (adjustedTime<firstTime+secondTime)
return color2;
else return color3;
}
public String getColorWithDate(int year, int month, int day, int hour, int minute, int second)
{
int totalSecond=((((365*(year-2000)+(month-1)*30+(day-1)*24+hour)*60)+minute)*60+second);
return getColor(totalSecond);
}
public void insertCrosser(Time[] times ) {
this.times= times;
}
public int checkViolation()
{
int number= 0;
for (Time time : times) {
if(getColorWithDate(time).equals("Red")) {
number++;
}
}
return number;
}
private String getColorWithDate(Time time) {
return getColorWithDate(time.year, time.month, time.day, time.hour, time.minute, time.second);
}
public static void main(String []args)
{
Signal signal= new Signal(120, "Red", 5, "Yellow", 30, "Green");
System.out.println(signal.getColor(30));
System.out.println(signal.getColor(120));
System.out.println(signal.getColorWithDate(2007, 3, 24, 12, 30, 24));
System.out.println(signal.getColorWithDate(2000, 1, 1, 0, 0, 0));
Time[] times2= new Time[3];
times2[0]= new Time(2004, 2, 3, 11, 25, 16);
times2[1]= new Time(2002, 7, 21, 5, 3, 55 );
times2[2]= new Time(2012, 1, 1, 0, 0, 1);
signal.insertCrosser(times2);
System.out.println(signal.checkViolation());
}
}
Time.java
public class Time{
public int year;
public int month;
public int day;
public int minute;
public int hour;
public int second;
public Time() {
}
public Time(int year, int month, int day, int hour, int minute, int second ) {
this.year= year;
this.month= month;
this.day= day;
this.hour= hour;
this.minute= minute;
this.second= second;
}
}