Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package ss.week2;
  2.  
  3. import ss.utils.TextIO;
  4.  
  5. public class ThreeWayLamp {
  6.  
  7.     private int state = 0; //0 -> off, 1 -> low, 2 -> medium, 3 -> high
  8.     private String[] states = new String[] {"off", "to low light", "to medium light", "to bright light"};
  9.    
  10.     void setOff() {
  11.         state = 0;
  12.         System.out.println("The lamp is set off");
  13.     }
  14.    
  15.     void setLow() {
  16.         state = 1;
  17.         System.out.println("The lamp is set to low light");
  18.     }
  19.  
  20.     void setMedium() {
  21.         state = 2;
  22.         System.out.println("The lamp is set to medium light");
  23.     }
  24.  
  25.     void setHigh() {
  26.         state = 3;
  27.         System.out.println("The lamp is set to bright light");
  28.     }
  29.  
  30.     void setNext() {
  31.         state = (state + 1) % 4;
  32.         System.out.println("The lamp is set " + states[state]);
  33.     }
  34.  
  35.     void printState() {
  36.         System.out.println("The lamp is currently set " + states[state]);
  37.     }
  38.  
  39.     void showMenu() {
  40.         System.out.println("OFF: Set the lamp to OFF (default value)\n"
  41.                             + "LOW: Set the lamp to LOW\n"
  42.                             + "MEDIUM: Set the lamp to MEDIUM\n"
  43.                             + "HIGH: Set the lamp to HIGH\n"
  44.                             + "STATE: Print the current setting of the lamp\n"
  45.                             + "NEXT: Change to the next setting, observing the order OFF → LOW → MEDIUM → HIGH →\n"
  46.                             + "OFF\n"
  47.                             + "HELP: Show a help menu, explaining how the user should interact with the program\n"
  48.                             + "EXIT: Quit the program");
  49.     }
  50.    
  51.     void askInput() {
  52.         String userInput;
  53.         boolean exitApp = false;
  54.        
  55.         showMenu();
  56.        
  57.         while(!exitApp) {
  58.             System.out.println("Enter command: ");
  59.             userInput = TextIO.getlnString();
  60.        
  61.             switch(userInput) {
  62.                 case "OFF":
  63.                     setOff();
  64.                     break;
  65.                
  66.                 case "LOW":
  67.                     setLow();
  68.                     break;
  69.                
  70.                 case "MEDIUM":
  71.                     setMedium();
  72.                     break;
  73.                
  74.                 case "HIGH":
  75.                     setHigh();
  76.                     break;
  77.                
  78.                 case "STATE":
  79.                     printState();
  80.                     break;
  81.                
  82.                 case "NEXT":
  83.                     setNext();
  84.                     break;
  85.            
  86.                 case "HELP":
  87.                     showMenu();
  88.                     break;
  89.            
  90.                 case "EXIT":
  91.                     exitApp = true;
  92.                     break;
  93.                    
  94.                 default:
  95.                     System.out.println("Command not found");
  96.                     break;
  97.             }
  98.         }
  99.        
  100.     }
  101.    
  102.     public static void main(String[] args) {
  103.         ThreeWayLamp lamp = new ThreeWayLamp();
  104.         lamp.askInput();
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement