«« ( Date ) »» // «« ( Thread ) »» // si3ts - 2011

SI3TS DZ2 - Ivan Gavrilovic, 0167/08

by IvanG
sreda, 05. januar 2011 - 14:00.

http://www.javadb.com/weight-converter-pound-to-kilogram-ounce-to-kilogram

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

/**
 *
 * @author www.javadb.com
 */
public class Main {

    public void start() throws IOException {

        boolean inputOk = false;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        //1 ounce = 0.0283495231 kilograms
        //1 pound = 0.45359237 kilograms

        double pound = 0;
        while (!inputOk) {
            System.out.println("Enter number in pound:");
            try {
                pound = Double.parseDouble(reader.readLine().trim());
                inputOk = true;
                if (pound < 0 || pound > Double.MAX_VALUE)
                {
                	
                	System.out.println("Number is not valid, try again.");
                	inputOk = false;
                }
                
            } catch (NumberFormatException e) {
                System.out.println("Invalid number, try again.");
            }
        }
        System.out.println(pound + " pound is equal to " + getPoundToKg(pound) + " kgs & " + getPoundToGrams(pound) + " grams");

        inputOk = false;
        double ounce = 0;
        while (!inputOk) {
            System.out.println("Enter number in ounce:");
            try {
                ounce = Double.parseDouble(reader.readLine().trim());
                inputOk = true;
                if (ounce < 0 || ounce > Double.MAX_VALUE)
                {
                	
                	System.out.println("Number is not valid, try again.");
                	inputOk = false;
                }
                
            } catch (NumberFormatException e) {
                System.out.println("Invalid number, try again.");
            }
        }
        System.out.println(ounce + " ounce is equal to " + getOunceToKg(ounce) + " kgs & " + getOunceToGrams(ounce) + " grams");
    }
    
    private int getPoundToKg(double pound) {
      
        double kg = pound * 0.45359237;
        return (int)Math.floor(kg);
    }

    private double getPoundToGrams(double pound) {
      
        double kg = pound * 0.45359237;
        return (kg - getPoundToKg(pound)) * 1000;
    }

    private int getOunceToKg(double ounce) {
      
        double kg = ounce * 0.0283495231;
        return (int)Math.floor(kg);
    }

    private double getOunceToGrams(double ounce) {
      
        double kg = ounce * 0.0283495231;
        return (kg - getOunceToKg(ounce)) * 1000;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {

            new Main().start();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}