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

BMI。BMIチェッカー


せめて、 Java らしくって感じで書いてたら丸一日費やしてしまった。
値入力の処理まで学習進まず。


頑張ってはみたものの正直アクセス修飾子とか例外処理、列挙型の理解とか
怪しいのでちゃんと書けてるか全く持って自信なし。


Eclipse 入れたのにサクラエディタしか使ってないのもどうかと思った。>俺


BmiChecker.jar


BmiChecker.java

/*---------------------------------------------------------------------------------
	
	BmiChecker
	
	version 0.01
	Apr 30, 2012
	
	by DumBo
	
---------------------------------------------------------------------------------*/


class BmiChecker {
	
	public static void main(String[] args) {
		
		Bmi bmi = new Bmi();
		
		//タイトルの表示
		System.out.printf("BmiChecker ver0.01 by DumBo\n\n");
		
		bmi.setSex(Sex.MALE);	//性別の登録
		bmi.setHeight(185.0);	//身長の登録
		bmi.setWeight(75.0);	//体重の登録
		
		System.out.printf("             Sex : %s\n", bmi.getSex());
		System.out.printf("          Height : %.1f cm\n", bmi.getHeight());
		System.out.printf("          Weight : %.1f kg\n\n", bmi.getWeight());
		
		System.out.printf("             Bmi : %.1f\n", bmi.getBmi());
		System.out.printf("     IdealWeight : %.1f kg\n", bmi.getIdealWeight());
		System.out.printf(" NecessaryCalory : %.1f kcal\n\n", bmi.getNecessaryCalory());
		
		System.out.printf("           Judge : %s\n", bmi.getJudge());
		
	}
	
}

enum Sex {
	
	MALE(22.0d), 
	FEMALE(21.0d);
	
	private final double idealBmi;
	
	private Sex(double idealBmi) { this.idealBmi = idealBmi; }
	
	public double getIdealBmi() { return idealBmi; }
	
}

class Bmi {
	
	private enum Judge {
		
		SLENDER("Slender"), 
		STANDARD("Standard"), 
		PLUMP("Plump"), 
		FAT("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;
	}
	
}