Yargi

turnYourLightsDownLow

Oct 29th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. // all classes as subclasses for pastebin
  2.  
  3. package turnYourLightsDownLow;
  4.  
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.stream.Collectors;
  9.  
  10. public class Lights {
  11.  
  12.     private enum Light {
  13.         RED, GREEN, YELLOW;
  14.  
  15.         private static Light[] lights = values();
  16.  
  17.     // implementation of next value
  18.         public Light next(){
  19.             return lights[(this.ordinal() + 1) % lights.length];
  20.         }
  21.     }
  22.  
  23.     private static class TrafficLight {
  24.  
  25.         private Light color;
  26.  
  27.     //constructor
  28.         public TrafficLight(Light color){
  29.             this.color = color;
  30.         }
  31.     // next color method calls next value method of light
  32.         public void nextColor(){
  33.             this.color = color.next();
  34.         }
  35.  
  36.         @Override
  37.         public String toString(){
  38.             return color.name();
  39.         }
  40.     }
  41.  
  42.     public static void main(String[] args) {
  43.  
  44.         Scanner sc = new Scanner(System.in);
  45.  
  46.     // arraylist of trafficlights
  47.         List<TrafficLight> trafficLights = Arrays.stream(sc.nextLine().split("\\s+"))
  48.                 .map(e -> new TrafficLight(Light.valueOf(e)))
  49.                 .collect(Collectors.toList());
  50.  
  51.         int n = Integer.parseInt(sc.nextLine());
  52.  
  53.     // cycle to switch lights and print to console
  54.         for (int i = 0; i < n; i++){
  55.  
  56.             trafficLights.forEach(TrafficLight::nextColor);
  57.             System.out.println(trafficLights.toString().replaceAll("[\\[\\],]", ""));
  58.  
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment