Package com.example.bookstore.domain

Source Code of com.example.bookstore.domain.Cart

package com.example.bookstore.domain;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
* Basic implementation of a cart.
*
*
*
*
*/
public class Cart implements Serializable {

    private Map<Book, Integer> books = new HashMap<Book, Integer>();

    public Map<Book, Integer> getBooks() {
        return Collections.unmodifiableMap(this.books);
    }

    public void addBook(Book book) {
        if (this.books.containsKey(book)) {
            int quantity = this.books.get(book);
            quantity++;
            this.books.put(book, quantity);
        } else {
            this.books.put(book, 1);
        }
    }

    public void removeBook(Book book) {
        this.books.remove(book);
    }

    public void clear() {
        this.books.clear();
    }

    @Override
    public String toString() {
        ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
        builder.append("books", this.books.keySet());
        return builder.build();
    }

}
TOP

Related Classes of com.example.bookstore.domain.Cart

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.