Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class homeAutoBot {
- public static void main(String[] args) {
- // I will approach Assignment 1 by working out what I can do with my current
- // knowledge.
- // Then I will slowly add to it over the next 3 weeks to create a fully
- // functioning autobot.
- // Introduction Messages
- JOptionPane.showMessageDialog(null, "Hello! I'm Fraizr the home automation bot.");
- JOptionPane.showMessageDialog(null, "Starting appliance input mode...");
- // Variables
- int appNum = 0;
- String command = "";
- int i = 0;
- //cancel goodbye
- while (command != null) {
- // user input # of appliances
- appNum = Integer.parseInt(JOptionPane.showInputDialog("How many applliances?"));
- while (appNum<=0) {
- appNum = Integer.parseInt(JOptionPane.showInputDialog("How many appliances (1 or more)?"));
- }
- // Creating array for entering appliances
- String[] appName = new String[appNum];
- boolean[] appState = new boolean[appNum];
- while (i < appNum) {
- appName[i] = JOptionPane.showInputDialog("Enter appliance number " + (i + 1) + " of " + appNum);
- i++;
- }
- i = 0;
- String message = "";
- // creates output so user can see apps and states
- while (i < appNum) {
- message += appName[i];
- if (appState[i] == false) {
- message += " (off), ";
- } else {
- message += " (on), ";
- }
- i++;
- }
- // List Appliances and ask for input
- command = JOptionPane.showInputDialog(null, "Avaliable Appliances: " + message);
- command = command.toLowerCase();
- String result = "";
- i = 0;
- while (i < appNum) {
- if (command.endsWith(appName[i])) {
- result += appName[i];
- } else
- i++;
- }
- JOptionPane.showMessageDialog(null, result);
- if (command.startsWith("turn on")) {
- if (appState[i] == true) {
- JOptionPane.showMessageDialog(null, "Sorry " + appName[i-1] + " is already on.");
- } else {
- JOptionPane.showMessageDialog(null, appName[i-1] + " is now on.");
- }
- }
- else if (command.startsWith("turn off")) {
- if (appState[i] == false) {
- JOptionPane.showMessageDialog(null, "Sorry " + appName[i-1] + " is already off.");
- } else {
- JOptionPane.showMessageDialog(null, appName[i-1] + " is now off.");
- }
- } else {
- JOptionPane.showInputDialog(null, "Sorry, you must type turn on or turn off.");
- }
- }
- JOptionPane.showMessageDialog(null, "See you next time!");
- }
- }
- //"Please say turn on or off for the following: " + message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement