001//@formatter:off
002/*
003 * Abstract Voucher
004 * Code-Beispiel zum Buch Patterns Kompakt, Verlag Springer Vieweg
005 * Copyright 2014 Karl Eilebrecht
006 * 
007 * Licensed under the Apache License, Version 2.0 (the "License"):
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019//@formatter:on
020package de.calamanari.pk.factorymethod;
021
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025/**
026 * Abstract Voucher is the abstract product in this FACTORY METHOD example scenario.
027 * 
028 * @author <a href="mailto:Karl.Eilebrecht(a/t)calamanari.de">Karl Eilebrecht</a>
029 */
030public abstract class AbstractVoucher {
031
032    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractVoucher.class);
033
034    /**
035     * The last name of the voucher's owner
036     */
037    private String lastName;
038
039    /**
040     * The first name of the voucher's owner
041     */
042    private String firstName;
043
044    /**
045     * The voucher's value
046     */
047    private double value;
048
049    /**
050     * Creates a new voucher
051     * 
052     * @param firstName person's first name
053     * @param lastName person's last name
054     * @param value the vouchers value
055     */
056    public AbstractVoucher(String firstName, String lastName, double value) {
057        if (firstName == null || firstName.trim().length() == 0 || lastName == null || lastName.trim().length() == 0 || value <= 0) {
058            throw new IllegalArgumentException("Arguments first name and last name " + "must be specified, value must be > 0 (given: firstName=" + firstName
059                    + ", lastName=" + lastName + ", value=" + value + ").");
060        }
061        this.firstName = firstName;
062        this.lastName = lastName;
063        this.value = value;
064    }
065
066    /**
067     * Returns the last name of the voucher's owner
068     * 
069     * @return last name
070     */
071    public String getLastName() {
072        return lastName;
073    }
074
075    /**
076     * Sets the last name of the voucher's owner
077     * 
078     * @param lastName name of the owner
079     */
080    public void setLastName(String lastName) {
081        this.lastName = lastName;
082    }
083
084    /**
085     * Returns the first name of the voucher's owner.
086     * 
087     * @return first name of the owner
088     */
089    public String getFirstName() {
090        return firstName;
091    }
092
093    /**
094     * Sets the first name of the voucher's owner
095     * 
096     * @param firstName first name of the owner
097     */
098    public void setFirstName(String firstName) {
099        this.firstName = firstName;
100    }
101
102    /**
103     * Returns the value of this voucher
104     * 
105     * @return value voucher's value
106     */
107    public double getValue() {
108        return value;
109    }
110
111    /**
112     * Sets the value of this voucher
113     * 
114     * @param value vouchers value
115     */
116    public void setValue(double value) {
117        this.value = value;
118    }
119
120    /**
121     * Returns this voucher's ID
122     * 
123     * @return id of voucher
124     */
125    public abstract String getId();
126
127    /**
128     * Returns the human readable code of this voucher, the user will enter when applying it to an order.
129     * 
130     * @return display code
131     */
132    public abstract String getVoucherDisplayCode();
133
134    @Override
135    public String toString() {
136        LOGGER.debug("toString() on {} called.", this.getClass().getSimpleName());
137        return this.getClass().getSimpleName() + "({id=" + this.getId() + ", displayCode=" + this.getVoucherDisplayCode() + ", firstName=" + this.getFirstName()
138                + ", lastName=" + this.getLastName() + ", value=" + this.getValue() + "})";
139    }
140}