在java中使用集合临时代替数据库,并实现部分功能。

有时候我们对小部分数据进行处理,如果使用数据库的话太过浪费,这时可以考虑使用集合暂时代替一下数据库。比如有两张表交易员和交易Trader(name,address),Transaction(year,sum,Trader)。需求,查出2019年的所有交易,并且按照交易额排序。
分析:
万物皆对象,Trader和Transaction都实现为一个类。
需要做的有两步1:筛选出所有2019年的交易。2:对交易进行排序。
那我们只要把2019年的交易存到一个集合中,然后对集合排序就行了,这就清晰多了。
代码实现

package Test;

import java.math.BigDecimal;
import java.util.*;

public class Test2 {
     static List source = new ArrayList();;
     static Transaction source1,source2,source3,source4,source5,source6;
     static {
         source1 = new Transaction(2019, 89000, new Trader("张三", "上海"));
     source2 = new Transaction(2019, 79000, new Trader("李四", "北京"));
     source3 = new Transaction(2012, 890000, new Trader("宁王", "北京"));
     source4 = new Transaction(2012, 5000, new Trader("王虎", "广州"));
     source5 = new Transaction(2014, 4000, new Trader("陈明", "广州"));
     source6 = new Transaction(2013, 88000, new Trader("刘备", "上海"));}
    public static void setup() {
        source.add(source1);
        source.add(source2);
        source.add(source3);
        source.add(source4);
        source.add(source5);
        source.add(source6);

    }


    public static void main(String[] args) throws Exception {
        Test2 t2 = new Test2();
        t2.setup();
        /*for(Transaction t : source){
            System.out.println(t.toString());
        }*/
        t2.test1();
    }
    /*tran
    查出2019年所有交易,并按交易额由小到大排序
     */
    public static void test1() {
        int year = 2019;
        List tran = new ArrayList();
        for (Transaction t:source) {
            if(t.year==2019){
                tran.add(t);
            }
        }

        Collections.sort(tran, new Comparator() {
            @Override
            public int compare(Transaction o1, Transaction o2) {
                return o1.sum-o2.sum;
            }
        });

        for (Transaction i:tran) {
            System.out.println(i.toString());
        }

    }
}

class Trader{
    String name;
    String address;

    public Trader(String name, String address) {
        super();
        this.name = name;
        this.address = address;
    }

    @Override
    public String toString() {
        return "Trader [name=" + name + ", address=" + address + "]";
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

}

class Transaction{
    int year;
    int sum;
    Trader trader;

    @Override
    public String toString() {
        return "Transaction{" +
                "year=" + year +
                ", sum=" + sum +
                ", trader=" + trader +
                '}';
    }

    public Transaction(int year, int sum, Trader trader) {
        super();
        this.year = year;
        this.sum = sum;
        this.trader = trader;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }

    public Trader getTrader() {
        return trader;
    }

    public void setTrader(Trader trader) {
        this.trader = trader;
    }


}

你可能感兴趣的:(java笔试)