├── src ├── main │ ├── resources │ │ ├── META-INF │ │ │ └── MANIFEST.MF │ │ └── images │ │ │ ├── icon.png │ │ │ ├── mass.png │ │ │ ├── money.png │ │ │ ├── distance.png │ │ │ ├── datastorage.png │ │ │ └── temperature.png │ └── java │ │ ├── GUI │ │ ├── ScreenProperties.java │ │ ├── screens │ │ │ ├── ScreenMass.java │ │ │ ├── ScreenMoney.java │ │ │ ├── ScreenLength.java │ │ │ ├── ScreenTemperature.java │ │ │ ├── ScreenDataStorage.java │ │ │ └── Screen.java │ │ ├── util │ │ │ ├── DistanceRenderer.java │ │ │ └── Util.java │ │ ├── Window.java │ │ └── NavBar.java │ │ ├── App.java │ │ ├── service │ │ ├── Coin.java │ │ ├── MoneyService.java │ │ └── Api.java │ │ └── units │ │ ├── Mass.java │ │ ├── Length.java │ │ ├── DataStorage.java │ │ ├── Unit.java │ │ ├── Temperature.java │ │ └── Money.java └── test │ └── java │ └── units │ ├── DataStorageTest.java │ ├── DistanceTest.java │ ├── Tester.java │ └── TemperatureTest.java ├── out └── artifacts │ └── Conversor_jar │ └── Conversor.jar ├── .gitignore ├── pom.xml └── README.md /src/main/resources/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Main-Class: App 3 | 4 | -------------------------------------------------------------------------------- /src/main/resources/images/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HugoJhonathan/Challenge-Oracle-ONE-conversordemoedas/HEAD/src/main/resources/images/icon.png -------------------------------------------------------------------------------- /src/main/resources/images/mass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HugoJhonathan/Challenge-Oracle-ONE-conversordemoedas/HEAD/src/main/resources/images/mass.png -------------------------------------------------------------------------------- /src/main/resources/images/money.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HugoJhonathan/Challenge-Oracle-ONE-conversordemoedas/HEAD/src/main/resources/images/money.png -------------------------------------------------------------------------------- /src/main/resources/images/distance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HugoJhonathan/Challenge-Oracle-ONE-conversordemoedas/HEAD/src/main/resources/images/distance.png -------------------------------------------------------------------------------- /out/artifacts/Conversor_jar/Conversor.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HugoJhonathan/Challenge-Oracle-ONE-conversordemoedas/HEAD/out/artifacts/Conversor_jar/Conversor.jar -------------------------------------------------------------------------------- /src/main/resources/images/datastorage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HugoJhonathan/Challenge-Oracle-ONE-conversordemoedas/HEAD/src/main/resources/images/datastorage.png -------------------------------------------------------------------------------- /src/main/resources/images/temperature.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HugoJhonathan/Challenge-Oracle-ONE-conversordemoedas/HEAD/src/main/resources/images/temperature.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Eclipse 2 | .classpath 3 | .projectg 4 | .settings/ 5 | 6 | # Intellij 7 | .idea/ 8 | *.iml 9 | *.iws 10 | 11 | # Mac 12 | .DS_Store 13 | 14 | # Maven 15 | log/ 16 | target/ -------------------------------------------------------------------------------- /src/main/java/GUI/ScreenProperties.java: -------------------------------------------------------------------------------- 1 | package GUI; 2 | 3 | import javax.swing.*; 4 | 5 | public interface ScreenProperties { 6 | 7 | ImageIcon getImageIcon(); 8 | 9 | String getName(); 10 | 11 | String getTitle(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/App.java: -------------------------------------------------------------------------------- 1 | import GUI.Window; 2 | import com.formdev.flatlaf.FlatDarkLaf; 3 | import service.MoneyService; 4 | 5 | import javax.swing.*; 6 | 7 | public class App { 8 | 9 | public static void main(String[] args) throws Exception { 10 | FlatDarkLaf.setup(); 11 | SwingUtilities.invokeLater(Window::new); 12 | MoneyService.updateDollarEquivalenceOfCoins(); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /src/main/java/GUI/screens/ScreenMass.java: -------------------------------------------------------------------------------- 1 | package GUI.screens; 2 | 3 | import GUI.util.Util; 4 | import units.Mass; 5 | import units.Unit; 6 | 7 | import javax.swing.*; 8 | 9 | public class ScreenMass extends Screen { 10 | 11 | @Override 12 | public Unit[] getValues() { 13 | return Mass.getAll(); 14 | } 15 | 16 | @Override 17 | public String getName() { 18 | return "Massa"; 19 | } 20 | 21 | @Override 22 | public ImageIcon getImageIcon() { 23 | return Util.getImageIcon("images/mass.png"); 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/GUI/screens/ScreenMoney.java: -------------------------------------------------------------------------------- 1 | package GUI.screens; 2 | 3 | import GUI.util.Util; 4 | import units.Money; 5 | import units.Unit; 6 | 7 | import javax.swing.*; 8 | 9 | public class ScreenMoney extends Screen { 10 | 11 | @Override 12 | public Unit[] getValues() { 13 | return Money.getAll(); 14 | } 15 | 16 | @Override 17 | public String getName() { 18 | return "Moedas"; 19 | } 20 | 21 | @Override 22 | public ImageIcon getImageIcon() { 23 | return Util.getImageIcon("images/money.png"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/service/Coin.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | public class Coin { 4 | private String code; 5 | private String codein; 6 | private String name; 7 | private double high; 8 | private double low; 9 | private double varBid; 10 | private double pctChange; 11 | private double bid; 12 | private double ask; 13 | private String timestamp; 14 | private String create_date; 15 | 16 | public String getCode() { 17 | return code; 18 | } 19 | 20 | public double getBid() { 21 | return bid; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/GUI/screens/ScreenLength.java: -------------------------------------------------------------------------------- 1 | package GUI.screens; 2 | 3 | import GUI.util.Util; 4 | import units.Length; 5 | import units.Unit; 6 | 7 | import javax.swing.*; 8 | 9 | public class ScreenLength extends Screen { 10 | 11 | @Override 12 | public Unit[] getValues() { 13 | return Length.getAll(); 14 | } 15 | 16 | @Override 17 | public String getName() { 18 | return "Comprimento"; 19 | } 20 | 21 | @Override 22 | public ImageIcon getImageIcon() { 23 | return Util.getImageIcon("images/distance.png"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/GUI/util/DistanceRenderer.java: -------------------------------------------------------------------------------- 1 | package GUI.util; 2 | 3 | import units.Unit; 4 | 5 | import javax.swing.*; 6 | import java.awt.*; 7 | 8 | public class DistanceRenderer extends DefaultListCellRenderer { 9 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 10 | super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 11 | Unit valUnit = (Unit) value; 12 | setText(valUnit.getSymbol() + " - " + valUnit.getName()); 13 | return this; 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/GUI/screens/ScreenTemperature.java: -------------------------------------------------------------------------------- 1 | package GUI.screens; 2 | 3 | import GUI.util.Util; 4 | import units.Temperature; 5 | import units.Unit; 6 | 7 | import javax.swing.*; 8 | 9 | public class ScreenTemperature extends Screen { 10 | 11 | @Override 12 | public Unit[] getValues() { 13 | return Temperature.getAll(); 14 | } 15 | 16 | @Override 17 | public String getName() { 18 | return "Temperatura"; 19 | } 20 | 21 | @Override 22 | public ImageIcon getImageIcon() { 23 | return Util.getImageIcon("images/temperature.png"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/GUI/screens/ScreenDataStorage.java: -------------------------------------------------------------------------------- 1 | package GUI.screens; 2 | 3 | import GUI.util.Util; 4 | import units.DataStorage; 5 | import units.Unit; 6 | 7 | import javax.swing.*; 8 | 9 | public class ScreenDataStorage extends Screen { 10 | 11 | @Override 12 | public Unit[] getValues() { 13 | return DataStorage.getAll(); 14 | } 15 | 16 | @Override 17 | public String getName() { 18 | return "Armazenamento"; 19 | } 20 | 21 | @Override 22 | public ImageIcon getImageIcon() { 23 | return Util.getImageIcon("images/datastorage.png"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/units/DataStorageTest.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import org.junit.Test; 4 | 5 | import static units.DataStorage.*; 6 | import static units.Tester.test; 7 | 8 | 9 | public class DataStorageTest { 10 | 11 | @Test 12 | public void dataStorage() { 13 | test(1, BIT).isEquals(0.125, BYTE); 14 | test(1, BYTE).isEquals(1, BYTE); 15 | test(1, KILOBYTE).isEquals(1024, BYTE); 16 | test(1, MEGABYTE).isEquals(1048576, BYTE); 17 | test(1, GIGABYTE).isEquals(1073741824.0, BYTE); 18 | test(1, TERABYTE).isEquals(1099511627776.0, BYTE); 19 | test(1, PETABYTE).isEquals(1125899906842624.0, BYTE); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/units/DistanceTest.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import org.junit.Test; 4 | 5 | import static units.Length.*; 6 | import static units.Tester.test; 7 | 8 | public class DistanceTest { 9 | 10 | @Test 11 | public void milimetro() { 12 | test(100, MILIMETRO).isEquals(100, MILIMETRO); 13 | test(100, MILIMETRO).isEquals(10, CENTIMETRO); 14 | test(100, MILIMETRO).isEquals(1, DECIMETRO); 15 | test(100, MILIMETRO).isEquals(0.1, METRO); 16 | test(100, MILIMETRO).isEquals(0.01, DECAMETRO); 17 | test(100, MILIMETRO).isEquals(0.001, HECTOMETRO); 18 | test(100, MILIMETRO).isEquals(0.0001, QUILOMETRO); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/service/MoneyService.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | import units.Money; 4 | 5 | import java.util.Map; 6 | import java.util.stream.Collectors; 7 | 8 | public class MoneyService { 9 | 10 | private static String joinedWithUSD = Money.getAllSymbols() 11 | .stream() 12 | .filter(e -> !e.equals("USD")) 13 | .map(e -> e + "-USD") 14 | .collect(Collectors.joining(",")); 15 | 16 | public static void updateDollarEquivalenceOfCoins() throws Exception { 17 | Map m = Api.findAllCurrenciesDollarEquivalent(joinedWithUSD); 18 | m.forEach((key, value) -> { 19 | String code = value.getCode(); 20 | double dollarEquivalent = value.getBid(); 21 | Money.getBySymbol(code).setDollarEquivalent(dollarEquivalent); 22 | }); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/units/Tester.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import org.junit.Assert; 4 | 5 | import java.math.BigDecimal; 6 | 7 | public class Tester { 8 | 9 | public static Builder test(double v, Unit unit) { 10 | return new Builder(v, unit); 11 | } 12 | 13 | public static class Builder { 14 | private BigDecimal value; 15 | private BigDecimal resultEsperado; 16 | private Unit para; 17 | private Unit de; 18 | 19 | private Builder(double v, Unit unit) { 20 | value = new BigDecimal(String.valueOf(v)); 21 | de = unit; 22 | } 23 | 24 | public void isEquals(double result, Unit para_) { 25 | para = para_; 26 | resultEsperado = new BigDecimal(String.valueOf(result)); 27 | BigDecimal res1 = de.convert(value, para); 28 | Assert.assertTrue(resultEsperado.compareTo(res1) == 0); 29 | Assert.assertEquals(0, resultEsperado.compareTo(res1)); 30 | } 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /src/main/java/units/Mass.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | public class Mass extends Unit { 7 | 8 | private static List all = new LinkedList<>(); 9 | 10 | public static final Mass KILOGRAMA = new Mass("Quilograma", "Kg", 1000.0); 11 | public static final Mass HECTOGRAMA = new Mass("Hectograma", "hg", 100.0); 12 | public static final Mass DECAGRAMA = new Mass("Decagrama", "dag", 10.0); 13 | public static final Mass GRAMA = new Mass("Grama", "g", 1.0); 14 | public static final Mass DECOGRAMA = new Mass("Decograma", "dg", 0.1); 15 | public static final Mass CENTIGRAMA = new Mass("Centigrama", "cg", 0.01); 16 | public static final Mass MILIGRAMA = new Mass("Miligrama", "mg", 0.001); 17 | 18 | private Mass(String name, String symbol, double gramsEquivalent) { 19 | super(name, symbol, gramsEquivalent); 20 | all.add(this); 21 | } 22 | 23 | public static Mass[] getAll() { 24 | return all.toArray(new Mass[0]); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/units/Length.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | public class Length extends Unit { 7 | 8 | private static List all = new LinkedList<>(); 9 | 10 | public static final Length MEGA_METRO = new Length("Megametro", "Mm", 1000000.0); 11 | public static final Length QUILOMETRO = new Length("Quilômetro", "km", 1000.0); 12 | public static final Length HECTOMETRO = new Length("Hectômetro", "Hm", 100.0); 13 | public static final Length DECAMETRO = new Length("Decâmetro", "dam", 10.0); 14 | public static final Length METRO = new Length("Metro", "m", 1.0); 15 | public static final Length DECIMETRO = new Length("Decímetro", "dm", 0.1); 16 | public static final Length CENTIMETRO = new Length("Centímetro", "cm", 0.01); 17 | public static final Length MILIMETRO = new Length("Milímetro", "mm", 0.001); 18 | 19 | private Length(String name, String symbol, double mettersEquivalent) { 20 | super(name, symbol, mettersEquivalent); 21 | all.add(this); 22 | } 23 | 24 | public static Length[] getAll() { 25 | return all.toArray(new Length[0]); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/units/DataStorage.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | public class DataStorage extends Unit { 7 | 8 | private static List all = new LinkedList<>(); 9 | 10 | public static final DataStorage PETABYTE = new DataStorage("Petabyte", "PB", 8.0 * 1024 * 1024 * 1024 * 1024 * 1024); 11 | public static final DataStorage TERABYTE = new DataStorage("Terabyte", "TB", 8.0 * 1024 * 1024 * 1024 * 1024); 12 | public static final DataStorage GIGABYTE = new DataStorage("Gigabyte", "GB", 8.0 * 1024 * 1024 * 1024); 13 | public static final DataStorage MEGABYTE = new DataStorage("Megabyte", "MB", 8.0 * 1024 * 1024); 14 | public static final DataStorage KILOBYTE = new DataStorage("Kilobyte", "KB", 8.0 * 1024); 15 | public static final DataStorage BYTE = new DataStorage("Byte", "B", 8); 16 | public static final DataStorage BIT = new DataStorage("bit", "b", 1); 17 | 18 | private DataStorage(String name, String symbol, double bitsEquivalent) { 19 | super(name, symbol, bitsEquivalent); 20 | all.add(this); 21 | } 22 | 23 | public static DataStorage[] getAll() { 24 | return all.toArray(new DataStorage[0]); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/GUI/util/Util.java: -------------------------------------------------------------------------------- 1 | package GUI.util; 2 | 3 | import javax.imageio.ImageIO; 4 | import javax.swing.*; 5 | import java.awt.*; 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.Objects; 9 | 10 | public class Util { 11 | 12 | static public ImageIcon getImageIcon(String path) { 13 | try { 14 | return new ImageIcon(ClassLoader.getSystemResource(path)); 15 | } catch (NullPointerException np) { 16 | System.out.println("ERROR:"); 17 | System.out.println(np.getMessage()); 18 | return null; 19 | } 20 | } 21 | 22 | public static Image getImage(String path) { 23 | Image image = null; 24 | URL url = ClassLoader.getSystemResource(path); 25 | if (Objects.nonNull(url)) { 26 | try { 27 | image = ImageIO.read(url); 28 | } catch (IOException e) { 29 | System.out.println(e.getMessage()); 30 | } 31 | } else { 32 | System.out.println("Image not found: " + path); 33 | } 34 | return image; 35 | } 36 | 37 | public static void increaseFont(Component component, float size) { 38 | Font font = component.getFont(); 39 | component.setFont(font.deriveFont(size)); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/units/Unit.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import java.math.BigDecimal; 4 | import java.math.MathContext; 5 | import java.math.RoundingMode; 6 | 7 | public abstract class Unit { 8 | 9 | private String name; 10 | private String symbol; 11 | protected double factor; 12 | 13 | public Unit(String name, String symbol, double factor) { 14 | this.name = name; 15 | this.symbol = symbol; 16 | this.factor = factor; 17 | } 18 | 19 | public BigDecimal convert(BigDecimal amount, T targetUnit) { 20 | BigDecimal sourceFactorBd = new BigDecimal(String.valueOf(getFactor())); 21 | BigDecimal targetFactorBd = new BigDecimal(String.valueOf(targetUnit.getFactor())); 22 | return amount 23 | .multiply(sourceFactorBd) 24 | .divide(targetFactorBd, MathContext.DECIMAL128) 25 | .setScale(4, RoundingMode.HALF_UP) 26 | .stripTrailingZeros(); 27 | } 28 | 29 | public String getFormattedValue(String value) { 30 | return value + " " + getSymbol(); 31 | } 32 | 33 | public String getSymbol() { 34 | return symbol; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public double getFactor() { 42 | return factor; 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /src/main/java/GUI/Window.java: -------------------------------------------------------------------------------- 1 | package GUI; 2 | 3 | import GUI.screens.*; 4 | import GUI.util.Util; 5 | 6 | import javax.swing.*; 7 | import java.awt.*; 8 | import java.util.Objects; 9 | 10 | public class Window extends JFrame { 11 | 12 | private NavBar navBar; 13 | private JPanel pageContentContainer; 14 | 15 | public Window() { 16 | setDefaultCloseOperation(EXIT_ON_CLOSE); 17 | init(); 18 | setVisible(true); 19 | } 20 | 21 | public NavBar getNavBar() { 22 | if (Objects.nonNull(navBar)) return navBar; 23 | navBar = new NavBar(getPageContentContainer()); 24 | navBar.createItem(new ScreenMoney()); 25 | navBar.createItem(new ScreenLength()); 26 | navBar.createItem(new ScreenDataStorage()); 27 | navBar.createItem(new ScreenMass()); 28 | navBar.createItem(new ScreenTemperature()); 29 | return navBar; 30 | } 31 | 32 | public JPanel getPageContentContainer() { 33 | if (Objects.nonNull(pageContentContainer)) return pageContentContainer; 34 | pageContentContainer = new JPanel(new CardLayout()); 35 | return pageContentContainer; 36 | } 37 | 38 | public void init() { 39 | setIconImage(Util.getImage("images/icon.png")); 40 | setResizable(true); 41 | setSize(600, 600); 42 | setLocationRelativeTo(null); 43 | add(getNavBar(), BorderLayout.WEST); 44 | add(getPageContentContainer(), BorderLayout.CENTER); 45 | getNavBar().markFirstNavItem(); 46 | } 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/service/Api.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.reflect.TypeToken; 5 | 6 | import java.io.BufferedReader; 7 | import java.io.IOException; 8 | import java.io.InputStreamReader; 9 | import java.lang.reflect.Type; 10 | import java.net.HttpURLConnection; 11 | import java.net.SocketException; 12 | import java.net.URL; 13 | import java.net.UnknownHostException; 14 | import java.util.Map; 15 | 16 | public class Api { 17 | 18 | static String url = "https://economia.awesomeapi.com.br/last/"; 19 | static Gson gson = new Gson(); 20 | static Type type = new TypeToken>() { 21 | }.getType(); 22 | 23 | public static Map findAllCurrenciesDollarEquivalent(String codes) throws Exception { 24 | 25 | String urlApi = url + codes; 26 | 27 | try { 28 | URL url = new URL(urlApi); 29 | HttpURLConnection conexao = (HttpURLConnection) url.openConnection(); 30 | 31 | if (conexao.getResponseCode() != 200) { 32 | throw new RuntimeException("HTTP error code : " + conexao.getResponseCode()); 33 | } 34 | 35 | BufferedReader response = new BufferedReader(new InputStreamReader((conexao.getInputStream()))); 36 | String stringOfResponseJson = convertJsonToString(response); 37 | Map coins = gson.fromJson(stringOfResponseJson, type); 38 | return coins; 39 | 40 | } catch (SocketException e) { 41 | throw new SocketException("No internet access!"); 42 | } catch (UnknownHostException e) { 43 | throw new UnknownHostException("Unable to connect to API!"); 44 | } 45 | } 46 | 47 | public static String convertJsonToString(BufferedReader buffereReader) throws IOException { 48 | String response = ""; 49 | String json = ""; 50 | while ((response = buffereReader.readLine()) != null) { 51 | json += response; 52 | } 53 | return json; 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /src/main/java/units/Temperature.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import java.math.BigDecimal; 4 | import java.math.MathContext; 5 | import java.math.RoundingMode; 6 | import java.text.NumberFormat; 7 | import java.util.LinkedList; 8 | import java.util.List; 9 | 10 | public class Temperature extends Unit { 11 | 12 | private static List all = new LinkedList<>(); 13 | private static NumberFormat nf = NumberFormat.getCurrencyInstance(); 14 | 15 | public static final Temperature CELSIUS = new Temperature("Celsius", "°C", 1.0, 0.0); 16 | public static final Temperature FAHRENHEIT = new Temperature("Fahrenheit", "°F", 1.8, 32.0); 17 | public static final Temperature KELVIN = new Temperature("Kelvin", "K", 1.0, 273.15); 18 | public static final Temperature NEWTON = new Temperature("Newton", "°N", 0.33, 0.0); 19 | public static final Temperature REAUMUR = new Temperature("Reaumur", "°Re", 0.8, 0.0); 20 | public static final Temperature RANKINE = new Temperature("Rankine", "°Ra", 1.8, 491.67); 21 | 22 | private double factor2; 23 | 24 | private Temperature(String name, String symbol, double factor1, double factor2) { 25 | super(name, symbol, factor1); 26 | this.factor2 = factor2; 27 | all.add(this); 28 | } 29 | 30 | public double getFactor2() { 31 | return factor2; 32 | } 33 | 34 | @Override 35 | public BigDecimal convert(BigDecimal amount, Temperature targetUnit) { 36 | 37 | BigDecimal sourceFactor1 = new BigDecimal(String.valueOf(getFactor())); 38 | BigDecimal sourceFactor2 = new BigDecimal(String.valueOf(getFactor2())); 39 | BigDecimal targetFactor1 = new BigDecimal(String.valueOf(targetUnit.getFactor())); 40 | BigDecimal targetFactor2 = new BigDecimal(String.valueOf(targetUnit.getFactor2())); 41 | 42 | return amount 43 | .subtract(sourceFactor2) 44 | .divide(sourceFactor1, MathContext.DECIMAL128) 45 | .multiply(targetFactor1) 46 | .add(targetFactor2) 47 | .setScale(4, RoundingMode.HALF_UP) 48 | .stripTrailingZeros(); 49 | } 50 | 51 | public static Temperature[] getAll() { 52 | return all.toArray(new Temperature[0]); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/units/Money.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import service.MoneyService; 4 | 5 | import java.math.BigDecimal; 6 | import java.text.NumberFormat; 7 | import java.util.Arrays; 8 | import java.util.Currency; 9 | import java.util.LinkedList; 10 | import java.util.List; 11 | import java.util.stream.Collectors; 12 | 13 | public class Money extends Unit { 14 | 15 | private static List all = new LinkedList<>(); 16 | private static NumberFormat nf = NumberFormat.getCurrencyInstance(); 17 | 18 | public static final Money REAL = new Money("Real", "BRL", 0); 19 | public static final Money DOLAR = new Money("Dólar", "USD", 1.0); 20 | public static final Money EURO = new Money("Euro", "EUR", 0); 21 | public static final Money LIBRA = new Money("Libra", "GBP", 0); 22 | public static final Money PESO_ARGENTINO = new Money("Peso Argentino", "ARS", 0); 23 | public static final Money PESO_CHILENO = new Money("Peso Chileno", "CLP", 0); 24 | 25 | private Money(String symbol, String name, double dollarEquivalent) { 26 | super(symbol, name, dollarEquivalent); 27 | all.add(this); 28 | } 29 | 30 | public BigDecimal updateAndConvert(BigDecimal amount, Money targetUnit) throws Exception { 31 | MoneyService.updateDollarEquivalenceOfCoins(); 32 | return super.convert(amount, targetUnit); 33 | } 34 | 35 | @Override 36 | public String getFormattedValue(String value) { 37 | nf.setCurrency(Currency.getInstance(getSymbol())); 38 | return nf.format(new BigDecimal(value)); 39 | } 40 | 41 | public static Money[] getAll() { 42 | return all.toArray(new Money[0]); 43 | } 44 | 45 | public void setDollarEquivalent(double dollarEquivalent) { 46 | this.factor = dollarEquivalent; 47 | } 48 | 49 | static public List getAllSymbols() { 50 | return Arrays.stream(getAll()) 51 | .map(coin -> coin.getSymbol()) 52 | .collect(Collectors.toList()); 53 | } 54 | 55 | static public Money getBySymbol(String symbol) { 56 | return Arrays.stream(getAll()) 57 | .filter(coin -> coin.getSymbol().equals(symbol)) 58 | .findFirst() 59 | .orElseThrow(() -> new IllegalArgumentException("There is no monetary value with the symbol " + symbol)); 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | groupId 8 | Conversor 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 8 13 | 8 14 | UTF-8 15 | 16 | 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.13.2 24 | test 25 | 26 | 27 | 28 | 29 | com.formdev 30 | flatlaf 31 | 3.0 32 | 33 | 34 | 35 | 36 | com.google.code.gson 37 | gson 38 | 2.10.1 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-shade-plugin 48 | 3.2.4 49 | 50 | true 51 | 52 | 53 | 54 | package 55 | 56 | shade 57 | 58 | 59 | ${project.artifactId}-${project.version}-jar-with-dependencies 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /src/test/java/units/TemperatureTest.java: -------------------------------------------------------------------------------- 1 | package units; 2 | 3 | import org.junit.Test; 4 | 5 | import static units.Temperature.*; 6 | import static units.Tester.test; 7 | 8 | public class TemperatureTest { 9 | 10 | @Test 11 | public void testConversaoCelsius() { 12 | test(400, CELSIUS).isEquals(400.0, CELSIUS); 13 | test(400, CELSIUS).isEquals(752.0, FAHRENHEIT); 14 | test(400, CELSIUS).isEquals(673.15, KELVIN); 15 | test(400, CELSIUS).isEquals(132, NEWTON); 16 | test(400, CELSIUS).isEquals(320, REAUMUR); 17 | test(400, CELSIUS).isEquals(1211.67, RANKINE); 18 | //test(400, CELSIUS).isEquals(-450, DELISLE); 19 | } 20 | 21 | @Test 22 | public void testConversaoKelvin() { 23 | test(400, KELVIN).isEquals(126.85, CELSIUS); 24 | test(400, KELVIN).isEquals(260.33, FAHRENHEIT); 25 | test(400, KELVIN).isEquals(400.0, KELVIN); 26 | test(400, KELVIN).isEquals(41.8605, NEWTON); 27 | test(400, KELVIN).isEquals(101.48, REAUMUR); 28 | test(400, KELVIN).isEquals(720, RANKINE); 29 | //test(400, KELVIN).isEquals(-40.275, DELISLE); 30 | } 31 | 32 | @Test 33 | public void testConversaoFahrenheit() { 34 | test(400, FAHRENHEIT).isEquals(204.4444, CELSIUS); 35 | test(400, FAHRENHEIT).isEquals(400, FAHRENHEIT); 36 | test(400, FAHRENHEIT).isEquals(477.5944, KELVIN); 37 | test(400, FAHRENHEIT).isEquals(67.4667, NEWTON); 38 | test(400, FAHRENHEIT).isEquals(163.5556, REAUMUR); 39 | test(400, FAHRENHEIT).isEquals(859.67, RANKINE); 40 | //test(400, FAHRENHEIT).isEquals(-156.6667, DELISLE); 41 | } 42 | 43 | @Test 44 | public void testConversaoNewton() { 45 | test(400, NEWTON).isEquals(1212.1212, CELSIUS); 46 | test(400, NEWTON).isEquals(2213.8182, FAHRENHEIT); 47 | test(400, NEWTON).isEquals(1485.2712, KELVIN); 48 | test(400, NEWTON).isEquals(400, NEWTON); 49 | test(400, NEWTON).isEquals(969.6970, REAUMUR); 50 | test(400, NEWTON).isEquals(2673.4882, RANKINE); 51 | //test(400, NEWTON).isEquals(-1668.1818, DELISLE); 52 | } 53 | 54 | @Test 55 | public void testConversaoReaumur() { 56 | test(400, REAUMUR).isEquals(500.0, CELSIUS); 57 | test(400, REAUMUR).isEquals(932, FAHRENHEIT); 58 | test(400, REAUMUR).isEquals(773.15, KELVIN); 59 | test(400, REAUMUR).isEquals(165, NEWTON); 60 | test(400, REAUMUR).isEquals(400.0, REAUMUR); 61 | test(400, REAUMUR).isEquals(1391.67, RANKINE); 62 | //test(400, REAUMUR).isEquals(-600, DELISLE); 63 | } 64 | 65 | @Test 66 | public void testConversaoRankine() { 67 | test(400, RANKINE).isEquals(-50.9278, CELSIUS); 68 | test(400, RANKINE).isEquals(-59.67, FAHRENHEIT); 69 | test(400, RANKINE).isEquals(222.2222, KELVIN); 70 | test(400, RANKINE).isEquals(-16.8062, NEWTON); 71 | test(400, RANKINE).isEquals(-40.7422, REAUMUR); 72 | test(400, RANKINE).isEquals(400, RANKINE); 73 | //test(400, RANKINE).isEquals(226.3917, DELISLE); 74 | } 75 | 76 | // @Test 77 | // public void testConversaoDelisle() { 78 | // test(400, DELISLE).isEquals(-166.6667, CELSIUS); 79 | // test(400, DELISLE).isEquals(-268, FAHRENHEIT); 80 | // test(400, DELISLE).isEquals(106.4833, KELVIN); 81 | // test(400, DELISLE).isEquals(-55, NEWTON); 82 | // test(400, DELISLE).isEquals(-133.3333, REAUMUR); 83 | // test(400, DELISLE).isEquals(191.67, RANKINE); 84 | // test(400, DELISLE).isEquals(400, DELISLE); 85 | // } 86 | } 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | badge-challenge 2 | 3 |

Challenge ONE Back End - Java

4 | 5 |
6 | 7 | 8 |
9 | 10 | ### Sprint 01: Crie seu próprio conversor de moeda 11 | 12 |
13 | 14 | ## História 15 | 16 | Nesta oportunidade foi solicitado para nós Devs a criação de um conversor de moeda utilizando a linguagem Java. As 17 | características solicitadas por nosso cliente são as seguintes: 18 | 19 | O conversor de moeda deverá: 20 | 21 | - Converter de Reais a Dólar 22 | - Converter de Reais a Euro 23 | - Converter de Reais a Libras Esterlinas 24 | - Converter de Reais a Peso Argentino 25 | - Converter de Reais a Peso Chileno 26 | 27 | Lembrando que deve ser possível também converter de forma inversa. 28 | 29 | Como desafio extra incentivamos vocês a deixar fluir sua criatividade, se posso converter moedas, será que posso tal vez 30 | adicionar a meu programa outro tipo de conversões como temperatura por exemplo? 31 | 32 |
33 | 34 | ## Resultado 35 | 36 | 37 | Download JAR 38 | 39 | _(necessário JRE 8 ou superior)_ 40 | 41 | https://user-images.githubusercontent.com/40052440/226792500-a882da58-52c5-4f67-ad39-3cbef97aed18.mp4 42 | 43 | ### Conversão 44 | 45 | - As unidades de conversão são divididas em Classes, cada classe possui suas unidades em forma de atributo estático (que 46 | seria uma instância da Classe). Cada unidade possui um **símbolo**, um **fator multiplicador** (que é usado no método 47 | para fazer a conversão entre as unidades) e o seu **nome**. 48 | - Uma unidade pode ser **convertida para outra** chamando seu próprio método convert, assim: 49 | 50 | ```java 51 | BigDecimal result = REAL.convert(new BigDecimal("1"), DOLAR); 52 | ``` 53 | 54 | ! O fator multiplicador das moedas é o valor equivalente de 1 em relação ao Dolar, para facilitar a conversão entre 55 | elas. 56 | 57 | ### GUI 58 | 59 | - [x] Conforme sugerido para este Challenge, foi utilizado o kit de componentes GUI do próprio Java, chamado **Swing**, 60 | onde podemos criar componentes e interfaces gráficas com o paradígma orientado a objetos. 61 | 62 | - [x] Fácil expansão e adição de novas unidades de conversão 63 | - Para adicionar novas unidades, basta criar uma subclasse 64 | de 65 | Unit. 66 | - Para adicionar a tela referente a essa nova unidade de conversão basta: 67 | 68 | 1- Criar uma classe que estenda a classe 69 | abstrata 70 | Screen. 71 | 72 | 2- Implementar e concretizar os métodos abstratos. 73 | 74 | 3- Adicionar essa nova tela ao objeto na NavBar, 75 | conforme 76 | estes. 77 | - Para criar qualquer outra tela, basta criar um classe que estenda de JPanel e implemente a 78 | interface 79 | Screen_Properties e adicioná-la à NavBar. 80 | 81 |
82 | 83 | ## Tecnologias utilizadas 84 | 85 | - Linguagem: Java 8 86 | - IDE: IntelliJ IDEA 87 | - Inteface (GUI): Swing c/ FlatLaf 88 | - Testes: JUnit 89 | - Currency API: AwesomeAPI-api-de-moedas 90 | 91 |

92 | [](https://www.oracle.com/br/education/oracle-next-education/) 93 | Aplicação Desktop de **Conversor de Unidades** desenvolvido como Challenge, durante a formação Backend Java, do 94 | programa ONE (Oracle Next Education) através da 95 | plataforma de ensino Alura. 96 | -------------------------------------------------------------------------------- /src/main/java/GUI/NavBar.java: -------------------------------------------------------------------------------- 1 | package GUI; 2 | 3 | import GUI.screens.Screen; 4 | 5 | import javax.swing.*; 6 | import javax.swing.border.Border; 7 | import javax.swing.border.EmptyBorder; 8 | import javax.swing.plaf.ColorChooserUI; 9 | import javax.swing.plaf.ColorUIResource; 10 | import java.awt.*; 11 | import java.awt.event.MouseAdapter; 12 | import java.awt.event.MouseEvent; 13 | import java.io.IOException; 14 | import java.net.URI; 15 | import java.net.URISyntaxException; 16 | import java.util.Arrays; 17 | import java.util.HashMap; 18 | import java.util.Map; 19 | import java.util.Objects; 20 | 21 | public class NavBar extends JPanel { 22 | 23 | private JLabel title; 24 | private JPanel navItems; 25 | private JPanel footer; 26 | private final JPanel pageContentContainer; 27 | private final CardLayout cardLayout; 28 | private final Map navItemAndPainel = new HashMap<>(); 29 | private final Color navBarColor = new Color(17, 17, 17); 30 | private final Color navBarSelected = new Color(0, 0, 0); 31 | private final String hiperlink = "Developed by hugo_"; 32 | 33 | public NavBar(JPanel component) { 34 | pageContentContainer = component; 35 | cardLayout = (CardLayout) component.getLayout(); 36 | init(); 37 | } 38 | 39 | public void init() { 40 | setLayout(new BorderLayout(0, 0)); 41 | setBackground(navBarColor); 42 | setPreferredSize(new Dimension(180, 0)); 43 | add(getTitle(), BorderLayout.PAGE_START); 44 | add(getNavItems(), BorderLayout.CENTER); 45 | add(getFooter(), BorderLayout.PAGE_END); 46 | } 47 | 48 | public JLabel getTitle() { 49 | if (Objects.nonNull(title)) return title; 50 | title = new JLabel("CONVERSORES"); 51 | title.setBorder(new EmptyBorder(10, 10, 10, 10)); 52 | title.setHorizontalAlignment(SwingConstants.CENTER); 53 | title.setForeground(new Color(0, 180, 0)); 54 | return title; 55 | } 56 | 57 | public JPanel getNavItems() { 58 | if (Objects.nonNull(navItems)) return navItems; 59 | navItems = new JPanel(); 60 | navItems.setOpaque(false); 61 | navItems.setLayout(new GridLayout(10, 1)); 62 | return navItems; 63 | } 64 | 65 | public void createItem(T screen) { 66 | JButton button = new JButton(screen.getName()); 67 | button.setHorizontalAlignment(SwingConstants.LEFT); 68 | button.setIcon(screen.getImageIcon()); 69 | button.setBorder(new EmptyBorder(10, 10, 10, 10)); 70 | button.addActionListener(e -> selectPage(screen)); 71 | button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 72 | navItems.add(button); 73 | pageContentContainer.add(screen, screen.getName()); 74 | navItemAndPainel.put(screen, button); 75 | } 76 | 77 | public JPanel getFooter() { 78 | if (Objects.nonNull(footer)) return footer; 79 | JLabel label = new JLabel(hiperlink); 80 | label.setBorder(new EmptyBorder(10, 10, 10, 10)); 81 | label.setHorizontalAlignment(SwingConstants.CENTER); 82 | footer = new JPanel(); 83 | footer.setLayout(new GridLayout(0, 1)); 84 | footer.setOpaque(false); 85 | footer.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 86 | footer.addMouseListener(new MouseAdapter() { 87 | @Override 88 | public void mouseClicked(MouseEvent e) { 89 | if (Desktop.isDesktopSupported()) { 90 | try { 91 | Desktop.getDesktop().browse(new URI("https://github.com/HugoJhonathan")); 92 | } catch (IOException | URISyntaxException ex) { 93 | ex.printStackTrace(); 94 | } 95 | } 96 | } 97 | }); 98 | footer.addMouseListener(new MouseAdapter() { 99 | @Override 100 | public void mouseEntered(MouseEvent e) { 101 | label.setText("" + hiperlink + ""); 102 | } 103 | 104 | @Override 105 | public void mouseExited(MouseEvent e) { 106 | label.setText(hiperlink); 107 | } 108 | }); 109 | 110 | 111 | footer.add(label); 112 | return footer; 113 | } 114 | 115 | public void markFirstNavItem() { 116 | if (pageContentContainer.getComponents().length < 1) { 117 | return; 118 | } 119 | selectPage((Screen) pageContentContainer.getComponent(0)); 120 | } 121 | 122 | public void selectPage(T screen) { 123 | JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this); 124 | if (Objects.nonNull(frame)) { 125 | frame.setTitle(screen.getTitle()); 126 | } 127 | cardLayout.show(pageContentContainer, screen.getName()); 128 | navItemAndPainel.forEach((sc, button) -> { 129 | button.setBackground(navBarColor); 130 | }); 131 | Component navItem = navItemAndPainel.get(screen); 132 | navItem.setBackground(navBarSelected); 133 | } 134 | 135 | } -------------------------------------------------------------------------------- /src/main/java/GUI/screens/Screen.java: -------------------------------------------------------------------------------- 1 | package GUI.screens; 2 | 3 | import GUI.ScreenProperties; 4 | import GUI.util.DistanceRenderer; 5 | import GUI.util.Util; 6 | import units.Money; 7 | import units.Unit; 8 | 9 | import javax.swing.*; 10 | import javax.swing.border.EmptyBorder; 11 | import javax.swing.event.DocumentEvent; 12 | import javax.swing.event.DocumentListener; 13 | import java.awt.*; 14 | import java.math.BigDecimal; 15 | import java.util.Arrays; 16 | import java.util.Objects; 17 | 18 | public abstract class Screen extends JPanel implements ScreenProperties { 19 | 20 | private JTextField input; 21 | private JTextField output; 22 | protected JComboBox selectInputUnit; 23 | protected JComboBox selectOutputUnit; 24 | protected JPanel panelForm; 25 | private JTextPane jpanel; 26 | 27 | public Screen() { 28 | init(); 29 | events(); 30 | } 31 | 32 | public abstract Unit[] getValues(); 33 | 34 | public abstract String getName(); 35 | 36 | public abstract ImageIcon getImageIcon(); 37 | 38 | @Override 39 | public String getTitle() { 40 | return "Conversor de " + getName(); 41 | } 42 | 43 | protected void init() { 44 | initPanelForm(); 45 | setLayout(new BorderLayout()); 46 | setBorder(new EmptyBorder(15, 15, 15, 15)); 47 | setName(getName()); 48 | add(panelForm, BorderLayout.PAGE_START); 49 | add(getJpanel(), BorderLayout.PAGE_END); 50 | } 51 | 52 | private String createTableWithAllConversion(BigDecimal amount, Unit unit) { 53 | StringBuilder sb = new StringBuilder(); 54 | String value = unit.getFormattedValue(String.valueOf(amount)); 55 | sb.append(""); 56 | for (Unit u : getValues()) { 57 | String convert = unit.convert(amount, u).toPlainString(); 58 | sb.append(""); 59 | sb.append(""); 60 | sb.append(""); 61 | sb.append(""); 62 | } 63 | sb.append("
" + value + " (" + unit.getName() + ") equivale a:
" + u.getFormattedValue(convert) + "" + u.getName() + "
"); 64 | return sb.toString(); 65 | } 66 | 67 | public JTextPane getJpanel() { 68 | if (Objects.nonNull(jpanel)) return jpanel; 69 | jpanel = new JTextPane(); 70 | jpanel.setContentType("text/html"); 71 | jpanel.setOpaque(false); 72 | jpanel.setEditable(false); 73 | return jpanel; 74 | } 75 | 76 | private void initPanelForm() { 77 | GridLayout gl_divForm = new GridLayout(0, 2); 78 | gl_divForm.setVgap(15); 79 | gl_divForm.setHgap(15); 80 | JPanel divForm = new JPanel(gl_divForm); 81 | divForm.add(getInput()); 82 | divForm.add(getSelectInputUnit()); 83 | divForm.add(getOutput()); 84 | divForm.add(getSelectOutputUnit()); 85 | panelForm = divForm; 86 | } 87 | 88 | public JTextField getInput() { 89 | if (Objects.nonNull(input)) return input; 90 | input = new JTextField(); 91 | Util.increaseFont(input, 16); 92 | return input; 93 | } 94 | 95 | public JComboBox getSelectInputUnit() { 96 | if (Objects.nonNull(selectInputUnit)) return selectInputUnit; 97 | selectInputUnit = new JComboBox<>(getValues()); 98 | selectInputUnit.setRenderer(new DistanceRenderer()); 99 | return selectInputUnit; 100 | } 101 | 102 | public JComboBox getSelectOutputUnit() { 103 | if (Objects.nonNull(selectOutputUnit)) return selectOutputUnit; 104 | selectOutputUnit = new JComboBox<>(getValues()); 105 | selectOutputUnit.setSelectedIndex(1); 106 | selectOutputUnit.setRenderer(new DistanceRenderer()); 107 | return selectOutputUnit; 108 | } 109 | 110 | public JTextField getOutput() { 111 | if (Objects.nonNull(output)) return output; 112 | output = new JTextField(); 113 | output.setBorder(null); 114 | output.setEditable(false); 115 | output.setForeground(new Color(0, 180, 0)); 116 | Util.increaseFont(output, 18); 117 | return output; 118 | } 119 | 120 | public void events() { 121 | input.getDocument().addDocumentListener(new DocumentListener() { 122 | @Override 123 | public void insertUpdate(DocumentEvent e) { 124 | btnCalcularClick(); 125 | } 126 | 127 | @Override 128 | public void removeUpdate(DocumentEvent e) { 129 | btnCalcularClick(); 130 | } 131 | 132 | @Override 133 | public void changedUpdate(DocumentEvent e) { 134 | } 135 | }); 136 | selectInputUnit.addActionListener((e) -> btnCalcularClick()); 137 | selectOutputUnit.addActionListener((e) -> btnCalcularClick()); 138 | } 139 | 140 | protected void btnCalcularClick() { 141 | Unit sourceUnit = (Unit) selectInputUnit.getSelectedItem(); 142 | Unit targetUnit = (Unit) selectOutputUnit.getSelectedItem(); 143 | String resultString; 144 | try { 145 | BigDecimal amount = new BigDecimal(input.getText()); 146 | if (sourceUnit instanceof Money) { 147 | resultString = ((Money) sourceUnit) 148 | .updateAndConvert(amount, (Money) targetUnit) 149 | .toPlainString(); 150 | } else { 151 | resultString = sourceUnit.convert(amount, targetUnit).toPlainString(); 152 | } 153 | getOutput().setText(targetUnit.getFormattedValue(resultString)); 154 | getJpanel().setText(createTableWithAllConversion(amount, sourceUnit)); 155 | } catch (NumberFormatException nfe) { 156 | if (!getInput().getText().isEmpty()) { 157 | getOutput().setText("Invalid input value!"); 158 | } else { 159 | getOutput().setText(null); 160 | } 161 | getJpanel().setText(null); 162 | getInput().grabFocus(); 163 | } catch (ArithmeticException ae) { 164 | getOutput().setText("Arithmetic error!"); 165 | getJpanel().setText(ae.getMessage()); 166 | } catch (Exception e) { 167 | getOutput().setText(null); 168 | getJpanel().setText(e.getMessage()); 169 | } 170 | } 171 | 172 | } 173 | 174 | --------------------------------------------------------------------------------