Java练习demo 20190402 优惠券扣减

实体类:

package org.jimmy.autosearch2019.pojo;

import java.math.BigDecimal;

public class TestEntity20190402 {

    private Integer time;
    private BigDecimal discountMoney;
    
    public Integer getTime() {
        return time;
    }
    public void setTime(Integer time) {
        this.time = time;
    }
    public BigDecimal getDiscountMoney() {
        return discountMoney;
    }
    public void setDiscountMoney(BigDecimal discountMoney) {
        this.discountMoney = discountMoney;
    }
    
}

业务扣减类:

package org.jimmy.autosearch2019.test;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.jimmy.autosearch2019.pojo.TestEntity20190402;

/**
 * @author ラピスラズリ(Dawn)
 * @date 2019年4月2日 上午11:15:39
 * @detail 循环扣减demo
 */
public class Test20190402 {

    public static ArrayList<TestEntity20190402> list = new ArrayList<TestEntity20190402>(); 
    
    static {
        TestEntity20190402 testEntity = new TestEntity20190402();
        testEntity.setTime(1);
        testEntity.setDiscountMoney(new BigDecimal(100));
        list.add(testEntity);
        testEntity = new TestEntity20190402();
        testEntity.setTime(5);
        testEntity.setDiscountMoney(new BigDecimal(10));
        list.add(testEntity);
        testEntity = new TestEntity20190402();
        testEntity.setTime(3);
        testEntity.setDiscountMoney(new BigDecimal(20));
        list.add(testEntity);
        testEntity = new TestEntity20190402();
        testEntity.setTime(2);
        testEntity.setDiscountMoney(new BigDecimal(50));
        list.add(testEntity);
    }
    
    public static void main(String[] args) {
        sort();
        TestEntity20190402 getMiniTestEntity = getMiniTimeTestEntity();
        BigDecimal total = new BigDecimal(155);
        ArrayList<TestEntity20190402> tempList = new ArrayList<TestEntity20190402>(); 
        while((total = total.subtract(getMiniTestEntity.getDiscountMoney())).intValue() > 0) {
            tempList = new ArrayList<TestEntity20190402>(); 
            for(int i = 1; i < list.size(); i++) {
                tempList.add(list.get(i));
            }
            list = tempList;
            getMiniTestEntity = getMiniTimeTestEntity();
        }
        BigDecimal currentDiscountMoney = null;
        if(total.intValue() < 0) {
            total = total.add(getMiniTestEntity.getDiscountMoney());
            currentDiscountMoney = getMiniTestEntity.getDiscountMoney();
        }
        System.out.println(total.intValue());
        System.out.println(currentDiscountMoney.intValue());
    }
    
    public static TestEntity20190402 getMiniTimeTestEntity() {
        /*for(int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getTime() + "," + list.get(i).getDiscountMoney());
        }*/
        //获取第一个就是时间最早的
        TestEntity20190402 testEntity = list.get(0);
        return testEntity;
    }
    
    public static void sort() {
        for(int i = 0; i < list.size(); i++) {
            for(int j = i + 1; j < list.size(); j++) {
                TestEntity20190402 outerTestEntity = list.get(i);
                TestEntity20190402 innerTestEntity = list.get(j);
                if(innerTestEntity.getTime() < outerTestEntity.getTime()) {
                    list.set(i, innerTestEntity);
                    list.set(j, outerTestEntity);
                }
            }
        }
    }
    
}

 

posted @ 2019-04-04 11:19  ラピスラズリ(Dawn)  阅读(232)  评论(0编辑  收藏  举报