Java tutorial
/* * #%L * GarethHealy :: Dune World :: Sandworms * %% * Copyright (C) 2013 - 2015 Gareth Healy * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ package com.garethahealy.duneworld.sandworms.services; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.UUID; import com.garethahealy.duneworld.sandworms.entities.LongitudeAndLatitude; import com.garethahealy.duneworld.sandworms.entities.Sandworm; import org.joda.time.DateTime; public class SandwormService { private ArrakisService arrakisService; private List<Sandworm> worms; private Random wormSelectorRand; public SandwormService(ArrakisService arrakisService) { this.arrakisService = arrakisService; this.worms = new ArrayList<Sandworm>(); this.wormSelectorRand = new Random(DateTime.now().getMillis()); } public void start() { } public void stop() { this.worms.clear(); this.worms = null; } public Sandworm createWorm() { LongitudeAndLatitude cords = arrakisService.getLongitudeAndLatitude(); Sandworm worm = new Sandworm(); worm.setId(UUID.randomUUID()); worm.setLatitude(cords.getLatitude()); worm.setLongitude(cords.getLongitude()); worms.add(worm); return worm; } public void generateSpice() { Integer max = this.worms.size() <= 1 ? 0 : this.worms.size() - 1; Integer index = max <= 0 ? 0 : wormSelectorRand.nextInt(max); if (index > 0) { Sandworm answer = this.worms.get(index); answer.incrementSpice(); } } }