generated from Andressep/java-codespace
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package Util; | ||
import Util.SScan; | ||
import java.util.List; | ||
|
||
public class Menu { | ||
public static void mostrarMenu(List<String> opciones) { | ||
System.out.println("Menú de opciones:"); | ||
for (int i = 0; i < opciones.size(); i++) { | ||
System.out.printf("%c. %s\n", 'a' + i, opciones.get(i)); | ||
} | ||
} | ||
|
||
public static String seleccionarOpcionMenu() { | ||
SScan sscan = new SScan(); | ||
String opcion = sscan.getString("Elige una opción: "); | ||
return opcion; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package Util; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.util.Scanner; | ||
|
||
public class SScan { | ||
|
||
private Scanner sc; | ||
|
||
public SScan() { | ||
sc = new Scanner(System.in); | ||
} | ||
|
||
public Object escanear(String type) { | ||
switch (type) { | ||
case "int": | ||
return getInt("Ingresa un entero: "); | ||
case "float": | ||
return getFloat("Ingresa un float: "); | ||
case "long": | ||
return getLong("Ingresa un long integer: "); | ||
case "double": | ||
return getDouble("Ingresa un double: "); | ||
case "byte": | ||
return getByte("Ingresa un byte: "); | ||
case "short": | ||
return getShort("Ingresa un short integer: "); | ||
case "biginteger": | ||
return getBigInteger("Ingresa un BigInteger: "); | ||
case "bigdecimal": | ||
return getBigDecimal("Ingresa un BigDecimal: "); | ||
case "string": | ||
return getString("Ingresa un string: "); | ||
case "boolean": | ||
return getBoolean("Ingresa un boolean (true/false): "); | ||
default: | ||
System.out.println("Tipo inválido, intente nuevamente."); | ||
return null; | ||
} | ||
} | ||
|
||
// INT | ||
public int getInt(String prompt) { | ||
System.out.print(prompt); | ||
while (!sc.hasNextInt()) { | ||
System.out.print("Entrada inválida. Por favor ingrese un entero: "); | ||
sc.next(); | ||
} | ||
return sc.nextInt(); | ||
} | ||
|
||
// Float | ||
public float getFloat(String prompt) { | ||
System.out.print(prompt); | ||
while (!sc.hasNextFloat()) { | ||
System.out.print("Entrada inválida. Por favor ingrese un float: "); | ||
sc.next(); | ||
} | ||
return sc.nextFloat(); | ||
} | ||
|
||
// Long | ||
public long getLong(String prompt) { | ||
System.out.print(prompt); | ||
while (!sc.hasNextLong()) { | ||
System.out.print("Entrada inválida. Por favor ingrese un long integer: "); | ||
sc.next(); | ||
} | ||
return sc.nextLong(); | ||
} | ||
|
||
// Double | ||
public double getDouble(String prompt) { | ||
System.out.print(prompt); | ||
while (!sc.hasNextDouble()) { | ||
System.out.print("Entrada inválida. Por favor ingrese un double: "); | ||
sc.next(); | ||
} | ||
return sc.nextDouble(); | ||
} | ||
|
||
// Byte | ||
public byte getByte(String prompt) { | ||
System.out.print(prompt); | ||
while (!sc.hasNextByte()) { | ||
System.out.print("Entrada inválida. Por favor ingrese un byte: "); | ||
sc.next(); | ||
} | ||
return sc.nextByte(); | ||
} | ||
|
||
// Short | ||
public short getShort(String prompt) { | ||
System.out.print(prompt); | ||
while (!sc.hasNextShort()) { | ||
System.out.print("Entrada inválida. Por favor ingrese un short integer: "); | ||
sc.next(); | ||
} | ||
return sc.nextShort(); | ||
} | ||
|
||
// BigInteger | ||
public BigInteger getBigInteger(String prompt) { | ||
System.out.print(prompt); | ||
while (!sc.hasNextBigInteger()) { | ||
System.out.print("Entrada inválida. Por favor ingrese un BigInteger: "); | ||
sc.next(); | ||
} | ||
return sc.nextBigInteger(); | ||
} | ||
|
||
// BigDecimal | ||
public BigDecimal getBigDecimal(String prompt) { | ||
System.out.print(prompt); | ||
while (!sc.hasNextBigDecimal()) { | ||
System.out.print("Entrada inválida. Por favor ingrese un BigDecimal: "); | ||
sc.next(); | ||
} | ||
return sc.nextBigDecimal(); | ||
} | ||
|
||
// String | ||
public String getString(String prompt) { | ||
System.out.print(prompt); | ||
return sc.nextLine(); | ||
} | ||
|
||
// Boolean | ||
public boolean getBoolean(String prompt) { | ||
System.out.print(prompt); | ||
while (!sc.hasNextBoolean()) { | ||
System.out.print("Entrada inválida. Por favor ingrese verdadero o falso: "); | ||
sc.next(); | ||
} | ||
return sc.nextBoolean(); | ||
} | ||
|
||
public void close() { | ||
sc.close(); | ||
} | ||
} |