import java.util.Scanner;
//public class OpApp1{
public class Main{
static Scanner sc = new Scanner(System.in);
public static void main(String args[]){
System.out.println("Bienvenido a la calculadora\n");
operar();
System.out.println("\nGracias por su visita\n");
}
static void operar(){
Vista vista = new Vista();
Modelo modelo = new Modelo();
int m, n, op; // op = opción
m = leerEntero();
n = leerEntero();
do {
op = vista.menu();
if(op<5) vista.responder(op, m, n, modelo);
} while(op!=5);
}
static int leerEntero(){
return Validator.getInt(sc, "Ingrese un entero: ");
}
}
class Vista{
Scanner sc = new Scanner(System.in);
int menu(){
System.out.print("\nOperación que requiere:\n");
System.out.print("1) Sumar: m + n\n");
System.out.print("2) Restar: m – n\n");
System.out.print("3) Multiplicar: m * n\n");
System.out.print("4) Dividir: m/n\n");
System.out.print("5) Salir:\n");
return Validator.getInt(sc, "Elija su opción: ", 1, 5);
}
void responder(int op, int m, int n, Modelo modelo){
switch(op){
case 1: System.out.printf("%d + %d = %d\n", m, n, modelo.sumar(m,n)); break;
case 2: System.out.printf("%d - %d = %d\n", m, n, modelo.restar(m,n)); break;
case 3: System.out.printf("%d * %d = %d\n", m, n, modelo.multiplicar(m,n)); break;
case 4: if(n==0) System.out.printf("No se puede dividir, divisor = 0");
else System.out.printf("%d / %d = %.2f\n", m, n, modelo.dividir(m,n));
}
}
}
class Modelo{
int sumar(int m, int n) {return m+n;}
int restar(int m, int n) {return m-n;}
int multiplicar(int m, int n) {return m*n;}
float dividir(int m, int n) {return (float)m/n;}
}
class Validator {
public static String getString(Scanner sc, String prompt){
System.out.print(prompt);
String s = sc.next();
sc.nextLine();
return s;
}
public static int getInt(Scanner sc, String prompt){
int i = 0 ;
boolean isValid = false;
while (isValid == false){
System.out.print(prompt);
if (sc.hasNextInt()){
i = sc.nextInt();
isValid = true;
} else System.out.println("Error! Valor entero no válido. Intente de nuevo.");
sc.nextLine();
}
return i;
}
public static int getInt(Scanner sc, String prompt, int min, int max){
int i = 0;
boolean isValid = false;
while (isValid == false){
i = getInt(sc, prompt);
if (i < min) System.out.println("Error! El número debe ser mayor o igual que " + min + ".");
else if (i > max) System.out.println("Error! El número debe ser menor o igual que " + max + ".");
else isValid = true;
}
return i ;
}
public static double getDouble(Scanner sc, String prompt){
double d = 0 ;
boolean isValid = false;
while (isValid == false){
System.out.print(prompt);
if (sc.hasNextDouble()){
d = sc.nextDouble();
isValid = true;
} else System.out.println("Error! Valor decimal no válido. Intente de nuevo.");
sc.nextLine();
}
return d;
}
public static double getDouble(Scanner sc, String prompt, double min, double max) {
double d = 0 ;
boolean isValid = false;
while (isValid == false){
d = getDouble(sc, prompt);
if (d < min) System.out.println("Error! El número debe ser mayor o igual que " + min + ".");
else if (d > max) System.out.println("Error! El número debe ser menor o igual que " + max + ".");
else isValid = true;
}
return d;
}
public static float getFloat(Scanner sc, String prompt){
float d = 0 ;
boolean isValid = false;
while (isValid == false){
System.out.print(prompt);
if (sc.hasNextFloat()){
d = sc.nextFloat();
isValid = true;
} else System.out.println("Error! Valor decimal no válido. Intente de nuevo.");
sc.nextLine();
}
return d;
}
public static float getFloat(Scanner sc, String prompt, float min, float max) {
float d = 0 ;
boolean isValid = false;
while (isValid == false){
d = getFloat(sc, prompt);
if (d < min) System.out.println("Error! El número debe ser mayor o igual que " + min + ".");
else if (d > max) System.out.println("Error! El número debe ser menor o igual que " + max + ".");
else isValid = true;
}
return d;
}
}