import java.beans.*; import java.util.*; public class SuperPower extends Country implements VetoableChangeListener { protected static Vector bigGuys = new Vector(); protected static final int SALT_LIMIT = 2000000; // S.A.L.T // some extra attribute thats makes a Country that bit special protected int numNuclearWarHeads; //------------------------------------------------------------------- // SuperPower Constructors //------------------------------------------------------------------- public SuperPower() { // all beans need default constructors! this("Unnamed superpower", 0); } public SuperPower(String name, int destructivePower) { super(name, "Independent"); this.numNuclearWarHeads = destructivePower; // add new superpower to list of superpowers bigGuys.addElement(this); } //------------------------------------------------------------------- // set()/get() for 'ArsenalSize' property //------------------------------------------------------------------- public void setArsenalSize(int size) { numNuclearWarHeads = size; } public int getArsenalSize() { // if real arsenal is smaller than many believe, adjust upwards if ( numNuclearWarHeads < 500000 ) { return (int) (numNuclearWarHeads * 2.5); } // if real arsenal is larger than allowed by treaty, adjust down if ( numNuclearWarHeads > SALT_LIMIT ) { return (int) (SALT_LIMIT * 1.1); } return numNuclearWarHeads; } //------------------------------------------------------------------- // method vetoableChange() is similar to propertyChange() except that // the listener can VETO the change. This is done by throwing the // PropertyVetoException. //------------------------------------------------------------------- public void vetoableChange(PropertyChangeEvent countryChanged) throws PropertyVetoException { String howChanged; // extract details of property change howChanged = (String) countryChanged.getNewValue(); System.out.print("UN member "+ countryName + " assesses the situation.. '"); System.out.println(countryChanged.getOldValue() +"' -> '"+howChanged+"'"); // if a sensitive country wants to change its independent // status, then all superpowers pay attention... if ( ! howChanged.equals("Independent") ) { // if the country wants to ally itself to another // superpower, and that superpower has more firepower than // itself, then the smaller superpower vetos the shift int allyPosition = howChanged.indexOf("ally of"); if ( allyPosition != -1 ) { for (int i=0; i < bigGuys.size(); i++) { String allyName = howChanged.substring(allyPosition+8); SuperPower ally = (SuperPower) (bigGuys.elementAt(i)); if ( ! ally.getCountryName().equals(allyName) ) { continue; } if ( ally.getArsenalSize() > numNuclearWarHeads ) { System.out.println(countryName + " vetos change!"); String reason = countryName + " will not allow "+ "Kuwait to become "+ howChanged; throw new PropertyVetoException(reason, countryChanged); } } // endfor } // endif tricky country wants to change color } // endif geopolitical trigger } } // End of Class SuperPower