본문 바로가기

실시간 운영체제

[실시간 운영체제] 길이 변환기

길이 변환 가능한 코드입니다.

 

 

package com.example.length_converter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText myText;
    private RadioButton RB_from_meter, RB_from_yard,RB_from_mile;
    private RadioButton RB_to_meter, RB_to_yard, RB_to_mile;
    private RadioGroup RB_group_from, RB_group_to;
    private TextView result, notice;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        myText = (EditText) findViewById(R.id.myText);

        RB_from_meter = (RadioButton) findViewById(R.id.rb_from_meter);
        RB_from_yard = (RadioButton) findViewById(R.id.rb_from_yard);
        RB_from_mile = (RadioButton) findViewById(R.id.rb_from_mile);

        RB_to_meter = (RadioButton) findViewById(R.id.rb_to_meter);
        RB_to_yard = (RadioButton) findViewById(R.id.rb_to_yard);
        RB_to_mile = (RadioButton) findViewById(R.id.rb_to_mile);

        RB_group_from = (RadioGroup) findViewById(R.id.rb_group_from);
        RB_group_to = (RadioGroup) findViewById(R.id.rb_group_to);

        result = (TextView) findViewById(R.id.result_design);
        notice = (TextView) findViewById(R.id.notice_design);
    }

    public void click_convert(View view)
    {
        String input_string = myText.getText().toString();

        if (input_string.length() != 0) {

            notice.setText("");

            float input_length = Float.parseFloat(input_string);
            float output_length;

            if (RB_from_meter.isChecked()) {
                if (RB_to_yard.isChecked()) {
                    output_length = input_length * 1.094f;
                    result.setText("meter = " + String.valueOf(output_length) + "yard");
                } else if (RB_to_mile.isChecked()) {
                    output_length = input_length * 0.000621371f;
                    result.setText("meter = " + String.valueOf(output_length) + "mile");
                } else {
                    notice.setText("Please make your choice...");
                }
            } else if (RB_from_yard.isChecked()) {
                if (RB_to_meter.isChecked()) {
                    output_length = input_length * 0.9144f;
                    result.setText("yard = " + String.valueOf(output_length) + "meter");
                } else if (RB_to_mile.isChecked()) {
                    output_length = input_length * 0.000568182f;
                    result.setText("yard = " + String.valueOf(output_length) + "mile");
                } else {
                    notice.setText("Please make your choice...");
                }
            } else if (RB_from_mile.isChecked()) {
                if (RB_to_meter.isChecked()) {
                    output_length = input_length * 1609.34f;
                    result.setText("mile = " + String.valueOf(output_length) + "meter");
                } else if (RB_to_yard.isChecked()) {
                    output_length = input_length * 1760f;
                    result.setText("mile = " + String.valueOf(output_length) + "yard");
                } else {
                    notice.setText("Please make your choice...");
                }
            }
            else{
                notice.setText("Please make your choice...");
            }
        }
        else{
            notice.setText("Please insert a length...");
        }

    }

    public void click_clear(View view)
    {
        myText.setText("");
        result.setText("");

        RB_group_from.clearCheck();
        RB_group_to.clearCheck();

        RB_to_meter.setEnabled(true);
        RB_to_yard.setEnabled(true);
        RB_to_mile.setEnabled(true);
    }

    public void click_rb_from_meter (View view)
    {
        RB_to_meter.setEnabled(false);
        RB_to_yard.setEnabled(true);
        RB_to_mile.setEnabled(true);

        RB_group_to.clearCheck();

        result.setText("meter = ");
    }
    public void click_rb_from_yard (View view)
    {
        RB_to_meter.setEnabled(true);
        RB_to_yard.setEnabled(false);
        RB_to_mile.setEnabled(true);

        RB_group_to.clearCheck();

        result.setText("yard = ");
    }
    public void click_rb_from_mile (View view)
    {
        RB_to_meter.setEnabled(true);
        RB_to_yard.setEnabled(true);
        RB_to_mile.setEnabled(false);

        RB_group_to.clearCheck();

        result.setText("mile = ");
    }

}