SHARE
TWEET

Untitled

a guest Aug 25th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.swing.JOptionPane;
  2.  
  3. public class homeAutoBot {
  4.  
  5.     public static void main(String[] args) {
  6.         // I will approach Assignment 1 by working out what I can do with my current
  7.         // knowledge.
  8.         // Then I will slowly add to it over the next 3 weeks to create a fully
  9.         // functioning autobot.
  10.  
  11.        
  12.         // Introduction Messages
  13.         JOptionPane.showMessageDialog(null, "Hello! I'm Fraizr the home automation bot.");
  14.         JOptionPane.showMessageDialog(null, "Starting appliance input mode...");
  15.  
  16.         // Variables
  17.         int appNum = 0;
  18.         String command = "";
  19.         int i = 0;
  20.        
  21.         //cancel goodbye
  22.         while (command != null) {
  23.            
  24.        
  25.        
  26.         // user input # of appliances
  27.         appNum = Integer.parseInt(JOptionPane.showInputDialog("How many applliances?"));
  28.         while (appNum<=0) {
  29.             appNum = Integer.parseInt(JOptionPane.showInputDialog("How many appliances (1 or more)?"));
  30.         }
  31.         // Creating array for entering appliances
  32.         String[] appName = new String[appNum];
  33.         boolean[] appState = new boolean[appNum];
  34.         while (i < appNum) {
  35.             appName[i] = JOptionPane.showInputDialog("Enter appliance number " + (i + 1) + " of " + appNum);
  36.             i++;
  37.  
  38.         }
  39.  
  40.         i = 0;
  41.         String message = "";
  42.         // creates output so user can see apps and states
  43.         while (i < appNum) {
  44.  
  45.             message += appName[i];
  46.  
  47.             if (appState[i] == false) {
  48.                 message += " (off), ";
  49.             } else {
  50.                 message += " (on), ";
  51.             }
  52.             i++;
  53.         }
  54.  
  55.  
  56.         // List Appliances and ask for input
  57.         command = JOptionPane.showInputDialog(null, "Avaliable Appliances: " + message);
  58.  
  59.         command = command.toLowerCase();
  60.  
  61.         String result = "";
  62.         i = 0;
  63.         while (i < appNum) {
  64.             if (command.endsWith(appName[i])) {
  65.                 result += appName[i];
  66.             } else
  67.                 i++;
  68.  
  69.         }
  70.         JOptionPane.showMessageDialog(null, result);
  71.  
  72.         if (command.startsWith("turn on")) {
  73.  
  74.             if (appState[i] == true) {
  75.                 JOptionPane.showMessageDialog(null, "Sorry " + appName[i-1] + " is already on.");
  76.             } else {
  77.                 JOptionPane.showMessageDialog(null, appName[i-1] + " is now on.");
  78.             }
  79.         }
  80.  
  81.         else if (command.startsWith("turn off")) {
  82.             if (appState[i] == false) {
  83.                 JOptionPane.showMessageDialog(null, "Sorry " + appName[i-1] + " is already off.");
  84.             } else {
  85.                 JOptionPane.showMessageDialog(null, appName[i-1] + " is now off.");
  86.             }
  87.         } else {
  88.             JOptionPane.showInputDialog(null, "Sorry, you must type turn on or turn off.");
  89.         }
  90.         }
  91.         JOptionPane.showMessageDialog(null, "See you next time!");
  92.     }
  93.  
  94. }
  95. //"Please say turn on or off for the following: " + message
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top