“My enemies are many,my equals are none. In the shade of olive trees,they said Italy could never be conquered.In the land of pharoahs and kings, they said Egypt could never be humbled.In the realm of forest and snow,they said russia could never be tamed.Now they say nothing.They fear me ,like a force of nature,a dealer in thunder and death.I say I am Napoleon,I am emperor........Burn it”
import java.util.HashSet; public class Napoleon { private HashSet enemies = new HashSet(); // cdps = Chrysanthemum Damage Per Second public int cdpm = 100000; Napoleon() { enemies.add(new Enemy("Italy", 100)); enemies.add(new Enemy("Egypt", 100)); enemies.add(new Enemy("Russia ", 100)); } private void speak() { System.out.println("My enemy number: " + enemies.size()); HashSet trueEnemies = new HashSet(); for (Enemy e : enemies) { if (e.cdpm > cdpm) trueEnemies.add(e); } System.out.println("The number of enemies who can beat me: " + trueEnemies.size());
for (Enemy e : enemies) { do { e.shout(); } while (e.canBeatNapoleon(this)); } for (Enemy e : enemies) { e.shout(); } cdpm = 10000000; System.out.println("I am Napoleon cdpm " + cdpm); System.out.println("Dispose enemy list..."); enemies.clear(); System.out.println("Enemy list disposed!"); } public static void main(String[] args) { (new Napoleon()).speak(); } } class Enemy { private String name; public int cdpm; Enemy(String name, int cdpm) { this.name = name; this.cdpm = cdpm; } public void shout() { if (cdpm > 0) System.out.println(name + " says: we are invincible!!!"); else System.out.println(name + " says: ......"); } public boolean canBeatNapoleon(Napoleon n) { if (cdpm cdpm = -1; return false; } return true; } } output: My enemy number: 3 The number of enemies who can beat me: 0 Egypt says: we are invincible!!! Italy says: we are invincible!!! Russia says: we are invincible!!! Egypt says: ...... Italy says: ...... Russia says: ...... I am Napoleon cdpm 10000000 Dispose enemy list... Enemy list disposed!