Java 謎題4:液體(Liquid)

jdon發表於2019-09-19

你可以透過機場保安計劃嗎?
您的手提行李不允許攜帶液體:

package liquid;
 
import java.lang.String;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
 
public class Luggage {
    private final Collection<String> items;
 
    public Luggage(Collection<String> items) {
        items = Collections.unmodifiableCollection(new ArrayList<String>(items));
        for (String item : items) {
            if (item.contains("liquid")) {
                throw new SecurityException("No liquids allowed in your hand luggage!");
            }
        }
        this.items = items;
    }
 
    public Collection<String> getItems() {
        return items;
    }
 
    public void fly() {
        if (items.contains("liquid water")) {
            // The goal is to reach this line
            System.out.println("Oh no, water on a plane! We're all going to die!");
        }
    }
}


作為一個口渴的乘客,你能隨身帶些水嗎?

相關文章