import java.util.Scanner;
public class OpApp3{
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");
}
static void operar(){
int m, n, op, k; // op = opción
m = leerEntero();
n = leerEntero();
System.out.println();
Vista vista;
Modelo modelo;
do {
k = (int)(Math.random()*2)+1;
vista = VistaFactory.getVista(k);
op = vista.menu();
if(op<5){
k = (int)(Math.random()*2)+1;
modelo = ModeloFactory.getModelo(k);
vista.responder(op, m, n, modelo);
}
} while(op!=5);
}
static int leerEntero(){
return Validator.getInt(sc, "Ingrese un entero: ");
}
}
class VistaFactory{
static Vista getVista(int k){
Vista vista;
if(k==1) vista = new Vista1();
else vista = new Vista2();
return vista;
}
}
interface Vista{
int menu();
void responder(int op, int m, int n, Modelo modelo);
}
class Vista1 implements Vista{
static Scanner sc = new Scanner(System.in);
public int menu(){
System.out.print("Vista 1: Operació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);
}
public void responder(int op, int m, int n, Modelo modelo){
switch(op){
case 1: System.out.printf("%d + %d = %d\n\n", m, n, modelo.sumar(m,n)); break;
case 2: System.out.printf("%d - %d = %d\n\n", m, n, modelo.restar(m,n)); break;
case 3: System.out.printf("%d * %d = %d\n\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\n", m, n, modelo.dividir(m,n));
}
}
}
class Vista2 implements Vista{
static Scanner sc = new Scanner(System.in);
public int menu(){
System.out.print("Vista 2: Operació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);
}
public void responder(int op, int m, int n, Modelo modelo){
switch(op){
case 1: System.out.printf("%d + %d = %d\n\n", m, n, modelo.sumar(m,n)); break;
case 2: System.out.printf("%d - %d = %d\n\n", m, n, modelo.restar(m,n)); break;
case 3: System.out.printf("%d * %d = %d\n\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\n", m, n, modelo.dividir(m,n));break;
}
}
}
class ModeloFactory{
static Modelo getModelo(int k){
Modelo modelo;
if(k==1) modelo = new Modelo1();
else modelo = new Modelo2();
return modelo;
}
}
interface Modelo{
int sumar(int m, int n);
int restar(int m, int n);
int multiplicar(int m, int n);
float dividir(int m, int n);
}
class Modelo1 implements Modelo{
Modelo1(){
System.out.print("Modelo 1: ");
}
public int sumar(int m, int n) {return m+n;}
public int restar(int m, int n) {return m-n;}
public int multiplicar(int m, int n) {return m*n;}
public float dividir(int m, int n) {return (float)m/n;}
}
class Modelo2 implements Modelo{
Modelo2(){
System.out.print("Modelo 2: ");
}
public int sumar(int m, int n) {return m+n;}
public int restar(int m, int n) {return m-n;}
public int multiplicar(int m, int n) {return m*n;}
public 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;
}
}