bài 9

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.4 kB
2
Indexable
Never
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package s00009;

import java.util.Scanner;

/**
 *
 * @author MSI GTX
 */
public class Calculator {

    Scanner sc = new Scanner(System.in);
    private double bmi;
    private double weight;
    private double height;
    private double memory;

    /**
     * create menus suitable to perform functions
     *
     */
    public void menu() {
        System.out.println("1.Normal Calculator");
        System.out.println("2.BMI Calculator");
        System.out.println("3.Exit");
        System.out.print("Enter your Option: ");
    }

    /**
     * menu functions
     */
    public void formatMenu() {
        while (true) {
            menu();
            int Option = checkOption();
            switch (Option) {
                case 1:
                    normalCalculator();
                    break;
                case 2:
                    BMICalculator();
                    break;
                case 3:
                    return;
            }
        }

    }

    /**
     * Check user input number limit
     *
     * @return
     */
    public int checkOption() {
        //loop until user input correct
        while (true) {
            try {
                int result = Integer.parseInt(sc.nextLine().trim());
                if (result < 1 || result > 3) {
                    throw new NumberFormatException();
                }
                return result;
            } catch (NumberFormatException e) {
                System.out.println("Please enter number limt [1-3]");
                System.out.print("Enter again: ");
            }
        }
    }

    /**
     * Check weight and height must be greater than 0
     *
     * @return
     */
    public double checkInputBMI() {
        //loop until user input correct
        while (true) {
            try {
                double result = Double.parseDouble(sc.nextLine().trim());
                if (result <= 0) {
                    throw new NumberFormatException();
                }
                return result;
            } catch (NumberFormatException e) {
                System.out.println("Please enter BMI is digit > 0");
                System.out.print("Enter again: ");
            }
        }
    }

    /**
     * Check division cannot equal 0
     *
     * @return
     */
    public double checkMemory() {
        //loop until user input correct
        System.out.print("Enter number: ");
        while (true) {
            try {
                double result = Double.parseDouble(sc.nextLine());
                if (result == 0) {
                    throw new NumberFormatException();
                }
                return result;
            } catch (NumberFormatException e) {
                System.out.println("The divisor must be greater than zero");
                System.out.print("Enter again: ");
            }
        }
    }

    /**
     * Check whether the input number is real or not
     *
     * @return
     */
    public double checkNumber() {
        //loop until user input correct
        System.out.print("Enter number: ");
        while (true) {
            try {
                double result = Double.parseDouble(sc.nextLine());
                if (result >= 0 || result <= 0) {
                    return result;
                }
            } catch (NumberFormatException e) {
                System.out.println("Please enter real number");
                System.out.print("Enter again: ");
            }
        }
    }

    /**
     * Allow user input operator
     *
     * @return
     */
    public String checkOperator() {
        //loop until user input correct
        while (true) {
            String result = sc.nextLine().trim();
            if (result.isEmpty()) {
                System.err.println("Not empty");
            } else if (result.equalsIgnoreCase("+") || result.equalsIgnoreCase("-")
                    || result.equalsIgnoreCase("*") || result.equalsIgnoreCase("/")
                    || result.equalsIgnoreCase("^") || result.equalsIgnoreCase("=")) {
                return result;
            } else {
                System.err.println("Please input (+, -, *, /, ^)");
            }
            System.out.print("Enter again: ");
        }
    }

    /**
     * Let the user enter the calculation
     */
    public void normalCalculator() {
        memory = checkNumber();
        while (true) {
            System.out.print("Enter operator: ");
            String operator = checkOperator();
            if (operator.equalsIgnoreCase("+")) {
                memory += checkNumber();
                System.out.println("Memory: " + memory);
                System.out.println("You can enter (+, -, *, /, ^) to continue or '=' finish and return to the menu");
            }
            if (operator.equalsIgnoreCase("-")) {
                memory -= checkNumber();
                System.out.println("Memory: " + memory);
                System.out.println("You can enter (+, -, *, /, ^) to continue or '=' finish and return to the menu");
            }
            if (operator.equalsIgnoreCase("*")) {
                memory *= checkNumber();
                System.out.println("Memory: " + memory);
                System.out.println("You can enter (+, -, *, /, ^) to continue or '=' finish and return to the menu");
            }
            if (operator.equalsIgnoreCase("/")) {
                memory /= checkMemory();
                System.out.println("Memory: " + memory);
                System.out.println("You can enter (+, -, *, /, ^) to continue or '=' finish and return to the menu");
            }
            if (operator.equalsIgnoreCase("^")) {
                memory = Math.pow(memory, checkNumber());
                System.out.println("Memory: " + memory);
                System.out.println("You can enter (+, -, *, /, ^) to continue or '=' finish and return to the menu");
            }
            if (operator.equalsIgnoreCase("=")) {
                System.out.println("Result: " + memory);
                return;
            }
        }

    }

    /**
     * Display result BMI status
     *
     * @return
     */
    public String BMIStatus() {
        if (bmi < 19) {
            return "Under-standard.";
        } else if (bmi >= 19 && bmi < 25) {
            return "Standard.";
        } else if (bmi >= 25 && bmi < 30) {
            return "Overweight.";
        } else if (bmi >= 30 && bmi < 40) {
            return "Fat-should lose weight";
        } else {
            return "Very fat - should lose weight immediately";
        }
    }

    /**
     * Allow user BMI calculator
     */
    public void BMICalculator() {
        System.out.print("Enter Weight(kg): ");
        weight = checkInputBMI();
        System.out.print("Enter Height(cm): ");
        height = checkInputBMI();
        bmi = weight * 10000 / (height * height);
        System.out.printf("BMI number: %.2f\n", bmi);
        System.out.println("BMI Status: " + BMIStatus());
    }

}