dhurdev

Light Room - Java

Nov 11th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class LightRoom {
  4.  
  5.     private List<Integer> getHouseInfo() {
  6.         List<Integer> bulpsOnList = new ArrayList<Integer>();
  7.         int totalRooms, totalBulbs, bulpsSwitchedOn = 0;
  8.         String windowOption, shadyOption;
  9.         boolean isWindowAttached, isShadyOutside;
  10.        
  11.         Scanner in = new Scanner(System.in);
  12.         System.out.print("Total Rooms in the House: ");
  13.         totalRooms = in.nextInt();
  14.         in.nextLine();
  15.        
  16.         for (int i = 0; i < totalRooms; i++) {
  17.             System.out.print("\nEnter Total Bulbs In The Room "+(i+1)+": ");
  18.             totalBulbs = in.nextInt();
  19.             in.nextLine();
  20.            
  21.             do{
  22.                 System.out.print("Does The Room Attached With Window (y/n)? ");
  23.                 windowOption = in.nextLine();
  24.                 isWindowAttached = ("yY".contains(windowOption));
  25.             }
  26.             while(windowOption.length() != 1 || !("yYnN".contains(windowOption)));
  27.            
  28.             do{
  29.                 System.out.print("Does The Outside Of The Room Is Shady (y/n)? ");
  30.                 shadyOption = in.nextLine();
  31.                 isShadyOutside = ("yY".contains(shadyOption));
  32.             }
  33.             while(shadyOption.length() != 1 || !("yYnN".contains(shadyOption)));
  34.            
  35.             if (!isWindowAttached) {
  36.                 bulpsSwitchedOn = totalBulbs;
  37.             } else if (isWindowAttached && !isShadyOutside) {
  38.                 bulpsSwitchedOn = 0;
  39.             } else if (isWindowAttached && isShadyOutside) {
  40.                 bulpsSwitchedOn = totalBulbs / 2;
  41.             }
  42.  
  43.             bulpsOnList.add(bulpsSwitchedOn);
  44.         }
  45.         return bulpsOnList;
  46.     }
  47.    
  48.     private void displayResult(List<Integer> list) {
  49.         int totalRoom = 0, totalBulbs = 0;
  50.        
  51.         for (Integer number : list) {
  52.             totalRoom = number > 0 ? ++totalRoom : totalRoom;
  53.             totalBulbs = number > 0 ? totalBulbs+=number : totalBulbs;
  54.         }
  55.        
  56.         System.out.println("\nTotal Rooms Which Have Lights On = "+totalRoom);
  57.         System.out.println("Total Number of Light Bulps Switched On = "+totalBulbs);
  58.     }
  59.  
  60.     public static void main(String args[]) {
  61.         LightRoom house = new LightRoom();
  62.         List<Integer> list = house.getHouseInfo();
  63.         house.displayResult(list);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment