001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.tctest.spring.integrationtests.tests.sellitem;
017:
018: import java.io.Serializable;
019:
020: import org.springframework.core.style.ToStringCreator;
021:
022: public class Sale implements Serializable {
023:
024: private double price;
025:
026: private int itemCount;
027:
028: private String category;
029:
030: private boolean shipping;
031:
032: private String shippingType;
033:
034: public String getCategory() {
035: return category;
036: }
037:
038: public void setCategory(String category) {
039: this .category = category;
040: }
041:
042: public int getItemCount() {
043: return itemCount;
044: }
045:
046: public void setItemCount(int itemCount) {
047: this .itemCount = itemCount;
048: }
049:
050: public double getPrice() {
051: return price;
052: }
053:
054: public void setPrice(double price) {
055: this .price = price;
056: }
057:
058: public boolean isShipping() {
059: return shipping;
060: }
061:
062: public void setShipping(boolean shipping) {
063: this .shipping = shipping;
064: }
065:
066: public String getShippingType() {
067: return shippingType;
068: }
069:
070: public void setShippingType(String shippingType) {
071: this .shippingType = shippingType;
072: }
073:
074: // business logic methods
075:
076: /**
077: * Returns the base amount of the sale, without discount or delivery costs.
078: */
079: public double getAmount() {
080: return price * itemCount;
081: }
082:
083: /**
084: * Returns the discount rate to apply.
085: */
086: public double getDiscountRate() {
087: double discount = 0.02;
088: if ("A".equals(category)) {
089: if (itemCount >= 100) {
090: discount = 0.1;
091: }
092: } else if ("B".equals(category)) {
093: if (itemCount >= 200) {
094: discount = 0.2;
095: }
096: }
097: return discount;
098: }
099:
100: /**
101: * Returns the savings because of the discount.
102: */
103: public double getSavings() {
104: return getDiscountRate() * getAmount();
105: }
106:
107: /**
108: * Returns the delivery cost.
109: */
110: public double getDeliveryCost() {
111: double delCost = 0.0;
112: if ("S".equals(shippingType)) {
113: delCost = 10.0;
114: } else if ("E".equals(shippingType)) {
115: delCost = 20.0;
116: }
117: return delCost;
118: }
119:
120: /**
121: * Returns the total cost of the sale, including discount and delivery cost.
122: */
123: public double getTotalCost() {
124: return getAmount() + getDeliveryCost() - getSavings();
125: }
126:
127: public String toString() {
128: return new ToStringCreator(this ).append("price", price).append(
129: "itemCount", itemCount).toString();
130: }
131: }
|