読者です 読者をやめる 読者になる 読者になる

BMI。2、BMIチェッカー


キーボード入力をサポートしてみた版です。
それ以外はまあ、前とあまり変わりないです。


BmiChecker.jar


BmiChecker.java

/*---------------------------------------------------------------------------------
	
	BmiChecker
	
	version 0.02
	May 02, 2012
	
	by DumBo
	
---------------------------------------------------------------------------------*/

import java.io.*;


class BmiChecker {
	
	public static void main(String[] args) {
		
		Bmi bmi = new Bmi();
		Input input = new Input();
		
		String name;
		int sex;
		double height;
		double weight;
		
		//タイトルの表示
		System.out.printf("BMIチェッカー ver0.02 by DumBo\n\n");
		
		//情報の入力
		name = input.getText("名前を入力してください。>");
		
		do {
			sex = input.getNumber("性別を選んでください。1:男, 2:女 >");
		} while(!(sex == 1 || sex == 2));
		
		do {
			height = input.getRealNumber("身長(cm)を入力してください。>");
		} while(height == 0);
		
		do {
			weight = input.getRealNumber("体重(kg)を入力してください。>");
		} while(weight == 0);
		
		//情報の登録
		bmi.setSex((sex == 1) ? Sex.MALE : Sex.FEMALE);
		bmi.setHeight(height);
		bmi.setWeight(weight);
		
		//結果の表示
		System.out.printf("\n          名前 : %s\n", name);
		System.out.printf("          性別 : %s\n", (bmi.getSex()).getText());
		System.out.printf("          身長 : %.1f cm\n", bmi.getHeight());
		System.out.printf("          体重 : %.1f kg\n\n", bmi.getWeight());
		
		System.out.printf("        BMI : %.1f\n", bmi.getBmi());
		System.out.printf("      理想体重 : %.1f kg\n", bmi.getIdealWeight());
		System.out.printf("  必要カロリー : %.1f kcal\n\n", bmi.getNecessaryCalory());
		
		System.out.printf("          判定 : %s\n\n", bmi.getJudge());
		
	}
	
}


class Input {
	
	private BufferedReader readBuffer;
	
	Input() {
		readBuffer = new BufferedReader(new InputStreamReader(System.in), 1);
	}
	
	public int getNumber(String message) {
		return (int) getRealNumber(message);
	}
	
	public double getRealNumber(String message) {
		double realNumber = Double.NaN;
		while(Double.isNaN(realNumber)) {
			try {
				System.out.print(message);
				System.out.flush();
				realNumber = Double.valueOf(readBuffer.readLine()).doubleValue();
			} catch(NumberFormatException e) {
				realNumber = Double.NaN;
			} catch(Exception e) {
				System.err.println(e);
				System.exit(1);
			}
		}
		return realNumber;
	}
	
	public String getText(String message) {
		String text = "";
		while((text.length()) == 0) {
			try {
				System.out.print(message);
				System.out.flush();
				text = readBuffer.readLine();
			} catch(Exception e) {
				System.err.println(e);
				System.exit(1);
			}
		}
		return text;
	}
	
}


enum Sex {
	
	MALE("男", 22.0d), 
	FEMALE("女", 21.0d);
	
	private final String text;
	private final double idealBmi;
	
	private Sex(String text, double idealBmi) {
		this.text = text;
		this.idealBmi = idealBmi;
	}
	
	public String getText() { return text; }
	public double getIdealBmi() { return idealBmi; }
	
}


class Bmi {
	
	private enum Judge {
		
		SLENDER("やせ"), 
		STANDARD("普通"), 
		PLUMP("過体重"), 
		FAT("肥満");
		
		private final String text;
		
		private Judge(String text) { this.text = text; }
		
		public String getText() { return text; };
		
	}
	
	private Sex sex;
	private double height;
	private double weight;
	
	public Sex getSex() { return sex; }
	public void setSex(Sex sex) { this.sex = sex; }
	public double getHeight() { return height; }
	public void setHeight(double height) { this.height = height; }
	public double getWeight() { return weight; }
	public void setWeight(double weight) { this.weight = weight; }
	
	public double getNecessaryCalory() {
		return getIdealWeight() * 30;
	}
	
	public double getIdealWeight() {
		return height * height * sex.getIdealBmi() / 10000;
	}
	
	public String getJudge() {
		
		String judge;
		double bmi = getBmi();
		
		if     (bmi < 20.0d)                 { judge = Judge.SLENDER.getText();  }	//BMI値が20未満         = やせ
		else if(bmi >= 20.0d && bmi < 24.0d) { judge = Judge.STANDARD.getText(); }	//BMI値が20以上24未満   = 普通
		else if(bmi >= 24.0d && bmi < 26.4d) { judge = Judge.PLUMP.getText();    }	//BMI値が24以上26.4未満 = 過体重
		else                                 { judge = Judge.FAT.getText();      }	//BMI値が26.4以上       = 肥満
		
		return judge;
		
	}
	
	public double getBmi() {
		//例外のスロー
		if(height == 0.0d) {
			throw new IllegalArgumentException("Divide by zero");
		}
		return weight / height / height * 10000;
	}
	
}