到目前为止我所拥有的:
class abstract Product {
public int daysTillExpiration;
public int dateofManufacture ;
public Product ( int daysTillExpiration, int dateofManufacture ) {
this.daysTillExpiration = daysTillExpiration;
this.dateofManufacture = dateofManufacture;
}
int getDaysTillExpiration(){
return daysTillExpiration;
}
int getDateofManufacture() {
return dateofManufacture;
}
}
班级厨房:
import java.util.ArrayList;
public class Kitchen {
ArrayList<Product> contents = new ArrayList<>();
ArrayList<Product> getcontents ()
{
return contents;
}
}
我创建了产品类(鸡蛋,葡萄酒和奶酪)并扩展了产品类。
我需要做什么;
我有一个Kitchen
,Product
中的每个Kitchen
都有一个到期日,
这是在创建后初始化x
天
x = for wine 3 years
for cheese 10 days
for eggs 2 days
询问应该来自Kitchen
- 厨房中的哪个产品首先到期?
答案 0 :(得分:0)
假设您有一个类似于
的Product类 public class Product
{
private Date purchaseDate = null;
private int daysTillExpiry = 0;
private Date expiryDate = null;
private String productDescription = null;
public Product(Date purchaseDate, int daysTillExpiry, String productDescription)
{
this.purchaseDate = purchaseDate;
this.daysTillExpiry = daysTillExpiry;
this.productDescription = productDescription;
Calendar calendar = Calendar.getInstance();
calendar.setTime(purchaseDate);
calendar.add(Calendar.DAY_OF_YEAR, daysTillExpiry);
expiryDate = calendar.getTime();
}
public Date getPurchaseDate()
{
return purchaseDate;
}
public int getDaysTillExpiry()
{
return daysTillExpiry;
}
public Date getExpiryDate()
{
return expiryDate;
}
public String getProductDescription()
{
return productDescription;
}
}
和像
这样的Kitchen类 public class Kitchen
{
private List<Product> productList = new ArrayList<Product>();
public List<Product> getProductList()
{
return productList;
}
}
然后它只是使用Comparator
我在下面创建了一个测试类,在Comparator
方法
printList()
public class ProductsExpire
{
private Kitchen kitchen = new Kitchen();
public static void main(String[] args)
{
ProductsExpire productsExpire = new ProductsExpire();
productsExpire.addProduct(new Date() ,1095,"wine");
productsExpire.addProduct(new Date() ,10,"cheese");
productsExpire.addProduct(new Date() ,3,"eggs");
productsExpire.printList();
}
public void addProduct(Date purchaseDate, int daysTillExpiry, String productDescription)
{
kitchen.getProductList().add(new Product(purchaseDate, daysTillExpiry, productDescription));
}
public void printList()
{
Collections.sort(kitchen.getProductList(), new Comparator<Product>()
{
@Override
public int compare(Product leftProduct, Product rightProduct)
{
return leftProduct.getExpiryDate().compareTo(rightProduct.getExpiryDate());
}
});
for (Product product : kitchen.getProductList())
{
System.out.println(product.getProductDescription() + " " + product.getExpiryDate());
}
}
}
答案 1 :(得分:0)
<强>代码:强>
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class QuickTester {
public static void main(String[] args) {
Kitchen myKitchen = new Kitchen();
myKitchen.addProduct(new Wine("Red Wine", 20150611));
myKitchen.addProduct(new Cheese("White Cheese", 20150611));
myKitchen.addProduct(new Wine("White Wine", 20150611));
myKitchen.addProduct(new Egg("Big Egg", 20150611));
Product p = myKitchen.getEarliestExpiringProduct();
System.out.println("Name: " + p.getName() +
", Days Left: " + p.getDaysTillExpiration());
}
}
abstract class Product {
private String name;
private int daysTillExpiration;
private int dateOfManufacture;
public Product(String name, int daysTillExpiration,
int dateOfManufacture) {
this.name = name;
this.daysTillExpiration = daysTillExpiration;
this.dateOfManufacture = dateOfManufacture;
}
public String getName() { return name; }
public int getDaysTillExpiration() { return daysTillExpiration; }
public int getDateofManufacture() { return dateOfManufacture; }
}
class Wine extends Product {
public Wine(String name, int dateOfManufacture) {
super(name, 3 * 365, dateOfManufacture);
}
}
class Cheese extends Product {
public Cheese(String name, int dateOfManufacture) {
super(name, 10, dateOfManufacture);
}
}
class Egg extends Product {
public Egg(String name, int dateOfManufacture) {
super(name, 2, dateOfManufacture);
}
}
class Kitchen {
ArrayList<Product> contents = new ArrayList<>();
ArrayList<Product> getContents() {
return contents;
}
public void addProduct(Product newProduct) {
contents.add(newProduct);
}
public void removeProduct(Product p) {
contents.remove(p);
}
public void sortByDaysLeft() {
Collections.sort(contents, new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
return ((Integer)p1.getDaysTillExpiration()).compareTo(
p2.getDaysTillExpiration());
}
});
}
public Product getEarliestExpiringProduct() {
sortByDaysLeft();
return contents.get(0);
}
}
<强>输出:强>
Name: Big Egg, Days Left: 2
这是一个工作版本,尝试弄清楚它是如何工作的(并停止重新发布相同的问题)
特别是Comparator<Product>
,这将帮助您“排序厨房”并让您回归
即将到期的产品。有关Comparator的更多信息。
注意:这不使用dateOfManufacture
如果您打算使用dateOfManufacture
,则应将其声明为Date而不是int。