Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Balagso, John Mark Vincent C.

BSIT-WMA-3B

Activity 4:

Code:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// Initialize Scanner object to take user input

Scanner scanner = new Scanner(System.in);

// Print the initial message

System.out.println("-------Traffic Light Control System-----");

// Variables to keep track of the number of times each color is pressed

int greenCount = 0;

int redCount = 0;

int orangeCount = 0;

// Infinite loop to continuously take user input

while (true) {

// Create a new TrafficLight object with initial color "red" and duration 30 seconds

TrafficLight trafficLight = new TrafficLight("red", 30);

// Prompt the guard for traffic light button input

System.out.print("Guard pressed what traffic light button : ");

// Read user input and convert it to lowercase for case-insensitive comparisons

String input = scanner.nextLine().toLowerCase();


// Handle different user inputs using a switch statement

switch (input) {

// If input is "red"

case "red":

// Set traffic light color to "red" and duration to 30 seconds

trafficLight.setColor("red");

trafficLight.setDuration(30);

// Increment the red count

redCount++;

// Print the current traffic light state and a message

System.out.println("The traffic light turned: " + trafficLight.getColor());

System.out.println("Duration set by guard: " + trafficLight.getDuration() + " seconds");

System.out.println("STOP!!!!\n");

break;

// If input is "green"

case "green":

// Set traffic light color to "green" and duration to 20 seconds

trafficLight.setColor("green");

trafficLight.setDuration(20);

// Increment the green count

greenCount++;

// Print the current traffic light state and a message

System.out.println("The traffic light turned: " + trafficLight.getColor());

System.out.println("Duration set by guard: " + trafficLight.getDuration() + " seconds");

System.out.println("GO!!!\n");

break;

// If input is "orange"

case "orange":

// Set traffic light color to "orange" and duration to 15 seconds

trafficLight.setColor("orange");

trafficLight.setDuration(15);

// Increment the orange count


orangeCount++;

// Print the current traffic light state and a message

System.out.println("The traffic light turned: " + trafficLight.getColor());

System.out.println("Duration set by guard: " + trafficLight.getDuration() + " seconds");

System.out.println("SLOWDOWN!!!\n");

break;

// Check if all three colors have been pressed at least once

if (greenCount > 0 && redCount > 0 && orangeCount > 0) {

// If yes, print a special message and reset the color counts

System.out.println("-------TrafficLight Program For Mr.OOP-----.");

greenCount = 0;

redCount = 0;

orangeCount = 0;

// TrafficLight class definition

class TrafficLight {

// Private attributes to store color and duration

private String color;

private int duration;

// Constructor to initialize TrafficLight object with color and duration

public TrafficLight(String color, int duration) {

this.color = color;

this.duration = duration;

}
// Getter method for color

public String getColor() {

return color;

// Setter method for color

public void setColor(String color) {

this.color = color;

// Getter method for duration

public int getDuration() {

return duration;

// Setter method for duration

public void setDuration(int duration) {

this.duration = duration;

}}
Output:

You might also like