private Collection<Library> allocateBooksForLibraries(final String book, final int quantity,
			final Collection<Library> allLibrariesToBeAllocated) {

		final Collection<Library> allocatedLibraries = new HashSet<Library>();

		int remaningQty = quantity;

		for (Library library : allLibrariesToBeAllocated) {

			if (library.getWeighting() != null && library.getWeighting() > 0) {

				int noOfBooksToBeAllocated = (int) (quantity * (library.getWeighting() * VALUE_HUNDRED_PERCENT));

				// if there are books to be allocated and the weighting is low.
				// Then, allocate books for less priority libraries
				if (noOfBooksToBeAllocated == VALUE_ZERO) {
					noOfBooksToBeAllocated = VALUE_ONE;
				}

				// If the book is already in the library,add the book
				if (library.getAllocations().containsKey(book)) {
					BookAllocation allocatedBook = library.getAllocations().get(book);
					allocatedBook.setAllocatedQuantity(allocatedBook.getAllocatedQuantity() + noOfBooksToBeAllocated);
				} else {
					// New Book
					final BookAllocation newAllocation = new BookAllocation();
					newAllocation.setTitle(book);
					newAllocation.setAllocatedQuantity(noOfBooksToBeAllocated);
					library.getAllocations().put(book, newAllocation);
				}

				allocatedLibraries.add(library);
				remaningQty = remaningQty - noOfBooksToBeAllocated;

				// If the books are less than the libraries, exit.
				if (remaningQty == VALUE_ZERO) {
					break;
				}
			}

		}

		// If there is a remainder allocate one each for highest priority.
		if (validateQuantity(remaningQty)) {

			for (Library library : allLibrariesToBeAllocated) {
				if (remaningQty == VALUE_ZERO) {
					break;
				}
				remaningQty--;

				final BookAllocation allocatedBook = library.getAllocations().get(book);
				allocatedBook.setAllocatedQuantity(allocatedBook.getAllocatedQuantity() + VALUE_ONE);
				allocatedLibraries.add(library);
			}
		}

		return allocatedLibraries;
	}