写一个商品类Goods,属性有商品编号,商品名称,商品价格和上架时间。

要求默认按编号排序,同时实现按名称、价格(从大到小)、上架时间排序的方法

public class Goods implements Comparable{
	private String no;
	private String name;
	private int price;
	private Date time;
	
	public Goods() {
		super();
	}

	public Goods(String no, String name, int price, Date time) {
		super();
		this.no = no;
		this.name = name;
		this.price = price;
		this.time = time;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	@Override
	public int compareTo(Goods o) {
		// TODO Auto-generated method stub
		return this.no.compareTo(o.getNo());
	}
}
import java.util.Comparator;

public class GoodsTimeComparator implements Comparator {

	@Override
	public int compare(Goods o1, Goods o2) {
		// TODO Auto-generated method stub
		return o1.getTime().compareTo(o2.getTime());
	}

}
import java.util.Comparator;

public class GoodsPriceComparator implements Comparator {

	@Override
	public int compare(Goods o1, Goods o2) {
		// TODO Auto-generated method stub
		return o2.getPrice()-o1.getPrice();
	}

}
import java.util.Comparator;

public class GoodsNameComparator implements Comparator{

	@Override
	public int compare(Goods o1, Goods o2) {
		// TODO Auto-generated method stub
		return o1.getName().compareTo(o2.getName());
	}

}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class GoodsTest {

	public static void main(String[] args) throws ParseException {
		// TODO Auto-generated method stub
//		Date date = new Date();
//		String s1 = "2018-06-03";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		sdf.format(s1);
		
		Date d = sdf.parse("2012-05-8");
		Date d1 =(Date) sdf.parse("2014-06-9");
		Date d2 = sdf.parse("2018-08-6");
		Date d3 = sdf.parse("2011-01-11");
		Date d4 = sdf.parse("2016-03-20");

		List list = new ArrayList();
		list.add(new Goods("A001","苹果",8,d));
		list.add(new Goods("C001","香蕉",4,d1));
		list.add(new Goods("D001","榴莲",12,d2));
		list.add(new Goods("B001","西瓜",2,d3));
		list.add(new Goods("E001","哈密瓜",12,d4));
//		System.out.println(list);
//		
		Collections.sort(list);
		System.out.println("*****按照商品编号排序*****");
		for (Goods goods : list) {
			System.out.print(goods.getNo() + "\t");
		}
		System.out.println();
		Collections.sort(list,new GoodsNameComparator());
		System.out.println("*****按照商品名称排序*****");
		for (Goods goods : list) {
			System.out.print(goods.getName() + "\t");
		}
		System.out.println();
		Collections.sort(list, new GoodsPriceComparator());
		System.out.println("*****按照商品价格排序*****");
		for (Goods goods : list) {
			System.out.print(goods.getPrice() + "\t");
		}
		System.out.println();
		Collections.sort(list,new GoodsTimeComparator());
		System.out.println("*****按照商品上架日期排序*****");
		for (Goods goods : list) {
			System.out.print(goods.getTime() + "\t");
		}
	}

}





你可能感兴趣的:(JAVA学习之旅)