环境准备
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<root>
<category id="1000" name="Laptops" discount="20130701:0.99">
<saleDate date="20130630">
<product id="100012" cost="2900" saleCount="40" salePrice="4099">
ThinkPadE430_1
</product>
<product id="100013" cost="2900" saleCount="55" salePrice="3399">
LenovoG480A_1
</product>
<product id="100014" cost="2900" saleCount="63" salePrice="2900">
HaseeA488-Isd3_1
</product>
</saleDate>
<saleDate date="20130701">
<product id="100012" cost="2900" saleCount="48" salePrice="4099">
ThinkPadE430_1_1
</product>
<product id="100013" cost="2900" saleCount="55" salePrice="3399">
LenovoG480A_1_1
</product>
<product id="100014" cost="2900" saleCount="63" salePrice="2900">
HaseeA488-Isd3_1_1
</product>
</saleDate>
</category>
<category id="2000" name="CPU" discount="none">
<saleDate date="20130630">
<product id="100012" cost="2900" saleCount="40" salePrice="4099">
ThinkPadE430_2
</product>
<product id="100013" cost="2900" saleCount="55" salePrice="3399">
LenovoG480A
</product>
<product id="100014" cost="2900" saleCount="63" salePrice="2900">
HaseeA488-Isd3
</product>
</saleDate>
<saleDate date="20130701">
<product id="100012" cost="2900" saleCount="48" salePrice="4099">
ThinkPadE430
</product>
<product id="100013" cost="2900" saleCount="55" salePrice="3399">
LenovoG480A
</product>
<product id="100014" cost="2900" saleCount="63" salePrice="2900">
HaseeA488-Isd3
</product>
</saleDate>
</category>
</root>
实体类
public class Category {
private String id;
private String name;
private String discount;
public Category() {
}
public Category(String id, String name, String discount) {
this.id = id;
this.name = name;
this.discount = discount;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getDiscount() {
return discount;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDiscount(String discount) {
this.discount = discount;
}
@Override
public String toString() {
return "Category{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", discount='" + discount + '\'' +
'}';
}
}
public class Product {
private String id;
private String cost;
private String saleCount;
private String salePrice;
private Category category;
private String date;
public Product() {
}
public Product(String id, String cost, String saleCount, String salePrice, Category category, String date) {
this.id = id;
this.cost = cost;
this.saleCount = saleCount;
this.salePrice = salePrice;
this.category = category;
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCost() {
return cost;
}
public void setCost(String cost) {
this.cost = cost;
}
public String getSaleCount() {
return saleCount;
}
public void setSaleCount(String saleCount) {
this.saleCount = saleCount;
}
public String getSalePrice() {
return salePrice;
}
public void setSalePrice(String salePrice) {
this.salePrice = salePrice;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "Product{" +
"id='" + id + '\'' +
", cost='" + cost + '\'' +
", saleCount='" + saleCount + '\'' +
", salePrice='" + salePrice + '\'' +
", category=" + category +
", date='" + date + '\'' +
'}';
}
}
业务方法
public List<Product> getProducts() {
List<Product> list = new ArrayList<>();
try {
Reader resourceAsReader = Resources.getResourceAsReader("SalesRecords.xml");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(resourceAsReader);
Element rootElement = document.getRootElement();
Iterator<Element> elementIterator = rootElement.elementIterator();
int count = 0;
for (Iterator iterator = elementIterator; iterator.hasNext(); ) {
Element element = (Element) iterator.next();
Iterator<Element> elementIterator1 = element.elementIterator();
for (Iterator iterator1 = elementIterator1; iterator1.hasNext(); ) {
Element element1 = (Element) iterator1.next();
List<Element> elements = element1.elements();
Attribute attribute = element1.attribute(0);
for (Element element2 : elements) {
List<Attribute> list_Attributes = element2.attributes();
count++;
Product product = new Product();
for (int i = 0; i < list_Attributes.size(); i++) {
if (i == 0) product.setId(list_Attributes.get(i).getValue());
if (i == 1) product.setCost(list_Attributes.get(i).getValue());
if (i == 2) product.setSaleCount(list_Attributes.get(i).getValue());
if (i == 3) product.setSalePrice(list_Attributes.get(i).getValue());
if (attribute.getName().equals("date"))product.setDate(attribute.getValue());
if (count <=6){
product.setCategory(getCategorys().get(0));
} else {
product.setCategory(getCategorys().get(1));
}
}
list.add(product);
}
}
}
} catch (IOException | DocumentException ioException) {
throw new RuntimeException(ioException);
}
return list;
}
public List<Category> getCategorys() {
List<Category> list = new ArrayList<Category>();
try {
Reader resourceAsReader = Resources.getResourceAsReader("SalesRecords.xml");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(resourceAsReader);
Element rootElement = document.getRootElement();
List<Element> elements = rootElement.elements();
list = elements.stream().map(element -> {
Category category = new Category();
for (int i = 0; i < element.attributeCount(); i++) {
Attribute attribute = element.attribute(i);
String value = attribute.getValue();
if (i == 0) {
category.setId(value);
} else if (i == 1) {
category.setName(value);
} else if (i == 2) {
category.setDiscount(value);
}
}
return category;
}).collect(Collectors.toList());
} catch (IOException | DocumentException ioException) {
throw new RuntimeException(ioException);
}
return list;
}
计算每天的利润,并且转成json格式
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baizhi.entity.Product;
import com.baizhi.service.SalesRecordsService;
import com.baizhi.service.TotalService;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
@Service
public class TotalServiceImpl implements TotalService {
@Autowired
SalesRecordsService salesRecordsService;
@Override
public JSONObject totalArraySale() {
List<Product> products = salesRecordsService.getProducts();
double total_20130630 = 0.0;
double total_20130701 = 0.0;
double total_20130630_None = 0.0;
double total_20130701_None = 0.0;
JSONObject jsonObject = new JSONObject();
for (Product product : products) {
String discount = product.getCategory().getDiscount();
String saleCount = product.getSaleCount();
int count = Integer.parseInt(saleCount);
String costf = product.getCost();
int cost = Integer.parseInt(costf);
String salePricef = product.getSalePrice();
int salePrice = Integer.parseInt(salePricef);
if (product.getDate().equals("20130630")){
if (!discount.equals("none")){
String discountDigital = discount.substring(discount.indexOf(":")+1, discount.length());
total_20130630+=(salePrice-cost)*(1-Double.parseDouble(discountDigital))*count;
}else{
total_20130630_None+=(salePrice-cost)*count;
}
}else if (product.getDate().equals("20130701")){
if (!discount.equals("none")){
String discountDigital = discount.substring(discount.indexOf(":")+1, discount.length());
total_20130701+=(salePrice-cost)*(1-Double.parseDouble(discountDigital))*count;
}else{
total_20130701_None+=(salePrice-cost)*count;
}
}
}
BigDecimal bigDecimal = new BigDecimal(total_20130630 + total_20130630_None);
double sale_7 = new BigDecimal(total_20130701 + total_20130701_None).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
BigDecimal bigDecimal1 = bigDecimal.setScale(2, RoundingMode.HALF_EVEN);
double sale = bigDecimal1.doubleValue();
jsonObject.put("20130630",sale);
jsonObject.put("20130701",sale_7);
return jsonObject;
}
@Override
public JSON totalAllSale() {
JSONObject object = new JSONObject();
JSONObject jsonObject = totalArraySale();
Double o1 = (Double)jsonObject.get("20130630");
Double o2 = (Double)jsonObject.get("20130701");
long o3 = Math.round(o1 + o2);
object.put("fit",o3);
return object;
}
}