/** * Creates an integer random array with numbers in the interval [0; max]. * * @param len length of the array. * @param max maximum random value. * @return the array */ static int[] randomArray(int len, int max) { assert len >= 0; assert max >= 0; int[] result = new int[len]; for (int i = 0; i < len; i++) result[i] = (int) (Math.random() * (max + 1)); return result; }
/** * Gives random available port in the given range. * * @param from if <=0 then default value 49152 is used * @param to if <=0 then default value 65535 is used */ protected int randomLocalPort(int from, int to){ from = (from <=0) ? 49152 : from; to = (to <= 0) ? 65535 : to; while(true){ int candidate = (int) ((Math.random() * (to-from)) + from); if(isFreePort(candidate)){ return candidate; } System.out.println(String.format("Port %s is in use", candidate)); } }
private void scannerLoop() throws ParseException { Scanner reader = new Scanner(in); String line = reader.nextLine(); while (!line.equals("startScheduling")) { String[] params = line.split("\\(|,( )*|\\)"); switch (params[0]) { case "create": if (params.length == 4) { User temp = find(params[1]); if (temp == null) { User user = new User(params[1], Integer.parseInt(params[2]), Boolean.parseBoolean(params[3])); users.add(user); } else { out.print("Error: user with this name already exist"); } } else { out.print("Error: wrong arguments"); } break; case "modify": if (params.length == 4) { User temp = find(params[1]); if (!(temp == null)) { temp.setName(params[1]); temp.setTimezone(Integer.parseInt(params[2])); temp.setStatus(Boolean.parseBoolean(params[3])); } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "addEvent": if (params.length == 4) { User temp = find(params[1]); if (!(temp == null)) { Pattern pattern = Pattern.compile( "(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[012])\\.((19|20)\\d\\d)\\-([01]?[0-9]|2[0-3])\\:[0-5][0-9]\\:[0-5][0-9]"); Matcher matcher = pattern.matcher(params[3]); if (matcher.matches()) { DateFormat df = new SimpleDateFormat("dd.MM.yyyy'-'HH:mm:ss"); Date result = df.parse(params[3]); result.setTime( result.getTime() + (defaultTimezone - temp.getTimezone()) * 3600 * 1000); Event event = new Event(result, params[2]); temp.addEvent(event); } else { out.print("Error: wrong dateformat"); } } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "addRandomTimeEvent": if (params.length == 5) { User temp = find(params[1]); if (!(temp == null)) { Pattern pattern = Pattern.compile( "(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[012])\\.((19|20)\\d\\d)\\-([01]?[0-9]|2[0-3])\\:[0-5][0-9]\\:[0-5][0-9]"); Matcher matcherFrom = pattern.matcher(params[3]); Matcher matcherTo = pattern.matcher(params[4]); if (matcherFrom.matches() && matcherTo.matches()) { long dateFrom = Timestamp.valueOf(params[3]).getTime(); long dateTo = Timestamp.valueOf(params[4]).getTime(); long diff = dateFrom - dateTo + 1; Event event = new Event(new Date(dateFrom + (long) (Math.random() * diff)), params[2]); temp.addEvent(event); } else { out.print("Error: wrong dateformat"); } } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "removeEvent": if (params.length == 3) { User temp = find(params[1]); if (!(temp == null)) { temp.removeEvent(params[2]); } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "cloneEvent": if (params.length == 4) { User temp = find(params[1]); User tempTo = find(params[3]); if (!(temp == null) && !(tempTo == null)) { Event event = temp.findEvent(params[2]); tempTo.addEvent(event); } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "showInfo": if (params.length == 2) { User temp = find(params[1]); if (!(temp == null)) { temp.showInfo(); } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; } line = reader.nextLine(); } }