Android Open Source - NerdZoo Zoo






From Project

Back to project page NerdZoo.

License

The source code is released under:

Apache License

If you think the Android project NerdZoo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bignerdranch.android.nerdzoo.model;
/*from  w  ww  . j a  v a2 s  . c o m*/
import com.bignerdranch.android.nerdzoo.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class Zoo {

    private static final int TYPES_OF_ANIMALS = 6;

    private List<Animal> mAnimalList;

    @Inject
    public Zoo() {
        setupInitialList();
    }

    private void setupInitialList() {
        mAnimalList = new ArrayList<Animal>();
    }

    public Animal findAnimalById(UUID id) {
        for (Animal animal : mAnimalList) {
            if (id.equals(animal.getId())) {
                return animal;
            }
        }
        return null;
    }

    public int findPositionById(UUID id) {
        for (int i = 0; i < mAnimalList.size(); i++) {
            Animal animal = mAnimalList.get(i);
            if (id.equals(animal.getId())) {
                return i;
            }
        }
        return 0;
    }

    public int size() {
        return mAnimalList.size();
    }

    public Animal get(int position) {
        return mAnimalList.get(position);
    }

    public void add() {
        mAnimalList.add(generateRandomAnimal());
    }

    public void remove(int position) {
        mAnimalList.remove(position);
    }

    public void clear() {
        mAnimalList.clear();
    }

    public static int getTypesOfAnimals() {
        return TYPES_OF_ANIMALS;
    }

    private Animal generateRandomAnimal() {
        Random r = new Random();
        int random = r.nextInt(TYPES_OF_ANIMALS) + 1;
        switch (random) {
            case 1:
                return new Animal(UUID.randomUUID(), R.string.animal_lion, R.string.animal_lion_description, R.drawable.animal_lion);
            case 2:
                return new Animal(UUID.randomUUID(), R.string.animal_tiger, R.string.animal_tiger_description, R.drawable.animal_tiger);
            case 3:
                return new Animal(UUID.randomUUID(), R.string.animal_cheetah, R.string.animal_cheetah_description, R.drawable.animal_cheetah);
            case 4:
                return new Animal(UUID.randomUUID(), R.string.animal_leopard, R.string.animal_leopard_description, R.drawable.animal_leopard);
            case 5:
                return new Animal(UUID.randomUUID(), R.string.animal_lynx, R.string.animal_lynx_description, R.drawable.animal_lynx);
            case 6:
                return new Animal(UUID.randomUUID(), R.string.animal_cat, R.string.animal_cat_description, R.drawable.animal_cat);
            default:
                return null;
        }
    }

}




Java Source Code List

com.bignerdranch.android.nerdzoo.ApplicationTest.java
com.bignerdranch.android.nerdzoo.BaseApplication.java
com.bignerdranch.android.nerdzoo.BaseModule.java
com.bignerdranch.android.nerdzoo.anim.BaseItemAnimator.java
com.bignerdranch.android.nerdzoo.anim.PathAnimator.java
com.bignerdranch.android.nerdzoo.anim.RevealAnimator.java
com.bignerdranch.android.nerdzoo.anim.ZooItemAnimator.java
com.bignerdranch.android.nerdzoo.base.BaseActivity.java
com.bignerdranch.android.nerdzoo.base.BaseDrawerActivity.java
com.bignerdranch.android.nerdzoo.base.BaseNormalActivity.java
com.bignerdranch.android.nerdzoo.controller.AboutFragment.java
com.bignerdranch.android.nerdzoo.controller.AnimalActivity.java
com.bignerdranch.android.nerdzoo.controller.AnimalFragment.java
com.bignerdranch.android.nerdzoo.controller.MainActivity.java
com.bignerdranch.android.nerdzoo.controller.ZooFragment.java
com.bignerdranch.android.nerdzoo.drawer.DrawerAdapter.java
com.bignerdranch.android.nerdzoo.drawer.DrawerItem.java
com.bignerdranch.android.nerdzoo.model.Animal.java
com.bignerdranch.android.nerdzoo.model.Zoo.java
com.bignerdranch.android.nerdzoo.util.BuildUtils.java