KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > integrationtests > tests > sellitem > Sale


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.tctest.spring.integrationtests.tests.sellitem;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.springframework.core.style.ToStringCreator;
21
22 public class Sale implements Serializable JavaDoc {
23
24     private double price;
25
26     private int itemCount;
27
28     private String JavaDoc category;
29
30     private boolean shipping;
31
32     private String JavaDoc shippingType;
33
34     public String JavaDoc getCategory() {
35         return category;
36     }
37
38     public void setCategory(String JavaDoc category) {
39         this.category = category;
40     }
41
42     public int getItemCount() {
43         return itemCount;
44     }
45
46     public void setItemCount(int itemCount) {
47         this.itemCount = itemCount;
48     }
49
50     public double getPrice() {
51         return price;
52     }
53
54     public void setPrice(double price) {
55         this.price = price;
56     }
57
58     public boolean isShipping() {
59         return shipping;
60     }
61
62     public void setShipping(boolean shipping) {
63         this.shipping = shipping;
64     }
65
66     public String JavaDoc getShippingType() {
67         return shippingType;
68     }
69
70     public void setShippingType(String JavaDoc shippingType) {
71         this.shippingType = shippingType;
72     }
73
74     // business logic methods
75

76     /**
77      * Returns the base amount of the sale, without discount or delivery costs.
78      */

79     public double getAmount() {
80         return price * itemCount;
81     }
82
83     /**
84      * Returns the discount rate to apply.
85      */

86     public double getDiscountRate() {
87         double discount = 0.02;
88         if ("A".equals(category)) {
89             if (itemCount >= 100) {
90                 discount = 0.1;
91             }
92         } else if ("B".equals(category)) {
93             if (itemCount >= 200) {
94                 discount = 0.2;
95             }
96         }
97         return discount;
98     }
99
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 JavaDoc toString() {
128         return new ToStringCreator(this).append("price", price).append(
129                 "itemCount", itemCount).toString();
130     }
131 }
Popular Tags