📄 cart.java
字号:
/*
* Copyright 2006 Borys Burnayev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rdacorp.petstore.domain;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author Borys Burnayev
*/
public class Cart {
private List<OrderItem> items;
public Cart() {
items = new ArrayList<OrderItem>(10);
}
public List getItems() {
return items;
}
public void addItem(OrderItem item) {
if (items.contains(item))
return;
items.add(item);
}
public void removeItem(OrderItem item) {
items.remove(item);
}
public void updateQuantity(OrderItem item, long quantity) {
item.setQuantity(quantity);
}
public BigDecimal getTotal() {
BigDecimal total = new BigDecimal(0);
for (int i = 0; i < items.size(); i++) {
OrderItem orderItem = (OrderItem) items.get(i);
total = total.add(orderItem.getExtendedPrice());
}
return total;
}
public String toString() {
String cart = "Cart:\n";
for (int i = 0; i < items.size(); i++) {
cart.concat(items.get(i).toString());
}
return cart;
}
public OrderItem findOrderItemByItemCode(String code) {
for (Iterator ii = items.iterator(); ii.hasNext();) {
OrderItem orderItem = (OrderItem) ii.next();
if (orderItem.getItem().getCode().equals(code))
return orderItem;
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -