Back to project page ZorbsCity.
The source code is released under:
GNU General Public License
If you think the Android project ZorbsCity listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jonathan.geoffroy.zorbscity.view.shell; //from w w w . ja va 2 s . co m import java.util.Scanner; import jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Road; import jonathan.geoffroy.zorbscity.model.cityObjects.buildings.houses.FreeHouse; import jonathan.geoffroy.zorbscity.model.helpers.Coord2D; import jonathan.geoffroy.zorbscity.view.CityView; public class CityShellView extends CityView { public static final int ROAD = 0, FREEHOUSE = 1; public CityShellView(String cityName) { super(cityName); } @Override public void run() { boolean continued = true; Scanner scanner = new Scanner(System.in); String userIn; String[] userSplitting; String userCommand; while (continued) { userIn = scanner.nextLine(); userSplitting = userIn.split(" "); userCommand = userSplitting[0]; if(userCommand.equals("add")) { add_command(userSplitting); } else if(userCommand.equals("rm")) { rm_command(userSplitting); } else if(userCommand.equals("show")) { show_command(); } else if(userCommand.equals("next")) { next_command(userSplitting); } else if(userCommand.equals("money")) { money_command(userSplitting); } else if (userCommand.equals("help")) { help_command(); } else { help_command(); } } scanner.close(); } /** * Command to show the help menu */ private void help_command() { System.out.println("add [buildingType] [x][y]"); System.out.println("rm [x][y]"); System.out.println("show"); System.out.println("next [nbSteps]"); System.out.println("money"); System.out.println("money [amountToAdd]"); } private void money_command(String[] args) { if(args.length == 1) { System.out.println("" + city.getMoney()); } else { try { int moneyToAdd = Integer.parseInt(args[1]); city.addMoney(moneyToAdd); } catch (NumberFormatException e) { e.printStackTrace(); } } } /** * command to reach the next step of the simulator * @param args nothing, to do just 1 step, or a number to reach _number_ steps */ private void next_command(String[] args) { int nbSteps; if(args.length == 1) { nbSteps = 1; } else { try { nbSteps = Integer.parseInt(args[1]); } catch (NumberFormatException e) { nbSteps = 1; } } for(int i = 0; i < nbSteps; i++) { simulator.nextStep(); } } /** * Command to show the city */ private void show_command() { for(int i = 0; i < city.getHeight(); i++) { for(int j = 0; j < city.getWidth(); j++) { System.out.print("" + String.format("%3d", city.getMapping(new Coord2D(j, i))) + " "); } System.out.println(); } } /** * Command to remove a Structure. * You must give the x & y coordinates of structure to remove * @param args x & y coordinates */ private void rm_command(String[] args) { if(args.length < 3) { System.out.println("add x y"); } else { try { int x = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); simulator.rm(new Coord2D(x, y)); } catch (NumberFormatException e) { e.printStackTrace(); } } } /** * Command to add a structure * You must give the structure number, and the x & y coordinates of the structure to add * @param args the structure number, x & y coordinates */ private void add_command(String[] args) { if(args.length < 4) { System.out.println("add x y structureType"); } else { try { int x = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); int structureType = Integer.parseInt(args[3]); switch(structureType) { case ROAD: simulator.add(new Road(), new Coord2D(x, y)); break; case FREEHOUSE: simulator.add(new FreeHouse(), new Coord2D(x, y)); break; } simulator.rm(new Coord2D(x, y)); } catch (NumberFormatException e) { e.printStackTrace(); } } } }