package whiteship; import java.io.BufferedReader; import java.io.InputStreamReader; public class SpaceShipToy { /* * To build a spaceship, you need a capsule, a booster, three stages, and * two monkeys (borrowed from a Barrel of Monkeys). */ public Capsule capsule = new Capsule(); public Booster booster = new Booster(); public Stage[] stage = new Stage[3]; public SpaceShipToy() { for (int i = 0; i < 3; i++) stage[i] = new Stage(); } private void _killTime(int seconds) { if (seconds <= 0) return; for (int i = 0; i < seconds; i++) ; } static final int MINUTE = 60; static final int CAPSULE = 2 * MINUTE; static final int BOOSTER = 5 * MINUTE; static final int STAGE = 3 * MINUTE; static final int MONKEY = 10 * MINUTE; class Capsule { public Monkey chimp1 = new Monkey(); public Monkey chimp2 = new Monkey(); public Capsule() { System.out.println("Start building the capsule..."); _killTime(CAPSULE); chimp1.build(); chimp2.build(); System.out.println("Capsule complete."); } } class Booster { public Booster() { System.out.println("Start booster..."); _killTime(BOOSTER); System.out.println("Blast off."); } } class Stage { public Stage() { System.out.println("Start stage..."); _killTime(STAGE); System.out.println("Stage complete."); } } class Monkey { public void start() { System.out.println("Start the monkey business..."); } public void build() { start(); _killTime(MONKEY); finish(); } public void finish() { System.out.println("Monkey business complete."); } } public static void main(String[] args) throws java.io.IOException { final int NUMBERTOYS = 9; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); SpaceShipToy[] toys = new SpaceShipToy[NUMBERTOYS]; String input = in.readLine().trim(); System.out.println("Toy factory is up and running..."); System.out.flush(); for (int i = 0; i < NUMBERTOYS; i++) toys[i] = null; while (!input.equalsIgnoreCase("q")) { if (input == null || input.length() != 1 || !Character.isDigit(input.charAt(0))) { System.err.println("Unknown option. Try 0-9, q"); input = in.readLine().trim(); continue; } int number = Integer.valueOf(input).intValue(); if (number == 9) { new SpaceShipToy(); System.out.println("Whoops... Lost one..."); } else { if (toys[number] != null) { System.out.println("Shipping toy # " + number); toys[number] = null; } else { System.out.println("Building toy # " + number); toys[number] = new SpaceShipToy(); } } input = in.readLine().trim(); } } }