import java.util.Arrays;
import java.util.Date;
public class WarehouseDemo {
public static void main(String[] args) {
// 创建仓库
Warehouse warehouse = new Warehouse();
// 添加物品
Food food1 = new Food("Apple", 0.2, new Date(120, 6, 1));
Food food2 = new Food("Banana", 0.3, new Date(120, 7, 1));
Drink drink1 = new Drink("Water", 0.5, new Date(120, 6, 10));
Drink drink2 = new Drink("Juice", 0.8, new Date(120, 7, 15));
Bomb bomb = new Bomb("Atomic Bomb", 1000);
Gun gun1 = new Gun("Pistol", 5);
Gun gun2 = new Gun("Rifle", 0);
warehouse.addItem(food1);
warehouse.addItem(food2);
warehouse.addItem(drink1);
warehouse.addItem(drink2);
warehouse.addItem(bomb);
warehouse.addItem(gun1);
warehouse.addItem(gun2);
// 按重量排序
Arrays.sort(warehouse.getItems());
// 遍历仓库中的物品
for (Item item : warehouse.getItems()) {
System.out.println("Name: " + item.getName() + ", Weight: " + item.getWeight());
if (item instanceof Consumable) {
Consumable consumable = (Consumable) item;
if (consumable.isExpired()) {
System.out.println("This item has expired.");
} else {
if (item instanceof Food) {
((Food) item).eat();
} else {
((Drink) item).drink();
}
}
} else if (item instanceof Weapon) {
Weapon weapon = (Weapon) item;
if (weapon instanceof Bomb) {
((Bomb) weapon).use();
} else {
((Gun) weapon).use();
}
}
}
}
}
class Warehouse {
private Item[] items = new Item[0];
private int index = 0;
public void addItem(Item item) {
if (item != null) {
if (index == items.length) {
Item[] newItems = new Item[index + 1];
for (int i = 0; i < index; i++) {
newItems[i] = items[i];
}
items = newItems;
}
items[index] = item;
index++;
}
}
public Item[] getItems() {
return items;
}
}
class Item implements Comparable<Item> {
private String name;
private double weight;
public Item(String name, double weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public double getWeight() {
return weight;
}
@Override
public int compareTo(Item o) {
if (this.weight > o.weight) {
return 1;
} else if (this.weight < o.weight) {
return -1;
} else {
return 0;
}
}
}
interface Consumable {
boolean isExpired();
}
class Food extends Item implements Consumable {
private Date expiryDate;
private boolean eaten = false;
public Food(String name, double weight, Date expiryDate) {
super(name, weight);
this.expiryDate = expiryDate;
}
@Override
public boolean isExpired() {
return expiryDate.before(new Date());
}
public void eat() {
if (!eaten && !isExpired()) {
System.out.println("Eating " + getName());
eaten = true;
} else {
System.out.println("This food cannot be eaten.");
}
}
}
class Drink extends Item implements Consumable {
private Date expiryDate;
private boolean drank = false;
public Drink(String name, double weight, Date expiryDate) {
super(name, weight);
this.expiryDate = expiryDate;
}
@Override
public boolean isExpired() {
return expiryDate.before(new Date());
}
public void drink() {
if (!drank && !isExpired()) {
System.out.println("Drinking " + getName());
drank = true;
} else {
System.out.println("This drink cannot be drunk.");
}
}
}
interface Weapon {
void use();
}
class Bomb extends Item implements Weapon {
private boolean used = false;
public Bomb(String name, double weight) {
super(name, weight);
}
public void use() {
if (!used) {
System.out.println("Boom");
used = true;
} else {
System.out.println("This bomb cannot be used.");
}
}
}
class Gun extends Item implements Weapon {
private int bullets;
public Gun(String name, double weight) {
super(name, weight);
bullets = (int) weight;
}
public void use() {
if (bullets > 0) {
System.out.println("Bang");
bullets--;
} else {
System.out.println("This gun has no bullets left.");
}
}
}