diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..acb1a34 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,62 @@ +{ + "cSpell.words": [ + "billón", + "billones", + "catorce", + "CENTIS", + "ciento", + "cientos", + "cinco", + "cincuenta", + "convertir", + "cuarenta", + "cuatro", + "cuatrocientos", + "DECENAS", + "diecinueve", + "dieciocho", + "DIECIS", + "dieciséis", + "diecisiete", + "diez", + "doce", + "doscientos", + "empezar", + "entero", + "Entrada", + "estar", + "Ingrese", + "inválido", + "menor", + "millón", + "millones", + "mycompany", + "novecientos", + "noventa", + "nueve", + "número", + "Número", + "ochenta", + "ocho", + "ochocientos", + "palabra", + "palabras", + "puede", + "quinientos", + "Saliendo", + "salir", + "seis", + "seiscientos", + "sesenta", + "setecientos", + "setenta", + "siete", + "Tiene", + "trece", + "treinta", + "trescientos", + "UNIDADES", + "vacío", + "veinte" + ] +} \ No newline at end of file diff --git a/src/main/java/com/mycompany/app/App.java b/src/main/java/com/mycompany/app/App.java index 2e01c30..821bd2b 100644 --- a/src/main/java/com/mycompany/app/App.java +++ b/src/main/java/com/mycompany/app/App.java @@ -14,35 +14,30 @@ public class App { void spawn_cli() { // Enter data using BufferReader var reader = new BufferedReader(new InputStreamReader(System.in)); + String input; + while (true) { - // Reading data using readLine - String name = new String("asdasd"); - try { - System.out.println("Ingrese info"); - name = reader.readLine(); - System.out.println("FIn"); - } catch (Exception e) { - System.out.println("Errir"); - System.out.println(e); - } - - System.out.println("FIn2"); - // Printing the read line - System.out.println(name); - // let mut input = String::new(); - // print!("\nIngrese un número para convertir a palabras\nIngrese `exit` para - // salir:\n\n"); - // fn read_line(input: &mut String) { - // input.clear(); - // std::io::stdin().read_line(input).unwrap(); - // } + // Reading data using readLine + try { + System.out.println("Ingrese numero o `exit` para salir:"); + input = reader.readLine().trim(); + } catch (Exception e) { + System.out.println("Entrada invalida: " + e); + continue; + } + if (input.equals("exit")) { + System.out.println("Saliendo..."); + break; + } + if (input.equals("clear")) { + System.out.println("\033[H\033[2J"); + continue; + } + } // loop { - // print!("Ingrese su número: "); - // flush(); - // read_line(&mut input); - // let input = input.trim(); + // match input { // "exit" => { // clear_terminal(); @@ -87,29 +82,98 @@ public static void main(String[] args) { App app = new App(); app.spawn_cli(); - System.out.println("Goodbye World!"); + var palabra = Int2Words.to_cardinal(1_001_020_642); + System.out.println("Goodbye World!: " + palabra); + System.out.println("=" + Int2Words.to_cardinal(2_001_020_642)); + System.out.println("=" + Int2Words.to_cardinal(2_002_020_642)); + System.out.println("=" + Int2Words.to_cardinal(2_020_021_642)); + System.out.println("=" + Int2Words.to_cardinal(2_000_020_642)); } } class Int2Words { - // let mut vec = vec![]; - // while num > 0 { - // vec.push((num % 1000) as i16); - // num /= 1000; - // } - // vec.reverse(); - // let prettied = - // vec.into_iter().map(|num| - // format!("{num:03}")).collect::>().join(","); + static String to_cardinal(Integer num) { + if (num == 0) { + return "cero"; + } + var triplets = Utils.into_triplets(num); + + var words = new ArrayList(); + for (int i = triplets.length - 1; i >= 0; i--) { + // Read the triplets in an inverted order + var triplet = triplets[triplets.length - i - 1]; + if (triplet == 0) { + continue; + } + int hundreds = (triplet / 100) % 10; + int tens = (triplet / 10) % 10; + int units = triplet % 10; + if (hundreds > 0) { + if (triplet == 100) { + // Edge case when triplet is a hundred + words.add("cien"); + } else { + words.add(CENTIS[hundreds]); + } + } - // print!("{:?}", prettied.trim_start_matches('0')); - // flush(); + if (tens != 0 || units != 0) { + // for edge case when unit value is 1 and is not the last triplet + String unit_word = null; + if (units == 1 && i != 0) { + unit_word = "un"; + } else { + unit_word = UNIDADES[units]; + } + if (tens == 0) { + // case `?_102` => `? ciento dos` + words.add(unit_word); + } else if (tens == 1) { + // case `?_119` => `? ciento diecinueve` + // case `?_110` => `? ciento diez` + words.add(DIECIS[units]); + } else { + // case `?_142 => `? cuarenta y dos` + String ten = DECENAS[tens]; + if (units == 0) { + words.add(ten); + } else { + words.add(String.format("%s y %s", ten, unit_word)); + } + } + if (i != 0 && triplet != 0) { + if (i > MILES.length - 1) { + return String.format("Número demasiado grande: %d - Maximo: %d", num, Integer.MAX_VALUE); + } + // Boolean that checks if next MILES is plural + boolean plural = triplet != 1; + if (plural) { + words.add(MILES[i]); + } else { + words.add(MIL[i]); + } + } + + } + } + return String.join(" ", words); + } + static final String[] UNIDADES = { "", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve" }; + static final String[] DIECIS = { "diez", "once", "doce", "trece", "catorce", "quince", "dieciséis", "diecisiete", + "dieciocho", "diecinueve" }; + static final String[] DECENAS = { "", "", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", + "ochenta", "noventa" }; + static final String[] CENTIS = { "", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos", + "seiscientos", + "setecientos", "ochocientos", "novecientos" }; + static final String[] MILES = { "", "mil", "millones", "billones" }; + static final String[] MIL = { "", "mil", "millón", "billón" }; } class Utils { - static Integer[] into_triplets(Integer num) { + static public Integer[] into_triplets(Integer num) { var vec = new ArrayList(); while (num > 0) {