Created
July 11, 2013 22:22
-
-
Save PotterSys/5979780 to your computer and use it in GitHub Desktop.
Solemne 3 POO1501
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
/** | |
* Clase colaboradora para generar certificados, que representa a la persona | |
* | |
* author Juan Correa | |
*/ | |
public class Persona { | |
private String rut; | |
private String sexo; | |
private int edad; | |
private boolean donante; | |
public Persona() { | |
this.rut = ""; | |
this.sexo = ""; | |
this.edad = 0; | |
this.donante = false; | |
} | |
public Persona(String rut, String sexo, int edad, boolean donante) { | |
this.rut = rut; | |
this.sexo = sexo; | |
this.edad = edad; | |
this.donante = donante; | |
} | |
public String getRut() { | |
return rut; | |
} | |
/** | |
* Implementa regla de negocio solicitada en Parte 2, lista 1, punto 3 | |
* @param rut | |
*/ | |
public void setRut(String rut) { | |
if( rut.length() >= 8 ){ | |
this.rut = rut; | |
} else { | |
System.out.println("El RUT debe tener al menos 8 caracteres"); | |
} | |
} | |
public String getSexo() { | |
return sexo; | |
} | |
public void setSexo(String sexo) { | |
if( sexo.toUpperCase().equals("M") || sexo.toUpperCase().equals("F") ){ | |
this.sexo = sexo; | |
} else { | |
System.out.println("El sexo/género debe ser ingresado sólo como \"M\" o \"F\""); | |
} | |
} | |
public int getEdad() { | |
return edad; | |
} | |
/** | |
* Agrega regla de negocio de Parte 2, lista 1, punto 1 | |
* @param edad | |
*/ | |
public void setEdad(int edad) { | |
if( edad >= 0 && edad < 121 ){ | |
this.edad = edad; | |
} else { | |
System.out.println("La edad debe estar entre 0 y 120 años"); | |
} | |
} | |
public boolean isDonante() { | |
return donante; | |
} | |
public void setDonante(boolean donante) { | |
this.donante = donante; | |
} | |
public String prettifyDonante(){ | |
if ( this.isDonante() ){ | |
return "Sí"; | |
} | |
return "No"; | |
} | |
public void imprimirAtributos(){ | |
System.out.println(" = Datos de persona = "); | |
System.out.println("Rut: "+ getRut() ); | |
System.out.println("Sexo: "+ getSexo() ); | |
System.out.println("Edad: "+ getEdad() ); | |
System.out.println("Es donante?: "+ prettifyDonante() ); | |
System.out.println(""); | |
} | |
/** | |
* Resuelve parte 1, punto 2 | |
*/ | |
public void hacerDonante(){ | |
this.setDonante( true ); | |
} | |
} |
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
/** | |
* Clase base del certificado | |
* | |
* author Juan Correa | |
*/ | |
public class Certificado { | |
private Persona persona; | |
private int codigo; | |
private String tipo; | |
private String fecha; | |
private Double precio; | |
public Certificado() { | |
this.persona = new Persona(); | |
this.codigo = 0; | |
this.tipo = ""; | |
this.fecha = ""; | |
this.precio = 0.0; | |
} | |
public Certificado(Persona persona, int codigo, String tipo, String fecha, Double precio) { | |
this.persona = persona; | |
this.codigo = codigo; | |
this.tipo = tipo; | |
this.fecha = fecha; | |
this.precio = precio; | |
} | |
public Persona getPersona() { | |
return persona; | |
} | |
public void setPersona(Persona persona) { | |
this.persona = persona; | |
} | |
public int getCodigo() { | |
return codigo; | |
} | |
public void setCodigo(int codigo) { | |
this.codigo = codigo; | |
} | |
public String getTipo() { | |
return tipo; | |
} | |
/** | |
* Implementa regla de negocio de Parte 2, lista 1, punto 2 | |
* @param tipo | |
*/ | |
public void setTipo(String tipo) { | |
switch( tipo.toLowerCase() ){ | |
case "nacimiento": | |
case "matrimonio": | |
case "defunción": | |
this.tipo = tipo.toLowerCase(); | |
break; | |
default: | |
System.out.println("El tipo de certificado sólo puede ser \"nacimiento\", \"matrimonio\" y \"defunción\""); | |
break; | |
} | |
this.tipo = tipo; | |
} | |
public String getFecha() { | |
return fecha; | |
} | |
public void setFecha(String fecha) { | |
this.fecha = fecha; | |
} | |
public Double getPrecio() { | |
return precio; | |
} | |
public void setPrecio(Double precio) { | |
this.precio = precio; | |
} | |
public void imprimirAtributos(){ | |
System.out.println("= Datos de certificado = "); | |
getPersona().imprimirAtributos(); | |
System.out.println("Código: "+ getCodigo() ); | |
System.out.println("Tipo: "+ getTipo() ); | |
System.out.println("Fecha: "+ getFecha() ); | |
System.out.println("Precio: "+ getPrecio() ); | |
System.out.println(""); | |
} | |
/** | |
* Resuelve Parte 1, punto 1 | |
*/ | |
public void imprimirDatosCertificado(){ | |
System.out.printf("%s\t\t%s\t\t%s", Integer.toString( this.getCodigo() ), this.getPersona().getRut(), this.getTipo() ); | |
} | |
/** | |
* Resuelve Parte 1, punto 2 | |
*/ | |
public void hacerDonante(){ | |
this.getPersona().hacerDonante(); | |
} | |
/** | |
* Resuelve Parte 1, punto 3 | |
*/ | |
public boolean esCertMatrimonioYmayorDeEdad(){ | |
if( this.getTipo().equals("matrimonio") && this.getPersona().getEdad() > 17 ){ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Resuelve Parte 1, punto 4 | |
*/ | |
public void descuentoDefuncion(){ | |
if( this.getTipo().equals("defunción") ){ | |
this.setPrecio( ( this.getPrecio() / 10 ) * 9.0 ); | |
System.out.println("[efecutado descuento por ser certificado de defunción]"); | |
} | |
} | |
/** | |
* Resuleve Parte 1, punto 5 | |
*/ | |
public void aumentaPrecio( Double alza ){ | |
if( alza > 0 ){ | |
this.setPrecio( this.getPrecio() + alza ); | |
} | |
} | |
public String prettifyBoolean( boolean dato ){ | |
if( dato ) { | |
return "Sí"; | |
} | |
return "No"; | |
} | |
} |
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
import java.util.Random; | |
import java.util.Scanner; | |
/** | |
* Clase que contiene el main() del programa | |
* | |
* @author Juan Correa | |
*/ | |
public class Principal { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
Scanner sc = new Scanner( System.in ); | |
String temp = ""; | |
boolean datosValidos = false; | |
/** | |
* Resuelve lo solicitado en Parte 2, lista 2 | |
*/ | |
// Resuelve punto 1 | |
Certificado cert = new Certificado(); | |
// Pedimos los datos por teclado; primero de la persona, y luego del certificado | |
System.out.println("Ingrese el Rut de la persona: "); | |
cert.getPersona().setRut( sc.nextLine() ); | |
System.out.println("Ingrese el sexo de la persona: "); | |
cert.getPersona().setSexo( sc.nextLine() ); | |
System.out.println("Ingrese la edad de la persona: "); | |
cert.getPersona().setEdad( sc.nextInt() ); | |
sc.nextLine(); // de esta forma, sacamos el salto de línea | |
while( temp.equals("S") == false && temp.equals("N") == false ){ | |
System.out.println("¿Es donante la persona?. Escriba \"S\" para Sí, y \"N\" para No: "); | |
temp = sc.nextLine(); | |
temp = temp.toUpperCase(); | |
switch (temp) { | |
case "S": | |
cert.getPersona().setDonante(true); | |
break; | |
case "N": | |
cert.getPersona().setDonante(false); | |
break; | |
} | |
} | |
Random serie = new Random(); | |
cert.setCodigo( serie.nextInt(999999) ); | |
while( datosValidos == false ){ | |
temp = ""; | |
System.out.println("Ingrese el tipo de certificado: "); | |
System.out.println("[1] Nacimiento"); | |
System.out.println("[2] Matrimonio"); | |
System.out.println("[3] Defunción"); | |
temp = sc.nextLine(); | |
switch( temp ){ | |
case "1": | |
cert.setTipo("nacimiento"); | |
datosValidos = true; | |
break; | |
case "2": | |
cert.setTipo("matrimonio"); | |
datosValidos = true; | |
break; | |
case "3": | |
cert.setTipo("defunción"); | |
datosValidos = true; | |
break; | |
} | |
} | |
System.out.println("Ingrese la fecha de hoy [ejemplo: 3/07/2013]: "); | |
cert.setFecha( sc.nextLine() ); | |
double priceTemp = 0.0; | |
while( priceTemp < 1.0 ){ | |
System.out.println("Ingrese el precio del certificado: "); | |
priceTemp = Double.parseDouble( sc.nextLine() ); | |
} | |
cert.setPrecio( priceTemp ); | |
cert.imprimirAtributos(); | |
// Resuelve punto 2 | |
System.out.println("Ingrese la nueva edad para la Persona:"); | |
cert.getPersona().setEdad( sc.nextInt() ); | |
sc.nextLine(); | |
// Resuelve punto 3 | |
System.out.println("La nueva edad de la persona es: "+ cert.getPersona().getEdad() ); | |
// Resuelve punto 4 | |
System.out.println(""); | |
cert.imprimirDatosCertificado(); | |
System.out.println(""); | |
cert.getPersona().hacerDonante(); | |
System.out.println("Es certificado de matrimonio, y la persona es mayor de edad?: "+ cert.prettifyBoolean( cert.esCertMatrimonioYmayorDeEdad() )); | |
cert.descuentoDefuncion(); | |
priceTemp = -1.0; | |
while( priceTemp < 0.0 ){ | |
System.out.println("Ingrese la cantidad a subir del precio: "); | |
priceTemp = Double.parseDouble( sc.nextLine() ); | |
} | |
cert.aumentaPrecio( priceTemp ); | |
cert.imprimirAtributos(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment