📄 societytest_2.java.txt
字号:
/* The class to test a "complex" Society:
* six Persons (initially)
*
* Author: Dmitry Gakhovich
* E-mail: jim_g@lycos.com or d.gakhovich@witt.ac.nz
* (C) 2004
*/
import javax.swing.*;
import java.awt.BorderLayout;
import java.util.ArrayList;
public class SocietyTest_2{
/* The scenario for this Society is a bit more complex.
* First, we have initially 6 Persons, three males and three females
* Many things here happen randomly.
* One of males (random) falls in love with some random female Person.
* His love may be accepted or declined...
* But in any case, finally one of our guys marries to one of our girls
* (the process is random!...)
* After they are married we get a new Person (new Object in our Society!).
* It may be girl or boy.
* And after we run a census. And we run it only one - at the end
* (I believe you may guess what result would be if you uncomment line 39)
*/
public static void main(String []args){
Society sc = new Society();
Person m1 = new Person("Arnold", "male", 35);
sc.showMessage("Arnold", "arnold.gif");
Person m2 = new Person("George", "male", 30);
sc.showMessage("George", "george.gif");
Person m3 = new Person("Henry", "male", 25);
sc.showMessage("Henry", "henry.gif");
Person f1 = new Person("Mary", "female", 25);
sc.showMessage("Mary", "mary.gif");
Person f2 = new Person("Ann", "female", 25);
sc.showMessage("Ann", "ann.gif");
Person f3 = new Person("Suzi", "female", 25);
sc.showMessage("Suzi", "suzi.gif");
// add people to the Society list
sc.addPeople(m1);sc.addPeople(m2);sc.addPeople(m3);
sc.addPeople(f1);sc.addPeople(f2);sc.addPeople(f3);
// may run census first time - but what is the point! :)
// sc.doCensus();
// find lucky ones who will marry :)
Person guy = null;
Person girl = null;
boolean happyLove = false;
//select all males
ArrayList males = sc.getAdultMales();
while(!happyLove){//run until find happy couple :)
//select one male randomly from the list
int rand = (int)(Math.random()*males.size());
guy = (Person)males.get(rand);//get one guy randomly
girl = guy.fallInLove();//one of adult females - randomly
// see Person's fallInLove() method
String info = guy.getName() + " loves " + girl.getName();
sc.showMessage(info,"inlove.gif" );
// does she accept his love?!
happyLove = girl.acceptLove(); //chances 50/50 (again randomly)...
if(happyLove){ // if love is accepted (he's lucky!)
String info2 = girl.getName() + " loves "
+ guy.getName() + " too!";
sc.showMessage(info2, "love.gif");
}
else{
// love was not accepted. Yes, it's life...
String info3 = guy.getName() +
"'s heart is broken!\n" +
girl.getName() + " does not love him..." +
"\n\nTry again...";
sc.showMessage(info3, "broken.gif");
}
}
sc.weddingCeremony(guy, girl);
sc.showMessage(guy.getName() + " and " + girl.getName()+ " get married!", "marr.gif");
JWindow window = new JWindow();
window.getContentPane().setLayout(new BorderLayout());
window.getContentPane().add(new JLabel(new ImageIcon("champagne.gif")), BorderLayout.CENTER);//
window.setSize(200, 200); window.setLocation(350, 250); window.validate();
window.setVisible(true);//
// now - just music, see AudioPlayer class
// or remove the entire block if don't want music
// ======================================================
AudioPlayer ap = new AudioPlayer("wedding1.mid");
ap.startPlay("wedding1.mid", false);
sc.pause(19);//19 s is optimal
window.setVisible(false);
window = null;
ap.stopPlay();
// ======================================================
sc.pause(1);
sc.showMessage("One year later...", "clock.gif");
// by the way, everybody getting older...
// (increase age by 1)
for(int i = 0; i< sc.people.size(); i++){
Person tmp = (Person)sc.people.get(i);
tmp.setAge(tmp.getAge()+1);
}
sc.pause(1); // just wait 1 second
Person baby = girl.getBaby();
String babyGender = baby.getGender();
if(babyGender.equals("male")){
baby.setName("Alex");
}
else{
baby.setName("Tania");
}
// now have to registrate our baby :), i.e. add to the society list
sc.addPeople(baby);
String inf = girl.getName() + " gave a birth to " + baby.getName();
sc.showMessage(inf, "baby.gif");
sc.showMessage("And the society increased by one...", "increase.gif");
sc. pause(1); // just wait 1 second
sc.showMessage("Now it is time to run Census...", "idea.gif");
// Do census...
System.out.println("\n\t**************************\n\t*\t\t\t *");
System.out.println("\t* C E N S U S *");
System.out.println("\t*\t\t\t *\n\t**************************\n");
sc.doCensus();
//System.exit(0);
}
}//end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -