cart:
package com.xiong.cart.entity;
import com.xiong.cart.ProductCodeEnums;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Cart {
@Override
public String toString() {
return "Cart{" +
"totalPrice=" + totalPrice +
", score=" + score +
", list=" + list +
'}';
}
private Double totalPrice=0.0;
private Integer score=0;
private List<Product> list=new ArrayList<>();
public Double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(Double totalPrice) {
this.totalPrice = totalPrice;
}
public void addPrice(Double price) {
this.totalPrice += price;
}
public void addScore(Integer score) {
this.score += score;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public List<Product> getList() {
return list;
}
public void setList(List<Product> list) {
this.list = list;
}
public void addProduct(Product product){
this.list.add(product);
}
}
product:
package com.xiong.cart.entity;
import com.xiong.cart.ProductCodeEnums;
public class Product {
private String productName;
private ProductCodeEnums productCode;
private Double price;
private Integer number;
public Product(String productName, ProductCodeEnums productCode, Double price, Integer number) {
this.productName = productName;
this.productCode = productCode;
this.price = price;
this.number = number;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public ProductCodeEnums getProductCode() {
return productCode;
}
public void setProductCode(ProductCodeEnums productCode) {
this.productCode = productCode;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
ProductCodeEnums:
package com.xiong.cart;
public enum ProductCodeEnums {
DIS_10,
DIS_20,
BUY_2_GET_1
}
MainTest:
package com.xiong.cart;
import com.xiong.cart.entity.Cart;
import com.xiong.cart.entity.Product;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;
public class MainTest {
public static void main(String[] args) {
//
Cart cart = new Cart();
ArrayList<Product> objects = new ArrayList<>();
objects.add(new Product("黄瓜",ProductCodeEnums.DIS_10,1.2,40));
objects.add(new Product("西红柿",ProductCodeEnums.DIS_10,2.0,20));
objects.add(new Product("方便面",ProductCodeEnums.BUY_2_GET_1,5.0,3));
objects.add(new Product("鸡蛋",ProductCodeEnums.BUY_2_GET_1,1.0,15));
objects.add(new Product("苹果",ProductCodeEnums.DIS_20,2.0,22));
cart.setList(objects);
count(cart);
System.out.println(cart.toString());
}
public static void count (Cart cart){
List<Product> list = cart.getList();
cart.getScore();
for(Product product:list){
ProductCodeEnums productCode = product.getProductCode();
if(productCode==ProductCodeEnums.BUY_2_GET_1){
int i = product.getNumber() - (product.getNumber() / 3);
double v = i * product.getPrice();
cart.addPrice(v);
}else if(productCode==ProductCodeEnums.DIS_10){
double v = product.getPrice() * product.getNumber() * 0.9;
cart.addPrice(v);
cart.addScore(((int)v/10));
}else if(productCode==ProductCodeEnums.DIS_20){
double v = product.getPrice() * product.getNumber() * 0.8;
cart.addPrice(v);
cart.addScore(((int)v/20));
}
}
}
}