├── .gitignore ├── README ├── pom.xml └── src ├── test └── java │ └── at │ └── jku │ └── oracle │ └── OracleTest.java └── main └── java └── at └── jku └── oracle └── Oracle.java /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .classpath 3 | .project 4 | bin 5 | target 6 | .settings -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Java application to help Students at JKU with the decision where to have lunch. If invoked without arguments then a predefined list of restaurants near the campus of the JKU is chosen by the "Oracle". However, if none of the predefined places fits your needs (or taste), alternative places can be passed as arguments, from which one is then chosen by the "Oracle". -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | at.jku 5 | oracle 6 | 0.0.1-SNAPSHOT 7 | lunch-oracle 8 | 9 | 10 | commons-cli 11 | commons-cli 12 | 1.2 13 | 14 | 15 | junit 16 | junit 17 | 4.8.2 18 | test 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/test/java/at/jku/oracle/OracleTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.oracle; 2 | 3 | import java.util.Arrays; 4 | 5 | import junit.framework.TestCase; 6 | import at.jku.oracle.Oracle.Restaurant; 7 | 8 | public class OracleTest extends TestCase { 9 | 10 | private Oracle oracle; 11 | 12 | public void testHelp() { 13 | 14 | String gnirts = oracle.ask("-h"); 15 | assertNull(gnirts); 16 | 17 | String tanga = oracle.ask("-help"); 18 | assertNull(tanga); 19 | } 20 | 21 | public void testWithNoArgs() { 22 | String chosenRestaurant = oracle.ask(); 23 | assertNotNull(chosenRestaurant); 24 | 25 | try { 26 | Restaurant restaurant = Restaurant.valueOf(chosenRestaurant); 27 | assertNotNull(restaurant); 28 | } catch (EnumConstantNotPresentException ecnpe) { 29 | fail("should not raise an exception"); 30 | } 31 | } 32 | 33 | public void testWithNogsArg() { 34 | 35 | String chosenRestaurant = oracle.ask("-nogs"); 36 | assertNotNull(chosenRestaurant); 37 | assertEquals(Restaurant.A2ChinaRestaurant.name(), chosenRestaurant); 38 | 39 | } 40 | 41 | public void testWithOwnRestaurants() { 42 | String chosenRestaurant = oracle.ask("-a", "uno", "dos", "tres"); 43 | assertNotNull(chosenRestaurant); 44 | 45 | String[] restaurants = new String[] { "uno", "dos", "tres" }; 46 | Arrays.sort(restaurants); 47 | int index = Arrays.binarySearch(restaurants, chosenRestaurant); 48 | 49 | assertTrue(index >= 0); 50 | assertEquals(restaurants[index], chosenRestaurant); 51 | } 52 | 53 | @Override 54 | protected void setUp() throws Exception { 55 | super.setUp(); 56 | 57 | oracle = new Oracle(); 58 | } 59 | 60 | @Override 61 | protected void tearDown() throws Exception { 62 | super.tearDown(); 63 | 64 | oracle = null; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/at/jku/oracle/Oracle.java: -------------------------------------------------------------------------------- 1 | package at.jku.oracle; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Random; 6 | 7 | import org.apache.commons.cli.CommandLine; 8 | import org.apache.commons.cli.CommandLineParser; 9 | import org.apache.commons.cli.GnuParser; 10 | import org.apache.commons.cli.HelpFormatter; 11 | import org.apache.commons.cli.Option; 12 | import org.apache.commons.cli.OptionBuilder; 13 | import org.apache.commons.cli.Options; 14 | import org.apache.commons.cli.ParseException; 15 | 16 | public class Oracle { 17 | 18 | public static enum Restaurant { 19 | McDonalds, A2ChinaRestaurant, JKUMensa, KHGMensa 20 | } 21 | 22 | public static void main(String[] args) { 23 | 24 | Oracle oracle = new Oracle(); 25 | String choice = oracle.ask(args); 26 | 27 | System.out.println("Today's best choice is: " + choice); 28 | 29 | } 30 | 31 | private final Options options; 32 | private final Random r; 33 | private final String[] defaultRestaurants; 34 | 35 | private final Option nogsOpt; 36 | private final Option ownRestaurantsOpt; 37 | private final Option helpOpt; 38 | 39 | @SuppressWarnings("static-access") 40 | public Oracle() { 41 | super(); 42 | 43 | r = new Random(); 44 | 45 | Restaurant[] values = Restaurant.values(); 46 | List restaurantNames = new ArrayList(values.length); 47 | for (Restaurant restaurant : values) { 48 | restaurantNames.add(restaurant.name()); 49 | } 50 | defaultRestaurants = restaurantNames.toArray(new String[values.length]); 51 | 52 | nogsOpt = OptionBuilder.withDescription("let the mighty nogs decide").create("nogs"); 53 | ownRestaurantsOpt = OptionBuilder.withDescription("alternative restaurants").hasOptionalArgs().create("a"); 54 | helpOpt = OptionBuilder.withLongOpt("help").withDescription("print this help").create("h"); 55 | 56 | options = new Options(); 57 | options.addOption(ownRestaurantsOpt); 58 | options.addOption(nogsOpt); 59 | options.addOption(helpOpt); 60 | } 61 | 62 | public String ask(String... args) { 63 | // create the parser 64 | CommandLineParser parser = new GnuParser(); 65 | try { 66 | // parse the command line arguments 67 | CommandLine line = parser.parse(options, args); 68 | 69 | if (line.hasOption(nogsOpt.getOpt())) { 70 | return Restaurant.A2ChinaRestaurant.name(); 71 | } else if (line.hasOption(ownRestaurantsOpt.getOpt())) { 72 | String[] restaurants = line.getOptionValues(ownRestaurantsOpt.getOpt()); 73 | return selectRandom(restaurants); 74 | } else if (line.hasOption(helpOpt.getOpt()) || line.hasOption(helpOpt.getLongOpt())) { 75 | // automatically generate the help statement 76 | HelpFormatter formatter = new HelpFormatter(); 77 | String cmd = "java " + Oracle.class.getName(); 78 | String header = "providing no arguments let the oracle choose from the predefined arguments"; 79 | formatter.printHelp(cmd, header, options, null); 80 | return null; 81 | } else { 82 | return selectRandom(defaultRestaurants); 83 | } 84 | 85 | } catch (ParseException exp) { 86 | throw new IllegalStateException("Parsing failed. Reason: " + exp.getMessage(), exp); 87 | } 88 | } 89 | 90 | private String selectRandom(String[] args) { 91 | return args[r.nextInt(args.length)]; 92 | } 93 | } 94 | --------------------------------------------------------------------------------