Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ss.week2;
- import ss.utils.TextIO;
- public class ThreeWayLamp {
- private int state = 0; //0 -> off, 1 -> low, 2 -> medium, 3 -> high
- private String[] states = new String[] {"off", "to low light", "to medium light", "to bright light"};
- void setOff() {
- state = 0;
- System.out.println("The lamp is set off");
- }
- void setLow() {
- state = 1;
- System.out.println("The lamp is set to low light");
- }
- void setMedium() {
- state = 2;
- System.out.println("The lamp is set to medium light");
- }
- void setHigh() {
- state = 3;
- System.out.println("The lamp is set to bright light");
- }
- void setNext() {
- state = (state + 1) % 4;
- System.out.println("The lamp is set " + states[state]);
- }
- void printState() {
- System.out.println("The lamp is currently set " + states[state]);
- }
- void showMenu() {
- System.out.println("OFF: Set the lamp to OFF (default value)\n"
- + "LOW: Set the lamp to LOW\n"
- + "MEDIUM: Set the lamp to MEDIUM\n"
- + "HIGH: Set the lamp to HIGH\n"
- + "STATE: Print the current setting of the lamp\n"
- + "NEXT: Change to the next setting, observing the order OFF → LOW → MEDIUM → HIGH →\n"
- + "OFF\n"
- + "HELP: Show a help menu, explaining how the user should interact with the program\n"
- + "EXIT: Quit the program");
- }
- void askInput() {
- String userInput;
- boolean exitApp = false;
- showMenu();
- while(!exitApp) {
- System.out.println("Enter command: ");
- userInput = TextIO.getlnString();
- switch(userInput) {
- case "OFF":
- setOff();
- break;
- case "LOW":
- setLow();
- break;
- case "MEDIUM":
- setMedium();
- break;
- case "HIGH":
- setHigh();
- break;
- case "STATE":
- printState();
- break;
- case "NEXT":
- setNext();
- break;
- case "HELP":
- showMenu();
- break;
- case "EXIT":
- exitApp = true;
- break;
- default:
- System.out.println("Command not found");
- break;
- }
- }
- }
- public static void main(String[] args) {
- ThreeWayLamp lamp = new ThreeWayLamp();
- lamp.askInput();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement