Mass Units Converter (Java)

in #progranming7 years ago (edited)

714/5000
Hello again, here I bring a preview of an application that I am working on, this is an erasure of what will be just a mini function to make conversions of units of mass, the long-term idea is to make a great application that will allow us perform calculations, analysis, graphs, etc. of Secondary and University Physics problems.

The algorithm: it is simple is based on the use of float variables, to save momoria space, and the use of the structure in case, or "switch case".

I took this graph as a reference that gives us an idea of what is being executed (it is not a flow diagram at all, it is only the graphic reference to perform the calculations)

Imagen5.5.png
package conversor.de.unidades.de.masa;

import javax.swing.JOptionPane;

/**


  • */
    public class ConversorDeUnidadesDeMasa {@author ROBERT

    /**


    • */
      public static void main(String[] args) {
      // TODO code application logic here@param args the command line arguments

      float gramo,decigramo,centigramo,miligramo,decagramo,hectogramo,kilogramo,conversion;
      int opcion_p,opcion_kg,opcion_hg,opcion_dag,opcion_g,opcion_dg,opcion_cg,opcion_mg;

      opcion_p=Integer.parseInt(JOptionPane.showInputDialog(null, "Conversor de Unidades de Masa\n\n"
      +"1)Kilogramo\n"
      + "2)Hectogramo\n"
      + "3)Decagramo\n"
      + "4)Gramo\n"
      + "5)Decigramo\n"
      + "6)Centigramo\n"
      + "7)Miligramo\n"
      + "8)Salir"));

      switch(opcion_p) {
      case 1:kilogramo=Float.parseFloat(JOptionPane.showInputDialog(null, "Digite cantidad en kg"));
      opcion_kg=Integer.parseInt(JOptionPane.showInputDialog(null, "Convertir a: \n"
      + "1)Hectogramo\n"
      + "2)Decagramo\n"
      + "3)Gramo\n"
      + "4)Decigramo\n"
      + "5)Centigramo\n"
      + "6)Miligramo\n"));
      switch(opcion_kg) {
      case 1: conversion=kilogramo10;
      JOptionPane.showMessageDialog(null, +conversion+" hg");
      break;
      case 2:conversion=kilogramo
      100;
      JOptionPane.showMessageDialog(null, +conversion+" dag");
      break;
      case 3:conversion=kilogramo1000;
      JOptionPane.showMessageDialog(null, +conversion+" g");
      break;
      case 4:conversion=kilogramo
      10000;
      JOptionPane.showMessageDialog(null, +conversion+" dg");
      break;
      case 5:conversion=kilogramo100000;
      JOptionPane.showMessageDialog(null, +conversion+" cg");
      break;
      case 6:conversion=kilogramo
      1000000;
      JOptionPane.showMessageDialog(null, +conversion+" mg");
      break;
      default:
      JOptionPane.showMessageDialog(null, "Opcion Invalida");
      break;

      }
      case 2:hectogramo=Float.parseFloat(JOptionPane.showInputDialog(null, "Digite cantidad en hg"));
      opcion_hg=Integer.parseInt(JOptionPane.showInputDialog(null, "Convertir a: \n"
      + "1)Kilogramo\n"
      + "2)Decagramo\n"
      + "3)Gramo\n"
      + "4)Decigramo\n"
      + "5)Centigramo\n"
      + "6)Miligramo\n"));
      switch(opcion_hg) {
      case 1: conversion=hectogramo/10;
      JOptionPane.showMessageDialog(null, +conversion+" kg");
      break;
      case 2:conversion=hectogramo10;
      JOptionPane.showMessageDialog(null, +conversion+" dag");
      break;
      case 3:conversion=hectogramo
      100;
      JOptionPane.showMessageDialog(null, +conversion+" g");
      break;
      case 4:conversion=hectogramo1000;
      JOptionPane.showMessageDialog(null, +conversion+" dg");
      break;
      case 5:conversion=hectogramo
      10000;
      JOptionPane.showMessageDialog(null, +conversion+" cg");
      break;
      case 6:conversion=hectogramo100000;
      JOptionPane.showMessageDialog(null, +conversion+" mg");
      break;
      default:
      JOptionPane.showMessageDialog(null, "Opcion Invalida");
      break;
      }
      case 3:decagramo=Float.parseFloat(JOptionPane.showInputDialog(null, "Digite cantidad en dag"));
      opcion_dag=Integer.parseInt(JOptionPane.showInputDialog(null, "Convertir a: \n"
      + "1)Kilogramo\n"
      + "2)Hectogramo\n"
      + "3)Gramo\n"
      + "4)Decigramo\n"
      + "5)Centigramo\n"
      + "6)Miligramo\n"));
      switch(opcion_dag) {
      case 1: conversion=decagramo/100;
      JOptionPane.showMessageDialog(null, +conversion+" kg");
      break;
      case 2:conversion=decagramo/10;
      JOptionPane.showMessageDialog(null, +conversion+" hg");
      break;
      case 3:conversion=decagramo
      10;
      JOptionPane.showMessageDialog(null, +conversion+" g");
      break;
      case 4:conversion=decagramo100;
      JOptionPane.showMessageDialog(null, +conversion+" dg");
      break;
      case 5:conversion=decagramo
      1000;
      JOptionPane.showMessageDialog(null, +conversion+" cg");
      break;
      case 6:conversion=decagramo*10000;
      JOptionPane.showMessageDialog(null, +conversion+" mg");
      break;
      default:
      JOptionPane.showMessageDialog(null, "Opcion Invalida");
      break;
      }

      case 4:gramo=Float.parseFloat(JOptionPane.showInputDialog(null, "Digite cantidad en g"));
      opcion_g=Integer.parseInt(JOptionPane.showInputDialog(null, "Convertir a: \n"
      + "1)Kilogramo\n"
      + "2)Hectogramo\n"
      + "3)decagramo\n"
      + "4)Decigramo\n"
      + "5)Centigramo\n"
      + "6)Miligramo\n"));
      switch(opcion_g) {
      case 1: conversion=gramo/1000;
      JOptionPane.showMessageDialog(null, +conversion+" kg");
      break;
      case 2:conversion=gramo/100;
      JOptionPane.showMessageDialog(null, +conversion+" hg");
      break;
      case 3:conversion=gramo/10;
      JOptionPane.showMessageDialog(null, +conversion+" dag");
      break;
      case 4:conversion=gramo10;
      JOptionPane.showMessageDialog(null, +conversion+" dg");
      break;
      case 5:conversion=gramo
      100;
      JOptionPane.showMessageDialog(null, +conversion+" cg");
      break;
      case 6:conversion=gramo1000;
      JOptionPane.showMessageDialog(null, +conversion+" mg");
      break;
      default:
      JOptionPane.showMessageDialog(null, "Opcion Invalida");
      break;
      }
      case 5:decigramo=Float.parseFloat(JOptionPane.showInputDialog(null, "Digite cantidad en dg"));
      opcion_dg=Integer.parseInt(JOptionPane.showInputDialog(null, "Convertir a: \n"
      + "1)Kilogramo\n"
      + "2)Hectogramo\n"
      + "3)decagramo\n"
      + "4)gramo\n"
      + "5)Centigramo\n"
      + "6)Miligramo\n"));
      switch(opcion_dg) {
      case 1: conversion=decigramo/10000;
      JOptionPane.showMessageDialog(null, +conversion+" kg");
      break;
      case 2:conversion=decigramo/1000;
      JOptionPane.showMessageDialog(null, +conversion+" hg");
      break;
      case 3:conversion=decigramo/100;
      JOptionPane.showMessageDialog(null, +conversion+" dag");
      break;
      case 4:conversion=decigramo/10;
      JOptionPane.showMessageDialog(null, +conversion+" g");
      break;
      case 5:conversion=decigramo
      10;
      JOptionPane.showMessageDialog(null, +conversion+" cg");
      break;
      case 6:conversion=decigramo100;
      JOptionPane.showMessageDialog(null, +conversion+" mg");
      break;
      default:
      JOptionPane.showMessageDialog(null, "Opcion Invalida");
      break;
      }
      case 6:centigramo=Float.parseFloat(JOptionPane.showInputDialog(null, "Digite cantidad en cg"));
      opcion_cg=Integer.parseInt(JOptionPane.showInputDialog(null, "Convertir a: \n"
      + "1)Kilogramo\n"
      + "2)Hectogramo\n"
      + "3)decagramo\n"
      + "4)gramo\n"
      + "5)decigramo\n"
      + "6)Miligramo\n"));
      switch(opcion_cg) {
      case 1: conversion=centigramo/100000;
      JOptionPane.showMessageDialog(null, +conversion+" kg");
      break;
      case 2:conversion=centigramo/10000;
      JOptionPane.showMessageDialog(null, +conversion+" hg");
      break;
      case 3:conversion=centigramo/1000;
      JOptionPane.showMessageDialog(null, +conversion+" dag");
      break;
      case 4:conversion=centigramo/100;
      JOptionPane.showMessageDialog(null, +conversion+" g");
      break;
      case 5:conversion=centigramo/10;
      JOptionPane.showMessageDialog(null, +conversion+" dg");
      break;
      case 6:conversion=centigramo
      10;
      JOptionPane.showMessageDialog(null, +conversion+" mg");
      break;
      default:
      JOptionPane.showMessageDialog(null, "Opcion Invalida");
      break;
      }
      case 7:miligramo=Float.parseFloat(JOptionPane.showInputDialog(null, "Digite cantidad en mg"));
      opcion_mg=Integer.parseInt(JOptionPane.showInputDialog(null, "Convertir a: \n"
      + "1)Kilogramo\n"
      + "2)Hectogramo\n"
      + "3)Decagramo\n"
      + "4)Gramo\n"
      + "5)Decigramo\n"
      + "6)Centigramo\n"));
      switch(opcion_mg) {
      case 1: conversion=miligramo/1000000;
      JOptionPane.showMessageDialog(null, +conversion+" kg");
      break;
      case 2:conversion=miligramo/100000;
      JOptionPane.showMessageDialog(null, +conversion+" hg");
      break;
      case 3:conversion=miligramo/10000;
      JOptionPane.showMessageDialog(null, +conversion+" dag");
      break;
      case 4:conversion=miligramo/1000;
      JOptionPane.showMessageDialog(null, +conversion+" g");
      break;
      case 5:conversion=miligramo/100;
      JOptionPane.showMessageDialog(null, +conversion+" dg");
      break;
      case 6:conversion=miligramo/10;
      JOptionPane.showMessageDialog(null, +conversion+" cg");
      break;
      default:
      JOptionPane.showMessageDialog(null, "Opcion Invalida");
      break;
      }
      case 8:
      break;
      }

    }

}

Sort:  

Congratulations @hydrastudio99! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

<p dir="auto"><a href="http://steemitboard.com/@hydrastudio99" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/post4day.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/post4day.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/post4day.png 2x" /> You published 4 posts in one day <p dir="auto">Click on any badge to view your own Board of Honor on SteemitBoard.<br /> For more information about SteemitBoard, click <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here <p dir="auto">If you no longer want to receive notifications, reply to this comment with the word <code>STOP <blockquote> <p dir="auto">By upvoting this notification, you can help all Steemit users. Learn how <a href="https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here!