Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Assembling a Home System....

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Assembling a Home System....

    package edu.calstatela.cs201.tran.hw2;
     
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class DynamicMenu {
     
    	// configuration file
    	String deviceConfFilename = null;
    	String locationConfFilename = null;
    	String menuConfFilename = null;
     
    	// device.conf
    	String[] deviceType = null;
     
    	// location.conf
    	String[] location = null;
     
    	// menu.conf
    	String[] deviceName = null;
    	int[] deviceTypeId = null;
    	int[] locationId = null;
    	int[] Low = null;
    	int[] High = null;
    	int[] Step = null;
     
    	// states variable(s)
    	int[] deviceState = null;
     
    	// common scanner input
    	Scanner scanner = new Scanner(System.in);
     
    	public static void main(String[] args) {
    		new DynamicMenu().run(args);
    	}
     
    	void run(String args[]) {
    		parseArguments(args);
    		readConfigurationFiles();
     
    		boolean done = false;
    		while (!done) {
    			displayMainMenu();
     
    			int userSelection = getUserInput("Please select an option: ");
     
    			switch (userSelection) {
     
    			case 1:
    				listDeviceTypes();
    				break;
    			case 2:
    				listLocations();
    				break;
    			case 3:
    				listAllDevices();
    				break;
    			case 4:
    				listAllDevicesByLocations();
    				break;
    			case 5:
    				listAllDevicesByDeviceTypes();
    				break;
    			case 6:
    				changeDeviceSetting();
    				break;
    			case 7:
    				System.exit(0);
    				break;
    			default:
    				System.out.println("illegal selection");
    			}
     
    			displayState();
    		}
    	}
     
    	private void changeDeviceSetting() {
    		// TODO
    		// displayAllDevices
    		// input item to change (input should be one of the IDs)
    		// if item is on/off display: 1. one , 2. off
    		// if item is sliding: show value options between min and max -- take step into consideration.  For example: min=1 max=10 step=3 options = 1,4,7,10
    		// record change to state (update deviceState array)
    	}
     
    	private void listAllDevicesByLocations() {
    		// TODO for each location, list all devices belonging to that location
     
    	}
     
    	private void listAllDevicesByDeviceTypes() {
    		// TODO for each device type, list all devices of that device type
     
    	}
     
    	private void listAllDevices() {
    		// TODO display all device information
    		// FORMAT:
    		// device ID   Name   Type   Location   State
    	}
     
    	private void listLocations() {
    		// TODO display all locations
    		// FORMAT:
    		// location ID  Name
    	}
     
    	private void listDeviceTypes() {
    		System.out.println("==> list all device types <==");
    		for (int i = 0; i < deviceType.length; i++) {
    			System.out.println(i + " " + deviceType[i]);
    		}
    	}
     
    	private void displayState() {
    		// ** you need to implement this method **
     
    		// DISPLAY ALL DEVICES AND THEIR CURRENT STATES
    		// FORMAT:
    		// DEVICE ID    DEVICE NAME (DEVICE TYPE)     STATE
     
    		// FOR EXAMPLE:
    		// 0            Mesa (garage door)            0/1
    		// 1            SL1 (sliding-light)           25/100
    	}
     
    	private void displayMainMenu() {
    		System.out.println("*****************************************");
    		System.out.println("*  DYNAMIC MENU HOME AUTOMATION SYSTEM  *");
    		System.out.println("*                                       *");
    		System.out.println("* 1. LIST DEVICE TYPES                  *");
    		System.out.println("* 2. LIST LOCATIONS                     *");
    		System.out.println("* 3. LIST ALL DEVICES                   *");
    		System.out.println("* 4. LIST ALL DEVICES BY LOCATION       *");
    		System.out.println("* 5. LIST ALL DEVICES BY TYPE           *");
    		System.out.println("* 6. CHANGE DEVICE SETTING              *");
    		System.out.println("* 7. EXIT SYSTEM                        *");
    		System.out.println("*                                       *");
    		System.out.println("*****************************************");
    	}	
     
    	private int getUserInput(String message) {
     
    		try {
    			System.out.print(message);
    			int input = scanner.nextInt();
    			return input;
    		} catch (Exception e) {
    			System.out.println("illegal input");
    			return -1;
    		}
     
    	}
     
    	private void displayUsageAndExit() {
    		System.out.println("usage: DynamicMenu -device <DEVICE conf file> -location <LOCATION conf file> -menu <MENU conf file>");
    		System.exit(-1);
    	}
     
    	private void parseArguments(String[] args) {
     
    		try {
    			for (int i = 0; i < args.length; i++) {
    				if (args[i].equals("-device"))
    					deviceConfFilename = args[i + 1];
    				else if (args[i].equals("-location"))
    					locationConfFilename = args[i + 1];
    				else if (args[i].equals("-menu"))
    					menuConfFilename = args[i + 1];
    				else {
    					System.out.println("illegal option: " + args[i]);
    					displayUsageAndExit();
    				}
    				i = i + 1;
    			}
    		} catch (Exception e) {
    			System.out.println("illegal commandline argument");
    			displayUsageAndExit();
    		}
     
    		if (deviceConfFilename == null ||
    			locationConfFilename == null ||
    			menuConfFilename == null) {
    			System.out.println("all three options are required");
    			displayUsageAndExit();
    		}
     
    	}
     
    	private String[] readFile(String filename) {
    		ArrayList<String> myBuffer = new ArrayList<String>();
     
    		try {
    		    BufferedReader in = new BufferedReader(new FileReader(filename));
    		    String str;
    		    while ((str = in.readLine()) != null) {
    		    	str.replaceFirst("\\s+", ""); // replace all leading space with ""
    		        if (str.startsWith("#"))
    		        	continue;
    		        myBuffer.add(str);
    		    }
    		    in.close();
    		} catch (IOException e) {
    			System.out.println(e.getMessage());
    			System.exit(-1);
    		}
     
    		String[] buffer = new String[myBuffer.size()];
    		myBuffer.toArray(buffer);
     
    		return buffer;
     
    	}
     
    	private void readConfigurationFiles() {
    		// read DEVICE conf file
    		deviceType = readFile(deviceConfFilename);
     
    		// read LOCATION conf file
    		location = readFile(locationConfFilename);
     
    		// read MENU conf file
    		String[] tmpBuffer = readFile(menuConfFilename);
    		int menuSize = tmpBuffer.length;
     
    		// allocation these buffer
    		deviceName = new String[menuSize];
    		deviceTypeId = new int[menuSize];;
    		locationId = new int[menuSize];
    		Low = new int[menuSize];
    		High = new int[menuSize];
    		Step = new int[menuSize];
    		deviceState = new int[menuSize];
     
    		for (int i = 0; i < menuSize; i++) {
    			String[] cols = tmpBuffer[i].split("\\s+");
    			deviceName[i] = cols[0];
    			deviceTypeId[i] = Integer.parseInt(cols[1]);
    			locationId[i] = Integer.parseInt(cols[2]);
    			Low[i] = Integer.parseInt(cols[3]);
    			High[i] = Integer.parseInt(cols[4]);
    			Step[i] = Integer.parseInt(cols[5]);
    			deviceState[i] = Low[i]; // assuming everyhing is low
    		}
    	}
    }

    So far I have gotten this far. But I have no idea how to fill in the missing parts that I left // comments with.

    http://csula.web.pcwerk.com/wp/wp-co...homework02.pdf

    This is the given assignment. Could someone help me fill in the rest of the code. I'm missing quite a huge chunk I understand but this is as far as I can go. When I try to run it, It asks for the .conf files.

    The idea of the homework is that I will have a menu with the given options, Device Name, Device Location, Device on/off/percentage, and a list of each of these things. I got some of the code down but I'm not sure how to fill in the rest of it. Or how to input the .conf files in there

    These are the sample .conf files my teacher assigned.
    http://csula.web.pcwerk.com/wp/wp-co...vice.conf_.txt
    http://csula.web.pcwerk.com/wp/wp-co...tion.conf_.txt
    http://csula.web.pcwerk.com/wp/wp-co...menu.conf_.txt

    The teacher wanted the coding in that format so it is like that. I do understand there are other ways of doing this. Could someone please help~! Due in 6 hours....

    Thank you.
    Last edited by kclumsyxasian; November 7th, 2011 at 07:31 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Assembling a Home System....

    So you wnat us to read your assignment, read your butt-load of code and determine what you need as well as what gaps there are in your knowledge and understanding. All that with zero information from you.
    Quote Originally Posted by kclumsyxasian View Post
    Due in 6 hours....
    Good luck with that!
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Assembling a Home System....

    Edited before you posted >.< sorry!

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,342
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Assembling a Home System....

    The chances of anyone 'filling in the rest of the code' are slim, and in all honestly against forum rules. My suggestions: 1) read the 'getting help' link in my signature 2) Read this article 3) Prepare a bit more - due in 6 hours does not give yourself much breathing room.

Similar Threads

  1. Long term project work from home developer
    By internext in forum Paid Java Projects
    Replies: 17
    Last Post: May 2nd, 2012, 07:51 PM
  2. A place to call home
    By Tsarin in forum Member Introductions
    Replies: 1
    Last Post: October 14th, 2011, 07:56 AM
  3. home work ..
    By shane1987 in forum Collections and Generics
    Replies: 8
    Last Post: November 16th, 2010, 08:45 AM
  4. How to Get users home directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 0
    Last Post: September 2nd, 2010, 10:37 AM
  5. How to Get users home directory
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: September 2nd, 2010, 10:37 AM