티스토리 뷰

Java/Spring

[Spring] BMI Calculator

praybe 2021. 12. 13. 11:21

생성 순서: BMICalculator > MYInfo > xml > BMIMain

 

파일 위치:

 

 

 BMICalculator

package com.javalec.ex.bmi;

public class BMICalculator {
	
	private double lowWeight;
	private double normal;
	private double overWeight;
	private double obesity;

	//디폴트 생성자
	//게터세터
	//생성자 
	
	public BMICalculator() {}

	public BMICalculator(double lowWeight, double normal, double overWeight, double obesity) {
		this.lowWeight = lowWeight;
		this.normal = normal;
		this.overWeight = overWeight;
		this.obesity = obesity;
	}


	public double getLowWeight() {
		return lowWeight;
	}

	public void setLowWeight(double lowWeight) {
		this.lowWeight = lowWeight;
	}

	public double getNormal() {
		return normal;
	}

	public void setNormal(double normal) {
		this.normal = normal;
	}

	public double getOverWeight() {
		return overWeight;
	}

	public void setOverWeight(double overWeight) {
		this.overWeight = overWeight;
	}

	public double getObesity() {
		return obesity;
	}

	public void setObesity(double obesity) {
		this.obesity = obesity;
	}
	
	
	//함수넣기
	public void bmicalculation(double weight, double height) {
			
		double h = height * 0.01 ;
		double result = weight / (h*h);
			
		System.out.println("BMI 지수 : " + (int)result);
			
		if(result > obesity) {
			System.out.println("비만 입니다.");
		} else if(result > overWeight) {
			System.out.println("과체중 입니다.");
		}else if(result > normal) {
			System.out.println("정상 입니다.");
		}else {
			System.out.println("저체중 입니다.");
		}
	}



}

 

MYInfo

package com.javalec.ex.bmi;

import java.util.ArrayList;

public class MyInfo {
	
	private String name;
	private double height;
	private double weight;
	private ArrayList<String> hobbys;
	private BMICalculator bmiCalculator;
	
	
	public MyInfo() {}

	public MyInfo(String name, double height, double weight, ArrayList<String> hobbys, BMICalculator bmiCalculator) {
		super();
		this.name = name;
		this.height = height;
		this.weight = weight;
		this.hobbys = hobbys;
		this.bmiCalculator = bmiCalculator;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	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 ArrayList<String> getHobbys() {
		return hobbys;
	}

	public void setHobbys(ArrayList<String> hobbys) {
		this.hobbys = hobbys;
	}

	public BMICalculator getBmiCalculator() {
		return bmiCalculator;
	}

	public void setBmiCalculator(BMICalculator bmiCalculator) {
		this.bmiCalculator = bmiCalculator;
	}
	
	
	//함수만들기
	public void getInfo() {
		System.out.println("이름: " + name);
		System.out.println("키: " + height);
		System.out.println("몸무게: " + weight);
		System.out.println("취미: " + hobbys);
		bmiCalculation();
		
	}
	
	public void bmiCalculation() {
		bmiCalculator.bmicalculation(weight, height);
	}


}

 

 xml 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


	<bean id="bmiCalculator" class="com.javalec.ex.bmi.BMICalculator">
		<property name="lowWeight">
			<value>18.5</value>
		</property>
		<property name="normal">
			<value>23</value>
		</property>
		<property name="overWeight">
			<value>25</value>
		</property>
		<property name="obesity">
			<value>30</value>
		</property>
	</bean>
	
	
	
	<bean id="myInfo" class="com.javalec.ex.bmi.MyInfo">
		<property name="name">
			<value>홍길동</value>
		</property>
		<property name="height">
			<value>187</value>
		</property>
		<property name="weight">
			<value>84</value>
		</property>
		<property name="hobbys">
			<list>
				<value>수영</value>
				<value>피아노</value>
				<value>독서</value>
			</list>
		</property>
		<property name="bmiCalculator">
			<!-- 객체라서 value 아님 -->
			<ref bean="bmiCalculator"/>	<!-- bean id부분 복붙 -->
		</property>
	</bean>
			
	
	
</beans>

 

BMIMain

package com.javalec.ex.bmi;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;


public class BmiMain {

	public static void main(String[] args) {
		String config="classpath:appCTX2.xml";
		AbstractApplicationContext ctx = new GenericXmlApplicationContext(config);
		
		MyInfo myInfo= ctx.getBean("myInfo", MyInfo.class);
		myInfo.getInfo();
		
		ctx.close();


	}

}

 

 

출력:

 

'Java > Spring' 카테고리의 다른 글

[Lombok] 롬복 설치하기  (0) 2022.05.09
[스프링] 버전 확인하기  (0) 2022.03.21
[스프링] 디스패쳐서블릿 Dispatcher Servlet  (0) 2022.03.21
[Spring] DI 예시 (interface + class + xml)  (0) 2021.12.13
댓글