java 程序 生子程序

package woman.ham.vagina;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

/**
 *
 * @author keeny
 *
 */
public class GrowSonFactory {
    public static final String BOY = "boy";
    public static final String GIRL = "girl";

    private static void product(String sex) {
        Man man = Man.newInstance();
        Woman woman = Woman.newInstance();
        Son mySon = null;

        while (true) {
            mySon = join(man, woman);
            sleepOneYear();
            if (mySon.getSex().equals(sex))
                break;
            else
                continue;
        }
        System.out.println("\n恭賀喜得貴子:" + mySon.getSex());
    }

    private static void sleepOneYear() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static Son join(Man man, Woman woman) {
        Son son = new Son();
        int strongestSperm = man.getSperm();
        System.out.println();
        int strongestOvum = woman.getOvum();
        if (strongestSperm == strongestOvum) {
            System.out.println("恭喜你:雙胞胎!");
        } else {
            if ((strongestSperm + strongestOvum) % 2 == 0) {
                son.setSex(BOY);
            } else {
                son.setSex(GIRL);
            }
        }
        return son;
    }

    private static String inputSex() {
        Scanner scanner = new Scanner(System.in);
        String sex = null;
        if (scanner.hasNext()) {
            sex = scanner.next();
        }
        if (sex == null) {
            System.out.println("輸入不能爲空!重新輸入:");
            return inputSex();
        }
        if (!sex.equalsIgnoreCase(BOY) && !sex.equalsIgnoreCase(GIRL)) {
            System.out.println("輸入性別格式錯誤(boy/girl):" + sex + "重新輸入:");
            return inputSex();
        }
        return sex;
    }

    public static void main(String[] args) {
        System.out.println("請選擇想要的性別(boy/girl):");
        String sex = inputSex();
        GrowSonFactory.product(sex);
    }
}

class Man {
    public static DecimalFormat format = new DecimalFormat("0.0000");

    public static Man newInstance() {
        return new Man();
    }

    public int getSperm() {
        TreeSet<Integer> sperms = new TreeSet<Integer>();
        double maxSperm = new Random().nextInt(Integer.MAX_VALUE / 10000);
        for (int i = 1; i < maxSperm + 1; i++) {
            System.out.print("\rIn sex ... "
                    + format.format((i / maxSperm) * 100) + " %\t" + i + "/"
                    + (int) maxSperm);
            sperms.add(new Random().nextInt(i));
        }
        return sperms.last();
    }
}

class Woman {
    public static DecimalFormat format = new DecimalFormat("0.0000");

    public static Woman newInstance() {
        return new Woman();
    }

    public int getOvum() {
        TreeSet<Integer> sperms = new TreeSet<Integer>();
        double maxOvum = new Random().nextInt(Integer.MAX_VALUE / 10000);
        for (int i = 1; i < maxOvum + 1; i++) {
            System.out.print("\rIn sex ... "
                    + format.format((i / maxOvum) * 100) + " %\t" + i + "/"
                    + (int) maxOvum);
            sperms.add(new Random().nextInt(i));
        }
        return sperms.last();
    }
}

class Son {
    private String sex;

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章