JAVA

(Java)Spring Flame Work (xml 방식 ) BMI 측정기

김만식이 2020. 10. 21. 16:20
package main.bmi;

public class BMIcalculator {

	private double lowweight;
	private double normal;
	private double overWeight;
	private double obbesity;
	
	
	public void bmicalculator(double weight , double height) {
		
		double h = height *0.01;
		double result = weight / (h*h);
		
		System.out.println("지수"+(int)result);
		
		if(result >obbesity ) {
			System.out.println("비만");
			
		}else if (result > overWeight) {
			System.out.println("과체중");
			
		} else if (result > normal) {
			System.out.println("정상");
			
		}else {
			System.out.println("저체중");
			
		}
	}
	
	public void setLowweight(double lowweight) {
		this.lowweight = lowweight;
	}


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


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

	public void setObbesity(double obbesity) {
		this.obbesity = obbesity;
	}
	
}

BMIcalculator.java(계산식처리 set)

 

package main.bmi;

import java.util.ArrayList;

public class Myinfo {
	
	private String name;
	private double height;
	private double weight;
	private ArrayList<String> hobbys;
	private BMIcalculator bmicalculcator;
	
	public void setName(String name) {
		this.name = name;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	public void setHobbys(ArrayList<String> hobbys) {
		this.hobbys = hobbys;
	}
	
	public void setBmicalculcator(BMIcalculator bmicalculcator) {
		this.bmicalculcator = bmicalculcator;
	}
	
	public void bmiCalulation() {
		bmicalculcator.bmicalculator(weight,height);		
	}
	
	
	public void getInfo() {
	System.out.println("이름"+name);
	System.out.println("키"+height);
	System.out.println("몸무게"+weight);
	System.out.println("취미"+hobbys);
	bmiCalulation();
		
	}
	
}

MyInfo.java(계산식을 받아 출력)

 

<?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="bmicalculcator" class="main.bmi.BMIcalculator">
		<property name='lowweight'>
			<value>30</value>
		</property>
		<property name='normal'>
			<value>60</value>
		</property>
		<property name='overWeight'>
			<value>80</value>
		</property>
		<property name='obbesity'>
			<value>100</value>
		</property>
	</bean>

	<bean id="Myinfo" class="main.bmi.Myinfo">
		<property name='name'>
			<value>김만식</value>
		</property>
		<property name='height'>
			<value>177</value>
		</property>
		<property name='weight'>
			<value>63</value>
		</property>
		<property name='hobbys'>
			<list>
				<value>코딩</value>
				<value>운동</value>
			</list>
		</property>
	 	<property name="bmicalculcator">
			<ref bean="bmicalculcator"/>
		</property>
	</bean>
</beans>

xml -> (체중별 수치와 사용자의 정보를 getBean으로 받아 메인에 넘김)

package main.bmi;

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

public class MainClass {

	public static void main(String[] args) {

		String location = "classpath:BMI.xml";

		AbstractApplicationContext ac = new GenericXmlApplicationContext(location);

		Myinfo myinfo = ac.getBean("Myinfo", Myinfo.class);

		myinfo.getInfo();

	}

}

  메인출력부