使用枚举创建商品类,商品类中有商品的id 商品的名字 商品的类型 商品的价格。向ArrayList中添加商品信息。 获得商品中商品类型是图书 价格排名前3的 图书的名字。将图书的名字打印出来。
时间: 2024-06-01 20:10:30 浏览: 16
enum ProductType {
BOOK,
ELECTRONICS,
CLOTHING,
FOOD
}
class Product {
private int id;
private String name;
private ProductType type;
private double price;
public Product(int id, String name, ProductType type, double price) {
this.id = id;
this.name = name;
this.type = type;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public ProductType getType() {
return type;
}
public double getPrice() {
return price;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Product> productList = new ArrayList<>();
productList.add(new Product(1, "The Great Gatsby", ProductType.BOOK, 9.99));
productList.add(new Product(2, "iPhone X", ProductType.ELECTRONICS, 999.99));
productList.add(new Product(3, "Levi's Jeans", ProductType.CLOTHING, 49.99));
productList.add(new Product(4, "Chocolate Bar", ProductType.FOOD, 1.99));
productList.add(new Product(5, "The Catcher in the Rye", ProductType.BOOK, 8.99));
productList.add(new Product(6, "Samsung Galaxy S9", ProductType.ELECTRONICS, 899.99));
productList.add(new Product(7, "Nike Shoes", ProductType.CLOTHING, 89.99));
productList.add(new Product(8, "Bananas", ProductType.FOOD, 0.99));
productList.add(new Product(9, "To Kill a Mockingbird", ProductType.BOOK, 7.99));
productList.add(new Product(10, "iPad Pro", ProductType.ELECTRONICS, 1199.99));
ArrayList<Product> bookList = new ArrayList<>();
for (Product product : productList) {
if (product.getType() == ProductType.BOOK) {
bookList.add(product);
}
}
Collections.sort(bookList, new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
return Double.compare(o2.getPrice(), o1.getPrice());
}
});
System.out.println("Top 3 most expensive books:");
for (int i = 0; i < 3; i++) {
System.out.println(bookList.get(i).getName());
}
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)