society.java.txt

来自「the teacher resource code about the seco」· 文本 代码 · 共 171 行

TXT
171
字号
/*	The class that represents a Society of Persons
 *	All Person objects are stored in an ArrayList "people".
 *	
 *	Author: Dmitry Gakhovich
 *	E-mail: jim_g@lycos.com  or d.gakhovich@witt.ac.nz
 *	(C) 2004
 */


import javax.swing.*;
import java.util.ArrayList;


public class Society{
	
	ArrayList people; //collection of all people in the Society
	/*	Constructor.
	 *	We built an empty Society with an empty book of registration :)
	 */
	public Society(){
		people = new ArrayList();
	}
	
	/*
	 *	Add a Person to the Society
	 */
	public void addPeople(Person p){
		people.add(p);
		p.setSociety(this);
	}
	
	/*
	 *	get a list of all Persons in the Society
	 */	
	public ArrayList getPeople(){
		return people;
	}
	
	/*
	 *	the Society provides a method for wedding ceremony
	 *	After the ceremony two Persons become husband and wife
	 *	and their marital status becomes "married"
	 *	Marriage is allowed only for persons of opposite gender
	 *	and age >= 18 y.o. 
	 */		
	public void weddingCeremony(Person p1, Person p2){
		
		// check age. If < 18 - "not approved" and return
		if(p1.getAge()<18 || p2.getAge()<18){
			showMessage("The marriage is not approved.\nPersons are too young!", null);
			return;			
		}
		// check sex  
		boolean b1 = (p1.getGender().equals("female") && p2.getGender().equals("male")); 
		boolean b2 = (p1.getGender().equals("male") && p2.getGender().equals("female"));
		if( b1 || b2){
		   p1.setPartner(p2);
		   p2.setPartner(p1);
		}
		else{
			showMessage("The marriage is not approved", null);
		}
		   
			
	}
	
	/*
	 *	Convenience method to get a list of all adult males in the Society.
	 *	Only adults may fall in love! :)
	 */
	public ArrayList getAdultMales(){
		ArrayList males = new ArrayList();
		for(int i  = 0; i<people.size(); i++){
			Person p = (Person)people.get(i);
			if(p.getGender().equals("male")&&p.getAge()>=18){
				males.add(p);	
			}				
		}
		return males;
	}
	
	/*
	 *	Convenience method to get a list of all adult females in the Society.
	 *	Only adults may fall in love! :)
	 */
	public ArrayList getAdultFemales(){
		ArrayList females = new ArrayList();
		for(int i  = 0; i<people.size(); i++){
			Person p = (Person)people.get(i);
			if(p.getGender().equals("female")&&p.getAge()>=18){
				females.add(p);	
			}				
		}
		return females;
	}
	/*
	 *	We need some tool to get detailed information about all persons 
	 *	in the Society
	 */	
	public void doCensus(){
		// iterate through all Persons in the society, 
		// i.e. through ArrayList persons:
		for(int i = 0; i<people.size(); i++){
			Person p = (Person)people.get(i);
			System.out.println(p.toString());
			System.out.println("------------------------");
			// data about the person follows
			if(p.getPartner()!=null){
				System.out.println("Marital Status: married");
				System.out.println("\tSpouse: " + p.getPartner().getName());				
			}
			else{
				System.out.println("Marital Status: not married");				
			}
			if(p.getFather()!=null){
				System.out.println("Parents:\n\tFather: " + p.getFather().getName());				
			}
			else{
				System.out.println("Parents:\n\tFather: no records");				
			}
			if(p.getFather()!=null){
				System.out.println("\tMother: " + p.getMother().getName());				
			}
			else{
				System.out.println("\tMother: no records");				
			}
			// Need to iterate through all children for the person
			ArrayList ch = p.getChildren();	//list of children 
			if(ch.size()>0){	// if there are any children
				System.out.println("Children:");					
				for(int j = 0; j<ch.size(); j++){
					Person child = (Person)ch.get(j);
//						System.out.println("Empty List: "+ ch.isEmpty());					
					String childRel;
					if(child.getGender().equals("female"))
						childRel = "Daughter: ";
					else
						childRel = "Son: ";
					System.out.println("\t" + childRel+child.getName()
					+ ", " + child.getAge()+ " y.o." );					
				}
			}
			else
				System.out.println("Children: no");
				
			System.out.println("\n====================================\n");														
		}		
	}	// end doCensus
		
	
	public void showMessage(String message, String filename){

		Object[] options = {"OK"};
		int n = JOptionPane.showOptionDialog(null,
		message,
    	"Our Society",
    	JOptionPane.YES_OPTION,
    	JOptionPane.INFORMATION_MESSAGE,
    	new ImageIcon(filename),options,options[0]);	
	}//end showMessage
	
	// convenience method to delay program execution for n seconds
	// Don't worry	:)
	public void pause(int sec){
		Thread t = Thread.currentThread();
		try{
			t.sleep(sec*1000);	
		}
		catch(Exception e){};
	}	
}//end class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?