BMICALCULATOR

표준체중계산기 (BMI Calculator)
BMIFrame
-label, button, radiobutton, textfield

– 표준체중
// 남자표준체중 = 신장(m) x 신장(m) x 22
// 여자표준체중 = 신장(m) x 신장(m) x 21

– BMI
// BMI = 체중(kg)/신장^2(m^2)
// 20 미만 저체중 (Underweight)
// 20~24 정상 (Normal)
// 25~29 과체중 (Overweight)
// 30 이상 비만 (Obesity)

– 하루 권장 섭취량
// 한국인 하루권장섭취량 남성 2600 kcal 여성 2100 kcal
// 기초대사량산출공식 (Herris Benedict Equation)
// 기초대사량(남성) = 66.47 + (13.75*weight) + (5*height) – (6.76*age)
// 기초대사량(여성) = 655.1 + (9.05*weight) + (1.85*height) – (4.68*age)
// 활동대사량 = 저활동적(1.3)/중간활동적(1.5)/활동적(1.75)
// 개인별 하루권장섭취량(kcal) = 기초대사량 * 활동대사량


enum BMI {
    UNDERWEIGHT, NORMAL, OVERWEIGHT, OBESITY
}

enum Gender {
    FEMALE, MALE
}

enum Activity {
    LOW, MEDIUM, HIGH;

    public static String[] names() {
        String[] names = new String[Activity.values().length];
        for (Activity t : Activity.values())
            names[t.ordinal()] = t.toString();
        return names;
    }

    public static Activity valueOf(int value) {
        switch(value) {
        case 0: return LOW;
        case 1: return MEDIUM;
        case 2: return HIGH;
        default: return null;
        }
    }
}

public class Person {
    protected String name;
    protected int age;
    protected Gender gender;

    public Person() { 
        this("", 0, Gender.FEMALE); 
    }

    public Person(String name, int age, Gender gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public void setName(String name) { 
        this.name = name; 
    }
    
    public void setAge(int age) { 
	this.age = age; 
    }
    
    public void setGender(Gender gender) { 
	this.gender = gender; 
    }

    public String getName() { 
	return name; 
    }

    public int getAge() { 
 	return age; 
    }

    public Gender getGender() { 
 	return gender; 
    }

    public void print() { 
	System.out.println(this); 
    }

    @Override 
    public String toString() { 
	return "Person Name=" + name + " Age=" + age + " Gender=" + gender; 
    }
}

public class BMICalculator extends Person {
    protected double height;
    protected double weight;
    protected Activity activity;

    public BMICalculator() {
        this("J", 20, Gender.FEMALE, 165.0, 55.0, Activity.MEDIUM);
    }

    public BMICalculator(String name, int age, Gender gender, double height, double weight, Activity activity) {
        super(name, age, gender);
        this.height = height;
        this.weight = weight;
        this.activity = activity;
    }

    public void setHeight(double height) {
	this.height = height;	
    }
    
    public void setWeight(double weight) {
	this.weight = weight;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;	
    }
    
    public double getHeight() {
	return height;
    }

    public double getWeight() {
	return weight;
    }

    public Activity getActivity() {
	return activity;
    }

    public BMI calculateBMI() {
        double bmi = weight / (height * 0.01) / (height * 0.01);
        if (bmi >= 0.0 && bmi < 20.0) return BMI.UNDERWEIGHT;
        else if (bmi >= 20.0 && bmi < 24.0) return BMI.NORMAL;
        else if (bmi >= 24.0 && bmi < 30.0) return BMI.OVERWEIGHT;
        else return BMI.OBESITY;
    }

    public double calculateStandardWeight() {
        if (this.gender == Gender.MALE)
            return height * height * 22 * 0.0001;
        return height * height * 21 * 0.0001;
    }

    // Herris Benedict Equation
    public double calculateDailyCaloryIntake() {
        double activityCalory = 1.0;
        switch (this.activity) {
        case LOW:
            activityCalory = 1.3;
            break;
        case MEDIUM:
            activityCalory = 1.5;
            break;
        case HIGH:
            activityCalory = 1.75;
            break;
        default:
            break;
        }

        if (this.gender == Gender.MALE)
            return 66.47 + (13.75 * this.weight) + (5.0 * this.height) - (6.76 * (double)(this.age)) * activityCalory;
        return 655.1 + (9.05 * this.weight) + (1.85 * this.height) - (4.68 * (double)(this.age)) * activityCalory;
    }

    @Override 
    public String toString() { 
	return "BMICalculator Name=" + name + " Age=" + age + " Gender=" + gender + " Height=" + height + " Weight=" + weight + " BMI=" + calculateBMI(); 
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class BMICalculatorFrame extends JFrame implements ActionListener, KeyListener {
    JLabel label1 = new JLabel("나이"); // 레이블 초기화
    JLabel label2 = new JLabel("키(cm)"); // 레이블 초기화
    JLabel label3 = new JLabel("몸무게(kg)"); // 레이블 초기화
    JLabel label4 = new JLabel("성별"); // 레이블 초기화
    JLabel label5 = new JLabel("활동량"); // 레이블 초기화
    JTextField textfield1 = new JTextField(10); // age 텍스트필드 초기화
    JTextField textfield2 = new JTextField(10); // height 텍스트필드 초기화
    JTextField textfield3 = new JTextField(10); // weight 텍스트필드 초기화
    JRadioButton rbutton1 = new JRadioButton("여자", true); // gender 라디오버튼
    JRadioButton rbutton2 = new JRadioButton("남자"); // gender 라디오버튼
    JComboBox<String> combobox1 = new JComboBox<String>(Activity.names()); // acitivity 콤보박스

    JButton button1 = new JButton("계산"); // bmi calculator 버튼 초기화

    JLabel label6 = new JLabel("BMI 지수"); // 레이블 초기화
    JLabel label7 = new JLabel("정상 몸무게"); // 레이블 초기화
    JLabel label8 = new JLabel("일일 칼로리섭취량"); // 레이블 초기화
    JTextField textfield4 = new JTextField(10); // bmi 텍스트필드 초기화
    JTextField textfield5 = new JTextField(10); // standard weight 텍스트필드 초기화
    JTextField textfield6 = new JTextField(10); // dailry calory intake 텍스트필드 초기화

    JPanel panel = new JPanel(); // 패널 초기화
    JPanel panel1 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel2 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel3 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel4 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel5 = new JPanel(); // 패널 초기화
    JPanel panel6 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel7 = new JPanel(); // 패널 초기화
    JPanel panel8 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel9 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel10 = new JPanel(new GridLayout(0, 2)); // 패널 초기화

    public BMICalculatorFrame() {
        super("BMICalculatorFrame");

        // frame
        panel1.add(label1);
        panel1.add(textfield1);

        panel2.add(label2);
        panel2.add(textfield2);

        panel3.add(label3);
        panel3.add(textfield3);

        panel5.add(rbutton1);
        panel5.add(rbutton2);
        panel4.add(label4);
        panel4.add(panel5);

        panel6.add(label5);
        panel6.add(combobox1);

        panel7.add(button1);

        panel8.add(label6);
        panel8.add(textfield4);

        panel9.add(label7);
        panel9.add(textfield5);

        panel10.add(label8);
        panel10.add(textfield6);

        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);
        panel.add(panel4);
        panel.add(panel6);
        panel.add(panel7);
        panel.add(panel8);
        panel.add(panel9);
        panel.add(panel10);
        add(panel);

        // event
        textfield1.addKeyListener(this);
        textfield2.addKeyListener(this);
        textfield3.addKeyListener(this);
        button1.addActionListener(this);

        setSize(350, 300);
        setResizable(false);
        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private Gender selectGender() {
        if (rbutton1.isSelected() return Gender.FEMALE;
        return Gender.MALE;
    }

    private Activity selectActivity() {
        return Activity.valueOf(combobox1.getSelectedIndex());
    }

    private void calculateStandardWeightAndBMI() {
        calculator.age = Integer.parseInt(textfield1.getText());
        calculator.height = Double.parseDouble(textfield2.getText());
        calculator.weight = Double.parseDouble(textfield3.getText());
        calculator.gender = selectGender();
        calculator.activity = selectActivity();
        System.out.println("age=" + calculator.age);
        System.out.println("height=" + calculator.height);
        System.out.println("weight=" + calculator.weight);
        System.out.println("gender=" + calculator.gender);
        System.out.println("activity=" + calculator.activity);
        BMI bmi = calculator.calculateBMI();
        double sw = calculator.calculateStandardWeight();
        double dailyIntake = calculator.calculateDailyCaloryIntake();
        textfield4.setText("" + bmi);
        textfield5.setText("" + sw);
        textfield6.setText("" + dailyIntake);
    }

    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        if (button == button1) {
            calculateStandardWeightAndBMI(); // 계산
        }
    }
    
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            Toolkit.getDefaultToolkit().beep(); 
            System.out.println("ENTER pressed");
            calculateStandardWeightAndBMI(); // 계산
        }
    }

    public static void main(String[] args) {
        new BMICalculatorFrame();
    }
}