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.